dateTimePicker.vue 6.9 KB

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