index.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <template>
  2. <view class="box">
  3. <div class="week">
  4. <div class="w_pre" v-for="(pre,idx) in weekList" :key="idx">{{pre}}</div>
  5. </div>
  6. <div class="date">
  7. <div class="d_pre" v-for="(pre,idx) in calendarList" :key="idx" @tap="selectDay(pre,idx)">
  8. <div class="day" :class="{'dq':pre.iscurrentday,'select':pre.select,'wd':pre.isweekday}">{{pre.day}}</div>
  9. <div class="status" :class="{'zc':pre.status==1,'yc':pre.status==2}" v-if="pre.isweekday"></div>
  10. </div>
  11. </div>
  12. </view>
  13. </template>
  14. <script>
  15. export default {
  16. data(){
  17. return {
  18. weekList:['一','二','三','四','五','六','日'],
  19. year:new Date().getFullYear(),
  20. month:new Date().getMonth()+1,
  21. calendarList:[]
  22. }
  23. },
  24. created() {
  25. this.getDataByMonth(this.year,this.month);
  26. },
  27. methods:{
  28. getDataByMonth(year,month){
  29. const daysInMonth = new Date(year, month, 0).getDate();
  30. let calendar = [];
  31. for (let day = 1; day <= daysInMonth; day++) {
  32. let date = new Date(year, month - 1, day); // 注意月份从0开始
  33. let nyr = `${year}-${String(month).padStart(2, '0')}-${String(day).padStart(2, '0')}`;
  34. calendar.push({
  35. nyr,
  36. year,
  37. month,
  38. day,
  39. week: date.getDay()||7,
  40. isweekday:(date.getDay()||7)<6,
  41. iscurrentday:nyr==new Date().Format('yyyy-MM-dd')
  42. });
  43. }
  44. let qlist = this.insertDays(year,month,calendar);
  45. let hlist = this.appendDays(year,month,calendar);
  46. let list = qlist.concat(calendar).concat(hlist);
  47. this.calendarList = JSON.parse(JSON.stringify(list));
  48. this.calendarList.forEach((c,i)=>{
  49. this.$set(this.calendarList[i],'select',false);
  50. this.$set(this.calendarList[i],'status',1);
  51. })
  52. },
  53. insertDays(year,month,calendar){
  54. let week = calendar[0].week;
  55. if(week==1) return []
  56. let qyear = month==1?(year-1):year;
  57. let qmonth = month==1?12:month-1;
  58. let daysInMonth = new Date(qyear, qmonth, 0).getDate();
  59. let list = [];
  60. for (let day = daysInMonth-week+2; day <= daysInMonth; day++) {
  61. let date = new Date(qyear, qmonth - 1, day);
  62. list.push({
  63. nyr: `${qyear}-${String(qmonth).padStart(2, '0')}-${String(day).padStart(2, '0')}`,
  64. year:qyear,
  65. month:qmonth,
  66. day,
  67. week: date.getDay()||7
  68. });
  69. }
  70. return list
  71. },
  72. appendDays(year,month,calendar){
  73. let week = calendar[calendar.length-1].week;
  74. if(week==7) return []
  75. let hyear = month==12?(year+1):year;
  76. let hmonth = month==12?1:month+1;
  77. let daysInMonth = new Date(hyear, hmonth, 0).getDate();
  78. let list = [];
  79. for (let day = 1; day <= 7-week; day++) {
  80. let date = new Date(hyear, hmonth - 1, day);
  81. list.push({
  82. nyr: `${hyear}-${String(hmonth).padStart(2, '0')}-${String(day).padStart(2, '0')}`,
  83. year:hyear,
  84. month:hmonth,
  85. day,
  86. week: date.getDay()||7
  87. });
  88. }
  89. return list
  90. },
  91. async selectDay(pre,idx){
  92. if(pre?.month==this.month){
  93. this.calendarList.forEach((c,i)=>{
  94. this.$set(this.calendarList[i],'select',i===idx);
  95. })
  96. }else{
  97. this.year = pre.year;
  98. this.month = pre.month;
  99. await this.getDataByMonth(pre.year,pre.month)
  100. let idx = this.calendarList.findIndex(c=>c.month==pre.month);
  101. this.$set(this.calendarList[idx],'select',true);
  102. this.$emit('selectDay',pre);
  103. }
  104. },
  105. }
  106. }
  107. </script>
  108. <style scoped lang="less">
  109. .box{
  110. .week{
  111. display: flex;
  112. .w_pre{
  113. width: calc(100% / 7);
  114. font-family: PingFangSC, PingFang SC;
  115. font-weight: 400;
  116. font-size: 28rpx;
  117. color: #9D9D9D;
  118. line-height: 72rpx;
  119. text-align: center;
  120. }
  121. }
  122. .date{
  123. display: flex;
  124. flex-wrap: wrap;
  125. .d_pre{
  126. width: calc(100% / 7);
  127. display: flex;
  128. flex-direction: column;
  129. align-items: center;
  130. padding: 15rpx 0;
  131. .day{
  132. width: 64rpx;
  133. height: 64rpx;
  134. border-radius: 16rpx;
  135. background: #FFFFFF;
  136. font-family: PingFangSC, PingFang SC;
  137. font-weight: 400;
  138. font-size: 28rpx;
  139. color: #9D9D9D;
  140. line-height: 64rpx;
  141. text-align: center;
  142. &.wd{
  143. color: #1D2129;
  144. }
  145. &.dq{
  146. color: #2E69EB;
  147. font-weight: bold;
  148. background: rgba(46,105,235,0.1);
  149. }
  150. &.select{
  151. color: #FFFFFF;
  152. font-weight: bold;
  153. background: #2E69EB;
  154. }
  155. }
  156. .status{
  157. width: 10rpx;
  158. height: 10rpx;
  159. border-radius: 50%;
  160. margin-top: 5rpx;
  161. &.zc{
  162. background: #2E69EB;
  163. }
  164. &.yc{
  165. background: #FEA400;
  166. }
  167. }
  168. }
  169. }
  170. }
  171. </style>