gui-date-between.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <template>
  2. <view>
  3. <view class="gui-flex gui-date-bt-block"
  4. :style="{backgroundColor:weekBg}">
  5. <text class="gui-date-bt-item gui-date-bt-week gui-block-text">一</text>
  6. <text class="gui-date-bt-item gui-date-bt-week gui-block-text">二</text>
  7. <text class="gui-date-bt-item gui-date-bt-week gui-block-text">三</text>
  8. <text class="gui-date-bt-item gui-date-bt-week gui-block-text">四</text>
  9. <text class="gui-date-bt-item gui-date-bt-week gui-block-text">五</text>
  10. <text class="gui-date-bt-item gui-date-bt-week gui-block-text">六</text>
  11. <text class="gui-date-bt-item gui-date-bt-week gui-block-text">日</text>
  12. </view>
  13. <view v-for="(days, daysIndex) in daysData" :key="daysIndex">
  14. <view class="gui-flex gui-date-bt-month-w">
  15. <text class="gui-date-bt-month gui-block-text"
  16. :style="{backgroundColor:startBg}">{{days[0][0]}}{{unit[0]}}{{days[0][1]}}{{unit[1]}}</text>
  17. </view>
  18. <view class="gui-flex gui-date-bt-block">
  19. <text class="gui-date-bt-item gui-date-bt-days"
  20. v-for="(item, index) in days[1]" :key="index"
  21. :style="{
  22. backgroundColor:item[1] ? sedBg : itemBg,
  23. color:item[1] ? sedColor : color
  24. }"
  25. :data-dt="item[0] == '' ? '' : days[0][0]+''+days[0][1]+''+item[0]"
  26. @tap.stop="selectDay">{{item[0]}}<text class="gui-date-bt-start"
  27. v-if="sedDays[0] == days[0][0]+''+days[0][1]+''+item[0] || sedDays[1] == days[0][0]+''+days[0][1]+''+item[0]"
  28. :style="{
  29. backgroundColor : sedDays[1] == days[0][0]+''+days[0][1]+''+item[0] ? endBg : startBg,
  30. color:sedColor}">{{item[0]}}</text></text>
  31. </view>
  32. </view>
  33. </view>
  34. </template>
  35. <script>
  36. /* 组件百分百原创 拒绝任何代码抄袭及思路抄袭 */
  37. export default{
  38. name : "gui-date-between",
  39. props : {
  40. weekBg : { type : String, default:'#F8F8F8'},
  41. unit : { type : Array, default:function(){return [' 年 ',' 月'];}},
  42. sedBg : { type : String, default:'rgba(54,136,255,0.8)'},
  43. itemBg : { type : String, default:'#FFFFFF'},
  44. color : { type : String, default:'#323232'},
  45. sedColor : { type : String, default:'#FFFFFF'},
  46. startBg : { type : String, default:'rgba(54,136,255,1)'},
  47. endBg : { type : String, default:'rgba(54,136,255,1)'},
  48. monthNumber : { type : Number, default:2}
  49. },
  50. data() {
  51. return {
  52. sedDays:[0,0],
  53. btDays :[],
  54. daysData : []
  55. }
  56. },
  57. created:function(){
  58. this.setMonth(this.currentMonth());
  59. },
  60. methods:{
  61. setBetween:function(days){
  62. if(days[1] == 0){days[1] = days[0];}
  63. days[0] = Number(days[0]);
  64. days[1] = Number(days[1]);
  65. this.sedDays = days;
  66. var countNumber = 0;
  67. var daysNew = [];
  68. this.daysData.forEach((itm)=>{
  69. var year = itm[0][0];
  70. var month = itm[0][1];
  71. var daysIn = [];
  72. itm[1].forEach((item)=>{
  73. var cDay = year+''+month+''+item[0];
  74. cDay = Number(cDay);
  75. if(cDay >= days[0] && cDay <= days[1]){
  76. item[1] = true;
  77. countNumber++;
  78. }else{
  79. item[1] = false;
  80. }
  81. daysIn.push(item);
  82. });
  83. daysNew.push([[year, month], daysIn]);
  84. });
  85. this.daysData = daysNew;
  86. this.$emit('selectDate', [days, countNumber]);
  87. },
  88. setMonth:function(month){
  89. var reg = /^([0-9]{4}).*([0-9]{2}).*$/;
  90. var res = month.match(reg);
  91. if(res == null){month = this.currentMonth(); res = month.match(reg);}
  92. this.setMonthBase(res);
  93. },
  94. setMonthBase : function(res){
  95. var daysData = [];
  96. if(res[2].substr(0,1) == '0'){res[2] = res[2].substr(1);}
  97. res[1] = Number(res[1]);
  98. res[2] = Number(res[2]);
  99. var year = res[1];
  100. var month = res[2];
  101. for(let i = 0; i < this.monthNumber; i++){
  102. var monthIn = month + i;
  103. var yearIn = year;
  104. if(monthIn > 12){monthIn = monthIn - 12; yearIn += 1;}
  105. if(monthIn < 10){monthIn = '0'+monthIn;}
  106. daysData[i] = [];
  107. daysData[i].push([yearIn, monthIn]);
  108. var days = this.getDays(yearIn,monthIn);
  109. var daysList = [];
  110. for (let ii = (0 - days[1]); ii < days[0]; ii++){
  111. if(ii >= 0){
  112. daysList.push([ii >= 9 ? ii + 1 : '0' + (ii+1), false]);
  113. }else{
  114. daysList.push(['',false]);
  115. }
  116. }
  117. daysData[i].push(daysList);
  118. }
  119. this.daysData = daysData;
  120. },
  121. currentMonth : function () {
  122. var date = new Date();
  123. var y = date.getFullYear();
  124. var m = date.getMonth() + 1;
  125. m = m < 10 ? ('0' + m) : m;
  126. return y + '年' + m + '月';
  127. },
  128. getDays : function(year,month){
  129. var d = new Date(year, month, 0);
  130. var days = d.getDate();
  131. var d2 = new Date(year, month - 1, 0);
  132. var startWeek = d2.getDay();
  133. return [days, startWeek];
  134. },
  135. selectDay:function(e){
  136. var day = e.currentTarget.dataset.dt;
  137. if(day == ''){return ;}
  138. day = Number(day);
  139. if(this.sedDays[0] == 0){ this.sedDays[0] = day; }
  140. else if(this.sedDays[1] == 0 || this.sedDays[0] == this.sedDays[1]){
  141. if(day > this.sedDays[0]){
  142. this.sedDays[1] = day;
  143. }else if(day < this.sedDays[0]){
  144. this.sedDays[1] = this.sedDays[0];
  145. this.sedDays[0] = day;
  146. }
  147. }else{
  148. this.sedDays[0] = day;
  149. this.sedDays[1] = 0;
  150. }
  151. this.setBetween(this.sedDays);
  152. }
  153. }
  154. }
  155. </script>
  156. <style scoped>
  157. .gui-date-bt-block{flex-direction:row; flex-wrap:wrap;}
  158. .gui-date-bt-item{width:98rpx; text-align:center; position:relative;}
  159. .gui-date-bt-week{line-height:80rpx; height:80rpx; font-size:28rpx;}
  160. .gui-date-bt-month-w{flex-direction:row; flex-wrap:nowrap; margin:25px 0;}
  161. .gui-date-bt-month{line-height:50rpx; text-align:center; color:#FFFFFF; width:258rpx; border-radius:30rpx; font-size:26rpx;}
  162. .gui-date-bt-days{line-height:98rpx; height:98rpx; width:98rpx; font-size:28rpx;}
  163. .gui-date-bt-start{position:absolute; font-size:28rpx; font-weight:bold; width:98rpx; height:98rpx; text-align:center; line-height:98rpx; z-index:1; left:0; top:0;}
  164. </style>