index.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <template>
  2. <view class="jnpf-date-time">
  3. <selectBox v-model="innerValue" :disabled='disabled' :placeholder="placeholder" @openSelect="openSelect"
  4. :select-open="selectShow" >
  5. </selectBox>
  6. <Select v-if="selectShow" v-model="selectShow" mode="time" :defaultTime="defaultTime" :params="params"
  7. :startDate="startDate" :endDate="endDate" :format='format' @close="handleClose" @confirm="handleConfirm" />
  8. </view>
  9. </template>
  10. <script>
  11. import Select from './Select.vue';
  12. import selectBox from '@/components/selectBox'
  13. export default {
  14. name: 'jnpf-dateTime',
  15. components: {
  16. Select,
  17. selectBox
  18. },
  19. props: {
  20. scene: {
  21. type: String,
  22. default: 'form'
  23. },
  24. inputType: {
  25. type: String,
  26. default: 'select'
  27. },
  28. modelValue: {
  29. type: [String, Number],
  30. default: ''
  31. },
  32. placeholder: {
  33. type: String,
  34. default: '请选择'
  35. },
  36. disabled: {
  37. type: Boolean,
  38. default: false
  39. },
  40. type: {
  41. type: String,
  42. default: 'date'
  43. },
  44. startTime: {
  45. type: [String, Number],
  46. default: 0
  47. },
  48. selectType: {
  49. type: String,
  50. default: ''
  51. },
  52. endTime: {
  53. type: [String, Number],
  54. default: 0
  55. },
  56. format: {
  57. type: String,
  58. default: 'yyyy-MM-dd HH:mm:ss'
  59. }
  60. },
  61. data() {
  62. return {
  63. startDate: '',
  64. endDate: '',
  65. params: {
  66. year: true,
  67. month: true,
  68. day: true,
  69. hour: true,
  70. minute: true,
  71. second: true,
  72. timestamp: true
  73. },
  74. defaultTime: '',
  75. selectShow: false,
  76. innerValue: '',
  77. startTimestamp: -25140,
  78. endTimestamp: 7289625599000,
  79. formatObj: {
  80. 'yyyy': 'yyyy',
  81. 'yyyy-MM': 'yyyy-mm',
  82. 'yyyy-MM-dd': 'yyyy-mm-dd',
  83. 'yyyy-MM-dd HH:mm': 'yyyy-mm-dd hh:MM',
  84. 'yyyy-MM-dd HH:mm:ss': 'yyyy-mm-dd hh:MM:ss',
  85. 'HH:mm:ss': 'hh:MM:ss',
  86. "HH:mm": "hh:MM",
  87. 'YYYY': 'yyyy',
  88. 'YYYY-MM': 'yyyy-mm',
  89. 'YYYY-MM-DD': 'yyyy-mm-dd',
  90. 'YYYY-MM-DD HH:mm': 'yyyy-mm-dd hh:MM',
  91. 'YYYY-MM-DD HH:mm:ss': 'yyyy-mm-dd hh:MM:ss',
  92. }
  93. }
  94. },
  95. watch: {
  96. modelValue: {
  97. handler(val) {
  98. this.setDefault()
  99. },
  100. immediate: true,
  101. deep: true
  102. },
  103. startTime(val) {
  104. this.setMode()
  105. },
  106. endTime(val) {
  107. this.setMode()
  108. }
  109. },
  110. created() {
  111. this.setMode()
  112. },
  113. methods: {
  114. setMode() {
  115. let str = this.formatObj[this.format] || 'yyyy-mm-dd hh:MM:ss'
  116. let formatArr = str.trim().split(" ")
  117. let startYear = '970'
  118. if (this.type === 'time') {
  119. let t = formatArr[0].split(":") || []
  120. this.params = {
  121. ...this.params,
  122. year: false,
  123. month: false,
  124. day: false,
  125. hour: t.includes('hh'),
  126. minute: t.includes('MM'),
  127. second: t.includes('ss'),
  128. }
  129. this.startDate = this.startTime ? this.getYearDate() + ' ' + this.startTime : this.getYearDate() +
  130. ' ' + "00:00:00"
  131. this.endDate = this.endTime ? this.getYearDate() + ' ' + this.endTime : this.getYearDate() + ' ' +
  132. "23:59:59"
  133. } else {
  134. let y = formatArr[0] ? formatArr[0].split("-") : []
  135. let t = formatArr[1] ? formatArr[1].split(":") : []
  136. this.params = {
  137. ...this.params,
  138. year: y.includes('yyyy'),
  139. month: y.includes('mm'),
  140. day: y.includes('dd'),
  141. hour: t.includes('hh'),
  142. minute: t.includes('MM'),
  143. second: t.includes('ss'),
  144. }
  145. // #ifdef APP
  146. const sys = uni.getSystemInfoSync()
  147. let platform = sys.platform
  148. startYear = platform === 'ios' ? '1899' : '970'
  149. // #endif
  150. this.startDate = this.startTime ? this.$u.timeFormat(this.startTime, str) : startYear + '-1-1 00:00:00'
  151. this.endDate = this.endTime ? this.$u.timeFormat(this.endTime, str) : '2500-12-31 23:59:59'
  152. }
  153. },
  154. getYearDate() {
  155. let date = new Date();
  156. let year = date.getFullYear()
  157. let month = date.getMonth() + 1
  158. let day = date.getDate()
  159. return year + '-' + month + '-' + day
  160. },
  161. setDefault() {
  162. if (!this.modelValue) return this.innerValue = ''
  163. if (this.type === 'time') {
  164. let valueArr = this.modelValue.split(':')
  165. let formatArr = this.formatObj[this.format].split(':')
  166. this.innerValue = this.modelValue
  167. if (valueArr.length != formatArr.length) this.innerValue = valueArr[0] + ':' + valueArr[1]
  168. this.defaultTime = this.getYearDate() + ' ' + this.modelValue
  169. } else {
  170. const format = 'yyyy-mm-dd hh:MM:ss'
  171. this.innerValue = this.$u.timeFormat(Number(this.modelValue), this.formatObj[this.format])
  172. this.defaultTime = this.$u.timeFormat(Number(this.modelValue), format)
  173. }
  174. },
  175. openSelect() {
  176. uni.hideKeyboard()
  177. if (this.disabled) return
  178. if (new Date(this.startDate).getTime() > new Date(this.endDate).getTime()) return this
  179. .$u.toast('开始时间不能大于结束时间')
  180. this.selectShow = true
  181. },
  182. handleConfirm(e) {
  183. let newFormat = this.format
  184. let timeType = newFormat === 'yyyy' ? '/01/01 00:00:00' : newFormat === 'yyyy-MM' ? '/01 00:00:00' :
  185. newFormat === 'yyyy-MM-dd' ?
  186. ' 00:00:00' : ''
  187. this.innerValue = ''
  188. if (this.params.year) this.innerValue += e.year
  189. if (this.params.month) this.innerValue += '-' + e.month
  190. if (this.params.day) this.innerValue += '-' + e.day
  191. if (this.params.hour) this.innerValue += (this.type === 'time' ? '' : ' ') + e.hour
  192. if (this.params.minute) this.innerValue += ':' + e.minute
  193. if (this.params.second) this.innerValue += ':' + e.second
  194. const value = this.type === 'time' ? this.innerValue : e.timestamp
  195. if (this.modelValue === value) return
  196. this.$emit('update:modelValue', value)
  197. this.$emit('change', value, this.selectType)
  198. },
  199. handleClose() {
  200. this.selectShow = false
  201. }
  202. }
  203. }
  204. </script>
  205. <style lang="scss" scoped>
  206. .jnpf-date-time {
  207. width: 100%;
  208. }
  209. </style>