uni-datePicker.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. <template>
  2. <div>
  3. <u-popup :show="modelshow" @close="close" :closeable="closeable" round="20rpx" mode="bottom">
  4. <view>
  5. <view class="popup-title">
  6. <view class="" style="width: 500rpx;margin: auto;">
  7. <u-tabs :list="contentTabslist" @click="click"
  8. :activeStyle="{ color: '#333', fontWeight: 'bold', fontSize: '36rpx' }"
  9. :itemStyle="{ height: '100rpx' }" :inactiveStyle="{ color: '#999', fontSize: '32rpx' }"
  10. :current="currentIndex" :scrollable="false" lineColor="#FDE935" lineWidth="60rpx"
  11. lineHeight="10rpx"></u-tabs>
  12. </view>
  13. </view>
  14. <template v-if="currentName == '选择月份'">
  15. <picker-view :indicator-style="indicatorStyle" :value="monthvalue" @change="monthbindChange"
  16. class="picker-view">
  17. <picker-view-column>
  18. <view class="item dis a-c j-c " v-for="(item, index) in years" :key="index">{{ item }}年
  19. </view>
  20. </picker-view-column>
  21. <picker-view-column>
  22. <view class="item dis a-c j-c " v-for="(item, index) in months" :key="index">{{ item }}月
  23. </view>
  24. </picker-view-column>
  25. </picker-view>
  26. </template>
  27. <template v-if="currentName == '自定义时间'">
  28. <view class="quickOptions dis f-c" v-if="quickOptionsShow">
  29. <view class=" dis a-c j-start">
  30. <view class="tab" :class="{ tabactive: OptionsTabIndex == index }"
  31. v-for="(item, index) in quickOptions" :key="index"
  32. @click="OptionsTabSelect(item, index)">
  33. <text>{{ item }}</text>
  34. </view>
  35. </view>
  36. </view>
  37. <view style="padding:20rpx 42rpx">
  38. <view class="popup-data dis j-s a-c">
  39. <text :class="startShow ? 'active' : ''" @click="startClick()">{{ interviewStartTime || '开始时间'
  40. }}</text>至
  41. <text :class="endShow ? 'active' : ''" @click="endClick()">{{ interviewEndTime || '结束时间'
  42. }}</text>
  43. </view>
  44. <picker-view :indicator-style="indicatorStyle" :value="value" @change="bindChange"
  45. class="picker-view">
  46. <picker-view-column>
  47. <view class="item dis a-c j-c " v-for="(item, index) in years" :key="index">{{ item }}年
  48. </view>
  49. </picker-view-column>
  50. <picker-view-column>
  51. <view class="item dis a-c j-c " v-for="(item, index) in months" :key="index">{{ item }}月
  52. </view>
  53. </picker-view-column>
  54. <picker-view-column>
  55. <view class="item dis a-c j-c " v-for="(item, index) in days" :key="index">{{ item }}日
  56. </view>
  57. </picker-view-column>
  58. </picker-view>
  59. </view>
  60. </template>
  61. </view>
  62. <view class="popup-footer dis j-s a-c">
  63. <view class="dis a-c j-c" @tap="reset">重置</view>
  64. <view class="dis a-c j-c" @tap="confirm">确定</view>
  65. </view>
  66. </u-popup>
  67. </div>
  68. </template>
  69. <script>
  70. export default {
  71. data() {
  72. return {
  73. modelshow: this.show,
  74. // 当前选中项下标不能超过tabs列表长度,否则u-tabs内部读取不到对应项报错(reading 'rect')
  75. currentIndex: Math.min(this.current, (this.contentTabslist || []).length - 1),
  76. currentName: "自定义时间",
  77. quickOptions: ["本周", "本月", "本年"], //快捷选项
  78. OptionsTabIndex: null, //下标
  79. startShow: true,
  80. endShow: false,
  81. interviewStartTime: '', //开始时间
  82. interviewEndTime: '', //结束时间
  83. monthTime: '', //月份
  84. years: [],
  85. year: null,
  86. year1: null,
  87. months: [],
  88. month: null,
  89. month1: null,
  90. days: [],
  91. day: null,
  92. hours: [],
  93. hour: null,
  94. minutes: [],
  95. minute: null,
  96. value: [],
  97. monthvalue: [],
  98. indicatorStyle: `height: 50px`,
  99. visible: true //控制日期显示
  100. }
  101. },
  102. props: {
  103. contentTabslist: {
  104. type: Array,
  105. default: () => [{ //选项卡
  106. name: '选择月份'
  107. }, {
  108. name: '自定义时间'
  109. }]
  110. },
  111. //关闭图标
  112. closeable: {
  113. type: Boolean,
  114. default: true,
  115. },
  116. show: {
  117. type: Boolean,
  118. default: false
  119. },
  120. // 快捷选项显示
  121. quickOptionsShow: {
  122. type: Boolean,
  123. default: true
  124. },
  125. current: {
  126. type: Number,
  127. default: 1,
  128. }
  129. },
  130. mounted() {
  131. this.init();
  132. },
  133. watch: {
  134. show(newVal) {
  135. this.modelshow = newVal
  136. // 弹窗打开时,自动定位到已选日期
  137. if (newVal) {
  138. this.$nextTick(() => {
  139. const targetDate = this.startShow ? this.interviewStartTime : this.interviewEndTime;
  140. if (targetDate) {
  141. this.updatePickerPosition(targetDate);
  142. }
  143. });
  144. }
  145. },
  146. current(newVal) {
  147. this.currentIndex = newVal
  148. }
  149. },
  150. methods: {
  151. //选项卡切换
  152. click(item) {
  153. this.currentName = item.name;
  154. this.currentIndex = item.index;
  155. this.$emit('tabClick', item.name);
  156. },
  157. //快捷选择
  158. OptionsTabSelect(item, index) {
  159. switch (item) {
  160. case "本周":
  161. const WeekRange = this.getWeekRange();
  162. this.interviewStartTime = WeekRange.start;
  163. this.interviewEndTime = WeekRange.end;
  164. break;
  165. case "本月":
  166. const currentMonthRange = this.getCurrentMonthRange();
  167. this.interviewStartTime = currentMonthRange.start;
  168. this.interviewEndTime = currentMonthRange.end;
  169. break;
  170. case "本年":
  171. const currentYear = new Date().getFullYear();
  172. this.interviewStartTime = `${currentYear}-01-01`;
  173. this.interviewEndTime = `${currentYear}-12-31`;
  174. break;
  175. default:
  176. break;
  177. }
  178. this.OptionsTabIndex = index;
  179. },
  180. //获取本月
  181. getCurrentMonthRange() {
  182. const today = new Date();
  183. const firstDay = new Date(today.getFullYear(), today.getMonth(), 1); // 本月第一天
  184. const lastDay = new Date(today.getFullYear(), today.getMonth() + 1, 0); // 本月最后一天
  185. // 格式化为 YYYY-MM-DD
  186. const formatDate = (date) => {
  187. const year = date.getFullYear();
  188. const month = String(date.getMonth() + 1).padStart(2, '0');
  189. const day = String(date.getDate()).padStart(2, '0');
  190. return `${year}-${month}-${day}`;
  191. };
  192. return {
  193. start: formatDate(firstDay),
  194. end: formatDate(lastDay)
  195. };
  196. },
  197. //获取本周
  198. getWeekRange(date = new Date()) {
  199. // 获取当前日期是星期几(0-6,0 是周日)
  200. const dayOfWeek = date.getDay();
  201. // 计算本周的第一天(周一)
  202. const startOfWeek = new Date(date);
  203. startOfWeek.setDate(date.getDate() - dayOfWeek + (dayOfWeek === 0 ? -6 : 1)); // 如果周日,则减去6天
  204. // 计算本周的最后一天(周日)
  205. const endOfWeek = new Date(startOfWeek);
  206. endOfWeek.setDate(startOfWeek.getDate() + 6);
  207. // 格式化为 YYYY-MM-DD
  208. const formatDate = (date) => {
  209. const year = date.getFullYear();
  210. const month = String(date.getMonth() + 1).padStart(2, '0');
  211. const day = String(date.getDate()).padStart(2, '0');
  212. return `${year}-${month}-${day}`;
  213. };
  214. return {
  215. start: formatDate(startOfWeek),
  216. end: formatDate(endOfWeek)
  217. };
  218. },
  219. open() {
  220. this.$refs.popup.open()
  221. },
  222. bindChange: function(e) {
  223. const val = e.detail.value; //年月日当前选中下标数组
  224. let year = this.years[val[0]] //获取对应年份下标
  225. let isDay = 30,
  226. days = [];
  227. const currentMonth = parseInt(this.months[val[1]]);
  228. if (currentMonth == 2) {
  229. if ((val[0] % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
  230. isDay = 29;
  231. } else {
  232. isDay = 28;
  233. }
  234. } else if ([1, 3, 5, 7, 8, 10, 12].includes(currentMonth)) {
  235. isDay = 31;
  236. } else {
  237. isDay = 30;
  238. }
  239. for (let i = 1; i <= isDay; i++) {
  240. days.push(String(i).padStart(2, '0'))
  241. }
  242. this.days = days;
  243. this.year = this.years[val[0]]
  244. this.month = this.months[val[1]]
  245. this.day = this.days[val[2]];
  246. // this.monthTime = this.year + '-' + this.month;
  247. if (this.startShow) {
  248. this.interviewStartTime = this.year + '-' + this.month + '-' + this.day
  249. }
  250. if (this.endShow) {
  251. this.interviewEndTime = this.year + '-' + this.month + '-' + this.day
  252. }
  253. // 同步更新 value,保持 picker 位置一致
  254. this.value = val;
  255. },
  256. //月份滚动事件
  257. monthbindChange: function(e) {
  258. const val = e.detail.value; //年月日当前选中下标数组
  259. this.year1 = this.years[val[0]] //获取对应年份下标
  260. this.month1 = this.months[val[1]]
  261. this.monthTime = this.year1 + '-' + this.month1;
  262. },
  263. init() {
  264. const date = new Date();
  265. const years = []
  266. const year = date.getFullYear()
  267. const months = []
  268. const month = date.getMonth() + 1
  269. const days = []
  270. const day = date.getDate()
  271. let isDay = 30;
  272. if (month == 2) {
  273. if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
  274. isDay = 29;
  275. } else {
  276. isDay = 28;
  277. }
  278. } else if ([1, 3, 5, 7, 8, 10, 12].includes(month)) {
  279. isDay = 31;
  280. } else {
  281. isDay = 30;
  282. }
  283. for (let i = date.getFullYear(); i >= 1900; i--) {
  284. years.push(i)
  285. }
  286. for (let i = 1; i <= 12; i++) {
  287. months.push(String(i).padStart(2, '0'))
  288. }
  289. for (let i = 1; i <= isDay; i++) {
  290. days.push(String(i).padStart(2, '0'))
  291. }
  292. this.years = years
  293. this.year = year
  294. this.year1 = year
  295. this.months = months
  296. this.month = String(month).padStart(2, '0')
  297. this.month1 = String(month).padStart(2, '0')
  298. this.days = days
  299. this.day = String(day).padStart(2, '0')
  300. this.value = [0, month - 1, day - 1]
  301. this.monthvalue = [0, month - 1]
  302. this.interviewStartTime = this.year + '-' + this.month + '-' + this.day
  303. this.interviewEndTime = this.year + '-' + this.month + '-' + this.day
  304. this.monthTime = this.year1 + '-' + this.month1;
  305. },
  306. close() {
  307. this.$emit('close');
  308. },
  309. reset() {
  310. this.interviewStartTime = ''
  311. this.interviewEndTime = ''
  312. this.startShow = false
  313. this.endShow = false
  314. },
  315. startClick() {
  316. this.startShow = true
  317. this.endShow = false
  318. // 已有已选日期时,滚动到该日期;否则用当前 picker 位置
  319. if (this.interviewStartTime) {
  320. this.updatePickerPosition(this.interviewStartTime);
  321. } else {
  322. this.interviewStartTime = this.year + '-' + this.month + '-' + this.day;
  323. }
  324. },
  325. endClick() {
  326. this.startShow = false
  327. this.endShow = true
  328. // 已有已选日期时,滚动到该日期;否则用当前 picker 位置
  329. if (this.interviewEndTime) {
  330. this.updatePickerPosition(this.interviewEndTime);
  331. } else {
  332. this.interviewEndTime = this.year + '-' + this.month + '-' + this.day;
  333. }
  334. },
  335. // 根据日期字符串更新 picker 滚动位置
  336. updatePickerPosition(dateStr) {
  337. if (!dateStr) return;
  338. const [yearStr, monthStr, dayStr] = dateStr.split('-');
  339. const targetYear = parseInt(yearStr);
  340. const targetMonth = parseInt(monthStr);
  341. const targetDay = parseInt(dayStr);
  342. // 查找年份和月份对应的下标 (this.years 是倒序,所以需要计算下标)
  343. const yearIndex = this.years.findIndex(y => y === targetYear);
  344. const monthIndex = targetMonth - 1; // months 数组是 1-12,下标 0-11
  345. // 根据年月重新生成天数数组
  346. let isDay = 30;
  347. if (targetMonth === 2) {
  348. if ((targetYear % 4 === 0 && targetYear % 100 !== 0) || (targetYear % 400 === 0)) {
  349. isDay = 29;
  350. } else {
  351. isDay = 28;
  352. }
  353. } else if ([1, 3, 5, 7, 8, 10, 12].includes(targetMonth)) {
  354. isDay = 31;
  355. } else {
  356. isDay = 30;
  357. }
  358. const days = [];
  359. for (let i = 1; i <= isDay; i++) {
  360. days.push(String(i).padStart(2, '0'));
  361. }
  362. this.days = days;
  363. const dayIndex = targetDay - 1;
  364. // 更新 picker 位置
  365. this.value = [yearIndex, monthIndex, dayIndex];
  366. this.year = targetYear;
  367. this.month = String(targetMonth).padStart(2, '0');
  368. this.day = String(targetDay).padStart(2, '0');
  369. },
  370. confirm() {
  371. if (this.currentName == "自定义时间") {
  372. this.$emit('customTime', this.interviewStartTime, this.interviewEndTime);
  373. } else {
  374. this.$emit('customMonth', this.monthTime);
  375. }
  376. },
  377. },
  378. }
  379. </script>
  380. <style lang="scss" scoped>
  381. .popup {
  382. border-radius: 10px 10px 0 0 !important;
  383. }
  384. .popup-title {
  385. border-bottom: 1rpx solid #EEEEEE;
  386. margin-bottom: 40rpx;
  387. text:first-child {
  388. font-weight: 600;
  389. }
  390. text:last-child {
  391. position: absolute;
  392. right: 10px;
  393. font-size: 21px;
  394. color: #999999;
  395. line-height: 23px;
  396. }
  397. }
  398. .quickOptions {
  399. padding: 0 42rpx;
  400. box-sizing: border-box;
  401. margin-bottom: 20rpx;
  402. .tab {
  403. padding: 9rpx 24rpx;
  404. box-sizing: border-box;
  405. font-size: 30rpx;
  406. color: #666;
  407. border-radius: 4rpx 4rpx 4rpx 4rpx;
  408. border: 1rpx solid #EEEEEE;
  409. margin-right: 30rpx;
  410. }
  411. .tabactive {
  412. background: #FDE935;
  413. color: #333;
  414. border: 1rpx solid #FDE935;
  415. }
  416. }
  417. .popup-footer {
  418. padding: 20rpx 45rpx;
  419. box-sizing: border-box;
  420. view {
  421. font-size: 30rpx;
  422. padding: 20rpx;
  423. flex: 1;
  424. }
  425. view:first-child {
  426. color: #333;
  427. background: transparent;
  428. border-radius: 50rpx;
  429. border: 1rpx solid #FDE935;
  430. margin-right: 28rpx;
  431. }
  432. view:last-child {
  433. background: #FDE935;
  434. border-radius: 50rpx;
  435. color: #1F1F1F;
  436. }
  437. }
  438. .popup-data {
  439. text {
  440. font-size: 14px;
  441. color: #999999;
  442. display: inline-block;
  443. width: 44%;
  444. text-align: center;
  445. border: 1px solid #DDDDDD;
  446. border-radius: 4rpx;
  447. font-size: 30rpx;
  448. padding: 7rpx;
  449. box-sizing: border-box;
  450. }
  451. .active {
  452. border: 1px solid #FDE935;
  453. color: #333;
  454. }
  455. }
  456. .picker-view {
  457. width: 100%;
  458. height: 400rpx;
  459. .item {
  460. font-size: 28rpx;
  461. color: #333;
  462. }
  463. }
  464. </style>