time-picker.vue 22 KB

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