u-picker.vue 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. <template>
  2. <u-popup :maskCloseAble="maskCloseAble" mode="bottom" :popup="false" v-model="value" length="auto" :safeAreaInsetBottom="safeAreaInsetBottom" @close="close" :z-index="uZIndex">
  3. <view class="u-datetime-picker">
  4. <view class="u-picker-title" v-if="jp==1">
  5. 定时开启
  6. </view>
  7. <view class="u-picker-title" v-if="jp==2">
  8. 定时关闭
  9. </view>
  10. <view class="expiration-time" v-if="jp==2&&showjp2bl">
  11. 次日 {{formatNumber(hour)+":"+formatNumber(minute)}}
  12. </view>
  13. </view>
  14. <view class="u-picker-body">
  15. <picker-view v-if="mode == 'region'" :value="valueArr" @change="change" class="u-picker-view" @pickstart="pickstart" @pickend="pickend">
  16. <picker-view-column v-if="!reset && params.province">
  17. <view class="u-column-item" v-for="(item, index) in provinces" :key="index">
  18. <view class="u-line-1">{{ item.label }}</view>
  19. </view>
  20. </picker-view-column>
  21. <picker-view-column v-if="!reset && params.city">
  22. <view class="u-column-item" v-for="(item, index) in citys" :key="index">
  23. <view class="u-line-1">{{ item.label }}</view>
  24. </view>
  25. </picker-view-column>
  26. <picker-view-column v-if="!reset && params.area">
  27. <view class="u-column-item" v-for="(item, index) in areas" :key="index">
  28. <view class="u-line-1">{{ item.label }}</view>
  29. </view>
  30. </picker-view-column>
  31. </picker-view>
  32. <picker-view v-else-if="mode == 'time'" :value="valueArr" @change="change" class="u-picker-view" @pickstart="pickstart" @pickend="pickend">
  33. <picker-view-column v-if="!reset && params.year">
  34. <view class="u-column-item" v-for="(item, index) in years" :key="index">
  35. {{ item }}
  36. <text class="u-text" v-if="showTimeTag">年</text>
  37. </view>
  38. </picker-view-column>
  39. <picker-view-column v-if="!reset && params.month">
  40. <view class="u-column-item" v-for="(item, index) in months" :key="index">
  41. {{ formatNumber(item) }}
  42. <text class="u-text" v-if="showTimeTag">月</text>
  43. </view>
  44. </picker-view-column>
  45. <picker-view-column v-if="!reset && params.day">
  46. <view class="u-column-item" v-for="(item, index) in days" :key="index">
  47. {{ formatNumber(item) }}
  48. <text class="u-text" v-if="showTimeTag">日</text>
  49. </view>
  50. </picker-view-column>
  51. <picker-view-column v-if="!reset && params.hour">
  52. <view class="u-column-item" v-for="(item, index) in hours" :key="index">
  53. {{ formatNumber(item) }}
  54. <text class="u-text" v-if="showTimeTag">时</text>
  55. </view>
  56. </picker-view-column>
  57. <picker-view-column v-if="!reset && params.minute">
  58. <view class="u-column-item" v-for="(item, index) in minutes" :key="index">
  59. {{ formatNumber(item) }}
  60. <text class="u-text" v-if="showTimeTag">分</text>
  61. </view>
  62. </picker-view-column>
  63. <picker-view-column v-if="!reset && params.second">
  64. <view class="u-column-item" v-for="(item, index) in seconds" :key="index">
  65. {{ formatNumber(item) }}
  66. <text class="u-text" v-if="showTimeTag">秒</text>
  67. </view>
  68. </picker-view-column>
  69. </picker-view>
  70. <picker-view v-else-if="mode == 'selector'" :value="valueArr" @change="change" class="u-picker-view" @pickstart="pickstart" @pickend="pickend">
  71. <picker-view-column v-if="!reset">
  72. <view class="u-column-item" v-for="(item, index) in range" :key="index">
  73. <view class="u-line-1">{{ getItemValue(item, 'selector') }}</view>
  74. </view>
  75. </picker-view-column>
  76. </picker-view>
  77. <picker-view v-else-if="mode == 'multiSelector'" :value="valueArr" @change="change" class="u-picker-view" @pickstart="pickstart" @pickend="pickend">
  78. <picker-view-column v-if="!reset" v-for="(item, index) in range" :key="index">
  79. <view class="u-column-item" v-for="(item1, index1) in item" :key="index1">
  80. <view class="u-line-1">{{ getItemValue(item1, 'multiSelector') }}</view>
  81. </view>
  82. </picker-view-column>
  83. </picker-view>
  84. </view>
  85. <view class="u-picker-header" @touchmove.stop.prevent="">
  86. <view class="u-btn-picker u-btn-picker--tips u-btn1"
  87. :style="{ color: cancelColor }"
  88. hover-class="u-opacity"
  89. :hover-stay-time="150"
  90. @tap="getResult('cancel')"
  91. >{{cancelText}}</view>
  92. <view class="u-picker__title">{{ title }}</view>
  93. <view
  94. class="u-btn-picker u-btn-picker--primary u-btn2"
  95. :style="{ color: moving ? cancelColor : confirmColor }"
  96. hover-class="u-opacity"
  97. :hover-stay-time="150"
  98. @touchmove.stop=""
  99. @tap.stop="getResult('confirm')"
  100. >
  101. {{confirmText}}
  102. </view>
  103. </view>
  104. </u-popup>
  105. </template>
  106. <script>
  107. // import provinces from './util/province.js';
  108. // import citys from './util/city.js';
  109. // import areas from './util/area.js';
  110. /**
  111. * picker picker弹出选择器
  112. * @description 此选择器有两种弹出模式:一是时间模式,可以配置年,日,月,时,分,秒参数 二是地区模式,可以配置省,市,区参数
  113. * @tutorial https://www.uviewui.com/components/picker.html
  114. * @property {Object} params 需要显示的参数,见官网说明
  115. * @property {String} mode 模式选择,region-地区类型,time-时间类型(默认time)
  116. * @property {String Number} start-year 可选的开始年份,mode=time时有效(默认1950)
  117. * @property {String Number} end-year 可选的结束年份,mode=time时有效(默认2050)
  118. * @property {Boolean} safe-area-inset-bottom 是否开启底部安全区适配(默认false)
  119. * @property {Boolean} show-time-tag 时间模式时,是否显示后面的年月日中文提示
  120. * @property {String} cancel-color 取消按钮的颜色(默认#606266)
  121. * @property {String} confirm-color 确认按钮的颜色(默认#2979ff)
  122. * @property {String} default-time 默认选中的时间,mode=time时有效
  123. * @property {String} confirm-text 确认按钮的文字
  124. * @property {String} cancel-text 取消按钮的文字
  125. * @property {String} default-region 默认选中的地区,中文形式,mode=region时有效
  126. * @property {String} default-code 默认选中的地区,编号形式,mode=region时有效
  127. * @property {Boolean} mask-close-able 是否允许通过点击遮罩关闭Picker(默认true)
  128. * @property {String Number} z-index 弹出时的z-index值(默认1075)
  129. * @property {Array} default-selector 数组形式,其中每一项表示选择了range对应项中的第几个
  130. * @property {Array} range 自定义选择的数据,mode=selector或mode=multiSelector时有效
  131. * @property {String} range-key 当range参数的元素为对象时,指定Object中的哪个key的值作为选择器显示内容
  132. * @event {Function} confirm 点击确定按钮,返回当前选择的值
  133. * @event {Function} cancel 点击取消按钮,返回当前选择的值
  134. * @example <u-picker v-model="show" mode="time"></u-picker>
  135. */
  136. export default {
  137. name: 'u-picker',
  138. props: {
  139. // picker中需要显示的参数
  140. params: {
  141. type: Object,
  142. default() {
  143. return {
  144. year: true,
  145. month: true,
  146. day: true,
  147. hour: false,
  148. minute: false,
  149. second: false,
  150. province: true,
  151. city: true,
  152. area: true,
  153. timestamp: true,
  154. };
  155. }
  156. },
  157. // 当mode=selector或者mode=multiSelector时,提供的数组
  158. range: {
  159. type: Array,
  160. default() {
  161. return [];
  162. }
  163. },
  164. // 当mode=selector或者mode=multiSelector时,提供的默认选中的下标
  165. defaultSelector: {
  166. type: Array,
  167. default() {
  168. return [0];
  169. }
  170. },
  171. // 当 range 是一个 Array<Object> 时,通过 range-key 来指定 Object 中 key 的值作为选择器显示内容
  172. rangeKey: {
  173. type: String,
  174. default: ''
  175. },
  176. jp: {
  177. type: String,
  178. default: '1'//1开始时间, 2结束时间
  179. },
  180. // 模式选择,region-地区类型,time-时间类型,selector-单列模式,multiSelector-多列模式
  181. mode: {
  182. type: String,
  183. default: 'time'
  184. },
  185. // 年份开始时间
  186. startYear: {
  187. type: [String, Number],
  188. default: 1950
  189. },
  190. // 年份结束时间
  191. endYear: {
  192. type: [String, Number],
  193. default: 2050
  194. },
  195. // "取消"按钮的颜色
  196. cancelColor: {
  197. type: String,
  198. default: '#606266'
  199. },
  200. // "确定"按钮的颜色
  201. confirmColor: {
  202. type: String,
  203. default: '#2979ff'
  204. },
  205. // 默认显示的时间,2025-07-02 || 2025-07-02 13:01:00 || 2025/07/02
  206. defaultTime: {
  207. type: String,
  208. default: ''
  209. },
  210. defaultStartTime: {
  211. type: String,
  212. default: ''
  213. },
  214. // 默认显示的地区,可传类似["河北省", "秦皇岛市", "北戴河区"]
  215. defaultRegion: {
  216. type: Array,
  217. default() {
  218. return [];
  219. }
  220. },
  221. // 时间模式时,是否显示后面的年月日中文提示
  222. showTimeTag: {
  223. type: Boolean,
  224. default: true
  225. },
  226. // 默认显示地区的编码,defaultRegion和areaCode同时存在,areaCode优先,可传类似["13", "1303", "130304"]
  227. areaCode: {
  228. type: Array,
  229. default() {
  230. return [];
  231. }
  232. },
  233. safeAreaInsetBottom: {
  234. type: Boolean,
  235. default: false
  236. },
  237. // 是否允许通过点击遮罩关闭Picker
  238. maskCloseAble: {
  239. type: Boolean,
  240. default: true
  241. },
  242. // 通过双向绑定控制组件的弹出与收起
  243. value: {
  244. type: Boolean,
  245. default: false
  246. },
  247. // 弹出的z-index值
  248. zIndex: {
  249. type: [String, Number],
  250. default: 0
  251. },
  252. // 顶部标题
  253. title: {
  254. type: String,
  255. default: ''
  256. },
  257. // 取消按钮的文字
  258. cancelText: {
  259. type: String,
  260. default: '取消'
  261. },
  262. // 确认按钮的文字
  263. confirmText: {
  264. type: String,
  265. default: '确认'
  266. }
  267. },
  268. data() {
  269. return {
  270. years: [],
  271. months: [],
  272. days: [],
  273. hours: [],
  274. minutes: [],
  275. seconds: [],
  276. year: 0,
  277. month: 0,
  278. day: 0,
  279. hour: 0,
  280. minute: 0,
  281. second: 0,
  282. reset: false,
  283. startDate: '',
  284. endDate: '',
  285. valueArr: [],
  286. // provinces: provinces,
  287. // citys: citys[0],
  288. // areas: areas[0][0],
  289. province: 0,
  290. city: 0,
  291. area: 0,
  292. moving: false, // 列是否还在滑动中,微信小程序如果在滑动中就点确定,结果可能不准确
  293. showjp2bl:false,
  294. };
  295. },
  296. mounted() {
  297. this.init();
  298. },
  299. computed: {
  300. propsChange() {
  301. // 引用这几个变量,是为了监听其变化
  302. return `${this.mode}-${this.defaultTime}-${this.startYear}-${this.endYear}-${this.defaultRegion}-${this.areaCode}`;
  303. },
  304. regionChange() {
  305. // 引用这几个变量,是为了监听其变化
  306. return `${this.province}-${this.city}`;
  307. },
  308. yearAndMonth() {
  309. return `${this.year}-${this.month}`;
  310. },
  311. uZIndex() {
  312. // 如果用户有传递z-index值,优先使用
  313. return this.zIndex ? this.zIndex : this.$u.zIndex.popup;
  314. }
  315. },
  316. watch: {
  317. propsChange() {
  318. this.reset = true;
  319. setTimeout(() => this.init(), 10);
  320. },
  321. // 如果地区发生变化,为了让picker联动起来,必须重置this.citys和this.areas
  322. regionChange(val) {
  323. this.citys = citys[this.province];
  324. this.areas = areas[this.province][this.city];
  325. },
  326. // watch监听月份的变化,实时变更日的天数,因为不同月份,天数不一样
  327. // 一个月可能有30,31天,甚至闰年2月的29天,平年2月28天
  328. yearAndMonth(val) {
  329. if (this.params.year) this.setDays();
  330. },
  331. // 微信和QQ小程序由于一些奇怪的原因(故同时对所有平台均初始化一遍),需要重新初始化才能显示正确的值
  332. value(n) {
  333. if (n) {
  334. this.reset = true;
  335. setTimeout(() => this.init(), 10);
  336. }
  337. }
  338. },
  339. methods: {
  340. showjp2(){
  341. if(this.defaultStartTime){
  342. var num=parseInt(this.defaultStartTime.replace(":",''))
  343. var bl=this.hour*100+this.minute<=num
  344. this.showjp2bl= bl
  345. //console.log(bl,this.defaultStartTime,this.hour*100+this.minute,num)
  346. }else{
  347. this.showjp2bl= false
  348. }
  349. //this.$forceUpdate()
  350. },
  351. // 标识滑动开始,只有微信小程序才有这样的事件
  352. pickstart() {
  353. // #ifdef MP-WEIXIN
  354. this.moving = true;
  355. // #endif
  356. },
  357. // 标识滑动结束
  358. pickend() {
  359. // #ifdef MP-WEIXIN
  360. this.moving = false;
  361. // #endif
  362. },
  363. // 对单列和多列形式的判断是否有传入变量的情况
  364. getItemValue(item, mode) {
  365. // 目前(2020-05-25)uni-app对微信小程序编译有错误,导致v-if为false中的内容也执行,错误导致
  366. // 单列模式或者多列模式中的getItemValue同时被执行,故在这里再加一层判断
  367. if (this.mode == mode) {
  368. return typeof item == 'object' ? item[this.rangeKey] : item;
  369. }
  370. },
  371. // 小于10前面补0,用于月份,日期,时分秒等
  372. formatNumber(num) {
  373. return +num < 10 ? '0' + num : String(num);
  374. },
  375. // 生成递进的数组
  376. generateArray: function(start, end) {
  377. // 转为数值格式,否则用户给end-year等传递字符串值时,下面的end+1会导致字符串拼接,而不是相加
  378. start = Number(start);
  379. end = Number(end);
  380. end = end > start ? end : start;
  381. // 生成数组,获取其中的索引,并剪出来
  382. return [...Array(end + 1).keys()].slice(start);
  383. },
  384. getIndex: function(arr, val) {
  385. let index = arr.indexOf(val);
  386. // 如果index为-1(即找不到index值),~(-1)=-(-1)-1=0,导致条件不成立
  387. return ~index ? index : 0;
  388. },
  389. //日期时间处理
  390. initTimeValue() {
  391. // 格式化时间,在IE浏览器(uni不存在此情况),无法识别日期间的"-"间隔符号
  392. let fdate = this.defaultTime.replace(/\-/g, '/');
  393. fdate = fdate && fdate.indexOf('/') == -1 ? `2020/01/01 ${fdate}` : fdate;
  394. let time = null;
  395. if (fdate) time = new Date(fdate);
  396. else time = new Date();
  397. // 获取年日月时分秒
  398. this.year = time.getFullYear();
  399. this.month = Number(time.getMonth()) + 1;
  400. this.day = time.getDate();
  401. this.hour = time.getHours();
  402. this.minute = time.getMinutes();
  403. this.second = time.getSeconds();
  404. },
  405. init() {
  406. this.valueArr = [];
  407. this.reset = false;
  408. if (this.mode == 'time') {
  409. this.initTimeValue();
  410. if (this.params.year) {
  411. this.valueArr.push(0);
  412. this.setYears();
  413. }
  414. if (this.params.month) {
  415. this.valueArr.push(0);
  416. this.setMonths();
  417. }
  418. if (this.params.day) {
  419. this.valueArr.push(0);
  420. this.setDays();
  421. }
  422. if (this.params.hour) {
  423. this.valueArr.push(0);
  424. this.setHours();
  425. }
  426. if (this.params.minute) {
  427. this.valueArr.push(0);
  428. this.setMinutes();
  429. }
  430. if (this.params.second) {
  431. this.valueArr.push(0);
  432. this.setSeconds();
  433. }
  434. } else if (this.mode == 'region') {
  435. if (this.params.province) {
  436. this.valueArr.push(0);
  437. this.setProvinces();
  438. }
  439. if (this.params.city) {
  440. this.valueArr.push(0);
  441. this.setCitys();
  442. }
  443. if (this.params.area) {
  444. this.valueArr.push(0);
  445. this.setAreas();
  446. }
  447. } else if (this.mode == 'selector') {
  448. this.valueArr = this.defaultSelector;
  449. } else if (this.mode == 'multiSelector') {
  450. this.valueArr = this.defaultSelector;
  451. this.multiSelectorValue = this.defaultSelector;
  452. }
  453. this.$forceUpdate();
  454. },
  455. // 设置picker的某一列值
  456. setYears() {
  457. // 获取年份集合
  458. this.years = this.generateArray(this.startYear, this.endYear);
  459. // 设置this.valueArr某一项的值,是为了让picker预选中某一个值
  460. this.valueArr.splice(this.valueArr.length - 1, 1, this.getIndex(this.years, this.year));
  461. },
  462. setMonths() {
  463. this.months = this.generateArray(1, 12);
  464. this.valueArr.splice(this.valueArr.length - 1, 1, this.getIndex(this.months, this.month));
  465. },
  466. setDays() {
  467. let totalDays = new Date(this.year, this.month, 0).getDate();
  468. this.days = this.generateArray(1, totalDays);
  469. let index = 0;
  470. // 这里不能使用类似setMonths()中的this.valueArr.splice(this.valueArr.length - 1, xxx)做法
  471. // 因为this.month和this.year变化时,会触发watch中的this.setDays(),导致this.valueArr.length计算有误
  472. if (this.params.year && this.params.month) index = 2;
  473. else if (this.params.month) index = 1;
  474. else if (this.params.year) index = 1;
  475. else index = 0;
  476. // 当月份变化时,会导致日期的天数也会变化,如果原来选的天数大于变化后的天数,则重置为变化后的最大值
  477. // 比如原来选中3月31日,调整为2月后,日期变为最大29,这时如果day值继续为31显然不合理,于是将其置为29(picker-column从1开始)
  478. if(this.day > this.days.length) this.day = this.days.length;
  479. this.valueArr.splice(index, 1, this.getIndex(this.days, this.day));
  480. },
  481. setHours() {
  482. this.hours = this.generateArray(0, 23);
  483. this.valueArr.splice(this.valueArr.length - 1, 1, this.getIndex(this.hours, this.hour));
  484. },
  485. setMinutes() {
  486. this.minutes = this.generateArray(0, 59);
  487. this.valueArr.splice(this.valueArr.length - 1, 1, this.getIndex(this.minutes, this.minute));
  488. },
  489. setSeconds() {
  490. this.seconds = this.generateArray(0, 59);
  491. this.valueArr.splice(this.valueArr.length - 1, 1, this.getIndex(this.seconds, this.second));
  492. },
  493. setProvinces() {
  494. // 判断是否需要province参数
  495. if (!this.params.province) return;
  496. let tmp = '';
  497. let useCode = false;
  498. // 如果同时配置了defaultRegion和areaCode,优先使用areaCode参数
  499. if (this.areaCode.length) {
  500. tmp = this.areaCode[0];
  501. useCode = true;
  502. } else if (this.defaultRegion.length) tmp = this.defaultRegion[0];
  503. else tmp = 0;
  504. // 历遍省份数组匹配
  505. provinces.map((v, k) => {
  506. if (useCode ? v.value == tmp : v.label == tmp) {
  507. tmp = k;
  508. }
  509. });
  510. this.province = tmp;
  511. this.provinces = provinces;
  512. // 设置默认省份的值
  513. this.valueArr.splice(0, 1, this.province);
  514. },
  515. setCitys() {
  516. if (!this.params.city) return;
  517. let tmp = '';
  518. let useCode = false;
  519. if (this.areaCode.length) {
  520. tmp = this.areaCode[1];
  521. useCode = true;
  522. } else if (this.defaultRegion.length) tmp = this.defaultRegion[1];
  523. else tmp = 0;
  524. citys[this.province].map((v, k) => {
  525. if (useCode ? v.value == tmp : v.label == tmp) {
  526. tmp = k;
  527. }
  528. });
  529. this.city = tmp;
  530. this.citys = citys[this.province];
  531. this.valueArr.splice(1, 1, this.city);
  532. },
  533. setAreas() {
  534. if (!this.params.area) return;
  535. let tmp = '';
  536. let useCode = false;
  537. if (this.areaCode.length) {
  538. tmp = this.areaCode[2];
  539. useCode = true;
  540. } else if (this.defaultRegion.length) tmp = this.defaultRegion[2];
  541. else tmp = 0;
  542. areas[this.province][this.city].map((v, k) => {
  543. if (useCode ? v.value == tmp : v.label == tmp) {
  544. tmp = k;
  545. }
  546. });
  547. this.area = tmp;
  548. this.areas = areas[this.province][this.city];
  549. this.valueArr.splice(2, 1, this.area);
  550. },
  551. close() {
  552. this.$emit('input', false);
  553. },
  554. // 用户更改picker的列选项
  555. change(e) {
  556. this.valueArr = e.detail.value;
  557. let i = 0;
  558. if (this.mode == 'time') {
  559. // 这里使用i++,是因为this.valueArr数组的长度是不确定长度的,它根据this.params的值来配置长度
  560. // 进入if规则,i会加1,保证了能获取准确的值
  561. if (this.params.year) this.year = this.years[this.valueArr[i++]];
  562. if (this.params.month) this.month = this.months[this.valueArr[i++]];
  563. if (this.params.day) this.day = this.days[this.valueArr[i++]];
  564. if (this.params.hour) this.hour = this.hours[this.valueArr[i++]];
  565. if (this.params.minute) this.minute = this.minutes[this.valueArr[i++]];
  566. if (this.params.second) this.second = this.seconds[this.valueArr[i++]];
  567. //console.log(this.hour ,this.minute )
  568. this.showjp2()
  569. } else if (this.mode == 'region') {
  570. if (this.params.province) this.province = this.valueArr[i++];
  571. if (this.params.city) this.city = this.valueArr[i++];
  572. if (this.params.area) this.area = this.valueArr[i++];
  573. } else if (this.mode == 'multiSelector') {
  574. let index = null;
  575. // 对比前后两个数组,寻找变更的是哪一列,如果某一个元素不同,即可判定该列发生了变化
  576. this.defaultSelector.map((val, idx) => {
  577. if (val != e.detail.value[idx]) index = idx;
  578. });
  579. // 为了让用户对多列变化时,对动态设置其他列的变更
  580. if (index != null) {
  581. this.$emit('columnchange', {
  582. column: index,
  583. index: e.detail.value[index]
  584. });
  585. }
  586. }
  587. },
  588. // 用户点击确定按钮
  589. getResult(event = null) {
  590. // #ifdef MP-WEIXIN
  591. if (this.moving) return;
  592. // #endif
  593. let result = {};
  594. // 只返回用户在this.params中配置了为true的字段
  595. if (this.mode == 'time') {
  596. if (this.params.year) result.year = this.formatNumber(this.year || 0);
  597. if (this.params.month) result.month = this.formatNumber(this.month || 0);
  598. if (this.params.day) result.day = this.formatNumber(this.day || 0);
  599. if (this.params.hour) result.hour = this.formatNumber(this.hour || 0);
  600. if (this.params.minute) result.minute = this.formatNumber(this.minute || 0);
  601. if (this.params.second) result.second = this.formatNumber(this.second || 0);
  602. if (this.params.timestamp) result.timestamp = this.getTimestamp();
  603. } else if (this.mode == 'region') {
  604. if (this.params.province) result.province = provinces[this.province];
  605. if (this.params.city) result.city = citys[this.province][this.city];
  606. if (this.params.area) result.area = areas[this.province][this.city][this.area];
  607. } else if (this.mode == 'selector') {
  608. result = this.valueArr;
  609. } else if (this.mode == 'multiSelector') {
  610. result = this.valueArr;
  611. }
  612. if (event) this.$emit(event, result);
  613. this.close();
  614. },
  615. // 获取时间戳
  616. getTimestamp() {
  617. // yyyy-mm-dd为安卓写法,不支持iOS,需要使用"/"分隔,才能二者兼容
  618. let time = this.year + '/' + this.month + '/' + this.day + ' ' + this.hour + ':' + this.minute + ':' + this.second;
  619. return new Date(time).getTime() / 1000;
  620. }
  621. }
  622. };
  623. </script>
  624. <style lang="scss" scoped>
  625. .u-datetime-picker {
  626. position: relative;
  627. z-index: 999;
  628. }
  629. .u-picker-view {
  630. height: 100%;
  631. box-sizing: border-box;
  632. }
  633. .u-picker-title{
  634. text-align: center;
  635. margin-top: 24rpx;
  636. color: rgba(16,16,16,1);
  637. font-size: 36rpx;
  638. font-weight: bold;
  639. margin-bottom: 4rpx;
  640. }
  641. .expiration-time{
  642. text-align: center;
  643. color: rgba(119,119,119,1);
  644. font-size: 24rpx;
  645. margin-bottom: 24rpx;
  646. }
  647. .u-picker-header {
  648. width: 100%;
  649. height: 90rpx;
  650. padding: 0 40rpx;
  651. display: flex;
  652. flex-direction: row;
  653. justify-content: space-between;
  654. align-items: center;
  655. box-sizing: border-box;
  656. font-size: 32rpx;
  657. position: relative;
  658. margin-bottom: 20rpx;
  659. }
  660. .u-picker-header::after {
  661. content: '';
  662. position: absolute;
  663. border-bottom: 1rpx solid #eaeef1;
  664. -webkit-transform: scaleY(0.5);
  665. transform: scaleY(0.5);
  666. bottom: 0;
  667. right: 0;
  668. left: 0;
  669. }
  670. .u-picker__title {
  671. color: $u-content-color;
  672. }
  673. .u-picker-body {
  674. text-align: center;
  675. width: 80%;
  676. height: 350rpx;
  677. overflow: hidden;
  678. background-color: #fff;
  679. margin: 0 auto 24rpx;
  680. }
  681. .u-column-item {
  682. display: flex;
  683. flex-direction: row;
  684. align-items: center;
  685. justify-content: center;
  686. font-size: 36rpx;
  687. color: $u-main-color;
  688. padding: 0 8rpx;
  689. }
  690. .u-text {
  691. font-size: 24rpx;
  692. padding-left: 8rpx;
  693. }
  694. .u-btn-picker {
  695. padding: 16rpx;
  696. width: 328rpx;
  697. box-sizing: border-box;
  698. text-align: center;
  699. text-decoration: none;
  700. border-radius: 8rpx;
  701. }
  702. .u-btn1{
  703. background-color: rgba(222,225,228,1);
  704. color: rgba(51,51,51,1) !important;
  705. }
  706. .u-btn2{
  707. background-color: rgba(22,119,255,1);
  708. color: rgba(255,255,255,1) !important;
  709. }
  710. .u-opacity {
  711. opacity: 0.5;
  712. }
  713. .u-btn-picker--primary {
  714. color: $u-type-primary;
  715. }
  716. .u-btn-picker--tips {
  717. color: $u-tips-color;
  718. }
  719. </style>