w-picker.vue 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. <template>
  2. <view class="w-picker">
  3. <view class="mask" :class="showPicker ? 'show' : ''" @tap="maskTap" @touchmove.stop.prevent catchtouchmove="true"></view>
  4. <view class="w-picker-cnt" :class="{'show':showPicker}">
  5. <view class="w-picker-hd" @touchmove.stop.prevent catchtouchmove="true">
  6. <view class="w-picker-btn" @tap="pickerCancel">取消</view>
  7. <view class="w-picker-btn" :style="{'color':themeColor}" @tap="pickerConfirm">确定</view>
  8. </view>
  9. <view class="w-picker-view" v-if="mode=='half'">
  10. <picker-view :indicator-style="itemHeight" :value="pickVal" @change="bindChange">
  11. <picker-view-column>
  12. <view class="w-picker-item" v-for="(item,index) in data.years" :key="index">{{item}}年</view>
  13. </picker-view-column>
  14. <picker-view-column>
  15. <view class="w-picker-item" v-for="(item,index) in data.months" :key="index">{{item}}月</view>
  16. </picker-view-column>
  17. <picker-view-column>
  18. <view class="w-picker-item" v-for="(item,index) in data.days" :key="index">{{item}}日</view>
  19. </picker-view-column>
  20. <picker-view-column>
  21. <view class="w-picker-item" v-for="(item,index) in data.areas" :key="index">{{item.label}}</view>
  22. </picker-view-column>
  23. </picker-view>
  24. </view>
  25. <view class="w-picker-view" v-if="mode=='date'">
  26. <picker-view :indicator-style="itemHeight" :value="pickVal" @change="bindChange">
  27. <picker-view-column>
  28. <view class="w-picker-item" v-for="(item,index) in data.years" :key="index">{{item}}年</view>
  29. </picker-view-column>
  30. <picker-view-column>
  31. <view class="w-picker-item" v-for="(item,index) in data.months" :key="index">{{item}}月</view>
  32. </picker-view-column>
  33. <picker-view-column>
  34. <view class="w-picker-item" v-for="(item,index) in data.days" :key="index">{{item}}日</view>
  35. </picker-view-column>
  36. </picker-view>
  37. </view>
  38. <view class="w-picker-view" v-if="mode=='yearMonth'">
  39. <picker-view :indicator-style="itemHeight" :value="pickVal" @change="bindChange">
  40. <picker-view-column>
  41. <view class="w-picker-item" v-for="(item,index) in data.years" :key="index">{{item}}年</view>
  42. </picker-view-column>
  43. <picker-view-column>
  44. <view class="w-picker-item" v-for="(item,index) in data.months" :key="index">{{item}}月</view>
  45. </picker-view-column>
  46. </picker-view>
  47. </view>
  48. <view class="w-picker-view" v-if="mode=='dateTime'">
  49. <picker-view :indicator-style="itemHeight" :value="pickVal" @change="bindChange">
  50. <picker-view-column>
  51. <view class="w-picker-item" v-for="(item,index) in data.years" :key="index">{{item}}年</view>
  52. </picker-view-column>
  53. <picker-view-column>
  54. <view class="w-picker-item" v-for="(item,index) in data.months" :key="index">{{item}}月</view>
  55. </picker-view-column>
  56. <picker-view-column >
  57. <view class="w-picker-item" v-for="(item,index) in data.days" :key="index">{{item}}日</view>
  58. </picker-view-column>
  59. <picker-view-column>
  60. <view class="w-picker-item" v-for="(item,index) in data.hours" :key="index">{{item}}时</view>
  61. </picker-view-column>
  62. <picker-view-column>
  63. <view class="w-picker-item" v-for="(item,index) in data.minutes" :key="index">{{item}}分</view>
  64. </picker-view-column>
  65. <!-- <picker-view-column>
  66. <view class="w-picker-item" v-for="(item,index) in data.seconds" :key="index">{{item}}秒</view>
  67. </picker-view-column> -->
  68. </picker-view>
  69. </view>
  70. <view class="w-picker-view" v-if="mode=='range'">
  71. <picker-view :indicator-style="itemHeight" :value="pickVal" @change="bindChange">
  72. <picker-view-column>
  73. <view class="w-picker-item" v-for="(item,index) in data.fyears" :key="index">{{item}}</view>
  74. </picker-view-column>
  75. <picker-view-column>
  76. <view class="w-picker-item" v-for="(item,index) in data.fmonths" :key="index">{{item}}</view>
  77. </picker-view-column>
  78. <picker-view-column>
  79. <view class="w-picker-item" v-for="(item,index) in data.fdays" :key="index">{{item}}</view>
  80. </picker-view-column>
  81. <picker-view-column>
  82. <view class="w-picker-item">-</view>
  83. </picker-view-column>
  84. <picker-view-column>
  85. <view class="w-picker-item" v-for="(item,index) in data.tyears" :key="index">{{item}}</view>
  86. </picker-view-column>
  87. <picker-view-column>
  88. <view class="w-picker-item" v-for="(item,index) in data.tmonths" :key="index">{{item}}</view>
  89. </picker-view-column>
  90. <picker-view-column>
  91. <view class="w-picker-item" v-for="(item,index) in data.tdays" :key="index">{{item}}</view>
  92. </picker-view-column>
  93. </picker-view>
  94. </view>
  95. <view class="w-picker-view" v-if="mode=='time'">
  96. <picker-view :indicator-style="itemHeight" :value="pickVal" @change="bindChange">
  97. <picker-view-column>
  98. <view class="w-picker-item" v-for="(item,index) in data.hours" :key="index">{{item}}时</view>
  99. </picker-view-column>
  100. <picker-view-column>
  101. <view class="w-picker-item" v-for="(item,index) in data.minutes" :key="index">{{item}}分</view>
  102. </picker-view-column>
  103. <picker-view-column>
  104. <view class="w-picker-item" v-for="(item,index) in data.seconds" :key="index">{{item}}秒</view>
  105. </picker-view-column>
  106. </picker-view>
  107. </view>
  108. <view class="w-picker-view" v-if="mode=='region'">
  109. <picker-view :indicator-style="itemHeight" :value="pickVal" @change="bindChange">
  110. <picker-view-column>
  111. <view class="w-picker-item" v-for="(item,index) in data.provinces" :key="index">{{item.label}}</view>
  112. </picker-view-column>
  113. <picker-view-column>
  114. <view class="w-picker-item" v-for="(item,index) in data.citys" :key="index">{{item.label}}</view>
  115. </picker-view-column>
  116. <picker-view-column>
  117. <view class="w-picker-item" v-for="(item,index) in data.areas" :key="index">{{item.label}}</view>
  118. </picker-view-column>
  119. </picker-view>
  120. </view>
  121. <view class="w-picker-view" v-if="mode=='selector'">
  122. <picker-view :indicator-style="itemHeight" :value="pickVal" @change="bindChange">
  123. <picker-view-column>
  124. <view class="w-picker-item" v-for="(item,index) in data" :key="index">{{item.label}}</view>
  125. </picker-view-column>
  126. </picker-view>
  127. </view>
  128. <view class="w-picker-view" v-if="mode=='limit'">
  129. <picker-view :indicator-style="itemHeight" :value="pickVal" @change="bindChange">
  130. <picker-view-column>
  131. <view class="w-picker-item" v-for="(item,index) in data.date" :key="index">{{item.label}}</view>
  132. </picker-view-column>
  133. <picker-view-column>
  134. <view class="w-picker-item" v-for="(item,index) in data.hours" :key="index">{{item.label}}时</view>
  135. </picker-view-column>
  136. <picker-view-column>
  137. <view class="w-picker-item" v-for="(item,index) in data.minutes" :key="index">{{item.label}}分</view>
  138. </picker-view-column>
  139. </picker-view>
  140. </view>
  141. <view class="w-picker-view" v-if="mode=='limitHour'">
  142. <picker-view :indicator-style="itemHeight" :value="pickVal" @change="bindChange">
  143. <picker-view-column>
  144. <view class="w-picker-item" v-for="(item,index) in data.date" :key="index">{{item.label}}</view>
  145. </picker-view-column>
  146. <picker-view-column>
  147. <view class="w-picker-item" v-for="(item,index) in data.areas" :key="index">{{item.label}}</view>
  148. </picker-view-column>
  149. <picker-view-column>
  150. <view class="w-picker-item" v-for="(item,index) in data.hours" :key="index">{{item.label}}时</view>
  151. </picker-view-column>
  152. </picker-view>
  153. </view>
  154. </view>
  155. </view>
  156. </template>
  157. <script>
  158. import provinces from './city-data/province.js';
  159. import citys from './city-data/city.js';
  160. import areas from './city-data/area.js';
  161. import initPicker from "./w-picker.js";
  162. function oneOf (value, validList) {
  163. for (let i = 0; i < validList.length; i++) {
  164. if (value === validList[i]) {
  165. return true;
  166. }
  167. }
  168. throw new Error('mode无效,请选择有效的mode!');
  169. return false;
  170. }
  171. export default {
  172. data() {
  173. return {
  174. result:[],
  175. data:{},
  176. checkArr:[],
  177. checkValue:[],
  178. pickVal:[],
  179. showPicker:false,
  180. resultStr:"",
  181. itemHeight:`height: ${uni.upx2px(88)}px;`
  182. };
  183. },
  184. computed:{
  185. },
  186. props:{
  187. mode:{
  188. type:String,
  189. validator(mode){
  190. let modeList=['half','date', 'dateTime', 'yearMonth','time','region','selector','limit','limitHour','range'];//过滤无效mode;
  191. return oneOf(mode,modeList);
  192. },
  193. default(){
  194. return "date"
  195. }
  196. },
  197. themeColor:{
  198. type:String,
  199. default(){
  200. return "#f5a200"
  201. }
  202. },
  203. startYear:{
  204. type:[String,Number],
  205. default(){
  206. return "1970"
  207. }
  208. },
  209. endYear:{
  210. type:[String,Number],
  211. default(){
  212. return new Date().getFullYear()+''
  213. }
  214. },
  215. defaultVal:{
  216. type:Array,
  217. default(){
  218. return [0,0,0,0,0,0,0]
  219. }
  220. },
  221. step:{
  222. type:[String,Number],
  223. default:1
  224. },
  225. current:{
  226. type:Boolean,
  227. default:false
  228. },
  229. selectList:{
  230. type:Array,
  231. default(){
  232. return [];
  233. }
  234. },
  235. //以下参数仅对mode==limit有效
  236. dayStep:{
  237. type:[String,Number],
  238. default:7
  239. },
  240. startHour:{
  241. type:[String,Number],
  242. default:8
  243. },
  244. endHour:{
  245. type:[String,Number],
  246. default:20
  247. },
  248. minuteStep:{
  249. type:[String,Number],
  250. default:10
  251. },
  252. afterStep:{
  253. type:[String,Number],
  254. default:30
  255. },
  256. disabledAfter:{
  257. type:Boolean,
  258. default:false
  259. }
  260. },
  261. watch:{
  262. mode(){
  263. this.initData();
  264. },
  265. selectList(){
  266. this.initData();
  267. }
  268. },
  269. methods:{
  270. getRegionVal(value){
  271. let province=value[0];
  272. let city=value[1];
  273. let area=value[2];
  274. let a=0,b=0,c=0;
  275. provinces.map((v,k)=>{
  276. if(v.label==province){
  277. a=k;
  278. }
  279. })
  280. citys[a].map((v,k)=>{
  281. if(v.label==city){
  282. b=k;
  283. }
  284. })
  285. areas[a][b].map((v,k)=>{
  286. if(v.label==area){
  287. c=k;
  288. }
  289. })
  290. let dval=[a,b,c];
  291. return dval;
  292. },
  293. useCurrent(){
  294. let aToday=new Date();
  295. let tYear=aToday.getFullYear().toString();
  296. let tMonth=this.formatNum(aToday.getMonth()+1).toString();
  297. let tDay=this.formatNum(aToday.getDate()).toString();
  298. let tHours=this.formatNum(aToday.getHours()).toString();
  299. let tMinutes=this.formatNum(aToday.getMinutes()).toString();
  300. let tSeconds=this.formatNum(aToday.getSeconds()).toString();
  301. if(this.current){
  302. return [tYear,tMonth,tDay,tHours,(Math.floor(tMinutes/this.step)*this.step).toString(),tSeconds];
  303. }else{
  304. return this.defaultVal;
  305. }
  306. },
  307. formatNum(num){
  308. return num<10?'0'+num:num+'';
  309. },
  310. maskTap(){
  311. this.showPicker = false;
  312. },
  313. show(){
  314. this.showPicker = true;
  315. },
  316. hide(){
  317. this.showPicker = false;
  318. },
  319. pickerCancel(){
  320. this.$emit("cancel",{
  321. checkArr:this.checkArr,
  322. defaultVal:this.pickVal
  323. });
  324. this.showPicker = false;
  325. },
  326. pickerConfirm(e){
  327. switch(this.mode){
  328. case "range":
  329. let checkArr=this.checkArr;
  330. let fDateTime=new Date(checkArr[0],checkArr[1],checkArr[2]);
  331. let tDateTime=new Date(checkArr[3],checkArr[4],checkArr[5]);
  332. let dVal=this.pickVal;
  333. if(fDateTime>tDateTime){
  334. this.checkArr=[checkArr[3],checkArr[4],checkArr[5],checkArr[0],checkArr[1],checkArr[2]];
  335. this.pickVal=[dVal[4],dVal[5],dVal[6],0,dVal[0],dVal[1],dVal[2]];
  336. this.$emit("confirm",{
  337. checkArr:this.checkArr,
  338. from:checkArr[3]+"-"+checkArr[4]+"-"+checkArr[5],
  339. to:checkArr[0]+"-"+checkArr[1]+"-"+checkArr[2],
  340. defaultVal:this.pickVal,
  341. result:this.resultStr
  342. });
  343. }else{
  344. this.$emit("confirm",{
  345. checkArr:this.checkArr,
  346. from:checkArr[0]+"-"+checkArr[1]+"-"+checkArr[2],
  347. to:checkArr[3]+"-"+checkArr[4]+"-"+checkArr[5],
  348. defaultVal:this.pickVal,
  349. result:this.resultStr
  350. });
  351. }
  352. break;
  353. case "limit":
  354. let aTime=new Date().getTime();
  355. let bTime=new Date(this.resultStr.replace(/-/g,'/')).getTime();
  356. if(aTime>bTime){
  357. uni.showModal({
  358. title:"提示",
  359. content:"选择时间必须大于当前时间",
  360. confirmColor:this.themeColor
  361. });
  362. return;
  363. }
  364. this.$emit("confirm",{
  365. checkArr:this.checkArr,
  366. defaultVal:this.pickVal,
  367. result:this.resultStr
  368. });
  369. break;
  370. case "region":
  371. this.$emit("confirm",{
  372. checkArr:this.checkArr,
  373. checkValue:this.checkValue,
  374. defaultVal:this.pickVal,
  375. result:this.resultStr
  376. });
  377. break;
  378. default:
  379. this.$emit("confirm",{
  380. checkArr:this.checkArr,
  381. defaultVal:this.pickVal,
  382. result:this.resultStr
  383. });
  384. break;
  385. }
  386. //this.showPicker = false;
  387. },
  388. bindChange(val){
  389. let _this=this;
  390. let arr=val.detail.value;
  391. let year="",month="",day="",hour="",minute="",second="",note=[],province,city,area;
  392. let checkArr=_this.checkArr;
  393. let days=[];
  394. let months=[];
  395. let mode=_this.mode;
  396. let col1,col2,col3,d,a,h,m;
  397. switch(mode){
  398. case "limitHour":
  399. col1=_this.data.date[arr[0]];
  400. col2=_this.data.areas[arr[1]];
  401. col3=_this.data.hours[arr[2]];
  402. if(col1.value!=checkArr[0].value){
  403. arr[1]=0;
  404. arr[2]=0;
  405. let areas=initPicker.limitHour.initAreas(col1);
  406. _this.data.areas=areas;
  407. let hours=initPicker.limitHour.initHours(col1,_this.data.areas[arr[1]]);
  408. _this.data.hours=hours;
  409. };
  410. if(col2.value!=checkArr[1].value){
  411. arr[2]=0;
  412. let hours=initPicker.limitHour.initHours(col1,_this.data.areas[arr[1]]);
  413. _this.data.hours=hours;
  414. };
  415. d=_this.data.date[arr[0]]||_this.data.date[_this.data.date.length-1];
  416. a=_this.data.areas[arr[1]]||_this.data.areas[_this.data.areas.length-1];
  417. h=_this.data.hours[arr[2]]||_this.data.hours[_this.data.hours.length-1];
  418. _this.checkArr=[d,a,h];
  419. _this.resultStr=`${d.value+' '+a.label+' '+h.label+"时"}`;
  420. break;
  421. case "limit":
  422. col1=_this.data.date[arr[0]];
  423. col2=_this.data.hours[arr[1]];
  424. if(col1.value!=checkArr[0].value){
  425. let hours=initPicker.limit.initHours(_this.startHour,_this.endHour,_this.minuteStep,_this.afterStep,col1.value);
  426. let minutes=initPicker.limit.initMinutes(_this.startHour,_this.endHour,_this.minuteStep,_this.afterStep,col1.value,col2.value);
  427. _this.data.hours=hours;
  428. _this.data.minutes=minutes;
  429. };
  430. if(col2.value!=checkArr[1].value){
  431. let minutes=initPicker.limit.initMinutes(_this.startHour,_this.endHour,_this.minuteStep,_this.afterStep,col1.value,col2.value);
  432. _this.data.minutes=minutes;
  433. };
  434. d=_this.data.date[arr[0]]||_this.data.date[_this.data.date.length-1];
  435. h=_this.data.hours[arr[1]]||_this.data.hours[_this.data.hours.length-1];
  436. m=_this.data.minutes[arr[2]]||_this.data.minutes[_this.data.minutes.length-1];
  437. _this.checkArr=[d,h,m];
  438. _this.resultStr=`${d.value+' '+h.value+':'+m.value+":"+"00"}`;
  439. break;
  440. case "range":
  441. let fyear=_this.data.fyears[arr[0]]||_this.data.fyears[_this.data.fyears.length-1];
  442. let fmonth=_this.data.fmonths[arr[1]]||_this.data.fmonths[_this.data.fmonths.length-1];
  443. let fday=_this.data.fdays[arr[2]]||_this.data.fdays[_this.data.fdays.length-1];
  444. let tyear=_this.data.tyears[arr[4]]||_this.data.tyears[_this.data.tyears.length-1];
  445. let tmonth=_this.data.tmonths[arr[5]]||_this.data.tmonths[_this.data.tmonths.length-1];
  446. let tday=_this.data.tdays[arr[6]]||_this.data.tdays[_this.data.tdays.length-1];
  447. if(fyear!=checkArr[0]){
  448. days=initPicker.range.initDays(fyear,fmonth);
  449. _this.data.fdays=days;
  450. };
  451. if(fmonth!=checkArr[1]){
  452. days=initPicker.range.initDays(fyear,fmonth);
  453. _this.data.fdays=days;
  454. };
  455. if(tyear!=checkArr[3]){
  456. days=initPicker.range.initDays(tyear,tmonth);
  457. _this.data.tdays=days;
  458. };
  459. if(tmonth!=checkArr[4]){
  460. days=initPicker.range.initDays(tyear,tmonth);
  461. _this.data.tdays=days;
  462. };
  463. _this.checkArr=[fyear,fmonth,fday,tyear,tmonth,tday];
  464. _this.resultStr=`${fyear+'-'+fmonth+'-'+fday+'至'+tyear+'-'+tmonth+'-'+tday}`;
  465. break;
  466. case "half":
  467. year=_this.data.years[arr[0]]||_this.data.years[_this.data.years.length-1];
  468. month=_this.data.months[arr[1]]||_this.data.months[_this.data.months.length-1];
  469. day=_this.data.days[arr[2]]||_this.data.days[_this.data.days.length-1];
  470. area=_this.data.areas[arr[3]]||_this.data.areas[_this.data.areas.length-1];
  471. if(year!=checkArr[0]){
  472. days=initPicker.date.initDays(year,month,_this.disabledAfter);
  473. months=initPicker.date.initMonths(year,_this.disabledAfter);
  474. _this.data.days=days;
  475. _this.data.months=months;
  476. };
  477. if(month!=checkArr[1]){
  478. days=initPicker.date.initDays(year,month,_this.disabledAfter);
  479. _this.data.days=days;
  480. };
  481. _this.checkArr=[year,month,day,area];
  482. _this.resultStr=`${year+'-'+month+'-'+day+area.label}`;
  483. break;
  484. case "date":
  485. year=_this.data.years[arr[0]]||_this.data.years[_this.data.years.length-1];
  486. month=_this.data.months[arr[1]]||_this.data.months[_this.data.months.length-1];
  487. day=_this.data.days[arr[2]]||_this.data.days[_this.data.days.length-1];
  488. if(year!=checkArr[0]){
  489. days=initPicker.date.initDays(year,month,_this.disabledAfter);
  490. months=initPicker.date.initMonths(year,_this.disabledAfter);
  491. _this.data.days=days;
  492. _this.data.months=months;
  493. };
  494. if(month!=checkArr[1]){
  495. days=initPicker.date.initDays(year,month,_this.disabledAfter);
  496. _this.data.days=days;
  497. };
  498. _this.checkArr=[year,month,day];
  499. _this.resultStr=`${year+'-'+month+'-'+day}`;
  500. break;
  501. case "yearMonth":
  502. year=_this.data.years[arr[0]]||_this.data.years[_this.data.years.length-1];
  503. month=_this.data.months[arr[1]]||_this.data.months[_this.data.months.length-1];
  504. if(year!=checkArr[0]){
  505. months=initPicker.date.initMonths(year,_this.disabledAfter);
  506. _this.data.months=months;
  507. };
  508. _this.checkArr=[year,month];
  509. _this.resultStr=`${year+'-'+month}`;
  510. break;
  511. case "dateTime":
  512. year=_this.data.years[arr[0]]||_this.data.years[_this.data.years.length-1];
  513. month=_this.data.months[arr[1]]||_this.data.months[_this.data.months.length-1];
  514. day=_this.data.days[arr[2]]||_this.data.days[_this.data.days.length-1];
  515. hour=_this.data.hours[arr[3]]||_this.data.hours[_this.data.hours.length-1];
  516. minute=_this.data.minutes[arr[4]]||_this.data.minutes[_this.data.minutes.length-1];
  517. second=_this.data.seconds[arr[5]]||_this.data.seconds[_this.data.seconds.length-1];
  518. if(year!=checkArr[0]){
  519. days=initPicker.date.initDays(year,month);
  520. _this.data.days=days;
  521. };
  522. if(month!=checkArr[1]){
  523. days=initPicker.date.initDays(year,month);
  524. _this.data.days=days;
  525. };
  526. second = "00";
  527. _this.checkArr=[year,month,day,hour,minute,second];
  528. _this.resultStr=`${year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second}`;
  529. break;
  530. case "time":
  531. hour=_this.data.hours[arr[0]]||_this.data.hours[_this.data.hours.length-1];
  532. minute=_this.data.minutes[arr[1]]||_this.data.minutes[_this.data.minutes.length-1];
  533. second=_this.data.seconds[arr[2]]||_this.data.seconds[_this.data.seconds.length-1];
  534. _this.checkArr=[hour,minute,second];
  535. _this.resultStr=`${hour+':'+minute+':'+second}`;
  536. break;
  537. case "region":
  538. province=_this.data.provinces[arr[0]].label;
  539. city=_this.data.citys[arr[1]].label;
  540. area=_this.data.areas[arr[2]].label;
  541. if(province!=checkArr[0]){
  542. _this.data.citys = citys[arr[0]];
  543. _this.data.areas = areas[arr[0]][0];
  544. arr[1] = 0;
  545. arr[2] = 0;
  546. city=_this.data.citys[arr[1]].label;
  547. area=_this.data.areas[arr[2]].label;
  548. };
  549. if(city!=checkArr[1]){
  550. _this.data.areas = areas[arr[0]][arr[1]];
  551. arr[2]=0;
  552. area=_this.data.areas[arr[2]].label;
  553. };
  554. _this.checkArr=[province,city,area];
  555. _this.checkValue=[_this.data.provinces[arr[0]].value,_this.data.citys[arr[1]].value,_this.data.areas[arr[2]].value];
  556. _this.resultStr=province+city+area;
  557. break;
  558. case "selector":
  559. _this.checkArr=_this.data[arr[0]]||_this.data[_this.data.length-1];
  560. _this.resultStr=_this.data[arr[0]].label||_this.data[_this.data.length-1].label;
  561. break;
  562. }
  563. _this.$nextTick(()=>{
  564. _this.pickVal=arr;
  565. })
  566. },
  567. initData(){
  568. let _this=this;
  569. let data={};
  570. let mode=_this.mode;
  571. let year,month,day,hour,minute,second,province,city,area;
  572. let col1,col2,col3;
  573. let dVal=[];
  574. switch(mode){
  575. case "region":
  576. dVal=_this.getRegionVal(_this.defaultVal);
  577. data={
  578. provinces:provinces,
  579. citys:citys[dVal[0]],
  580. areas:areas[dVal[0]][dVal[1]]
  581. };
  582. break;
  583. case "selector":
  584. data=_this.selectList;
  585. dVal=_this.defaultVal;
  586. break;
  587. case "limit":
  588. data=initPicker.limit.init(_this.dayStep,_this.startHour,_this.endHour,_this.minuteStep,_this.afterStep);
  589. dVal=(data.defaultVal&&_this.current)?data.defaultVal:_this.defaultVal
  590. break;
  591. case "limitHour":
  592. data=initPicker.limitHour.init(_this.dayStep);
  593. dVal=(data.defaultVal&&_this.current)?data.defaultVal:_this.defaultVal
  594. break;
  595. case "range":
  596. data=initPicker.range.init(_this.startYear,_this.endYear,_this.useCurrent(),_this.current);
  597. dVal=(data.defaultVal&&_this.current)?data.defaultVal:_this.defaultVal
  598. break;
  599. default:
  600. data=initPicker.date.init(_this.startYear,_this.endYear,_this.mode,_this.step,_this.useCurrent(),_this.current,_this.disabledAfter);
  601. dVal=(data.defaultVal&&_this.current)?data.defaultVal:_this.defaultVal
  602. break;
  603. }
  604. _this.data=data;
  605. switch(mode){
  606. case "limitHour":
  607. col1=data.date[dVal[0]]||data.date[data.date.length-1];
  608. col2=data.areas[dVal[2]]||data.areas[data.areas.length-1];
  609. col3=data.hours[dVal[1]]||data.hours[data.hours.length-1];
  610. _this.checkArr=[col1,col2,col3];
  611. _this.resultStr=`${col1.value+' '+col2.label+' '+col3.label+'时'}`;
  612. break;
  613. case "limit":
  614. col1=data.date[dVal[0]]||data.date[data.date.length-1];
  615. col2=data.hours[dVal[1]]||data.hours[data.hours.length-1];
  616. col3=data.minutes[dVal[2]]||data.minutes[data.minutes.length-1];
  617. _this.checkArr=[col1,col2,col3];
  618. _this.resultStr=`${col1.value+' '+col2.value+':'+col3.value+":"+"00"}`;
  619. break;
  620. case "range":
  621. let fYear=data.fyears[dVal[0]]||data.fyears[data.fyears.length-1];
  622. let fmonth=data.fmonths[dVal[1]]||data.fmonths[data.fmonths.length-1];
  623. let fday=data.fdays[dVal[2]]||data.fdays[data.fdays.length-1];
  624. let tYear=data.tyears[dVal[4]]||data.tyears[data.tyears.length-1];
  625. let tmonth=data.tmonths[dVal[5]]||data.tmonths[data.tmonths.length-1];
  626. let tday=data.tdays[dVal[6]]||data.tdays[data.tdays.length-1];
  627. _this.checkArr=[fYear,fmonth,fday,tYear,tmonth,tday];
  628. _this.resultStr=`${fYear+'-'+fmonth+'-'+fday+'至'+tYear+'-'+tmonth+'-'+tday}`;
  629. break;
  630. case "half":
  631. year=data.years[dVal[0]]||data.years[data.years.length-1];
  632. month=data.months[dVal[1]]||data.months[data.months.length-1];
  633. day=data.days[dVal[2]]||data.days[data.days.length-1];
  634. area=data.areas[dVal[3]]||data.areas[data.areas.length-1];
  635. _this.checkArr=[year,month,day,area];
  636. _this.resultStr=`${year+'-'+month+'-'+day+' '+area.label}`;
  637. break;
  638. case "date":
  639. year=data.years[dVal[0]]||data.years[data.years.length-1];
  640. month=data.months[dVal[1]]||data.months[data.months.length-1];
  641. day=data.days[dVal[2]]||data.days[data.days.length-1];
  642. _this.checkArr=[year,month,day];
  643. _this.resultStr=`${year+'-'+month+'-'+day}`;
  644. break;
  645. case "yearMonth":
  646. year=data.years[dVal[0]]||data.years[data.years.length-1];
  647. month=data.months[dVal[1]]||data.months[data.months.length-1];
  648. _this.checkArr=[year,month];
  649. _this.resultStr=`${year+'-'+month}`;
  650. break;
  651. case "dateTime":
  652. year=data.years[dVal[0]]||data.years[data.years.length-1];
  653. month=data.months[dVal[1]]||data.months[data.months.length-1];
  654. day=data.days[dVal[2]]||data.days[data.days.length-1];
  655. hour=data.hours[dVal[3]]||data.hours[data.hours.length-1];
  656. minute=data.minutes[dVal[4]]||data.minutes[data.minutes.length-1];
  657. second=data.seconds[dVal[5]]||data.seconds[data.seconds.length-1];
  658. second="00"
  659. _this.resultStr=`${year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second}`;
  660. _this.checkArr=[year,month,day,hour,minute];
  661. break;
  662. case "time":
  663. hour=data.hours[dVal[0]]||data.hours[data.hours.length-1];
  664. minute=data.minutes[dVal[1]]||data.minutes[data.minutes.length-1];
  665. second=data.seconds[dVal[2]]||data.seconds[data.seconds.length-1];
  666. _this.checkArr=[hour,minute,second];
  667. _this.resultStr=`${hour+':'+minute+':'+second}`;
  668. break;
  669. case "region":
  670. province=data.provinces[dVal[0]];
  671. city=data.citys[dVal[1]];
  672. area=data.areas[dVal[2]];
  673. _this.checkArr=[province.label,city.label,area.label];
  674. _this.checkValue=[province.value,city.value,area.value];
  675. _this.resultStr=province.label+city.label+area.label;
  676. break;
  677. case "selector":
  678. _this.checkArr=data[dVal[0]]||data[data.length-1];
  679. _this.resultStr=data[dVal[0]].label||data[data.length-1].label;
  680. break;
  681. }
  682. _this.$nextTick(()=>{
  683. _this.pickVal=dVal;
  684. })
  685. },
  686. getParse:function(str,type) {
  687. if(type == 'date') {
  688. if(str != null && str != "") {
  689. var dataAr = str.split("-");
  690. var yearIndex = (dataAr.length > 0 && !isNaN(dataAr[0])) ? parseInt(dataAr[0]) - 1940 : 0;
  691. var monthIndex = (dataAr.length > 1 && !isNaN(dataAr[1])) ? parseInt(dataAr[1]) - 1 : 0;
  692. var dayIndex = (dataAr.length > 2 && !isNaN(dataAr[2])) ? parseInt(dataAr[2]) - 1 : 0;
  693. return [yearIndex,monthIndex,dayIndex]
  694. }
  695. return [0,0,0];
  696. }
  697. if(type == 'region') {
  698. if(str != null && str != "") {
  699. var dataAr = str.split("-");
  700. return[dataAr.length > 0 ? dataAr[0] : "",
  701. dataAr.length > 1 ? dataAr[1] : "",
  702. dataAr.length > 2 ? dataAr[2] : ""
  703. ]
  704. }
  705. return ["","",""];
  706. }
  707. }
  708. },
  709. mounted() {
  710. this.initData();
  711. }
  712. }
  713. </script>
  714. <style lang="scss">
  715. .w-picker{
  716. position: relative;
  717. z-index: 888;
  718. .mask {
  719. position: fixed;
  720. z-index: 1000;
  721. top: 0;
  722. right: 0;
  723. left: 0;
  724. bottom: 0;
  725. background: rgba(0, 0, 0, 0.6);
  726. visibility: hidden;
  727. opacity: 0;
  728. transition: all 0.3s ease;
  729. }
  730. .mask.show{
  731. visibility: visible;
  732. opacity: 1;
  733. }
  734. .w-picker-cnt {
  735. position: fixed;
  736. bottom: 0;
  737. left: 0;
  738. width: 100%;
  739. transition: all 0.3s ease;
  740. transform: translateY(100%);
  741. z-index: 3000;
  742. }
  743. .w-picker-cnt.show {
  744. transform: translateY(0);
  745. }
  746. .w-picker-hd {
  747. display: flex;
  748. align-items: center;
  749. padding: 0 30upx;
  750. height: 88upx;
  751. background-color: #fff;
  752. position: relative;
  753. text-align: center;
  754. font-size: 32upx;
  755. justify-content: space-between;
  756. .w-picker-btn{
  757. font-size: 30upx;
  758. }
  759. }
  760. .w-picker-hd:after {
  761. content: ' ';
  762. position: absolute;
  763. left: 0;
  764. bottom: 0;
  765. right: 0;
  766. height: 1px;
  767. border-bottom: 1px solid #e5e5e5;
  768. color: #e5e5e5;
  769. transform-origin: 0 100%;
  770. transform: scaleY(0.5);
  771. }
  772. .w-picker-item {
  773. text-align: center;
  774. width: 100%;
  775. height: 88upx;
  776. line-height: 88upx;
  777. text-overflow: ellipsis;
  778. white-space: nowrap;
  779. font-size: 30upx;
  780. }
  781. .w-picker-view {
  782. width: 100%;
  783. height: 476upx;
  784. overflow: hidden;
  785. background-color: rgba(255, 255, 255, 1);
  786. z-index: 666;
  787. }
  788. picker-view{
  789. height: 100%;
  790. }
  791. }
  792. </style>