index.vue 5.6 KB

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