uni-datePicker.vue 13 KB

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