gui-calendar.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <template>
  2. <gui-popup
  3. ref="guipopupforcalendar"
  4. position="bottom"
  5. :canCloseByShade="false"
  6. :zIndex="zIndex">
  7. <view class="gui-calendar gui-bg-white gui-box-shadow"
  8. @tap.stop.prevent="stopfun">
  9. <!-- 顶部日期及切换 -->
  10. <view class="gui-calendar-header gui-flex gui-rows gui-nowrap gui-justify-content-center gui-align-items-center">
  11. <text class="gui-calendar-header-btn gui-icons"
  12. @click="prevYear">&#xe600;&#xe600;</text>
  13. <text class="gui-calendar-header-btn gui-icons"
  14. @click="prevMonth">&#xe600;</text>
  15. <text class="gui-calendar-header-btn gui-icons"
  16. style="margin:20rpx;">{{cYear}} 年 {{cMonth}} 月</text>
  17. <text class="gui-calendar-header-btn gui-icons"
  18. @click="nextMonth">&#xe601;</text>
  19. <text class="gui-calendar-header-btn gui-icons"
  20. @click="nextYear">&#xe601;&#xe601;</text>
  21. </view>
  22. <!-- 星期 -->
  23. <view class="gui-flex gui-rows gui-nowrap gui-justify-content-center gui-align-items-center">
  24. <text class="gui-calendar-weeks gui-block-text" v-for="(item, index) in weeks" :key="index">{{item}}</text>
  25. </view>
  26. <!-- 日历列表 -->
  27. <view class="gui-bg-gray gui-flex gui-rows gui-wrap gui-calendar-days">
  28. <view class="gui-calendar-ditems gui-flex gui-columns gui-justify-content-center gui-align-items-center"
  29. :style="{
  30. backgroundColor: currentDayIn == cYear+'-'+cMonthStr+'-'+ item.date ? activeBgColor : bgColor,
  31. borderRadius:borderRadius}"
  32. @click="chooseDate(cYear+'-'+cMonthStr+'-'+item.date, item.date)"
  33. v-for="(item, index) in days" :key="index">
  34. <text class="gui-date-day"
  35. :style="{color : currentDayIn == (cYear+'-'+cMonthStr+'-'+item.date) ? '#FFFFFF' : 'rgba(69, 90, 100, 0.6)'}">{{item.date}}</text>
  36. <text class="gui-date-nl" v-if="isLunar"
  37. :style="{color : currentDayIn == (cYear+'-'+cMonthStr+'-'+item.date) ? '#FFFFFF' : 'rgba(69, 90, 100, 0.6)'}">{{item.nl}}</text>
  38. </view>
  39. </view>
  40. <!-- 时间选择 -->
  41. <view
  42. class="gui-flex gui-rows gui-justify-content-center gui-align-items-center gui-bg-gray "
  43. v-if="isTime">
  44. <picker mode="time" @change="timechange" :value="currentTimeIn">
  45. <text
  46. class="gui-date-time gui-border-b gui-border-t gui-block-text"
  47. style="border-color:#D1D1D1;" >时间 : {{currentTimeIn}}</text>
  48. </picker>
  49. </view>
  50. <!-- 按钮 -->
  51. <view class="gui-flex gui-rows gui-space-between gui-align-items-center">
  52. <view class="gui-date-btns-text" hover-class="gui-tap" @tap="close">
  53. <text class="gui-date-btns-text gui-block-text gui-color-gray">取消</text>
  54. </view>
  55. <view class="gui-date-btns-text" hover-class="gui-tap" @tap="submit">
  56. <text class="gui-date-btns-text gui-block-text gui-primary-color">确认</text>
  57. </view>
  58. </view>
  59. </view>
  60. </gui-popup>
  61. </template>
  62. <script>
  63. import guiCalendar from './gui-calendar.js';
  64. export default {
  65. name : "gui-calendar",
  66. props : {
  67. currentDate : { type : String, default : "" },
  68. isTime : { type : Boolean, default : true },
  69. bgColor : {type : String, default : "#F7F8FA"},
  70. activeBgColor : {type : String, default : "#008AFF"},
  71. borderRadius : {type : String, default : "6rpx"},
  72. isLunar : {type : Boolean, default : true },
  73. zIndex : {type : Number, default : 2}
  74. },
  75. data(){
  76. return {
  77. weeks : ['一', '二', '三', '四', '五', '六', '日'],
  78. cYear : 2016,
  79. cMonth : 6,
  80. cMonthStr : "06",
  81. cDay : "01",
  82. days : '',
  83. currentDayIn : '',
  84. currentTimeIn : ''
  85. }
  86. },
  87. created:function(){
  88. this.initTime();
  89. },
  90. watch:{
  91. currentDate : function(){this.initTime();},
  92. },
  93. methods: {
  94. stopfun:function(e){e.stopPropagation(); return ;},
  95. timechange : function(e){
  96. this.currentTimeIn = e.detail.value;
  97. },
  98. getDaysInOneMonth : function (){
  99. var d = new Date(this.cYear, this.cMonth, 0);
  100. return d.getDate();
  101. },
  102. getDay : function (){
  103. var d = new Date(this.cYear, this.cMonth - 1, 0);
  104. return d.getDay();
  105. },
  106. prevYear : function(){
  107. this.cYear = this.cYear - 1;
  108. this.changeMonth();
  109. },
  110. prevMonth : function(){
  111. this.cMonth = this.cMonth - 1;
  112. if (this.cMonth < 1) { this.cMonth = 12; this.cYear = this.cYear - 1; }
  113. this.cMonthStr = this.cMonth < 10 ? '0' + this.cMonth : this.cMonth;
  114. this.changeMonth();
  115. },
  116. nextMonth : function(){
  117. this.cMonth = this.cMonth + 1;
  118. if (this.cMonth > 12){this.cMonth = 1; this.cYear = this.cYear + 1;}
  119. this.cMonthStr = this.cMonth < 10 ? '0' + this.cMonth : this.cMonth;
  120. this.changeMonth();
  121. },
  122. nextYear : function(){
  123. this.cYear = this.cYear + 1;
  124. this.changeMonth();
  125. },
  126. changeMonth:function(){
  127. var daysList = [];
  128. var days = this.getDaysInOneMonth();
  129. var startWeek = this.getDay();
  130. var forSteps = 0;
  131. for (var i = (0 - startWeek); i < days; i++){
  132. if(i >= 0){
  133. daysList[forSteps] = {date : i >= 9 ? i + 1 : '0' + (i+1), nl : ''};
  134. daysList[forSteps].nl = guiCalendar.getLunarInfo(this.cYear, this.cMonth, i + 1);
  135. }else{
  136. daysList[forSteps] = '';
  137. }
  138. forSteps++;
  139. }
  140. this.days = daysList;
  141. },
  142. chooseDate: function (sedDate, isday) {
  143. if(!isday){return ;}
  144. this.currentDayIn = sedDate;
  145. if(this.isTime){return ;}
  146. this.$emit('changeDate', sedDate);
  147. },
  148. submit : function(){
  149. if(this.isTime){
  150. this.$emit('changeDate', this.currentDayIn + ' ' + this.currentTimeIn);
  151. this.$emit('confirm' , this.currentDayIn + ' ' + this.currentTimeIn);
  152. }else{
  153. this.$emit('changeDate', this.currentDayIn);
  154. this.$emit('confirm' , this.currentDayIn);
  155. }
  156. this.close();
  157. },
  158. //初始化时间
  159. initTime : function(){
  160. if(this.currentDate == ''){
  161. var dateObj = new Date();
  162. this.cYear = dateObj.getFullYear();
  163. this.cMonth = dateObj.getMonth() + 1;
  164. this.cMonthStr = this.cMonth < 10 ? '0' + this.cMonth : this.cMonth;
  165. this.cDay = dateObj.getDate();
  166. this.cDay = this.cDay < 10 ? '0' + this.cDay : this.cDay;
  167. this.currentDayIn = this.cYear + '-' + this.cMonthStr + '-' + this.cDay;
  168. this.currentTimeIn = '00:00';
  169. this.changeMonth();
  170. }else{
  171. var dates = this.currentDate.split(' ');
  172. if (!dates[1]) { dates[1] = '';}
  173. var dayArr = dates[0].split('-');
  174. this.cYear = Number(dayArr[0]);
  175. this.cMonth = dayArr[1];
  176. this.cDay = dayArr[2];
  177. var reg = new RegExp('^0[0-9]+$');
  178. if(reg.test(this.cMonth)){this.cMonth = this.cMonth.substr(1,1);}
  179. this.cMonth = Number(this.cMonth);
  180. this.cMonthStr = this.cMonth < 10 ? '0'+this.cMonth : this.cMonth;
  181. this.currentDayIn = dates[0];
  182. this.currentTimeIn = dates[1];
  183. this.changeMonth();
  184. }
  185. },
  186. open:function(){this.$refs.guipopupforcalendar.open();},
  187. close:function(){this.$refs.guipopupforcalendar.close();}
  188. }
  189. }
  190. </script>
  191. <style scoped>
  192. .gui-calendar{border-top-left-radius:10rpx; border-top-right-radius:10rpx; padding:10rpx 0;}
  193. .gui-calendar-header{height:80rpx;}
  194. .gui-calendar-header-btn{font-size:32rpx; padding:0 10rpx; color:rgba(69, 90, 100, 0.6);}
  195. .gui-calendar-weeks{width:100rpx; color:#2B2E3D; height:80rpx; text-align:center; font-size:30rpx; line-height:80rpx;}
  196. .gui-calendar-days{padding:25rpx;}
  197. .gui-calendar-ditems{width:96rpx; height:96rpx; margin:2rpx;}
  198. .gui-date-day{height:38rpx; line-height:38rpx; text-align:center; font-size:32rpx;}
  199. .gui-date-nl{height:26rpx; line-height:26rpx; font-size:20rpx; text-align:center;}
  200. .gui-date-btns-text{line-height:100rpx; font-size:28rpx; text-align:center; width:300rpx;}
  201. .gui-date-time{font-size:28rpx; line-height:80rpx; height:80rpx; margin-bottom:30rpx;}
  202. </style>