times.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <template>
  2. <view class="picker-box">
  3. <picker-view class="picker-view" :value="value" @change="bindChange">
  4. <picker-view-column>
  5. <view class="year-item picker-item" v-for="(item,index) in years" :key="`years_${index}`">{{item}}
  6. </view>
  7. </picker-view-column>
  8. <picker-view-column>
  9. <view class="month-item picker-item" v-for="(item,index) in months" :key="`months_${index}`">{{item}}
  10. </view>
  11. </picker-view-column>
  12. <picker-view-column>
  13. <view class="day-item picker-item" v-for="(item,index) in days" :key="`days_${index}`">{{item}}</view>
  14. </picker-view-column>
  15. </picker-view>
  16. </view>
  17. </template>
  18. <script>
  19. import {
  20. dayNums
  21. } from "@/utils/tool.js"
  22. export default {
  23. props: {
  24. time: {
  25. type: String,
  26. default: ''
  27. }
  28. },
  29. data() {
  30. return {
  31. valueTime: [],
  32. value: [0 , 0 , 0],
  33. years: [2022, 2023],
  34. months: ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'],
  35. days: []
  36. };
  37. },
  38. watch: {
  39. time: {
  40. handler(newTime, oldTime) {
  41. if (newTime) {
  42. this.splitTime(newTime)
  43. }
  44. },
  45. immediate: true,
  46. deep: true
  47. }
  48. },
  49. methods: {
  50. // 拆分时间
  51. splitTime(tims) {
  52. this.valueTime = tims.split('-')
  53. this.matchingYears()
  54. },
  55. // 匹配年
  56. matchingYears() {
  57. try {
  58. this.years.forEach((el, index) => {
  59. if (el == this.valueTime[0]) {
  60. // this.value[0] = index;
  61. this.$set(this.value , 0 , index)
  62. throw new Error()
  63. }
  64. })
  65. } catch {}
  66. this.matchingMonths()
  67. },
  68. // 匹配月
  69. matchingMonths() {
  70. try {
  71. this.months.forEach((el, index) => {
  72. if (el == this.valueTime[1]) {
  73. this.$set(this.value , 1 , index)
  74. throw new Error()
  75. }
  76. })
  77. } catch {};
  78. this.matchingDays()
  79. },
  80. // 匹配日
  81. matchingDays() {
  82. // 根据当前时间获取当前月份天数
  83. this.days = dayNums(this.valueTime)
  84. try {
  85. this.days.forEach((el, index) => {
  86. if (el == this.valueTime[2]) {
  87. this.$set(this.value , 2 , index)
  88. throw new Error()
  89. }
  90. })
  91. } catch {};
  92. },
  93. bindChange(e) {
  94. const val = e.detail.value;
  95. if (val[0] !== this.value[0]) {
  96. // 滑动年
  97. this.value = [val[0], 0, 0]
  98. } else if (val[1] !== this.value[1]) {
  99. // 滑动月
  100. this.value = [val[0], val[1], 0]
  101. } else if (val[2] !== this.value[2]) {
  102. // 滑动日
  103. this.value = val
  104. }
  105. this.modificationTime()
  106. },
  107. // 根据时间索引,修改时间
  108. modificationTime() {
  109. let year = this.years[this.value[0]]
  110. let month = this.months[this.value[1]]
  111. let day = this.days[this.value[2]]
  112. this.valueTime = [year, month, day]
  113. this.$emit("update:time", `${year}-${month}-${day}`);
  114. }
  115. // getDays() {
  116. // this.days = dayNums(this.valueTime)
  117. // },
  118. }
  119. }
  120. </script>
  121. <style lang="scss" scoped>
  122. .picker-box {
  123. width: 100%;
  124. height: 100%;
  125. }
  126. .picker-view {
  127. width: 100%;
  128. height: 100%;
  129. .year-item {
  130. padding-left: 80rpx;
  131. }
  132. .month-item {
  133. text-align: center;
  134. }
  135. .day-item {
  136. text-align: right;
  137. padding-right: 80rpx;
  138. }
  139. ::v-deep .uni-picker-view-indicator {
  140. height: 56rpx;
  141. }
  142. .picker-item {
  143. font-size: 28rpx;
  144. font-weight: 700;
  145. height: 56rpx;
  146. line-height: 56rpx;
  147. letter-spacing: 1.4px;
  148. }
  149. }
  150. </style>