ss-calendar.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. const date = current ? new Date(current) : new Date();
  89. const year = date.getFullYear();
  90. // 月份值默认从0开始
  91. const month = date.getMonth() + 1;
  92. const day = date.getDate();
  93. return {
  94. year,
  95. month,
  96. day
  97. };
  98. },
  99. // 日期处理
  100. getDateData(year, month) {
  101. // 新增月份时需要减少1个月
  102. const date = new Date(year,month-1,1);
  103. const firstDayWeek = date.getDay();
  104. const data = [...this.getEmptys(firstDayWeek), ...this.getDays(firstDayWeek)];
  105. return data;
  106. },
  107. // 查询日期列表有几个空格
  108. getEmptys(count) {
  109. let arr = [];
  110. if (count) {
  111. for (let i = 0; i < count; i++) {
  112. arr.push('');
  113. }
  114. }
  115. return arr;
  116. },
  117. getLastDay() {
  118. let { year, month } = this.getDate();
  119. month += 1;
  120. if (month > 11) {
  121. year += 1;
  122. month = 1;
  123. }
  124. let firstDayTimeStamp = new Date(`${year}/${month}/1`).getTime();
  125. let oneDayTimeStamp = 24 * 60 * 60 * 1000;
  126. let lastDay = new Date(firstDayTimeStamp - oneDayTimeStamp).getDate();
  127. return lastDay;
  128. },
  129. getDays() {
  130. const lastDay = this.getLastDay();
  131. const days = [];
  132. for (let i = 1; i <= lastDay; i++) {
  133. days.push(this.checks.includes(i) ? 'checked' : i);
  134. }
  135. return days;
  136. },
  137. format(num) {
  138. return num < 10 ? `0${num}` : num;
  139. }
  140. }
  141. };
  142. </script>
  143. <style lang="scss" scoped>
  144. .calendar__wrap {
  145. background-color: #fff;
  146. color: $uni-text-color;
  147. .header {
  148. padding: 0 24rpx;
  149. .current-date {
  150. text-align: center;
  151. font-size: $font-lg + 2rpx;
  152. // border-bottom: 2rpx solid #eee;
  153. padding: 32rpx 0;
  154. }
  155. }
  156. .body {
  157. .weeks {
  158. display: flex;
  159. font-size: $font-lg;
  160. padding: 10rpx 0;
  161. background-color: #f4f7ff;
  162. .week__item {
  163. flex: 1;
  164. text-align: center;
  165. }
  166. }
  167. .day__list {
  168. display: flex;
  169. flex-wrap: wrap;
  170. .day__item {
  171. display: flex;
  172. justify-content: center;
  173. width: 14.285%;
  174. text-align: center;
  175. padding: 30rpx 0;
  176. font-size: 34rpx;
  177. color: $font-color-light;
  178. .checked-box,
  179. .current-box {
  180. display: flex;
  181. align-items: center;
  182. justify-content: center;
  183. border-radius: 100%;
  184. box-sizing: border-box;
  185. position: relative;
  186. }
  187. image,
  188. .checked-box,
  189. .current-box,
  190. .check_text {
  191. width: 56rpx;
  192. height: 56rpx;
  193. line-height: 56rpx;
  194. }
  195. image,
  196. .check_text {
  197. position: absolute;
  198. top: 0;
  199. left: 0;
  200. }
  201. .checked {
  202. font-size: 40rpx;
  203. background-color: #3f9dff;
  204. color: #fff;
  205. }
  206. .current {
  207. padding: 12rpx;
  208. background-color: $background-color;
  209. color: #fff;
  210. font-size: 28rpx;
  211. }
  212. }
  213. }
  214. }
  215. }
  216. </style>