wk-week-picker.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <template>
  2. <view :style="{height: height + 'px'}" class="wk-date-picker picker">
  3. <view class="picker-header">
  4. <view class="cancel" @click="handleCancel">
  5. 取消
  6. </view>
  7. <view class="picker-header-title" />
  8. <view class="confirm" @click="handleConfirm">
  9. 确认
  10. </view>
  11. </view>
  12. <picker-view
  13. v-if="showPicker"
  14. v-model="valueIndexArr"
  15. indicator-class="indicator-active"
  16. class="picker-body"
  17. @change="bindChange">
  18. <picker-view-column class="picker-body-column">
  19. <view
  20. v-for="(item,index) in list"
  21. :key="index"
  22. class="picker-item">
  23. {{ item.start }}~{{ item.end }}
  24. </view>
  25. </picker-view-column>
  26. </picker-view>
  27. </view>
  28. </template>
  29. <script>
  30. import moment from 'moment'
  31. export default {
  32. name: 'WkDatePicker',
  33. inject: ['popup'],
  34. props: {
  35. value: {
  36. type: Object,
  37. default: () => {}
  38. },
  39. startTime: {
  40. type: String,
  41. default: null
  42. },
  43. endTime: {
  44. type: String,
  45. default: null
  46. }
  47. },
  48. data() {
  49. return {
  50. height: 0,
  51. valueArr: [],
  52. valueIndexArr: [],
  53. list: [],
  54. maxDate: [],
  55. minDate: [],
  56. showPicker: false
  57. }
  58. },
  59. mounted() {
  60. this.initDate()
  61. const that = this
  62. uni.getSystemInfo({
  63. success: res => {
  64. that.height = res.windowHeight * 0.4
  65. }
  66. })
  67. },
  68. methods: {
  69. /**
  70. * 初始化
  71. */
  72. initDate() {
  73. // 今天是本周的第几天
  74. let weekOfDay = moment().format('E')
  75. // 获取本周周一
  76. let start = moment().subtract(weekOfDay - 1, 'days')
  77. // 获取本周周末
  78. let end = moment().add(7 - weekOfDay, 'days')
  79. const arr = []
  80. arr.push({
  81. start: start.format('YYYY-MM-DD'),
  82. end: moment().format('YYYY-MM-DD')
  83. })
  84. console.log('time', start, end)
  85. for (let i = 0; i < 80; i++) {
  86. start = start.subtract(7, 'days')
  87. end = end.subtract(7, 'days')
  88. arr.unshift({
  89. start: start.format('YYYY-MM-DD'),
  90. end: end.format('YYYY-MM-DD')
  91. })
  92. }
  93. this.list = arr
  94. const startVal = moment(this.value.start).format('YYYY-MM-DD')
  95. let index = arr.findIndex(o => o.start === startVal)
  96. if (index === -1) {
  97. index = arr.length - 1
  98. }
  99. this.valueIndexArr = [index]
  100. this.showPicker = false
  101. this.$nextTick(() => {
  102. this.showPicker = true
  103. })
  104. },
  105. bindChange({ detail }) {
  106. this.valueIndexArr = detail.value
  107. },
  108. handleConfirm() {
  109. console.log('valueArr:', this.valueArr)
  110. console.log('valueIndexArr: ', this.valueIndexArr)
  111. const index = this.valueIndexArr[0]
  112. let value = this.list[index]
  113. this.$emit('input', value)
  114. this.$emit('select', value, () => {
  115. this.popup.close()
  116. })
  117. },
  118. handleCancel() {
  119. this.popup.close()
  120. }
  121. }
  122. }
  123. </script>
  124. <style scoped lang="scss">
  125. .picker {
  126. width: 100%;
  127. background-color: white;
  128. display: flex;
  129. flex-direction: column;
  130. overflow: hidden;
  131. .picker-header {
  132. height: 70rpx;
  133. font-size: 26rpx;
  134. border-bottom: 1rpx solid $border-color;
  135. padding: 0 30rpx;
  136. @include left;
  137. .picker-header-title {
  138. flex: 1;
  139. }
  140. .cancel {
  141. color: #666666;
  142. }
  143. .confirm {
  144. color: $theme-color;
  145. }
  146. }
  147. .picker-body {
  148. flex: 1;
  149. padding: 0 30rpx;
  150. .picker-body-column {
  151. .picker-item {
  152. font-size: 26rpx;
  153. @include center;
  154. }
  155. }
  156. }
  157. }
  158. </style>