u-picker.vue 22 KB

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