date-picker.vue 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. <template>
  2. <view class="w-picker-view">
  3. <picker-view v-if="fields=='year'" 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.years" :key="index">{{item}}年</view>
  6. </picker-view-column>
  7. </picker-view>
  8. <picker-view v-if="fields=='month'" class="d-picker-view" :indicator-style="itemHeight" :value="pickVal" @change="handlerChange">
  9. <picker-view-column>
  10. <view class="w-picker-item" v-for="(item,index) in range.years" :key="index">{{item}}年</view>
  11. </picker-view-column>
  12. <picker-view-column>
  13. <view class="w-picker-item" v-for="(item,index) in range.months" :key="index">{{item}}月</view>
  14. </picker-view-column>
  15. </picker-view>
  16. <picker-view v-if="fields=='day'" class="d-picker-view" :indicator-style="itemHeight" :value="pickVal" @change="handlerChange">
  17. <picker-view-column>
  18. <view class="w-picker-item" v-for="(item,index) in range.years" :key="index">{{item}}年</view>
  19. </picker-view-column>
  20. <picker-view-column>
  21. <view class="w-picker-item" v-for="(item,index) in range.months" :key="index">{{item}}月</view>
  22. </picker-view-column>
  23. <picker-view-column>
  24. <view class="w-picker-item" v-for="(item,index) in range.days" :key="index">{{item}}日</view>
  25. </picker-view-column>
  26. </picker-view>
  27. <picker-view v-if="fields=='hour'" class="d-picker-view" :indicator-style="itemHeight" :value="pickVal" @change="handlerChange">
  28. <picker-view-column>
  29. <view class="w-picker-item" v-for="(item,index) in range.years" :key="index">{{item}}年</view>
  30. </picker-view-column>
  31. <picker-view-column>
  32. <view class="w-picker-item" v-for="(item,index) in range.months" :key="index">{{item}}月</view>
  33. </picker-view-column>
  34. <picker-view-column>
  35. <view class="w-picker-item" v-for="(item,index) in range.days" :key="index">{{item}}日</view>
  36. </picker-view-column>
  37. <picker-view-column>
  38. <view class="w-picker-item" v-for="(item,index) in range.hours" :key="index">{{item}}时</view>
  39. </picker-view-column>
  40. </picker-view>
  41. <picker-view v-if="fields=='minute'" class="d-picker-view" :indicator-style="itemHeight" :value="pickVal" @change="handlerChange">
  42. <picker-view-column>
  43. <view class="w-picker-item" v-for="(item,index) in range.years" :key="index">{{item}}年</view>
  44. </picker-view-column>
  45. <picker-view-column>
  46. <view class="w-picker-item" v-for="(item,index) in range.months" :key="index">{{item}}月</view>
  47. </picker-view-column>
  48. <picker-view-column>
  49. <view class="w-picker-item" v-for="(item,index) in range.days" :key="index">{{item}}日</view>
  50. </picker-view-column>
  51. <picker-view-column>
  52. <view class="w-picker-item" v-for="(item,index) in range.hours" :key="index">{{item}}时</view>
  53. </picker-view-column>
  54. <picker-view-column>
  55. <view class="w-picker-item" v-for="(item,index) in range.minutes" :key="index">{{item}}分</view>
  56. </picker-view-column>
  57. </picker-view>
  58. <picker-view v-if="fields=='second'" class="d-picker-view" :indicator-style="itemHeight" :value="pickVal" @change="handlerChange">
  59. <picker-view-column>
  60. <view class="w-picker-item" v-for="(item,index) in range.years" :key="index">{{item}}年</view>
  61. </picker-view-column>
  62. <picker-view-column>
  63. <view class="w-picker-item" v-for="(item,index) in range.months" :key="index">{{item}}月</view>
  64. </picker-view-column>
  65. <picker-view-column>
  66. <view class="w-picker-item" v-for="(item,index) in range.days" :key="index">{{item}}日</view>
  67. </picker-view-column>
  68. <picker-view-column>
  69. <view class="w-picker-item" v-for="(item,index) in range.hours" :key="index">{{item}}时</view>
  70. </picker-view-column>
  71. <picker-view-column>
  72. <view class="w-picker-item" v-for="(item,index) in range.minutes" :key="index">{{item}}分</view>
  73. </picker-view-column>
  74. <picker-view-column>
  75. <view class="w-picker-item" v-for="(item,index) in range.seconds" :key="index">{{item}}秒</view>
  76. </picker-view-column>
  77. </picker-view>
  78. <!-- <picker-view class="d-picker-view" :indicator-style="itemHeight" :value="pickVal" @change="handlerChange">
  79. <picker-view-column>
  80. <view class="w-picker-item" v-for="(item,index) in range.years" :key="index">{{item}}年</view>
  81. </picker-view-column>
  82. <picker-view-column v-if="fields=='month'||fields=='day'||fields=='hour'||fields=='minute'||fields=='second'">
  83. <view class="w-picker-item" v-for="(item,index) in range.months" :key="index">{{item}}月</view>
  84. </picker-view-column>
  85. <picker-view-column v-if="fields=='day'||fields=='hour'||fields=='minute'||fields=='second'">
  86. <view class="w-picker-item" v-for="(item,index) in range.days" :key="index">{{item}}日</view>
  87. </picker-view-column>
  88. <picker-view-column v-if="fields=='hour'||fields=='minute'||fields=='second'">
  89. <view class="w-picker-item" v-for="(item,index) in range.hours" :key="index">{{item}}时</view>
  90. </picker-view-column>
  91. <picker-view-column v-if="fields=='minute'||fields=='second'">
  92. <view class="w-picker-item" v-for="(item,index) in range.minutes" :key="index">{{item}}分</view>
  93. </picker-view-column>
  94. <picker-view-column v-if="fields=='second'">
  95. <view class="w-picker-item" v-for="(item,index) in range.seconds" :key="index">{{item}}秒</view>
  96. </picker-view-column>
  97. </picker-view> -->
  98. </view>
  99. </template>
  100. <script>
  101. let _this=null;
  102. export default {
  103. data() {
  104. return {
  105. pickVal:[],
  106. range:{
  107. years:[],
  108. months:[],
  109. days:[],
  110. hours:[],
  111. minutes:[],
  112. seconds:[]
  113. },
  114. checkObj:{}
  115. };
  116. },
  117. props:{
  118. itemHeight:{
  119. type:String,
  120. default:"44px"
  121. },
  122. startYear:{
  123. type:[String,Number],
  124. default:""
  125. },
  126. endYear:{
  127. type:[String,Number],
  128. default:""
  129. },
  130. value:{
  131. type:[String,Array,Number],
  132. default:""
  133. },
  134. current:{//是否默认选中当前日期
  135. type:Boolean,
  136. default:false
  137. },
  138. disabledAfter:{//是否禁用当前之后的日期
  139. type:Boolean,
  140. default:false
  141. },
  142. fields:{
  143. type:String,
  144. default:"day"
  145. }
  146. },
  147. watch:{
  148. fields(val){
  149. this.initData();
  150. },
  151. value(val){
  152. this.initData();
  153. }
  154. },
  155. created() {
  156. _this=this;
  157. _this.initData();
  158. },
  159. methods:{
  160. formatNum(n){
  161. return (Number(n)<10?'0'+Number(n):Number(n)+'');
  162. },
  163. checkValue(value){
  164. let strReg,example
  165. switch(this.fields){
  166. case "year":
  167. strReg=/^\d{4}$/;
  168. example="2019";
  169. break;
  170. case "month":
  171. strReg=/^\d{4}-\d{2}$/;
  172. example="2019-02";
  173. break;
  174. case "day":
  175. strReg=/^\d{4}-\d{2}-\d{2}$/;
  176. example="2019-02-01";
  177. break;
  178. case "hour":
  179. strReg=/^\d{4}-\d{2}-\d{2} \d{2}(:\d{2}){1,2}?$/;
  180. example="2019-02-01 18:00:00或2019-02-01 18";
  181. break;
  182. case "minute":
  183. strReg=/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}(:\d{2}){0,1}?$/;
  184. example="2019-02-01 18:06:00或2019-02-01 18:06";
  185. break;
  186. case "second":
  187. strReg=/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/;
  188. example="2019-02-01 18:06:01";
  189. break;
  190. }
  191. if(!strReg.test(value)){
  192. console.log(new Error("请传入与mode、fields匹配的value值,例value="+example+""))
  193. }
  194. return strReg.test(value);
  195. },
  196. resetData(year,month,day,hour,minute){
  197. let curDate=_this.getCurrenDate();
  198. let curFlag=_this.current;
  199. let curYear=curDate.curYear;
  200. let curMonth=curDate.curMonth;
  201. let curDay=curDate.curDay;
  202. let curHour=curDate.curHour;
  203. let curMinute=curDate.curMinute;
  204. let curSecond=curDate.curSecond;
  205. let months=[],days=[],hours=[],minutes=[],seconds=[];
  206. let disabledAfter=_this.disabledAfter;
  207. let monthsLen=disabledAfter?(year*1<curYear?12:curMonth):12;
  208. let totalDays=new Date(year,month,0).getDate();//计算当月有几天;
  209. let daysLen=disabledAfter?((year*1<curYear||month*1<curMonth)?totalDays:curDay):totalDays;
  210. let hoursLen=disabledAfter?((year*1<curYear||month*1<curMonth||day*1<curDay)?24:curHour+1):24;
  211. let minutesLen=disabledAfter?((year*1<curYear||month*1<curMonth||day*1<curDay||hour*1<curHour)?60:curMinute+1):60;
  212. let secondsLen=disabledAfter?((year*1<curYear||month*1<curMonth||day*1<curDay||hour*1<curHour||minute*1<curMinute)?60:curSecond+1):60;
  213. for(let month=1;month<=monthsLen;month++){
  214. months.push(_this.formatNum(month));
  215. };
  216. for(let day=1;day<=daysLen;day++){
  217. days.push(_this.formatNum(day));
  218. }
  219. for(let hour=0;hour<hoursLen;hour++){
  220. hours.push(_this.formatNum(hour));
  221. }
  222. for(let minute=0;minute<minutesLen;minute++){
  223. minutes.push(_this.formatNum(minute));
  224. }
  225. for(let second=0;second<secondsLen;second++){
  226. seconds.push(_this.formatNum(second));
  227. }
  228. return{
  229. months,
  230. days,
  231. hours,
  232. minutes,
  233. seconds
  234. }
  235. },
  236. getData(dVal){
  237. //用来处理初始化数据
  238. let curFlag=_this.current;
  239. let disabledAfter=_this.disabledAfter;
  240. let fields=_this.fields;
  241. let curDate=_this.getCurrenDate();
  242. let curYear=curDate.curYear;
  243. let curMonthdays=curDate.curMonthdays;
  244. let curMonth=curDate.curMonth;
  245. let curDay=curDate.curDay;
  246. let curHour=curDate.curHour;
  247. let curMinute=curDate.curMinute;
  248. let curSecond=curDate.curSecond;
  249. let defaultDate=_this.getDefaultDate();
  250. let startYear=_this.getStartDate().getFullYear();
  251. let endYear=_this.getEndDate().getFullYear();
  252. //颗粒度,禁用当前之后日期仅对year,month,day,hour生效;分钟秒禁用没有意义,
  253. let years=[],months=[],days=[],hours=[],minutes=[],seconds=[];
  254. let year=dVal[0]*1;
  255. let month=dVal[1]*1;
  256. let day=dVal[2]*1;
  257. let hour=dVal[3]*1;
  258. let minute=dVal[4]*1;
  259. let monthsLen=disabledAfter?(year<curYear?12:curDate.curMonth):12;
  260. let daysLen=disabledAfter?((year<curYear||month<curMonth)?defaultDate.defaultDays:curDay):(curFlag?curMonthdays:defaultDate.defaultDays);
  261. let hoursLen=disabledAfter?((year<curYear||month<curMonth||day<curDay)?24:curHour+1):24;
  262. let minutesLen=disabledAfter?((year<curYear||month<curMonth||day<curDay||hour<curHour)?60:curMinute+1):60;
  263. let secondsLen=disabledAfter?((year<curYear||month<curMonth||day<curDay||hour<curHour||minute<curMinute)?60:curSecond+1):60;
  264. for(let year=startYear;year<=(disabledAfter?curYear:endYear);year++){
  265. years.push(year.toString())
  266. }
  267. for(let month=1;month<=monthsLen;month++){
  268. months.push(_this.formatNum(month));
  269. }
  270. for(let day=1;day<=daysLen;day++){
  271. days.push(_this.formatNum(day));
  272. }
  273. for(let hour=0;hour<hoursLen;hour++){
  274. hours.push(_this.formatNum(hour));
  275. }
  276. for(let minute=0;minute<minutesLen;minute++){
  277. minutes.push(_this.formatNum(minute));
  278. }
  279. // for(let second=0;second<(disabledAfter?curDate.curSecond+1:60);second++){
  280. // seconds.push(_this.formatNum(second));
  281. // }
  282. for(let second=0;second<60;second++){
  283. seconds.push(_this.formatNum(second));
  284. }
  285. return {
  286. years,
  287. months,
  288. days,
  289. hours,
  290. minutes,
  291. seconds
  292. }
  293. },
  294. getCurrenDate(){
  295. let curDate=new Date();
  296. let curYear=curDate.getFullYear();
  297. let curMonth=curDate.getMonth()+1;
  298. let curMonthdays=new Date(curYear,curMonth,0).getDate();
  299. let curDay=curDate.getDate();
  300. let curHour=curDate.getHours();
  301. let curMinute=curDate.getMinutes();
  302. let curSecond=curDate.getSeconds();
  303. return{
  304. curDate,
  305. curYear,
  306. curMonth,
  307. curMonthdays,
  308. curDay,
  309. curHour,
  310. curMinute,
  311. curSecond
  312. }
  313. },
  314. getDefaultDate(){
  315. let value=_this.value;
  316. let reg=/-/g;
  317. let defaultDate=value?new Date(value.replace(reg,"/")):new Date();
  318. let defaultYear=defaultDate.getFullYear();
  319. let defaultMonth=defaultDate.getMonth()+1;
  320. let defaultDay=defaultDate.getDate();
  321. let defaultDays=new Date(defaultYear,defaultMonth,0).getDate()*1;
  322. return{
  323. defaultDate,
  324. defaultYear,
  325. defaultMonth,
  326. defaultDay,
  327. defaultDays
  328. }
  329. },
  330. getStartDate(){
  331. let start=_this.startYear;
  332. let startDate="";
  333. let reg=/-/g;
  334. if(start){
  335. startDate=new Date(start+"/01/01");
  336. }else{
  337. startDate=new Date("1970/01/01");
  338. }
  339. return startDate;
  340. },
  341. getEndDate(){
  342. let end=_this.endYear;
  343. let reg=/-/g;
  344. let endDate="";
  345. if(end){
  346. endDate=new Date(end+"/12/01");
  347. }else{
  348. endDate=new Date();
  349. }
  350. return endDate;
  351. },
  352. getDval(){
  353. let value=_this.value;
  354. let fields=_this.fields;
  355. let dVal=null;
  356. let aDate=new Date();
  357. let year=_this.formatNum(aDate.getFullYear());
  358. let month=_this.formatNum(aDate.getMonth()+1);
  359. let day=_this.formatNum(aDate.getDate());
  360. let hour=_this.formatNum(aDate.getHours());
  361. let minute=_this.formatNum(aDate.getMinutes());
  362. let second=_this.formatNum(aDate.getSeconds());
  363. if(value){
  364. let flag=_this.checkValue(value);
  365. if(!flag){
  366. dVal=[year,month,day,hour,minute,second]
  367. }else{
  368. switch(_this.fields){
  369. case "year":
  370. dVal=value?[value]:[];
  371. break;
  372. case "month":
  373. dVal=value?value.split("-"):[];
  374. break;
  375. case "day":
  376. dVal=value?value.split("-"):[];
  377. break;
  378. case "hour":
  379. dVal=[...value.split(" ")[0].split("-"),...value.split(" ")[1].split(":")];
  380. break;
  381. case "minute":
  382. dVal=value?[...value.split(" ")[0].split("-"),...value.split(" ")[1].split(":")]:[];
  383. break;
  384. case "second":
  385. dVal=[...value.split(" ")[0].split("-"),...value.split(" ")[1].split(":")];
  386. break;
  387. }
  388. }
  389. }else{
  390. dVal=[year,month,day,hour,minute,second]
  391. }
  392. return dVal;
  393. },
  394. initData(){
  395. let startDate,endDate,startYear,endYear,startMonth,endMonth,startDay,endDay;
  396. let years=[],months=[],days=[],hours=[],minutes=[],seconds=[];
  397. let dVal=[],pickVal=[];
  398. let value=_this.value;
  399. let reg=/-/g;
  400. let range={};
  401. let result="",full="",year,month,day,hour,minute,second,obj={};
  402. let defaultDate=_this.getDefaultDate();
  403. let defaultYear=defaultDate.defaultYear;
  404. let defaultMonth=defaultDate.defaultMonth;
  405. let defaultDay=defaultDate.defaultDay;
  406. let defaultDays=defaultDate.defaultDays;
  407. let curFlag=this.current;
  408. let disabledAfter=this.disabledAfter;
  409. let curDate=_this.getCurrenDate();
  410. let curYear=curDate.curYear;
  411. let curMonth=curDate.curMonth;
  412. let curMonthdays=curDate.curMonthdays;
  413. let curDay=curDate.curDay;
  414. let curHour=curDate.curHour;
  415. let curMinute=curDate.curMinute;
  416. let curSecond=curDate.curSecond;
  417. let dateData=[];
  418. dVal=_this.getDval();
  419. startDate=this.getStartDate();
  420. endDate=this.getEndDate();
  421. startYear=startDate.getFullYear();
  422. startMonth=startDate.getMonth();
  423. startDay=startDate.getDate();
  424. endYear=endDate.getFullYear();
  425. endMonth=endDate.getMonth();
  426. endDay=endDate.getDate();
  427. dateData=_this.getData(dVal);
  428. years=dateData.years;
  429. months=dateData.months;
  430. days=dateData.days;
  431. hours=dateData.hours;
  432. minutes=dateData.minutes;
  433. seconds=dateData.seconds;
  434. switch(_this.fields){
  435. case "year":
  436. pickVal=disabledAfter?[
  437. dVal[0]&&years.indexOf(dVal[0])!=-1?years.indexOf(dVal[0]):0
  438. ]:(curFlag?[
  439. years.indexOf(curYear+'')
  440. ]:[
  441. dVal[0]&&years.indexOf(dVal[0])!=-1?years.indexOf(dVal[0]):0
  442. ]);
  443. range={years};
  444. year=dVal[0]?dVal[0]:years[0];
  445. result=full=`${year}`;
  446. obj={
  447. year
  448. }
  449. break;
  450. case "month":
  451. pickVal=disabledAfter?[
  452. dVal[0]&&years.indexOf(dVal[0])!=-1?years.indexOf(dVal[0]):0,
  453. dVal[1]&&months.indexOf(dVal[1])!=-1?months.indexOf(dVal[1]):0
  454. ]:(curFlag?[
  455. years.indexOf(curYear+''),
  456. months.indexOf(_this.formatNum(curMonth))
  457. ]:[
  458. dVal[0]&&years.indexOf(dVal[0])!=-1?years.indexOf(dVal[0]):0,
  459. dVal[1]&&months.indexOf(dVal[1])!=-1?months.indexOf(dVal[1]):0
  460. ]);
  461. range={years,months};
  462. year=dVal[0]?dVal[0]:years[0];
  463. month=dVal[1]?dVal[1]:months[0];
  464. result=full=`${year+'-'+month}`;
  465. obj={
  466. year,
  467. month
  468. }
  469. break;
  470. case "day":
  471. pickVal=disabledAfter?[
  472. dVal[0]&&years.indexOf(dVal[0])!=-1?years.indexOf(dVal[0]):0,
  473. dVal[1]&&months.indexOf(dVal[1])!=-1?months.indexOf(dVal[1]):0,
  474. dVal[2]&&days.indexOf(dVal[2])!=-1?days.indexOf(dVal[2]):0
  475. ]:(curFlag?[
  476. years.indexOf(curYear+''),
  477. months.indexOf(_this.formatNum(curMonth)),
  478. days.indexOf(_this.formatNum(curDay)),
  479. ]:[
  480. dVal[0]&&years.indexOf(dVal[0])!=-1?years.indexOf(dVal[0]):0,
  481. dVal[1]&&months.indexOf(dVal[1])!=-1?months.indexOf(dVal[1]):0,
  482. dVal[2]&&days.indexOf(dVal[2])!=-1?days.indexOf(dVal[2]):0
  483. ]);
  484. range={years,months,days};
  485. year=dVal[0]?dVal[0]:years[0];
  486. month=dVal[1]?dVal[1]:months[0];
  487. day=dVal[2]?dVal[2]:days[0];
  488. result=full=`${year+'-'+month+'-'+day}`;
  489. obj={
  490. year,
  491. month,
  492. day
  493. }
  494. break;
  495. case "hour":
  496. pickVal=disabledAfter?[
  497. dVal[0]&&years.indexOf(dVal[0])!=-1?years.indexOf(dVal[0]):0,
  498. dVal[1]&&months.indexOf(dVal[1])!=-1?months.indexOf(dVal[1]):0,
  499. dVal[2]&&days.indexOf(dVal[2])!=-1?days.indexOf(dVal[2]):0,
  500. dVal[3]&&hours.indexOf(dVal[3])!=-1?hours.indexOf(dVal[3]):0
  501. ]:(curFlag?[
  502. years.indexOf(curYear+''),
  503. months.indexOf(_this.formatNum(curMonth)),
  504. days.indexOf(_this.formatNum(curDay)),
  505. hours.indexOf(_this.formatNum(curHour)),
  506. ]:[
  507. dVal[0]&&years.indexOf(dVal[0])!=-1?years.indexOf(dVal[0]):0,
  508. dVal[1]&&months.indexOf(dVal[1])!=-1?months.indexOf(dVal[1]):0,
  509. dVal[2]&&days.indexOf(dVal[2])!=-1?days.indexOf(dVal[2]):0,
  510. dVal[3]&&hours.indexOf(dVal[3])!=-1?hours.indexOf(dVal[3]):0
  511. ]);
  512. range={years,months,days,hours};
  513. year=dVal[0]?dVal[0]:years[0];
  514. month=dVal[1]?dVal[1]:months[0];
  515. day=dVal[2]?dVal[2]:days[0];
  516. hour=dVal[3]?dVal[3]:hours[0];
  517. result=`${year+'-'+month+'-'+day+' '+hour}`;
  518. full=`${year+'-'+month+'-'+day+' '+hour+':00:00'}`;
  519. obj={
  520. year,
  521. month,
  522. day,
  523. hour
  524. }
  525. break;
  526. case "minute":
  527. pickVal=disabledAfter?[
  528. dVal[0]&&years.indexOf(dVal[0])!=-1?years.indexOf(dVal[0]):0,
  529. dVal[1]&&months.indexOf(dVal[1])!=-1?months.indexOf(dVal[1]):0,
  530. dVal[2]&&days.indexOf(dVal[2])!=-1?days.indexOf(dVal[2]):0,
  531. dVal[3]&&hours.indexOf(dVal[3])!=-1?hours.indexOf(dVal[3]):0,
  532. dVal[4]&&minutes.indexOf(dVal[4])!=-1?minutes.indexOf(dVal[4]):0
  533. ]:(curFlag?[
  534. years.indexOf(curYear+''),
  535. months.indexOf(_this.formatNum(curMonth)),
  536. days.indexOf(_this.formatNum(curDay)),
  537. hours.indexOf(_this.formatNum(curHour)),
  538. minutes.indexOf(_this.formatNum(curMinute)),
  539. ]:[
  540. dVal[0]&&years.indexOf(dVal[0])!=-1?years.indexOf(dVal[0]):0,
  541. dVal[1]&&months.indexOf(dVal[1])!=-1?months.indexOf(dVal[1]):0,
  542. dVal[2]&&days.indexOf(dVal[2])!=-1?days.indexOf(dVal[2]):0,
  543. dVal[3]&&hours.indexOf(dVal[3])!=-1?hours.indexOf(dVal[3]):0,
  544. dVal[4]&&minutes.indexOf(dVal[4])!=-1?minutes.indexOf(dVal[4]):0
  545. ]);
  546. range={years,months,days,hours,minutes};
  547. year=dVal[0]?dVal[0]:years[0];
  548. month=dVal[1]?dVal[1]:months[0];
  549. day=dVal[2]?dVal[2]:days[0];
  550. hour=dVal[3]?dVal[3]:hours[0];
  551. minute=dVal[4]?dVal[4]:minutes[0];
  552. full=`${year+'-'+month+'-'+day+' '+hour+':'+minute+':00'}`;
  553. result=`${year+'-'+month+'-'+day+' '+hour+':'+minute}`;
  554. obj={
  555. year,
  556. month,
  557. day,
  558. hour,
  559. minute
  560. }
  561. break;
  562. case "second":
  563. pickVal=disabledAfter?[
  564. dVal[0]&&years.indexOf(dVal[0])!=-1?years.indexOf(dVal[0]):0,
  565. dVal[1]&&months.indexOf(dVal[1])!=-1?months.indexOf(dVal[1]):0,
  566. dVal[2]&&days.indexOf(dVal[2])!=-1?days.indexOf(dVal[2]):0,
  567. dVal[3]&&hours.indexOf(dVal[3])!=-1?hours.indexOf(dVal[3]):0,
  568. dVal[4]&&minutes.indexOf(dVal[4])!=-1?minutes.indexOf(dVal[4]):0,
  569. dVal[5]&&seconds.indexOf(dVal[5])!=-1?seconds.indexOf(dVal[5]):0
  570. ]:(curFlag?[
  571. years.indexOf(curYear+''),
  572. months.indexOf(_this.formatNum(curMonth)),
  573. days.indexOf(_this.formatNum(curDay)),
  574. hours.indexOf(_this.formatNum(curHour)),
  575. minutes.indexOf(_this.formatNum(curMinute)),
  576. seconds.indexOf(_this.formatNum(curSecond)),
  577. ]:[
  578. dVal[0]&&years.indexOf(dVal[0])!=-1?years.indexOf(dVal[0]):0,
  579. dVal[1]&&months.indexOf(dVal[1])!=-1?months.indexOf(dVal[1]):0,
  580. dVal[2]&&days.indexOf(dVal[2])!=-1?days.indexOf(dVal[2]):0,
  581. dVal[3]&&hours.indexOf(dVal[3])!=-1?hours.indexOf(dVal[3]):0,
  582. dVal[4]&&minutes.indexOf(dVal[4])!=-1?minutes.indexOf(dVal[4]):0,
  583. dVal[5]&&seconds.indexOf(dVal[5])!=-1?seconds.indexOf(dVal[5]):0
  584. ]);
  585. range={years,months,days,hours,minutes,seconds};
  586. year=dVal[0]?dVal[0]:years[0];
  587. month=dVal[1]?dVal[1]:months[0];
  588. day=dVal[2]?dVal[2]:days[0];
  589. hour=dVal[3]?dVal[3]:hours[0];
  590. minute=dVal[4]?dVal[4]:minutes[0];
  591. second=dVal[5]?dVal[5]:seconds[0];
  592. result=full=`${year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second}`;
  593. obj={
  594. year,
  595. month,
  596. day,
  597. hour,
  598. minute,
  599. second
  600. }
  601. break;
  602. default:
  603. range={years,months,days};
  604. break;
  605. }
  606. _this.range=range;
  607. _this.checkObj=obj;
  608. _this.$nextTick(()=>{
  609. _this.pickVal=pickVal;
  610. });
  611. _this.$emit("change",{
  612. result:result,
  613. value:full,
  614. obj:obj
  615. })
  616. },
  617. handlerChange(e){
  618. let arr=[...e.detail.value];
  619. let data=_this.range;
  620. let year="",month="",day="",hour="",minute="",second="";
  621. let result="",full="",obj={};
  622. let months=null,days=null,hours=null,minutes=null,seconds=null;
  623. let disabledAfter=_this.disabledAfter;
  624. year=(arr[0]||arr[0]==0)?data.years[arr[0]]||data.years[data.years.length-1]:"";
  625. month=(arr[1]||arr[1]==0)?data.months[arr[1]]||data.months[data.months.length-1]:"";
  626. day=(arr[2]||arr[2]==0)?data.days[arr[2]]||data.days[data.days.length-1]:"";
  627. hour=(arr[3]||arr[3]==0)?data.hours[arr[3]]||data.hours[data.hours.length-1]:"";
  628. minute=(arr[4]||arr[4]==0)?data.minutes[arr[4]]||data.minutes[data.minutes.length-1]:"";
  629. second=(arr[5]||arr[5]==0)?data.seconds[arr[5]]||data.seconds[data.seconds.length-1]:"";
  630. switch(_this.fields){
  631. case "year":
  632. result=full=`${year}`;
  633. obj={
  634. year
  635. };
  636. break;
  637. case "month":
  638. result=full=`${year+'-'+month}`;
  639. if(this.disabledAfter)months=this.resetData(year,month,day,hour,minute).months;
  640. if(months)_this.range.months=months;
  641. obj={
  642. year,
  643. month
  644. }
  645. break;
  646. case "day":
  647. result=full=`${year+'-'+month+'-'+day}`;
  648. if(this.disabledAfter){
  649. months=this.resetData(year,month,day,hour,minute).months;
  650. days=this.resetData(year,month,day,hour,minute).days;
  651. }else{
  652. if(year%4==0||(month!=this.checkObj.month)){
  653. days=this.resetData(year,month,day,hour,minute).days;
  654. }
  655. }
  656. if(months)_this.range.months=months;
  657. if(days)_this.range.days=days;
  658. obj={
  659. year,
  660. month,
  661. day
  662. }
  663. break;
  664. case "hour":
  665. result=`${year+'-'+month+'-'+day+' '+hour}`;
  666. full=`${year+'-'+month+'-'+day+' '+hour+':00:00'}`;
  667. if(this.disabledAfter){
  668. months=this.resetData(year,month,day,hour,minute).months;
  669. days=this.resetData(year,month,day,hour,minute).days;
  670. hours=this.resetData(year,month,day,hour,minute).hours;
  671. }else{
  672. if(year%4==0||(month!=this.checkObj.month)){
  673. days=this.resetData(year,month,day,hour,minute).days;
  674. }
  675. }
  676. if(months)_this.range.months=months;
  677. if(days)_this.range.days=days;
  678. if(hours)_this.range.hours=hours;
  679. obj={
  680. year,
  681. month,
  682. day,
  683. hour
  684. }
  685. break;
  686. case "minute":
  687. full=`${year+'-'+month+'-'+day+' '+hour+':'+minute+':00'}`;
  688. result=`${year+'-'+month+'-'+day+' '+hour+':'+minute}`;
  689. if(this.disabledAfter){
  690. months=this.resetData(year,month,day,hour,minute).months;
  691. days=this.resetData(year,month,day,hour,minute).days;
  692. hours=this.resetData(year,month,day,hour,minute).hours;
  693. minutes=this.resetData(year,month,day,hour,minute).minutes;
  694. }else{
  695. if(year%4==0||(month!=this.checkObj.month)){
  696. days=this.resetData(year,month,day,hour,minute).days;
  697. }
  698. }
  699. if(months)_this.range.months=months;
  700. if(days)_this.range.days=days;
  701. if(hours)_this.range.hours=hours;
  702. if(minutes)_this.range.minutes=minutes;
  703. obj={
  704. year,
  705. month,
  706. day,
  707. hour,
  708. minute
  709. };
  710. break;
  711. case "second":
  712. result=full=`${year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second}`;
  713. this.resetData(year,month,day,hour,minute)
  714. if(this.disabledAfter){
  715. months=this.resetData(year,month,day,hour,minute).months;
  716. days=this.resetData(year,month,day,hour,minute).days;
  717. hours=this.resetData(year,month,day,hour,minute).hours;
  718. minutes=this.resetData(year,month,day,hour,minute).minutes;
  719. //seconds=this.resetData(year,month,day,hour,minute).seconds;
  720. }else{
  721. if(year%4==0||(month!=this.checkObj.month)){
  722. days=this.resetData(year,month,day,hour,minute).days;
  723. }
  724. }
  725. if(months)_this.range.months=months;
  726. if(days)_this.range.days=days;
  727. if(hours)_this.range.hours=hours;
  728. if(minutes)_this.range.minutes=minutes;
  729. //if(seconds)_this.range.seconds=seconds;
  730. obj={
  731. year,
  732. month,
  733. day,
  734. hour,
  735. minute,
  736. second
  737. }
  738. break;
  739. }
  740. this.checkObj=obj;
  741. _this.$emit("change",{
  742. result:result,
  743. value:full,
  744. obj:obj
  745. })
  746. }
  747. }
  748. }
  749. </script>
  750. <style lang="scss">
  751. @import "./w-picker.css";
  752. </style>