u-picker.vue 24 KB

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