u-picker-cere.vue 22 KB

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