time-picker.vue 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. <template>
  2. <view class="uni-datetime-picker">
  3. <view @click="initTimePicker">
  4. <slot>
  5. <view class="uni-datetime-picker-timebox-pointer"
  6. :class="{'uni-datetime-picker-disabled': disabled, 'uni-datetime-picker-timebox': border}">
  7. <text class="uni-datetime-picker-text">{{time}}</text>
  8. <view v-if="!time" class="uni-datetime-picker-time">
  9. <text class="uni-datetime-picker-text">{{selectTimeText}}</text>
  10. </view>
  11. </view>
  12. </slot>
  13. </view>
  14. <view v-if="visible" id="mask" class="uni-datetime-picker-mask" @click="tiggerTimePicker"></view>
  15. <view v-if="visible" class="uni-datetime-picker-popup" :class="[dateShow && timeShow ? '' : 'fix-nvue-height']"
  16. :style="fixNvueBug">
  17. <view class="uni-title">
  18. <text class="uni-datetime-picker-text">{{selectTimeText}}</text>
  19. </view>
  20. <view v-if="dateShow" class="uni-datetime-picker__container-box">
  21. <picker-view class="uni-datetime-picker-view" :indicator-style="indicatorStyle" :value="ymd"
  22. @change="bindDateChange">
  23. <picker-view-column>
  24. <view class="uni-datetime-picker-item" v-for="(item,index) in years" :key="index">
  25. <text class="uni-datetime-picker-item">{{lessThanTen(item)}}</text>
  26. </view>
  27. </picker-view-column>
  28. <picker-view-column>
  29. <view class="uni-datetime-picker-item" v-for="(item,index) in months" :key="index">
  30. <text class="uni-datetime-picker-item">{{lessThanTen(item)}}</text>
  31. </view>
  32. </picker-view-column>
  33. <picker-view-column>
  34. <view class="uni-datetime-picker-item" v-for="(item,index) in days" :key="index">
  35. <text class="uni-datetime-picker-item">{{lessThanTen(item)}}</text>
  36. </view>
  37. </picker-view-column>
  38. </picker-view>
  39. <!-- 兼容 nvue 不支持伪类 -->
  40. <text class="uni-datetime-picker-sign sign-left">-</text>
  41. <text class="uni-datetime-picker-sign sign-right">-</text>
  42. </view>
  43. <view v-if="timeShow" class="uni-datetime-picker__container-box">
  44. <picker-view class="uni-datetime-picker-view" :class="[hideSecond ? 'time-hide-second' : '']"
  45. :indicator-style="indicatorStyle" :value="hms" @change="bindTimeChange">
  46. <picker-view-column>
  47. <view class="uni-datetime-picker-item" v-for="(item,index) in hours" :key="index">
  48. <text class="uni-datetime-picker-item">{{lessThanTen(item)}}</text>
  49. </view>
  50. </picker-view-column>
  51. <picker-view-column>
  52. <view class="uni-datetime-picker-item" v-for="(item,index) in minutes" :key="index">
  53. <text class="uni-datetime-picker-item">{{lessThanTen(item)}}</text>
  54. </view>
  55. </picker-view-column>
  56. <picker-view-column v-if="!hideSecond">
  57. <view class="uni-datetime-picker-item" v-for="(item,index) in seconds" :key="index">
  58. <text class="uni-datetime-picker-item">{{lessThanTen(item)}}</text>
  59. </view>
  60. </picker-view-column>
  61. </picker-view>
  62. <!-- 兼容 nvue 不支持伪类 -->
  63. <text class="uni-datetime-picker-sign" :class="[hideSecond ? 'sign-center' : 'sign-left']">:</text>
  64. <text v-if="!hideSecond" class="uni-datetime-picker-sign sign-right">:</text>
  65. </view>
  66. <view class="uni-datetime-picker-btn">
  67. <view @click="clearTime">
  68. <text class="uni-datetime-picker-btn-text">{{clearText}}</text>
  69. </view>
  70. <view class="uni-datetime-picker-btn-group">
  71. <view class="uni-datetime-picker-cancel" @click="tiggerTimePicker">
  72. <text class="uni-datetime-picker-btn-text">{{cancelText}}</text>
  73. </view>
  74. <view @click="setTime">
  75. <text class="uni-datetime-picker-btn-text">{{okText}}</text>
  76. </view>
  77. </view>
  78. </view>
  79. </view>
  80. </view>
  81. </template>
  82. <script>
  83. import {
  84. initVueI18n
  85. } from '@dcloudio/uni-i18n'
  86. import i18nMessages from './i18n/index.js'
  87. const {
  88. t
  89. } = initVueI18n(i18nMessages)
  90. import {
  91. fixIosDateFormat
  92. } from './util'
  93. /**
  94. * DatetimePicker 时间选择器
  95. * @description 可以同时选择日期和时间的选择器
  96. * @tutorial https://ext.dcloud.net.cn/plugin?id=xxx
  97. * @property {String} type = [datetime | date | time] 显示模式
  98. * @property {Boolean} multiple = [true|false] 是否多选
  99. * @property {String|Number} value 默认值
  100. * @property {String|Number} start 起始日期或时间
  101. * @property {String|Number} end 起始日期或时间
  102. * @property {String} return-type = [timestamp | string]
  103. * @event {Function} change 选中发生变化触发
  104. */
  105. export default {
  106. name: 'UniDatetimePicker',
  107. data() {
  108. return {
  109. indicatorStyle: `height: 50px;`,
  110. visible: false,
  111. fixNvueBug: {},
  112. dateShow: true,
  113. timeShow: true,
  114. title: '日期和时间',
  115. // 输入框当前时间
  116. time: '',
  117. // 当前的年月日时分秒
  118. year: 1920,
  119. month: 0,
  120. day: 0,
  121. hour: 0,
  122. minute: 0,
  123. second: 0,
  124. // 起始时间
  125. startYear: 1920,
  126. startMonth: 1,
  127. startDay: 1,
  128. startHour: 0,
  129. startMinute: 0,
  130. startSecond: 0,
  131. // 结束时间
  132. endYear: 2120,
  133. endMonth: 12,
  134. endDay: 31,
  135. endHour: 23,
  136. endMinute: 59,
  137. endSecond: 59,
  138. }
  139. },
  140. options: {
  141. // #ifdef MP-TOUTIAO
  142. virtualHost: false,
  143. // #endif
  144. // #ifndef MP-TOUTIAO
  145. virtualHost: true
  146. // #endif
  147. },
  148. props: {
  149. type: {
  150. type: String,
  151. default: 'datetime'
  152. },
  153. value: {
  154. type: [String, Number],
  155. default: ''
  156. },
  157. modelValue: {
  158. type: [String, Number],
  159. default: ''
  160. },
  161. start: {
  162. type: [Number, String],
  163. default: ''
  164. },
  165. end: {
  166. type: [Number, String],
  167. default: ''
  168. },
  169. returnType: {
  170. type: String,
  171. default: 'string'
  172. },
  173. disabled: {
  174. type: [Boolean, String],
  175. default: false
  176. },
  177. border: {
  178. type: [Boolean, String],
  179. default: true
  180. },
  181. hideSecond: {
  182. type: [Boolean, String],
  183. default: false
  184. }
  185. },
  186. watch: {
  187. // #ifndef VUE3
  188. value: {
  189. handler(newVal) {
  190. if (newVal) {
  191. this.parseValue(fixIosDateFormat(newVal))
  192. this.initTime(false)
  193. } else {
  194. this.time = ''
  195. this.parseValue(Date.now())
  196. }
  197. },
  198. immediate: true
  199. },
  200. // #endif
  201. // #ifdef VUE3
  202. modelValue: {
  203. handler(newVal) {
  204. if (newVal) {
  205. this.parseValue(fixIosDateFormat(newVal))
  206. this.initTime(false)
  207. } else {
  208. this.time = ''
  209. this.parseValue(Date.now())
  210. }
  211. },
  212. immediate: true
  213. },
  214. // #endif
  215. type: {
  216. handler(newValue) {
  217. if (newValue === 'date') {
  218. this.dateShow = true
  219. this.timeShow = false
  220. this.title = '日期'
  221. } else if (newValue === 'time') {
  222. this.dateShow = false
  223. this.timeShow = true
  224. this.title = '时间'
  225. } else {
  226. this.dateShow = true
  227. this.timeShow = true
  228. this.title = '日期和时间'
  229. }
  230. },
  231. immediate: true
  232. },
  233. start: {
  234. handler(newVal) {
  235. this.parseDatetimeRange(fixIosDateFormat(newVal), 'start')
  236. },
  237. immediate: true
  238. },
  239. end: {
  240. handler(newVal) {
  241. this.parseDatetimeRange(fixIosDateFormat(newVal), 'end')
  242. },
  243. immediate: true
  244. },
  245. // 月、日、时、分、秒可选范围变化后,检查当前值是否在范围内,不在则当前值重置为可选范围第一项
  246. months(newVal) {
  247. this.checkValue('month', this.month, newVal)
  248. },
  249. days(newVal) {
  250. this.checkValue('day', this.day, newVal)
  251. },
  252. hours(newVal) {
  253. this.checkValue('hour', this.hour, newVal)
  254. },
  255. minutes(newVal) {
  256. this.checkValue('minute', this.minute, newVal)
  257. },
  258. seconds(newVal) {
  259. this.checkValue('second', this.second, newVal)
  260. }
  261. },
  262. computed: {
  263. // 当前年、月、日、时、分、秒选择范围
  264. years() {
  265. return this.getCurrentRange('year')
  266. },
  267. months() {
  268. return this.getCurrentRange('month')
  269. },
  270. days() {
  271. return this.getCurrentRange('day')
  272. },
  273. hours() {
  274. return this.getCurrentRange('hour')
  275. },
  276. minutes() {
  277. return this.getCurrentRange('minute')
  278. },
  279. seconds() {
  280. return this.getCurrentRange('second')
  281. },
  282. // picker 当前值数组
  283. ymd() {
  284. return [this.year - this.minYear, this.month - this.minMonth, this.day - this.minDay]
  285. },
  286. hms() {
  287. return [this.hour - this.minHour, this.minute - this.minMinute, this.second - this.minSecond]
  288. },
  289. // 当前 date 是 start
  290. currentDateIsStart() {
  291. return this.year === this.startYear && this.month === this.startMonth && this.day === this.startDay
  292. },
  293. // 当前 date 是 end
  294. currentDateIsEnd() {
  295. return this.year === this.endYear && this.month === this.endMonth && this.day === this.endDay
  296. },
  297. // 当前年、月、日、时、分、秒的最小值和最大值
  298. minYear() {
  299. return this.startYear
  300. },
  301. maxYear() {
  302. return this.endYear
  303. },
  304. minMonth() {
  305. if (this.year === this.startYear) {
  306. return this.startMonth
  307. } else {
  308. return 1
  309. }
  310. },
  311. maxMonth() {
  312. if (this.year === this.endYear) {
  313. return this.endMonth
  314. } else {
  315. return 12
  316. }
  317. },
  318. minDay() {
  319. if (this.year === this.startYear && this.month === this.startMonth) {
  320. return this.startDay
  321. } else {
  322. return 1
  323. }
  324. },
  325. maxDay() {
  326. if (this.year === this.endYear && this.month === this.endMonth) {
  327. return this.endDay
  328. } else {
  329. return this.daysInMonth(this.year, this.month)
  330. }
  331. },
  332. minHour() {
  333. if (this.type === 'datetime') {
  334. if (this.currentDateIsStart) {
  335. return this.startHour
  336. } else {
  337. return 0
  338. }
  339. }
  340. if (this.type === 'time') {
  341. return this.startHour
  342. }
  343. },
  344. maxHour() {
  345. if (this.type === 'datetime') {
  346. if (this.currentDateIsEnd) {
  347. return this.endHour
  348. } else {
  349. return 23
  350. }
  351. }
  352. if (this.type === 'time') {
  353. return this.endHour
  354. }
  355. },
  356. minMinute() {
  357. if (this.type === 'datetime') {
  358. if (this.currentDateIsStart && this.hour === this.startHour) {
  359. return this.startMinute
  360. } else {
  361. return 0
  362. }
  363. }
  364. if (this.type === 'time') {
  365. if (this.hour === this.startHour) {
  366. return this.startMinute
  367. } else {
  368. return 0
  369. }
  370. }
  371. },
  372. maxMinute() {
  373. if (this.type === 'datetime') {
  374. if (this.currentDateIsEnd && this.hour === this.endHour) {
  375. return this.endMinute
  376. } else {
  377. return 59
  378. }
  379. }
  380. if (this.type === 'time') {
  381. if (this.hour === this.endHour) {
  382. return this.endMinute
  383. } else {
  384. return 59
  385. }
  386. }
  387. },
  388. minSecond() {
  389. if (this.type === 'datetime') {
  390. if (this.currentDateIsStart && this.hour === this.startHour && this.minute === this.startMinute) {
  391. return this.startSecond
  392. } else {
  393. return 0
  394. }
  395. }
  396. if (this.type === 'time') {
  397. if (this.hour === this.startHour && this.minute === this.startMinute) {
  398. return this.startSecond
  399. } else {
  400. return 0
  401. }
  402. }
  403. },
  404. maxSecond() {
  405. if (this.type === 'datetime') {
  406. if (this.currentDateIsEnd && this.hour === this.endHour && this.minute === this.endMinute) {
  407. return this.endSecond
  408. } else {
  409. return 59
  410. }
  411. }
  412. if (this.type === 'time') {
  413. if (this.hour === this.endHour && this.minute === this.endMinute) {
  414. return this.endSecond
  415. } else {
  416. return 59
  417. }
  418. }
  419. },
  420. /**
  421. * for i18n
  422. */
  423. selectTimeText() {
  424. return t("uni-datetime-picker.selectTime")
  425. },
  426. okText() {
  427. return t("uni-datetime-picker.ok")
  428. },
  429. clearText() {
  430. return t("uni-datetime-picker.clear")
  431. },
  432. cancelText() {
  433. return t("uni-datetime-picker.cancel")
  434. }
  435. },
  436. mounted() {
  437. // #ifdef APP-NVUE
  438. const res = uni.getSystemInfoSync();
  439. this.fixNvueBug = {
  440. top: res.windowHeight / 2,
  441. left: res.windowWidth / 2
  442. }
  443. // #endif
  444. },
  445. methods: {
  446. /**
  447. * @param {Object} item
  448. * 小于 10 在前面加个 0
  449. */
  450. lessThanTen(item) {
  451. return item < 10 ? '0' + item : item
  452. },
  453. /**
  454. * 解析时分秒字符串,例如:00:00:00
  455. * @param {String} timeString
  456. */
  457. parseTimeType(timeString) {
  458. if (timeString) {
  459. let timeArr = timeString.split(':')
  460. this.hour = Number(timeArr[0])
  461. this.minute = Number(timeArr[1])
  462. this.second = Number(timeArr[2])
  463. }
  464. },
  465. /**
  466. * 解析选择器初始值,类型可以是字符串、时间戳,例如:2000-10-02、'08:30:00'、 1610695109000
  467. * @param {String | Number} datetime
  468. */
  469. initPickerValue(datetime) {
  470. let defaultValue = null
  471. if (datetime) {
  472. defaultValue = this.compareValueWithStartAndEnd(datetime, this.start, this.end)
  473. } else {
  474. defaultValue = Date.now()
  475. defaultValue = this.compareValueWithStartAndEnd(defaultValue, this.start, this.end)
  476. }
  477. this.parseValue(defaultValue)
  478. },
  479. /**
  480. * 初始值规则:
  481. * - 用户设置初始值 value
  482. * - 设置了起始时间 start、终止时间 end,并 start < value < end,初始值为 value, 否则初始值为 start
  483. * - 只设置了起始时间 start,并 start < value,初始值为 value,否则初始值为 start
  484. * - 只设置了终止时间 end,并 value < end,初始值为 value,否则初始值为 end
  485. * - 无起始终止时间,则初始值为 value
  486. * - 无初始值 value,则初始值为当前本地时间 Date.now()
  487. * @param {Object} value
  488. * @param {Object} dateBase
  489. */
  490. compareValueWithStartAndEnd(value, start, end) {
  491. let winner = null
  492. value = this.superTimeStamp(value)
  493. start = this.superTimeStamp(start)
  494. end = this.superTimeStamp(end)
  495. if (start && end) {
  496. if (value < start) {
  497. winner = new Date(start)
  498. } else if (value > end) {
  499. winner = new Date(end)
  500. } else {
  501. winner = new Date(value)
  502. }
  503. } else if (start && !end) {
  504. winner = start <= value ? new Date(value) : new Date(start)
  505. } else if (!start && end) {
  506. winner = value <= end ? new Date(value) : new Date(end)
  507. } else {
  508. winner = new Date(value)
  509. }
  510. return winner
  511. },
  512. /**
  513. * 转换为可比较的时间戳,接受日期、时分秒、时间戳
  514. * @param {Object} value
  515. */
  516. superTimeStamp(value) {
  517. let dateBase = ''
  518. if (this.type === 'time' && value && typeof value === 'string') {
  519. const now = new Date()
  520. const year = now.getFullYear()
  521. const month = now.getMonth() + 1
  522. const day = now.getDate()
  523. dateBase = year + '/' + month + '/' + day + ' '
  524. }
  525. if (Number(value)) {
  526. value = parseInt(value)
  527. dateBase = 0
  528. }
  529. return this.createTimeStamp(dateBase + value)
  530. },
  531. /**
  532. * 解析默认值 value,字符串、时间戳
  533. * @param {Object} defaultTime
  534. */
  535. parseValue(value) {
  536. if (!value) {
  537. return
  538. }
  539. if (this.type === 'time' && typeof value === "string") {
  540. this.parseTimeType(value)
  541. } else {
  542. let defaultDate = null
  543. defaultDate = new Date(value)
  544. if (this.type !== 'time') {
  545. this.year = defaultDate.getFullYear()
  546. this.month = defaultDate.getMonth() + 1
  547. this.day = defaultDate.getDate()
  548. }
  549. if (this.type !== 'date') {
  550. this.hour = defaultDate.getHours()
  551. this.minute = defaultDate.getMinutes()
  552. this.second = defaultDate.getSeconds()
  553. }
  554. }
  555. if (this.hideSecond) {
  556. this.second = 0
  557. }
  558. },
  559. /**
  560. * 解析可选择时间范围 start、end,年月日字符串、时间戳
  561. * @param {Object} defaultTime
  562. */
  563. parseDatetimeRange(point, pointType) {
  564. // 时间为空,则重置为初始值
  565. if (!point) {
  566. if (pointType === 'start') {
  567. this.startYear = 1920
  568. this.startMonth = 1
  569. this.startDay = 1
  570. this.startHour = 0
  571. this.startMinute = 0
  572. this.startSecond = 0
  573. }
  574. if (pointType === 'end') {
  575. this.endYear = 2120
  576. this.endMonth = 12
  577. this.endDay = 31
  578. this.endHour = 23
  579. this.endMinute = 59
  580. this.endSecond = 59
  581. }
  582. return
  583. }
  584. if (this.type === 'time') {
  585. const pointArr = point.split(':')
  586. this[pointType + 'Hour'] = Number(pointArr[0])
  587. this[pointType + 'Minute'] = Number(pointArr[1])
  588. this[pointType + 'Second'] = Number(pointArr[2])
  589. } else {
  590. if (!point) {
  591. pointType === 'start' ? this.startYear = this.year - 60 : this.endYear = this.year + 60
  592. return
  593. }
  594. if (Number(point)) {
  595. point = parseInt(point)
  596. }
  597. // datetime 的 end 没有时分秒, 则不限制
  598. const hasTime = /[0-9]:[0-9]/
  599. if (this.type === 'datetime' && pointType === 'end' && typeof point === 'string' && !hasTime.test(
  600. point)) {
  601. point = point + ' 23:59:59'
  602. }
  603. const pointDate = new Date(point)
  604. this[pointType + 'Year'] = pointDate.getFullYear()
  605. this[pointType + 'Month'] = pointDate.getMonth() + 1
  606. this[pointType + 'Day'] = pointDate.getDate()
  607. if (this.type === 'datetime') {
  608. this[pointType + 'Hour'] = pointDate.getHours()
  609. this[pointType + 'Minute'] = pointDate.getMinutes()
  610. this[pointType + 'Second'] = pointDate.getSeconds()
  611. }
  612. }
  613. },
  614. // 获取 年、月、日、时、分、秒 当前可选范围
  615. getCurrentRange(value) {
  616. const range = []
  617. for (let i = this['min' + this.capitalize(value)]; i <= this['max' + this.capitalize(value)]; i++) {
  618. range.push(i)
  619. }
  620. return range
  621. },
  622. // 字符串首字母大写
  623. capitalize(str) {
  624. return str.charAt(0).toUpperCase() + str.slice(1)
  625. },
  626. // 检查当前值是否在范围内,不在则当前值重置为可选范围第一项
  627. checkValue(name, value, values) {
  628. if (values.indexOf(value) === -1) {
  629. this[name] = values[0]
  630. }
  631. },
  632. // 每个月的实际天数
  633. daysInMonth(year, month) { // Use 1 for January, 2 for February, etc.
  634. return new Date(year, month, 0).getDate();
  635. },
  636. /**
  637. * 生成时间戳
  638. * @param {Object} time
  639. */
  640. createTimeStamp(time) {
  641. if (!time) return
  642. if (typeof time === "number") {
  643. return time
  644. } else {
  645. time = time.replace(/-/g, '/')
  646. if (this.type === 'date') {
  647. time = time + ' ' + '00:00:00'
  648. }
  649. return Date.parse(time)
  650. }
  651. },
  652. /**
  653. * 生成日期或时间的字符串
  654. */
  655. createDomSting() {
  656. const yymmdd = this.year +
  657. '-' +
  658. this.lessThanTen(this.month) +
  659. '-' +
  660. this.lessThanTen(this.day)
  661. let hhmmss = this.lessThanTen(this.hour) +
  662. ':' +
  663. this.lessThanTen(this.minute)
  664. if (!this.hideSecond) {
  665. hhmmss = hhmmss + ':' + this.lessThanTen(this.second)
  666. }
  667. if (this.type === 'date') {
  668. return yymmdd
  669. } else if (this.type === 'time') {
  670. return hhmmss
  671. } else {
  672. return yymmdd + ' ' + hhmmss
  673. }
  674. },
  675. /**
  676. * 初始化返回值,并抛出 change 事件
  677. */
  678. initTime(emit = true) {
  679. this.time = this.createDomSting()
  680. if (!emit) return
  681. if (this.returnType === 'timestamp' && this.type !== 'time') {
  682. this.$emit('change', this.createTimeStamp(this.time))
  683. this.$emit('input', this.createTimeStamp(this.time))
  684. this.$emit('update:modelValue', this.createTimeStamp(this.time))
  685. } else {
  686. this.$emit('change', this.time)
  687. this.$emit('input', this.time)
  688. this.$emit('update:modelValue', this.time)
  689. }
  690. },
  691. /**
  692. * 用户选择日期或时间更新 data
  693. * @param {Object} e
  694. */
  695. bindDateChange(e) {
  696. const val = e.detail.value
  697. this.year = this.years[val[0]]
  698. this.month = this.months[val[1]]
  699. this.day = this.days[val[2]]
  700. },
  701. bindTimeChange(e) {
  702. const val = e.detail.value
  703. this.hour = this.hours[val[0]]
  704. this.minute = this.minutes[val[1]]
  705. this.second = this.seconds[val[2]]
  706. },
  707. /**
  708. * 初始化弹出层
  709. */
  710. initTimePicker() {
  711. if (this.disabled) return
  712. const value = fixIosDateFormat(this.time)
  713. this.initPickerValue(value)
  714. this.visible = !this.visible
  715. },
  716. /**
  717. * 触发或关闭弹框
  718. */
  719. tiggerTimePicker(e) {
  720. this.visible = !this.visible
  721. },
  722. /**
  723. * 用户点击“清空”按钮,清空当前值
  724. */
  725. clearTime() {
  726. this.time = ''
  727. this.$emit('change', this.time)
  728. this.$emit('input', this.time)
  729. this.$emit('update:modelValue', this.time)
  730. this.tiggerTimePicker()
  731. },
  732. /**
  733. * 用户点击“确定”按钮
  734. */
  735. setTime() {
  736. this.initTime()
  737. this.tiggerTimePicker()
  738. }
  739. }
  740. }
  741. </script>
  742. <style lang="scss">
  743. $uni-primary: #007aff !default;
  744. .uni-datetime-picker {
  745. /* #ifndef APP-NVUE */
  746. /* width: 100%; */
  747. /* #endif */
  748. }
  749. .uni-datetime-picker-view {
  750. height: 130px;
  751. width: 270px;
  752. /* #ifndef APP-NVUE */
  753. cursor: pointer;
  754. /* #endif */
  755. }
  756. .uni-datetime-picker-item {
  757. height: 50px;
  758. line-height: 50px;
  759. text-align: center;
  760. font-size: 14px;
  761. }
  762. .uni-datetime-picker-btn {
  763. margin-top: 60px;
  764. /* #ifndef APP-NVUE */
  765. display: flex;
  766. cursor: pointer;
  767. /* #endif */
  768. flex-direction: row;
  769. justify-content: space-between;
  770. }
  771. .uni-datetime-picker-btn-text {
  772. font-size: 14px;
  773. color: $uni-primary;
  774. }
  775. .uni-datetime-picker-btn-group {
  776. /* #ifndef APP-NVUE */
  777. display: flex;
  778. /* #endif */
  779. flex-direction: row;
  780. }
  781. .uni-datetime-picker-cancel {
  782. margin-right: 30px;
  783. }
  784. .uni-datetime-picker-mask {
  785. position: fixed;
  786. bottom: 0px;
  787. top: 0px;
  788. left: 0px;
  789. right: 0px;
  790. background-color: rgba(0, 0, 0, 0.4);
  791. transition-duration: 0.3s;
  792. z-index: 998;
  793. }
  794. .uni-datetime-picker-popup {
  795. border-radius: 8px;
  796. padding: 30px;
  797. width: 270px;
  798. /* #ifdef APP-NVUE */
  799. height: 500px;
  800. /* #endif */
  801. /* #ifdef APP-NVUE */
  802. width: 330px;
  803. /* #endif */
  804. background-color: #fff;
  805. position: fixed;
  806. top: 50%;
  807. left: 50%;
  808. transform: translate(-50%, -50%);
  809. transition-duration: 0.3s;
  810. z-index: 999;
  811. }
  812. .fix-nvue-height {
  813. /* #ifdef APP-NVUE */
  814. height: 330px;
  815. /* #endif */
  816. }
  817. .uni-datetime-picker-time {
  818. color: grey;
  819. }
  820. .uni-datetime-picker-column {
  821. height: 50px;
  822. }
  823. .uni-datetime-picker-timebox {
  824. border: 1px solid #E5E5E5;
  825. border-radius: 5px;
  826. padding: 7px 10px;
  827. /* #ifndef APP-NVUE */
  828. box-sizing: border-box;
  829. cursor: pointer;
  830. /* #endif */
  831. }
  832. .uni-datetime-picker-timebox-pointer {
  833. /* #ifndef APP-NVUE */
  834. cursor: pointer;
  835. /* #endif */
  836. }
  837. .uni-datetime-picker-disabled {
  838. opacity: 0.4;
  839. /* #ifdef H5 */
  840. cursor: not-allowed !important;
  841. /* #endif */
  842. }
  843. .uni-datetime-picker-text {
  844. font-size: 14px;
  845. line-height: 50px
  846. }
  847. .uni-datetime-picker-sign {
  848. position: absolute;
  849. top: 53px;
  850. /* 减掉 10px 的元素高度,兼容nvue */
  851. color: #999;
  852. /* #ifdef APP-NVUE */
  853. font-size: 16px;
  854. /* #endif */
  855. }
  856. .sign-left {
  857. left: 86px;
  858. }
  859. .sign-right {
  860. right: 86px;
  861. }
  862. .sign-center {
  863. left: 135px;
  864. }
  865. .uni-datetime-picker__container-box {
  866. position: relative;
  867. display: flex;
  868. align-items: center;
  869. justify-content: center;
  870. margin-top: 40px;
  871. }
  872. .time-hide-second {
  873. width: 180px;
  874. }
  875. </style>