dateTimePicker.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <template>
  2. <view class="pickebox">
  3. <view class="pickebox-typebox" v-if="showType">
  4. <view :class="activeTab == index ? 'type-active':''" v-for="(item,index) in tabs" :key="index" @click="handleTab(item,index)">{{item.label}}</view>
  5. </view>
  6. <view class="pickebox-picker">
  7. <view class="pickebox-pickerbtn prev" @click="handlePrev">
  8. <image src="@/static/images/pages_watch/icons/arrow_time_yes_icon.png"></image>
  9. <!-- <image src="@/static/images/pages_watch/icons/arrow_time_no_icon.png"></image> -->
  10. </view>
  11. <view v-if="activeTab == 1" @click="openWeek">{{weekValue}}</view>
  12. <template v-else>
  13. <picker mode="date" :fields="fields" :value="value" style="display:inline-block;" @change="bindPickerChange">
  14. <view>{{_date}}</view>
  15. </picker>
  16. </template>
  17. <view class="pickebox-pickerbtn next" @click="handleNext">
  18. <image src="@/static/images/pages_watch/icons/arrow_time_yes_icon.png"></image>
  19. <!-- <image src="@/static/images/pages_watch/icons/arrow_time_no_icon.png"></image> -->
  20. </view>
  21. </view>
  22. <!-- 周 -->
  23. <uni-popup ref="popup" type="bottom">
  24. <myWeekPicker :value="value" @change="getWeeks"></myWeekPicker>
  25. </uni-popup>
  26. </view>
  27. </template>
  28. <script>
  29. import myWeekPicker from "@/pages_watch/components/myWeekPicker/myWeekPicker.vue"
  30. import weekjs from '../myWeekPicker/week.js';
  31. export default {
  32. name:"dateTimePicker",
  33. components: {
  34. myWeekPicker
  35. },
  36. props: {
  37. showType: {
  38. type: Boolean,
  39. default: true
  40. },
  41. // 0: yyyy-mm-dd
  42. // 1: yyyy年mm月dd日
  43. formatType: {
  44. type: Number,
  45. default: 0
  46. },
  47. from: {
  48. type: String,
  49. default: ""
  50. }
  51. },
  52. data() {
  53. return {
  54. tabs: [{
  55. label: '每日',
  56. value: 'day'
  57. },{
  58. label: '每周',
  59. value: 'week'
  60. },{
  61. label: '每月',
  62. value: 'month'
  63. }],
  64. activeTab: 0,
  65. // year、month、day,表示选择器的粒度,默认为 day
  66. fields: 'day',
  67. value: this.$timeFormat(),
  68. weekValue: "",
  69. startTime: "",
  70. endTime: ""
  71. }
  72. },
  73. computed: {
  74. _date() {
  75. if(this.formatType == 1 && this.value) {
  76. console.log(this.fields)
  77. if(this.fields == 'month') {
  78. return this.$timeFormat(new Date(this.value),'yyyy年mm月')
  79. } else {
  80. return this.$timeFormat(new Date(this.value),'yyyy年mm月dd日')
  81. }
  82. } else {
  83. return this.value
  84. }
  85. }
  86. },
  87. created() {
  88. if(this.from == "report") {
  89. this.activeTab = 2
  90. this.fields = 'month'
  91. this.value = this.$timeFormat(new Date(), 'yyyy-mm')
  92. } else if(this.from == "sports") {
  93. this.tabs = [{
  94. label: '每日',
  95. value: 'day'
  96. },{
  97. label: '每周',
  98. value: 'week'
  99. }]
  100. }
  101. this.resetTime()
  102. },
  103. methods: {
  104. handleTab(item,index) {
  105. this.activeTab = index
  106. this.fields = item.value == 'week' ? 'day' : item.value
  107. if(this.activeTab == 0) { // 每日
  108. this.value = this.$timeFormat()
  109. } else if(this.activeTab == 1) { // 每周
  110. this.value = this.$timeFormat()
  111. } else if(this.activeTab == 2) { // 每月
  112. this.value = this.$timeFormat(new Date(), 'yyyy-mm')
  113. }
  114. this.resetTime()
  115. },
  116. bindPickerChange(e) {
  117. this.value = e.detail.value
  118. this.resetTime()
  119. },
  120. handlePrev() {
  121. // 前一天
  122. const yesterday = new Date(this.value).setDate(new Date(this.value).getDate() - 1);
  123. // 前一个月
  124. let day = this.value
  125. if(this.activeTab == 2) {
  126. day = day + '-01'
  127. }
  128. const prevMonths = new Date(day).setMonth(new Date(day).getMonth() - 1);
  129. // 7天前日期
  130. const weekday = new Date(this.value).setDate(new Date(this.value).getDate() - 7);
  131. if(this.activeTab == 0) { // 每日
  132. this.value = this.$timeFormat(yesterday)
  133. } else if(this.activeTab == 1) { // 每周
  134. this.value = this.$timeFormat(weekday)
  135. } else if(this.activeTab == 2) { // 每月
  136. this.value = this.$timeFormat(prevMonths, 'yyyy-mm')
  137. }
  138. this.resetTime()
  139. },
  140. handleNext() {
  141. // 下一天
  142. const yesterday = new Date(this.value).setDate(new Date(this.value).getDate() + 1);
  143. // 下一个月
  144. let day = this.value
  145. if(this.activeTab == 2) {
  146. day = day + '-01'
  147. }
  148. const prevMonths = new Date(day).setMonth(new Date(day).getMonth() + 1);
  149. // 7天后日期
  150. const weekday = new Date(this.value).setDate(new Date(this.value).getDate() + 7);
  151. if(this.activeTab == 0) { // 每日
  152. this.value = this.$timeFormat(yesterday)
  153. } else if(this.activeTab == 1) { // 每周
  154. this.value = this.$timeFormat(weekday)
  155. } else if(this.activeTab == 2) { // 每月
  156. this.value = this.$timeFormat(prevMonths, 'yyyy-mm')
  157. }
  158. this.resetTime()
  159. },
  160. resetTime() {
  161. if(this.activeTab == 0) { // 每日
  162. this.startTime = this.value + " 00:00:00"
  163. this.endTime = this.value + " 23:59:59"
  164. } else if(this.activeTab == 1) { // 每周
  165. let y = this.value.split('-')[0];
  166. let m = this.value.split('-')[1];
  167. let res = weekjs.getWeeksByMonth(y, m,this.value,0);
  168. this.weekValue = res.week.formatVal
  169. this.startTime = this.weekValue.split('至')[0] + " 00:00:00"
  170. this.endTime = this.weekValue.split('至')[1] + " 23:59:59"
  171. } else if(this.activeTab == 2) { // 每月
  172. this.startTime = this.$timeFormat(this.value, 'yyyy-mm-dd') + " 00:00:00"
  173. const currentYear = this.value.split('-')[0]
  174. const currentMonth = this.value.split('-')[1]
  175. const lastday = new Date(currentYear, currentMonth, 0)
  176. this.endTime = this.$timeFormat(lastday, 'yyyy-mm-dd') + " 23:59:59"
  177. }
  178. this.$emit("onChange",[this.startTime,this.endTime,this.value,this.activeTab])
  179. },
  180. getWeeks(res) {
  181. if (res) {
  182. this.value = res.week.val
  183. this.weekValue = res.week.formatVal
  184. this.resetTime()
  185. }
  186. this.$refs['popup'].close()
  187. },
  188. openWeek() {
  189. this.$refs['popup'].open()
  190. }
  191. }
  192. }
  193. </script>
  194. <style lang="scss" scoped>
  195. @mixin u-flex($flexD, $alignI, $justifyC) {
  196. display: flex;
  197. flex-direction: $flexD;
  198. align-items: $alignI;
  199. justify-content: $justifyC;
  200. }
  201. .pickebox {
  202. font-family: PingFang SC, PingFang SC;
  203. font-weight: 400;
  204. font-size: 30rpx;
  205. color: #333333;
  206. .type-active {
  207. background: #FF7700;
  208. font-weight: 500;
  209. font-size: 30rpx;
  210. color: #FFFFFF;
  211. }
  212. &-typebox {
  213. height: 88rpx;
  214. @include u-flex(row, center, center);
  215. // gap: 16rpx;
  216. view {
  217. width: 124rpx;
  218. height: 64rpx;
  219. margin: 8rpx;
  220. border-radius: 32rpx 32rpx 32rpx 32rpx;
  221. color: #757575;
  222. line-height: 64rpx;
  223. text-align: center;
  224. }
  225. }
  226. &-picker {
  227. height: 88rpx;
  228. margin: 20rpx 0;
  229. @include u-flex(row, center, center);
  230. // gap: 32rpx;
  231. }
  232. &-pickerbtn {
  233. margin: 0 32rpx;
  234. image {
  235. width: 48rpx;
  236. height: 48rpx;
  237. background: #FFFFFF;
  238. border-radius: 50%;
  239. overflow: hidden;
  240. }
  241. }
  242. .next image{
  243. transform: rotateZ(180deg);
  244. }
  245. }
  246. </style>