u-picker-shop.vue 22 KB

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