ss-calendar.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <template>
  2. <view class="calendar__wrap">
  3. <view class="header">
  4. <view class="current-date">{{ currentDate }}</view>
  5. </view>
  6. <view class="body">
  7. <view class="weeks">
  8. <view class="week__item" v-for="week in weeks" :key="week">{{ week }}</view>
  9. </view>
  10. <view class="day__list">
  11. <view class="day__item" v-for="(item, index) in dateData" :key="index">
  12. <view class="checked-box" :class="[checksClass]" v-if="item === 'checked'">
  13. <text class="checked iconfont iconfavor" v-if="!checksIcon"></text>
  14. <image v-else :src="checksIcon" mode="aspectFit"></image>
  15. <view class="check_text" v-if="checkTextShow">{{ dayLoad(index) }}</view>
  16. </view>
  17. <text class="current-box" :class="[item === day ? (actionClass ? actionClass : 'current') : '']" v-else>{{ item }}</text>
  18. </view>
  19. </view>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. export default {
  25. props: {
  26. //选中的日期数据
  27. checks: {
  28. type: Array,
  29. default() {
  30. // 例子[1,2,3,4],表示本月1,2,3,4号4天被选中
  31. return [];
  32. }
  33. },
  34. // 选中物品的样式类
  35. checksClass: {
  36. type: String,
  37. default: ''
  38. },
  39. // 选中时图标
  40. checksIcon: {
  41. type: String
  42. },
  43. // 选中时是否显示文字
  44. checkTextShow: {
  45. type: Boolean,
  46. default: false
  47. },
  48. // 表示当前日期的样式
  49. actionClass: {
  50. type: String,
  51. default: ''
  52. }
  53. },
  54. data() {
  55. const { year, month, day } = this.getDate();
  56. const dateData = this.getDateData(year, month);
  57. const noDay = ( new Date(year,month-1,1)).getDay();
  58. return {
  59. year,
  60. month,
  61. day,
  62. dateData,
  63. noDay,//本月1号是星期几
  64. weeks: ['日', '一', '二', '三', '四', '五', '六'],
  65. };
  66. },
  67. computed: {
  68. // 获取当前日期
  69. currentDate() {
  70. return `${this.year}-${this.format(this.month)}`;
  71. }
  72. },
  73. watch: {
  74. checks(val) {
  75. const { year, month } = this.getDate();
  76. const dateData = this.getDateData(year, month);
  77. // 保存当前月份信息
  78. this.dateData = dateData;
  79. },
  80. },
  81. methods: {
  82. // 重新计算时间
  83. dayLoad: function(value) {
  84. return value + 1 - this.noDay;
  85. },
  86. // 获取当前日期
  87. getDate(current) {
  88. console.log(current);
  89. const date = current ? new Date(current) : new Date();
  90. const year = date.getFullYear();
  91. // 月份值默认从0开始
  92. const month = date.getMonth() + 1;
  93. const day = date.getDate();
  94. return {
  95. year,
  96. month,
  97. day
  98. };
  99. },
  100. // 日期处理
  101. getDateData(year, month) {
  102. // 新增月份时需要减少1个月
  103. const date = new Date(year,month-1,1);
  104. const firstDayWeek = date.getDay();
  105. const data = [...this.getEmptys(firstDayWeek), ...this.getDays(firstDayWeek)];
  106. return data;
  107. },
  108. // 查询日期列表有几个空格
  109. getEmptys(count) {
  110. let arr = [];
  111. if (count) {
  112. for (let i = 0; i < count; i++) {
  113. arr.push('');
  114. }
  115. }
  116. return arr;
  117. },
  118. getLastDay() {
  119. let { year, month } = this.getDate();
  120. month += 1;
  121. if (month > 11) {
  122. year += 1;
  123. month = 1;
  124. }
  125. let firstDayTimeStamp = new Date(`${year}/${month}/1`).getTime();
  126. let oneDayTimeStamp = 24 * 60 * 60 * 1000;
  127. let lastDay = new Date(firstDayTimeStamp - oneDayTimeStamp).getDate();
  128. return lastDay;
  129. },
  130. getDays() {
  131. const lastDay = this.getLastDay();
  132. const days = [];
  133. for (let i = 1; i <= lastDay; i++) {
  134. days.push(this.checks.includes(i) ? 'checked' : i);
  135. }
  136. return days;
  137. },
  138. format(num) {
  139. return num < 10 ? `0${num}` : num;
  140. }
  141. }
  142. };
  143. </script>
  144. <style lang="scss" scoped>
  145. .calendar__wrap {
  146. background-color: #fff;
  147. color: $uni-text-color;
  148. .header {
  149. padding: 0 24rpx;
  150. .current-date {
  151. text-align: center;
  152. font-size: $font-lg + 2rpx;
  153. // border-bottom: 2rpx solid #eee;
  154. padding: 32rpx 0;
  155. }
  156. }
  157. .body {
  158. .weeks {
  159. display: flex;
  160. font-size: $font-lg;
  161. padding: 10rpx 0;
  162. background-color: #f4f7ff;
  163. .week__item {
  164. flex: 1;
  165. text-align: center;
  166. }
  167. }
  168. .day__list {
  169. display: flex;
  170. flex-wrap: wrap;
  171. .day__item {
  172. display: flex;
  173. justify-content: center;
  174. width: 14.285%;
  175. text-align: center;
  176. padding: 30rpx 0;
  177. font-size: 34rpx;
  178. color: $font-color-light;
  179. .checked-box,
  180. .current-box {
  181. display: flex;
  182. align-items: center;
  183. justify-content: center;
  184. border-radius: 100%;
  185. box-sizing: border-box;
  186. position: relative;
  187. }
  188. image,
  189. .checked-box,
  190. .current-box,
  191. .check_text {
  192. width: 56rpx;
  193. height: 56rpx;
  194. line-height: 56rpx;
  195. }
  196. image,
  197. .check_text {
  198. position: absolute;
  199. top: 0;
  200. left: 0;
  201. }
  202. .checked {
  203. font-size: 40rpx;
  204. background-color: #3f9dff;
  205. color: #fff;
  206. }
  207. .current {
  208. padding: 12rpx;
  209. background-color: $background-color;
  210. color: #fff;
  211. font-size: 28rpx;
  212. }
  213. }
  214. }
  215. }
  216. }
  217. </style>