You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

datetime-picker.vue 2.9 KiB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <template>
  2. <!-- #ifndef MP-DINGTALK -->
  3. <l-label @click="click" :arrow="!disabled" :required="required" :title="title" :class="className" :style="style">
  4. {{ value || displayPlaceholder }}
  5. <l-datetime-panel
  6. v-if="datetimeModal"
  7. @confirm="confirm"
  8. @cancel="cancel"
  9. :val="value"
  10. :startYear="1900"
  11. ref="datetime"
  12. allShow
  13. isAll
  14. />
  15. </l-label>
  16. <!-- #endif -->
  17. <!-- #ifdef MP-DINGTALK -->
  18. <l-label :arrow="arrow" :required="required" :title="title" :class="className" :style="style">
  19. <text @click="click">{{ value || displayPlaceholder }}</text>
  20. </l-label>
  21. <!-- #endif -->
  22. </template>
  23. <script>
  24. export default {
  25. name: 'l-datetime-picker',
  26. props: {
  27. title: {},
  28. disabled: {},
  29. placeholder: {},
  30. required: {},
  31. value: {}
  32. },
  33. // #ifndef MP-DINGTALK
  34. data() {
  35. return {
  36. datetimeModal: false
  37. }
  38. },
  39. // #endif
  40. methods: {
  41. click(e) {
  42. if (this.disabled) {
  43. return
  44. }
  45. // #ifndef MP-DINGTALK
  46. // 支付宝
  47. if (this.datetimeModal) {
  48. return
  49. }
  50. const datetimePanel = this.$refs.datetime
  51. this.datetimeModal = true
  52. this.$nextTick(() => {
  53. datetimePanel.setDate(this.value)
  54. datetimePanel.show()
  55. this.$emit('open')
  56. })
  57. // #endif
  58. // #ifdef MP-DINGTALK
  59. // 钉钉
  60. const getCurrent = () => {
  61. const now = new Date()
  62. const year = now.getFullYear().toString()
  63. const month = (now.getMonth() + 1).toString().padStart(2, '0')
  64. const date = now
  65. .getDate()
  66. .toString()
  67. .padStart(2, '0')
  68. const hours = now
  69. .getHours()
  70. .toString()
  71. .padStart(2, '0')
  72. const minutes = now
  73. .getMinutes()
  74. .toString()
  75. .padStart(2, '0')
  76. return `${year}-${month}-${date} ${hours}:${minutes}`
  77. }
  78. const currentDate = this.value ? this.value : getCurrent()
  79. this.$emit('open')
  80. dd.datePicker({
  81. currentDate,
  82. format: 'yyyy-MM-dd HH:mm',
  83. success: ({ date }) => {
  84. if (date) {
  85. this.$emit('input', date + ':00')
  86. this.$emit('change', date + ':00')
  87. }
  88. },
  89. complete: () => {
  90. this.$emit('close')
  91. }
  92. })
  93. // #endif
  94. },
  95. // #ifndef MP-DINGTALK
  96. confirm({ selectRes }) {
  97. setTimeout(() => {
  98. this.datetimeModal = false
  99. this.$emit('input', selectRes)
  100. this.$emit('change', selectRes)
  101. this.$emit('close')
  102. }, 300)
  103. },
  104. cancel() {
  105. setTimeout(() => {
  106. this.datetimeModal = false
  107. }, 300)
  108. this.$emit('close')
  109. }
  110. // #endif
  111. },
  112. computed: {
  113. displayPlaceholder() {
  114. if (this.disabled) {
  115. return ''
  116. }
  117. if (this.placeholder) {
  118. return this.placeholder
  119. }
  120. return this.title ? `请选择${this.title}` : '请选择…'
  121. }
  122. }
  123. }
  124. </script>