date-picker.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <template>
  2. <div>
  3. <u-popup ref="popup" class="popup" :mask="true" mode="bottom" border-radius="20">
  4. <view>
  5. <view class="popup-title ">
  6. <text>时间筛选</text>
  7. <text @click="$refs.popup.close()">×</text>
  8. </view>
  9. <view class="quickOptions dis f-c ">
  10. <text class="title">快捷选择</text>
  11. <view class=" dis a-c j-start">
  12. <view class="tab" :class="{tabactive:OptionsTabIndex==index}"
  13. v-for="(item,index) in quickOptions" :key="index" @click="OptionsTabSelect(item,index)">
  14. <text>{{item}}</text>
  15. </view>
  16. </view>
  17. </view>
  18. <text style="margin-left: 42rpx;font-size: 30rpx;color: #333;">自定义时间</text>
  19. <view style="padding:20rpx 42rpx">
  20. <view class="popup-data dis j-s">
  21. <text :class="startShow? 'active' :''"
  22. @click="startClick()">{{ interviewStartTime||'开始时间' }}</text>至
  23. <text :class="endShow? 'active' :''" @click="endClick()">{{ interviewEndTime || '结束时间' }}</text>
  24. </view>
  25. <picker-view v-show="visible" :indicator-style="indicatorStyle" :value="value" @change="bindChange"
  26. class="picker-view">
  27. <picker-view-column>
  28. <view class="item dis a-c j-c " v-for="(item,index) in years" :key="index">{{item}}年</view>
  29. </picker-view-column>
  30. <picker-view-column>
  31. <view class="item dis a-c j-c " v-for="(item,index) in months" :key="index">{{item}}月</view>
  32. </picker-view-column>
  33. <picker-view-column>
  34. <view class="item dis a-c j-c " v-for="(item,index) in days" :key="index">{{item}}日</view>
  35. </picker-view-column>
  36. </picker-view>
  37. </view>
  38. </view>
  39. <view class="popup-footer dis j-s">
  40. <text @click.stop="reset">重置</text>
  41. <text @click.stop="confirm">确定</text>
  42. </view>
  43. </u-popup>
  44. </div>
  45. </template>
  46. <script>
  47. export default {
  48. data() {
  49. return {
  50. quickOptions: ["今日", "本月", "本年"], //快捷选项
  51. OptionsTabIndex: null, //下标
  52. startShow: false,
  53. endShow: false,
  54. interviewStartTime: '',
  55. interviewEndTime: '',
  56. years: [],
  57. year: null,
  58. months: [],
  59. month: null,
  60. days: [],
  61. day: null,
  62. hours: [],
  63. hour: null,
  64. minutes: [],
  65. minute: null,
  66. value: [],
  67. indicatorStyle: `height: 50px;color:#2D6DFF `,
  68. visible: false
  69. }
  70. },
  71. props: {
  72. // visible: {
  73. // type: Boolean,
  74. // default: true
  75. // },
  76. // interviewTime: {
  77. // type: String,
  78. // default() {
  79. // return '';
  80. // },
  81. // }
  82. },
  83. mounted() {
  84. this.init();
  85. },
  86. watch: {
  87. // visible: {
  88. // handler(newValue, oldValue) {
  89. // if (newValue) {
  90. // if (this.interviewTime) {
  91. // this.init(this.interviewTime);
  92. // }
  93. // }
  94. // },
  95. // immediate: false,
  96. // deep: true
  97. // }
  98. },
  99. methods: {
  100. //快捷选择
  101. OptionsTabSelect(item, index) {
  102. switch (item) {
  103. case "今日":
  104. let dotayDate = new Date().toISOString().split('T')[0] //今日日期
  105. this.interviewStartTime = dotayDate;
  106. this.interviewEndTime = dotayDate;
  107. break;
  108. case "本月":
  109. const currentMonthRange = this.getCurrentMonthRange();
  110. this.interviewStartTime = currentMonthRange.start;
  111. this.interviewEndTime = currentMonthRange.end;
  112. break;
  113. case "本年":
  114. const currentYear = new Date().getFullYear();
  115. this.interviewStartTime = `${currentYear}-01-01`;
  116. this.interviewEndTime = `${currentYear}-12-31`;
  117. break;
  118. default:
  119. break;
  120. }
  121. this.OptionsTabIndex = index;
  122. },
  123. //获取本月
  124. getCurrentMonthRange() {
  125. const today = new Date();
  126. const firstDay = new Date(today.getFullYear(), today.getMonth(), 1); // 本月第一天
  127. const lastDay = new Date(today.getFullYear(), today.getMonth() + 1, 0); // 本月最后一天
  128. // 格式化为 YYYY-MM-DD
  129. const formatDate = (date) => {
  130. const year = date.getFullYear();
  131. const month = String(date.getMonth() + 1).padStart(2, '0');
  132. const day = String(date.getDate()).padStart(2, '0');
  133. return `${year}-${month}-${day}`;
  134. };
  135. return {
  136. start: formatDate(firstDay),
  137. end: formatDate(lastDay)
  138. };
  139. },
  140. open() {
  141. this.$refs.popup.open()
  142. },
  143. bindChange: function(e) {
  144. const val = e.detail.value
  145. let year = this.years[val[0]]
  146. let isDay = 30,
  147. days = [];
  148. if (val[1] + 1 == 2) {
  149. if ((val[0] % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
  150. console.log('闰年')
  151. isDay = 29;
  152. } else {
  153. isDay = 28;
  154. console.log('平年')
  155. }
  156. } else if ([1, 3, 5, 7, 8, 10, 12].includes(val[1] + 1)) {
  157. isDay = 31;
  158. } else {
  159. isDay = 30;
  160. }
  161. for (let i = 1; i <= isDay; i++) {
  162. days.push(i)
  163. }
  164. this.days = days;
  165. this.year = this.years[val[0]]
  166. this.month = this.months[val[1]]
  167. this.day = this.days[val[2]]
  168. if (this.startShow) {
  169. this.interviewStartTime = this.year + '-' + this.month + '-' + this.day
  170. }
  171. if (this.endShow) {
  172. this.interviewEndTime = this.year + '-' + this.month + '-' + this.day
  173. }
  174. },
  175. init() {
  176. const date = new Date();
  177. const years = []
  178. const year = date.getFullYear()
  179. const months = []
  180. const month = date.getMonth() + 1
  181. const days = []
  182. const day = date.getDate()
  183. let isDay = 30;
  184. if (month == 2) {
  185. if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
  186. isDay = 29;
  187. } else {
  188. isDay = 28;
  189. }
  190. } else if ([1, 3, 5, 7, 8, 10, 12].includes(month)) {
  191. isDay = 31;
  192. } else {
  193. isDay = 30;
  194. }
  195. for (let i = date.getFullYear(); i >= 1900; i--) {
  196. years.push(i)
  197. }
  198. for (let i = 1; i <= 12; i++) {
  199. months.push(i)
  200. }
  201. for (let i = 1; i <= isDay; i++) {
  202. days.push(i)
  203. }
  204. this.years = years
  205. this.year = year
  206. this.months = months
  207. this.month = month
  208. this.days = days
  209. this.day = day
  210. this.value = [0, month - 1, day - 1]
  211. },
  212. // 补0
  213. padZeroStr(originStr) {
  214. if (+originStr < 10) {
  215. return String(originStr).padStart(2, '0')
  216. }
  217. return originStr + ''
  218. },
  219. close() {
  220. this.$emit('update:visible', false);
  221. },
  222. reset() {
  223. this.$emit('update:visible', false);
  224. this.interviewStartTime = ''
  225. this.interviewEndTime = ''
  226. },
  227. startClick() {
  228. // this.interviewStartTime=''
  229. this.interviewStartTime = this.year + '-' + this.month + '-' + this.day
  230. this.visible = true
  231. this.startShow = true
  232. this.endShow = false
  233. },
  234. endClick() {
  235. this.interviewEndTime = this.year + '-' + this.month + '-' + this.day
  236. this.visible = true
  237. this.startShow = false
  238. this.endShow = true
  239. },
  240. confirm() {
  241. // let monthStr = this.padZeroStr(this.month)
  242. // let dayStr = this.padZeroStr(this.day)
  243. // let hourStr = this.padZeroStr(this.hour)
  244. // let minuteStr = this.padZeroStr(this.minute)
  245. // this.timeValue = `${this.year}/${monthStr}/${dayStr} ${hourStr}:${minuteStr}:00`;
  246. this.$emit('confirmPickDate', this.interviewStartTime, this.interviewEndTime);
  247. this.$refs.popup.close()
  248. },
  249. },
  250. }
  251. </script>
  252. <style lang="scss" scoped>
  253. .popup {
  254. border-radius: 10px 10px 0 0 !important;
  255. }
  256. .popup-title {
  257. font-size: 15px;
  258. color: #333333;
  259. padding: 10px;
  260. margin-bottom: 10px;
  261. position: relative;
  262. text-align: center;
  263. border-bottom: 1rpx solid #eee;
  264. text:first-child {
  265. font-weight: 600;
  266. }
  267. text:last-child {
  268. position: absolute;
  269. right: 10px;
  270. font-size: 21px;
  271. color: #999999;
  272. line-height: 23px;
  273. }
  274. }
  275. .quickOptions {
  276. padding: 0 42rpx;
  277. box-sizing: border-box;
  278. margin-bottom: 37rpx;
  279. .title {
  280. font-size: 30rpx;
  281. color: #333;
  282. margin-bottom: 20rpx;
  283. }
  284. .tab {
  285. padding: 7rpx 20rpx;
  286. box-sizing: border-box;
  287. font-size: 26rpx;
  288. color: #666;
  289. border-radius: 4rpx 4rpx 4rpx 4rpx;
  290. border: 1rpx solid #EEEEEE;
  291. margin-right: 30rpx;
  292. }
  293. .tabactive {
  294. background: rgba(45, 109, 255, 0.1);
  295. color: #2D6DFF;
  296. border: 1rpx solid #fff;
  297. }
  298. }
  299. .popup-footer {
  300. margin: 10px 15px;
  301. text {
  302. font-size: 16px;
  303. padding: 5px 0;
  304. color: #FFFFFF;
  305. display: inline-block;
  306. width: 45%;
  307. text-align: center;
  308. }
  309. text:first-child {
  310. color: #2D6DFF;
  311. background: rgba(45, 109, 255, 0.1);
  312. border-radius: 5px 5px 5px 5px;
  313. }
  314. text:last-child {
  315. background: linear-gradient(132deg, #2DD9FF 0%, #2D6DFF 100%);
  316. border-radius: 5px 5px 5px 5px;
  317. }
  318. }
  319. .popup-data {
  320. line-height: 25px;
  321. text {
  322. font-size: 14px;
  323. color: #999999;
  324. display: inline-block;
  325. width: 45%;
  326. text-align: center;
  327. border: 1px solid #DDDDDD;
  328. border-radius: 2px 2px 2px 2px;
  329. }
  330. .active {
  331. border: 1px solid #2D6DFF;
  332. color: #2D6DFF;
  333. }
  334. }
  335. .picker-view {
  336. width: 100%;
  337. height: 150px;
  338. }
  339. </style>