shortterm-picker.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <template>
  2. <view class="w-picker-view">
  3. <picker-view class="d-picker-view" :indicator-style="itemHeight" :value="pickVal" @change="handlerChange">
  4. <picker-view-column>
  5. <view class="w-picker-item" v-for="(item,index) in range.dates" :key="index">{{item.label}}</view>
  6. </picker-view-column>
  7. <picker-view-column>
  8. <view class="w-picker-item" v-for="(item,index) in range.hours" :key="index">{{item.label}}时</view>
  9. </picker-view-column>
  10. <picker-view-column>
  11. <view class="w-picker-item" v-for="(item,index) in range.minutes" :key="index">{{item.label}}分</view>
  12. </picker-view-column>
  13. </picker-view>
  14. </view>
  15. </template>
  16. <script>
  17. let _this=null;
  18. export default {
  19. data() {
  20. return {
  21. pickVal:[],
  22. range:{},
  23. checkObj:{}
  24. };
  25. },
  26. props:{
  27. itemHeight:{
  28. type:String,
  29. default:"44px"
  30. },
  31. value:{
  32. type:[String,Array,Number],
  33. default:""
  34. },
  35. current:{//是否默认选中当前日期
  36. type:Boolean,
  37. default:false
  38. },
  39. expand:{
  40. type:[Number,String],
  41. default:30
  42. }
  43. },
  44. watch:{
  45. value(val){
  46. this.initData();
  47. }
  48. },
  49. created() {
  50. _this=this;
  51. _this.initData();
  52. },
  53. methods:{
  54. formatNum(n){
  55. return (Number(n)<10?'0'+Number(n):Number(n)+'');
  56. },
  57. checkValue(value){
  58. let strReg=/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}(:\d{2})?$/,example="2019-12-12 18:05:00或者2019-12-12 18:05";
  59. if(!strReg.test(value)){
  60. console.log(new Error("请传入与mode、fields匹配的value值,例value="+example+""))
  61. }
  62. return strReg.test(value);
  63. },
  64. resetData(year,month,day){
  65. let curDate=_this.getCurrenDate();
  66. let curFlag=_this.current;
  67. let curYear=curDate.curYear;
  68. let curMonth=curDate.curMonth;
  69. let curDay=curDate.curDay;
  70. let curHour=curDate.curHour;
  71. let months=[],days=[],sections=[];
  72. let disabledAfter=_this.disabledAfter;
  73. let monthsLen=disabledAfter?(year*1<curYear?12:curMonth):12;
  74. let totalDays=new Date(year,month,0).getDate();//计算当月有几天;
  75. for(let month=1;month<=monthsLen;month++){
  76. months.push(_this.formatNum(month));
  77. };
  78. for(let day=1;day<=daysLen;day++){
  79. days.push(_this.formatNum(day));
  80. }
  81. return{
  82. months,
  83. days,
  84. sections
  85. }
  86. },
  87. getData(dVal){
  88. //用来处理初始化数据
  89. let curFlag=_this.current;
  90. let disabledAfter=_this.disabledAfter;
  91. let dates=[],hours=[],minutes=[];
  92. let curDate=new Date();
  93. let curYear=curDate.getFullYear();
  94. let curMonth=curDate.getMonth();
  95. let curDay=curDate.getDate();
  96. let aDate=new Date(curYear,curMonth,curDay);
  97. for(let i=0;i<this.expand*1;i++){
  98. aDate=new Date(curYear,curMonth,curDay+i);
  99. let year=aDate.getFullYear();
  100. let month=aDate.getMonth()+1;
  101. let day=aDate.getDate();
  102. let label=year+"-"+_this.formatNum(month)+"-"+_this.formatNum(day);
  103. switch(i){
  104. case 0:
  105. label="今天";
  106. break;
  107. case 1:
  108. label="明天";
  109. break;
  110. case 2:
  111. label="后天";
  112. break
  113. }
  114. dates.push({
  115. label:label,
  116. value:year+"-"+_this.formatNum(month)+"-"+_this.formatNum(day)
  117. })
  118. };
  119. for(let i=0;i<24;i++){
  120. hours.push({
  121. label:_this.formatNum(i),
  122. value:_this.formatNum(i)
  123. })
  124. }
  125. for(let i=0;i<60;i++){
  126. minutes.push({
  127. label:_this.formatNum(i),
  128. value:_this.formatNum(i)
  129. })
  130. }
  131. return {
  132. dates,
  133. hours,
  134. minutes
  135. }
  136. },
  137. getDefaultDate(){
  138. let value=_this.value;
  139. let reg=/-/g;
  140. let defaultDate=value?new Date(value.replace(reg,"/")):new Date();
  141. let defaultYear=defaultDate.getFullYear();
  142. let defaultMonth=defaultDate.getMonth()+1;
  143. let defaultDay=defaultDate.getDate();
  144. let defaultDays=new Date(defaultYear,defaultMonth,0).getDate()*1;
  145. return{
  146. defaultDate,
  147. defaultYear,
  148. defaultMonth,
  149. defaultDay,
  150. defaultDays
  151. }
  152. },
  153. getDval(){
  154. let value=_this.value;
  155. let dVal=null;
  156. let aDate=new Date();
  157. let year=_this.formatNum(aDate.getFullYear());
  158. let month=_this.formatNum(aDate.getMonth()+1);
  159. let day=_this.formatNum(aDate.getDate());
  160. let date=_this.formatNum(year)+"-"+_this.formatNum(month)+"-"+_this.formatNum(day);
  161. let hour=aDate.getHours();
  162. let minute=aDate.getMinutes();
  163. if(value){
  164. let flag=_this.checkValue(value);
  165. if(!flag){
  166. dVal=[date,hour,minute]
  167. }else{
  168. let v=value.split(" ");
  169. dVal=[v[0],...v[1].split(":")];
  170. }
  171. }else{
  172. dVal=[date,hour,minute]
  173. }
  174. return dVal;
  175. },
  176. initData(){
  177. let startDate,endDate,startYear,endYear,startMonth,endMonth,startDay,endDay;
  178. let dates=[],hours=[],minutes=[];
  179. let dVal=[],pickVal=[];
  180. let value=_this.value;
  181. let reg=/-/g;
  182. let range={};
  183. let result="",full="",date,hour,minute,obj={};
  184. let defaultDate=_this.getDefaultDate();
  185. let defaultYear=defaultDate.defaultYear;
  186. let defaultMonth=defaultDate.defaultMonth;
  187. let defaultDay=defaultDate.defaultDay;
  188. let defaultDays=defaultDate.defaultDays;
  189. let curFlag=this.current;
  190. let disabledAfter=this.disabledAfter;
  191. let dateData=[];
  192. dVal=_this.getDval();
  193. dateData=_this.getData(dVal);
  194. dates=dateData.dates;
  195. hours=dateData.hours;
  196. minutes=dateData.minutes;
  197. pickVal=[
  198. dates.findIndex(n => n.value == dVal[0])!=-1?dates.findIndex(n => n.value == dVal[0]):0,
  199. hours.findIndex(n => n.value == dVal[1])!=-1?hours.findIndex(n => n.value == dVal[1]):0,
  200. minutes.findIndex(n => n.value == dVal[2])!=-1?minutes.findIndex(n => n.value == dVal[2]):0,
  201. ];
  202. range={dates,hours,minutes};
  203. date=dVal[0]?dVal[0]:dates[0].label;
  204. hour=dVal[1]?dVal[1]:hours[0].label;
  205. minute=dVal[2]?dVal[2]:minutes[0].label;
  206. result=full=`${date+' '+hour+':'+minute}`;
  207. obj={
  208. date,
  209. hour,
  210. minute
  211. }
  212. _this.range=range;
  213. _this.checkObj=obj;
  214. _this.$nextTick(()=>{
  215. _this.pickVal=pickVal;
  216. });
  217. _this.$emit("change",{
  218. result:result,
  219. value:full,
  220. obj:obj
  221. })
  222. },
  223. handlerChange(e){
  224. let arr=[...e.detail.value];
  225. let data=_this.range;
  226. let date="",hour="",minute="";
  227. let result="",full="",obj={};
  228. let disabledAfter=_this.disabledAfter;
  229. date=(arr[0]||arr[0]==0)?data.dates[arr[0]]||data.dates[data.dates.length-1]:"";
  230. hour=(arr[1]||arr[1]==0)?data.hours[arr[1]]||data.hours[data.hours.length-1]:"";
  231. minute=(arr[2]||arr[2]==0)?data.minutes[arr[2]]||data.minutes[data.minutes.length-1]:"";
  232. result=full=`${date.label+' '+hour.label+':'+minute.label+':00'}`;
  233. obj={
  234. date,
  235. hour,
  236. minute
  237. }
  238. this.checkObj=obj;
  239. _this.$emit("change",{
  240. result:result,
  241. value:full,
  242. obj:obj
  243. })
  244. }
  245. }
  246. }
  247. </script>
  248. <style lang="scss">
  249. @import "./w-picker.css";
  250. </style>