ss-calendar.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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">{{ 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. return {
  58. year,
  59. month,
  60. day,
  61. dateData,
  62. weeks: ['日', '一', '二', '三', '四', '五', '六']
  63. };
  64. },
  65. computed: {
  66. // 获取当前日期
  67. currentDate() {
  68. return `${this.year}-${this.format(this.month)}`;
  69. }
  70. },
  71. watch: {
  72. checks(val) {
  73. const { year, month } = this.getDate();
  74. const dateData = this.getDateData(year, month);
  75. this.dateData = dateData;
  76. }
  77. },
  78. methods: {
  79. getDate(current) {
  80. const date = current ? new Date(current) : new Date();
  81. const year = date.getFullYear();
  82. const month = date.getMonth() + 1;
  83. const day = date.getDate();
  84. return {
  85. year,
  86. month,
  87. day
  88. };
  89. },
  90. getDateData(year, month) {
  91. const date = new Date('${year}/${month}/1');
  92. const firstDayWeek = date.getDay();
  93. const data = [...this.getEmptys(firstDayWeek), ...this.getDays(firstDayWeek)];
  94. return data;
  95. },
  96. getEmptys(count) {
  97. let arr = [];
  98. if (count) {
  99. for (let i = 0; i < count; i++) {
  100. arr.push('');
  101. }
  102. }
  103. return arr;
  104. },
  105. getLastDay() {
  106. let { year, month } = this.getDate();
  107. month += 1;
  108. if (month > 11) {
  109. year += 1;
  110. month = 1;
  111. }
  112. let firstDayTimeStamp = new Date(`${year}/${month}/1`).getTime();
  113. let oneDayTimeStamp = 24 * 60 * 60 * 1000;
  114. let lastDay = new Date(firstDayTimeStamp - oneDayTimeStamp).getDate();
  115. return lastDay;
  116. },
  117. getDays() {
  118. const lastDay = this.getLastDay();
  119. const days = [];
  120. for (let i = 1; i <= lastDay; i++) {
  121. days.push(this.checks.includes(i) ? 'checked' : i);
  122. }
  123. return days;
  124. },
  125. format(num) {
  126. return num < 10 ? `0${num}` : num;
  127. }
  128. }
  129. };
  130. </script>
  131. <style lang="scss" scoped>
  132. .calendar__wrap {
  133. background-color: #fff;
  134. color: $uni-text-color;
  135. .header {
  136. padding: 0 24rpx;
  137. .current-date {
  138. text-align: center;
  139. font-size: $font-lg + 2rpx;
  140. // border-bottom: 2rpx solid #eee;
  141. padding: 32rpx 0;
  142. }
  143. }
  144. .body {
  145. .weeks {
  146. display: flex;
  147. font-size: $font-lg;
  148. padding: 10rpx 0;
  149. background-color: #f4f7ff;
  150. .week__item {
  151. flex: 1;
  152. text-align: center;
  153. }
  154. }
  155. .day__list {
  156. display: flex;
  157. flex-wrap: wrap;
  158. .day__item {
  159. display: flex;
  160. justify-content: center;
  161. width: 14.285%;
  162. text-align: center;
  163. padding: 30rpx 0;
  164. font-size: 34rpx;
  165. color: $font-color-light;
  166. .checked-box,
  167. .current-box {
  168. display: flex;
  169. align-items: center;
  170. justify-content: center;
  171. border-radius: 100%;
  172. box-sizing: border-box;
  173. position: relative;
  174. }
  175. image,
  176. .checked-box,
  177. .current-box,
  178. .check_text {
  179. width: 56rpx;
  180. height: 56rpx;
  181. line-height: 56rpx;
  182. }
  183. image,
  184. .check_text {
  185. position: absolute;
  186. top: 0;
  187. left: 0;
  188. }
  189. .checked {
  190. font-size: 40rpx;
  191. background-color: #3f9dff;
  192. color: #fff;
  193. }
  194. .current {
  195. padding: 12rpx;
  196. background-color: $base-color;
  197. color: #fff;
  198. font-size: 28rpx;
  199. }
  200. }
  201. }
  202. }
  203. }
  204. </style>