ss-calendar.vue 5.2 KB

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