wk-field-date-range.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <template>
  2. <view class="wk-field wk-field-date-range">
  3. <view v-if="_label" class="wk-field__label">
  4. <view v-if="field.isNull === 1" class="line" />
  5. {{ _label }}
  6. </view>
  7. <view class="wk-field__body" @click.stop="handleChoose('startTime')">
  8. <view v-if="startTime" class="wk-field__body-core">
  9. {{ startTime }}
  10. </view>
  11. <view v-else class="wk-field__body-core wk-placeholder placeholder">
  12. 开始时间
  13. </view>
  14. <image :src="$static('images/icon/calendar.png')" class="icon-pic" />
  15. </view>
  16. <view class="range-text">
  17. </view>
  18. <view class="wk-field__body" @click.stop="handleChoose('endTime')">
  19. <view v-if="endTime" class="wk-field__body-core">
  20. {{ endTime }}
  21. </view>
  22. <view v-else class="wk-field__body-core wk-placeholder placeholder">
  23. 结束时间
  24. </view>
  25. <image :src="$static('images/icon/calendar.png')" class="icon-pic" />
  26. </view>
  27. <uni-popup ref="popup" type="picker">
  28. <wk-date-picker
  29. v-model="selectedDate"
  30. :type="dateType"
  31. @select="handleSelect" />
  32. </uni-popup>
  33. </view>
  34. </template>
  35. <script>
  36. import mixins from './mixins'
  37. import moment from 'moment'
  38. export default {
  39. name: 'WkFieldDateRange',
  40. mixins: [mixins],
  41. data() {
  42. return {
  43. selectedDate: null,
  44. startTime: null,
  45. endTime: null,
  46. activateKey: ''
  47. }
  48. },
  49. computed: {
  50. dateType() {
  51. return Number(this.field.precisions) === 1 ? 'date' : 'datetime'
  52. }
  53. },
  54. watch: {
  55. formValue: {
  56. handler() {
  57. // this.checkedStatus = Boolean(Number(this.formValue))
  58. this.startTime = this.formValue[0] || null
  59. this.endTime = this.formValue[1] || null
  60. },
  61. immediate: true,
  62. deep: true
  63. }
  64. },
  65. methods: {
  66. /**
  67. * 格式化时间
  68. * @param {Object} val
  69. */
  70. formatTime(val) {
  71. if (!val) return ''
  72. if (this.dateType === 'date') return moment(val).format('YYYY-MM-DD')
  73. return val
  74. },
  75. /**
  76. * 去选择
  77. * @param {Object} key
  78. */
  79. handleChoose(key) {
  80. if (this.field.disabled) {
  81. if (this.config && this.config.disabledMsg) {
  82. this.$toast(this.config.disabledMsg)
  83. }
  84. return
  85. }
  86. this.activateKey = key
  87. this.$refs.popup.open()
  88. },
  89. /**
  90. * 选择
  91. * @param {Object} data
  92. * @param {Object} next
  93. */
  94. handleSelect(data, next) {
  95. next()
  96. let start = this.activateKey === 'startTime' ? data : this.startTime
  97. let end = this.activateKey === 'endTime' ? data : this.endTime
  98. if (start && end && moment(start).isAfter(end)) {
  99. this.$toast('开始时间不能大于结束时间')
  100. return
  101. }
  102. this[this.activateKey] = data
  103. this.formValue = [this.startTime, this.endTime]
  104. this.$emit('input', this.formValue)
  105. this.$emit('change', {
  106. index: this.index,
  107. field: this.field,
  108. value: this.formValue
  109. })
  110. }
  111. }
  112. }
  113. </script>
  114. <style scoped lang="scss">
  115. @import './wkField.scss';
  116. .range-text {
  117. font-size: $wk-font-sm;
  118. color: $gray;
  119. margin: 10rpx 0;
  120. }
  121. .wk-field__body {
  122. .wk-field__body-core.placeholder {
  123. font-size: $wk-font-base;
  124. }
  125. .icon-pic {
  126. width: 32rpx;
  127. height: 32rpx;
  128. }
  129. }
  130. </style>