dateSelectDl.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <template>
  2. <uni-popup ref="commonPopup" :animation="false" :is-mask-click="false"
  3. mask-background-color="rgba(0, 0, 0, 0.4)">
  4. <view class="phone-common-dialog">
  5. <view class="common-body-box">
  6. <view class="common-title">请选择日期</view>
  7. <view class="common-content" :class="dialogContentClass">
  8. <uni-datetime-picker v-model="infoDate.selected" type="daterange" @change="dateConfirm" />
  9. </view>
  10. <view class="common-btn-box">
  11. <view class="confirm-btn" @click="confirmBtn">确认</view>
  12. </view>
  13. </view>
  14. </view>
  15. </uni-popup>
  16. </template>
  17. <script setup>
  18. import { ref,reactive } from 'vue';
  19. const props = defineProps({
  20. title: {
  21. type: String,
  22. default: ''
  23. },
  24. content: {
  25. type: String,
  26. require: true,
  27. default: ''
  28. },
  29. dialogContentClass: {
  30. type: String,
  31. require: true,
  32. default: 'content-center-class'
  33. },
  34. notBtn: {
  35. type: String,
  36. require: true,
  37. default: '取消'
  38. },
  39. okBtn: {
  40. type: String,
  41. require: true,
  42. default: '确认'
  43. },
  44. });
  45. const commonPopup = ref(null); // 索引
  46. const $emit = defineEmits(['confirm-btn', 'date-reset'])
  47. /**
  48. * 获取任意时间
  49. */
  50. function getDate(date, AddDayCount = 0) {
  51. if (!date) {
  52. date = new Date()
  53. }
  54. if (typeof date !== 'object') {
  55. date = date.replace(/-/g, '/')
  56. }
  57. const dd = new Date(date)
  58. dd.setDate(dd.getDate() + AddDayCount) // 获取AddDayCount天后的日期
  59. const y = dd.getFullYear()
  60. const m = dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1 // 获取当前月份的日期,不足10补0
  61. const d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate() // 获取当前几号,不足10补0
  62. return {
  63. fullDate: y + '-' + m + '-' + d,
  64. year: y,
  65. month: m,
  66. date: d,
  67. day: dd.getDay()
  68. }
  69. }
  70. const infoDate = reactive({
  71. lunar: true,
  72. range: true,
  73. insert: false,
  74. selected: [],
  75. valueData: null
  76. })
  77. function dateConfirm(data) {
  78. infoDate.selected = data;
  79. }
  80. // 打开弹窗
  81. function handleShow(data) {
  82. if (data) {
  83. infoDate.selected = data;
  84. }
  85. commonPopup.value.open();
  86. }
  87. // 取消
  88. function handleClose() {
  89. commonPopup.value.close();
  90. }
  91. // 确认
  92. function confirmBtn(){
  93. if (!infoDate.selected.lenth||infoDate.selected.length>1) {
  94. $emit('confirm-btn',infoDate.selected);
  95. commonPopup.value.close();
  96. } else {
  97. uni.showToast({
  98. title: '请选择日期',
  99. icon: 'none'
  100. })
  101. }
  102. }
  103. function handleReset() {
  104. $emit('date-reset')
  105. infoDate.selected = []
  106. handleClose();
  107. }
  108. defineExpose({
  109. handleShow,
  110. handleClose
  111. })
  112. </script>
  113. <style>
  114. </style>