u-picker.vue 22 KB

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