time-picker.vue 22 KB

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