DateTimePicker.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. <script setup lang="ts">
  2. import { ref, computed, watch } from 'vue';
  3. import { message } from 'ant-design-vue';
  4. import dayjs, { type Dayjs } from 'dayjs';
  5. defineOptions({
  6. name: 'DateTimePicker',
  7. });
  8. interface DateOption {
  9. label: string;
  10. date: Dayjs;
  11. dateStr: string;
  12. displayDate: string;
  13. isToday: boolean;
  14. }
  15. interface Props {
  16. visible: boolean;
  17. pickerType?: 'date' | 'time';
  18. initialDate?: string | null;
  19. initialTime?: string | null;
  20. isFromTimeButton?: boolean;
  21. }
  22. const props = withDefaults(defineProps<Props>(), {
  23. pickerType: 'date',
  24. initialDate: null,
  25. initialTime: null,
  26. isFromTimeButton: false,
  27. });
  28. const emit = defineEmits<{
  29. 'update:visible': [value: boolean];
  30. 'date-select': [date: Dayjs];
  31. 'time-select': [time: string, date: Dayjs];
  32. 'date-confirm': [date: Dayjs];
  33. 'close': [];
  34. }>();
  35. const internalPickerType = ref<'date' | 'time'>(props.pickerType);
  36. const selectedDate = ref<Dayjs>(dayjs());
  37. const selectedTime = ref<string>('');
  38. // 起始日期
  39. const startDate = ref<Dayjs>(dayjs());
  40. const dateUpdateKey = ref(0);
  41. const datePickerVisible = ref(false);
  42. const datePickerValue = ref<Dayjs | undefined>(undefined);
  43. // 初始化选中日期
  44. function initializeSelectedDate() {
  45. if (props.initialDate) {
  46. const initialDate = dayjs(props.initialDate);
  47. selectedDate.value = initialDate;
  48. startDate.value = initialDate;
  49. } else {
  50. const today = dayjs();
  51. selectedDate.value = today; // 默认今天
  52. startDate.value = today;
  53. }
  54. }
  55. // 初始化选中时间
  56. function initializeSelectedTime() {
  57. selectedTime.value = props.initialTime || '';
  58. }
  59. watch(() => props.visible, (newVal) => {
  60. if (newVal) {
  61. internalPickerType.value = props.pickerType;
  62. initializeSelectedDate();
  63. initializeSelectedTime();
  64. }
  65. });
  66. watch(() => props.initialDate, () => {
  67. if (props.visible) {
  68. initializeSelectedDate();
  69. }
  70. }, { immediate: true });
  71. watch(() => props.pickerType, (newVal) => {
  72. internalPickerType.value = newVal;
  73. });
  74. // 获取5天的日期列表
  75. const dateOptions = computed(() => {
  76. const dates: DateOption[] = [];
  77. const today = dayjs();
  78. const tomorrow = today.add(1, 'day');
  79. const weekdays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
  80. const start = dayjs(startDate.value);
  81. for (let i = 0; i < 5; i++) {
  82. const date = start.clone().add(i, 'day');
  83. const dayOfWeek = date.day();
  84. const isToday = date.isSame(today, 'day');
  85. const isTomorrow = date.isSame(tomorrow, 'day');
  86. let label = '';
  87. if (isToday) {
  88. label = '今天';
  89. } else if (isTomorrow) {
  90. label = '明天';
  91. } else {
  92. label = weekdays[dayOfWeek];
  93. }
  94. dates.push({
  95. label,
  96. date: date.clone(),
  97. dateStr: date.format('YYYY-MM-DD'),
  98. displayDate: date.format('M月D日'),
  99. isToday: isToday,
  100. });
  101. }
  102. return dates;
  103. });
  104. const timeOptions = computed(() => {
  105. const times: string[] = [];
  106. for (let hour = 8; hour <= 20; hour++) {
  107. times.push(`${hour.toString().padStart(2, '0')}:00`);
  108. if (hour < 20) {
  109. times.push(`${hour.toString().padStart(2, '0')}:30`);
  110. }
  111. }
  112. return times;
  113. });
  114. // 判断某个时间是否被禁用
  115. function isTimeDisabled(time: string): boolean {
  116. if (!selectedDate.value) return false;
  117. const now = dayjs();
  118. const selectedDateStr = selectedDate.value.format('YYYY-MM-DD');
  119. const todayStr = now.format('YYYY-MM-DD');
  120. // 如果选择的日期是今天,禁用当前时间之前的时间点
  121. if (selectedDateStr === todayStr) {
  122. const [hour, minute] = time.split(':').map(Number);
  123. const timeDate = now.hour(hour).minute(minute).second(0).millisecond(0);
  124. // 如果时间早于当前时间,禁用
  125. return timeDate.isBefore(now);
  126. }
  127. // 如果选择的日期是过去的日期,禁用
  128. if (selectedDate.value.isBefore(now, 'day')) {
  129. return true;
  130. }
  131. return false;
  132. }
  133. function handleDateSelect(dateItem: DateOption) {
  134. selectedDate.value = dateItem.date;
  135. const selectedDateStr = dateItem.dateStr;
  136. const currentStartStr = startDate.value.format('YYYY-MM-DD');
  137. const currentEnd = startDate.value.add(4, 'day');
  138. const currentEndStr = currentEnd.format('YYYY-MM-DD');
  139. if (selectedDateStr < currentStartStr || selectedDateStr > currentEndStr) {
  140. startDate.value = dateItem.date;
  141. dateUpdateKey.value += 1;
  142. }
  143. }
  144. // 打开日期选择弹窗
  145. function handleOpenDatePicker() {
  146. datePickerValue.value = selectedDate.value.clone();
  147. datePickerVisible.value = true;
  148. }
  149. // 日期选择
  150. function handleDatePickerChange(value: string | Dayjs | null) {
  151. if (value) {
  152. const date = dayjs.isDayjs(value) ? value : dayjs(value);
  153. datePickerValue.value = date;
  154. } else {
  155. datePickerValue.value = undefined;
  156. }
  157. }
  158. // 确认日期选择
  159. function handleConfirmDatePicker() {
  160. if (!datePickerValue.value) {
  161. message.warning('请先选择日期');
  162. return;
  163. }
  164. // 获取选中的日期值
  165. const selectedDateValue = datePickerValue.value;
  166. let dateStr: string;
  167. if (dayjs.isDayjs(selectedDateValue)) {
  168. dateStr = selectedDateValue.format('YYYY-MM-DD');
  169. } else if (typeof selectedDateValue === 'string') {
  170. dateStr = selectedDateValue;
  171. } else {
  172. dateStr = dayjs(selectedDateValue).format('YYYY-MM-DD');
  173. }
  174. // 创建新的日期对象
  175. const newDate = dayjs(dateStr);
  176. startDate.value = newDate.clone();
  177. selectedDate.value = newDate.clone();
  178. dateUpdateKey.value += 1;
  179. datePickerVisible.value = false;
  180. emit('date-select', newDate.clone());
  181. if (props.isFromTimeButton) {
  182. internalPickerType.value = 'time';
  183. }
  184. }
  185. // 关闭日期选择弹窗
  186. function handleCloseDatePicker() {
  187. datePickerVisible.value = false;
  188. }
  189. // 处理时间选择
  190. function handleTimeSelect(time: string) {
  191. if (isTimeDisabled(time)) {
  192. return;
  193. }
  194. selectedTime.value = time;
  195. }
  196. // 确认时间选择
  197. function handleConfirmTimeSelect() {
  198. if (!selectedDate.value) {
  199. message.warning('请先选择日期');
  200. return;
  201. }
  202. if (!selectedTime.value) {
  203. message.warning('请先选择时间');
  204. return;
  205. }
  206. emit('date-select', selectedDate.value.clone());
  207. emit('time-select', selectedTime.value, selectedDate.value);
  208. handleClose();
  209. }
  210. // 关闭弹窗
  211. function handleClose() {
  212. emit('update:visible', false);
  213. emit('close');
  214. }
  215. </script>
  216. <template>
  217. <a-modal
  218. :open="visible"
  219. title="请选择开始服务时间"
  220. :width="internalPickerType === 'date' ? 600 : 800"
  221. @cancel="handleClose"
  222. @ok="handleConfirmTimeSelect"
  223. class="date-time-picker-modal">
  224. <div class="date-time-picker-content">
  225. <!-- 时间选择区域 -->
  226. <div class="time-selection-section">
  227. <div class="date-header">
  228. <div class="date-options-header" :key="`${startDate.format('YYYY-MM-DD')}-${dateUpdateKey}`">
  229. <div v-for="dateItem in dateOptions" :key="dateItem.dateStr" class="date-option-header"
  230. :class="{ active: selectedDate.format('YYYY-MM-DD') === dateItem.dateStr }"
  231. @click="handleDateSelect(dateItem)">
  232. <div class="date-label">{{ dateItem.label }}</div>
  233. <div class="date-value">{{ dateItem.displayDate }}</div>
  234. </div>
  235. </div>
  236. <div class="calendar-icon" @click="handleOpenDatePicker">📅</div>
  237. </div>
  238. <div class="time-grid">
  239. <template v-if="timeOptions && timeOptions.length > 0">
  240. <div v-for="time in timeOptions" :key="time" class="time-slot"
  241. :class="{
  242. active: selectedTime === time,
  243. disabled: isTimeDisabled(time)
  244. }"
  245. @click="handleTimeSelect(time)">
  246. {{ time }}
  247. </div>
  248. </template>
  249. <div v-else class="time-grid-empty">暂无时间选项</div>
  250. </div>
  251. </div>
  252. </div>
  253. </a-modal>
  254. <!-- 日期选择弹窗 -->
  255. <a-modal
  256. :open="datePickerVisible"
  257. title="请选择日期"
  258. :width="400"
  259. @cancel="handleCloseDatePicker"
  260. @ok="handleConfirmDatePicker"
  261. class="date-picker-modal">
  262. <div class="date-picker-content">
  263. <a-date-picker
  264. v-model:value="datePickerValue"
  265. format="YYYY-MM-DD"
  266. placeholder="请选择日期"
  267. style="width: 100%"
  268. @change="handleDatePickerChange" />
  269. </div>
  270. </a-modal>
  271. </template>
  272. <style scoped lang="scss">
  273. .date-time-picker-content {
  274. .date-selection-section {
  275. .date-options {
  276. display: flex;
  277. gap: 12px;
  278. margin-bottom: 24px;
  279. justify-content: center;
  280. .date-option {
  281. flex: 1;
  282. padding: 16px 12px;
  283. border: 1px solid #e8e8e8;
  284. border-radius: 4px;
  285. text-align: center;
  286. cursor: pointer;
  287. transition: all 0.3s;
  288. background: #fff;
  289. .date-label {
  290. font-size: 14px;
  291. color: #333;
  292. margin-bottom: 4px;
  293. font-weight: 500;
  294. }
  295. .date-value {
  296. font-size: 13px;
  297. color: #666;
  298. }
  299. &:hover {
  300. border-color: #8bc34a;
  301. background: #f9fff9;
  302. }
  303. &.active {
  304. background: #8bc34a;
  305. border-color: #8bc34a;
  306. .date-label,
  307. .date-value {
  308. color: #fff;
  309. }
  310. }
  311. }
  312. }
  313. .picker-actions {
  314. display: flex;
  315. justify-content: flex-end;
  316. gap: 12px;
  317. padding-top: 16px;
  318. border-top: 1px solid #f0f0f0;
  319. }
  320. }
  321. .time-selection-section {
  322. .date-header {
  323. display: flex;
  324. justify-content: space-between;
  325. align-items: center;
  326. margin-bottom: 20px;
  327. padding-bottom: 16px;
  328. border-bottom: 1px solid #f0f0f0;
  329. .date-options-header {
  330. display: flex;
  331. gap: 12px;
  332. flex: 1;
  333. .date-option-header {
  334. flex: 1;
  335. padding: 12px 8px;
  336. border: 1px solid #e8e8e8;
  337. border-radius: 4px;
  338. text-align: center;
  339. cursor: pointer;
  340. transition: all 0.3s;
  341. background: #fff;
  342. .date-label {
  343. font-size: 13px;
  344. color: #333;
  345. margin-bottom: 2px;
  346. font-weight: 500;
  347. }
  348. .date-value {
  349. font-size: 12px;
  350. color: #666;
  351. }
  352. &:hover {
  353. border-color: #8bc34a;
  354. background: #f9fff9;
  355. }
  356. &.active {
  357. background: #8bc34a;
  358. border-color: #8bc34a;
  359. .date-label,
  360. .date-value {
  361. color: #fff;
  362. }
  363. }
  364. }
  365. }
  366. .calendar-icon {
  367. font-size: 20px;
  368. margin-left: 16px;
  369. cursor: pointer;
  370. transition: transform 0.2s;
  371. &:hover {
  372. transform: scale(1.1);
  373. }
  374. }
  375. }
  376. .time-grid {
  377. display: grid;
  378. grid-template-columns: repeat(5, 1fr);
  379. gap: 8px;
  380. max-height: 400px;
  381. overflow-y: auto;
  382. padding-right: 8px;
  383. min-height: 200px;
  384. &::-webkit-scrollbar {
  385. width: 6px;
  386. }
  387. &::-webkit-scrollbar-thumb {
  388. background: #d9d9d9;
  389. border-radius: 3px;
  390. }
  391. &::-webkit-scrollbar-thumb:hover {
  392. background: #bfbfbf;
  393. }
  394. .time-slot {
  395. padding: 12px 8px;
  396. border: 1px solid #e8e8e8;
  397. border-radius: 4px;
  398. text-align: center;
  399. font-size: 14px;
  400. color: #333;
  401. cursor: pointer;
  402. transition: all 0.3s;
  403. background: #fff;
  404. min-height: 40px;
  405. display: flex;
  406. align-items: center;
  407. justify-content: center;
  408. box-sizing: border-box;
  409. &:hover:not(.disabled) {
  410. border-color: #8bc34a;
  411. background: #f9fff9;
  412. }
  413. &.active {
  414. background: #8bc34a;
  415. border-color: #8bc34a;
  416. color: #fff;
  417. }
  418. &.disabled {
  419. opacity: 0.4;
  420. cursor: not-allowed;
  421. background: #f5f5f5;
  422. color: #999;
  423. border-color: #e8e8e8;
  424. &:hover {
  425. border-color: #e8e8e8;
  426. background: #f5f5f5;
  427. }
  428. }
  429. }
  430. .time-grid-empty {
  431. grid-column: 1 / -1;
  432. text-align: center;
  433. padding: 40px;
  434. color: #999;
  435. }
  436. }
  437. }
  438. }
  439. </style>
  440. <style lang="scss">
  441. .date-time-picker-modal {
  442. .ant-modal-header {
  443. padding: 16px 24px;
  444. border-bottom: 1px solid #f0f0f0;
  445. .ant-modal-title {
  446. font-size: 16px;
  447. font-weight: 500;
  448. text-align: center;
  449. position: relative;
  450. }
  451. }
  452. .ant-modal-body {
  453. padding: 24px;
  454. }
  455. .date-time-picker-content {
  456. .time-selection-section {
  457. .time-grid {
  458. display: grid !important;
  459. grid-template-columns: repeat(5, 1fr) !important;
  460. gap: 8px !important;
  461. max-height: 400px;
  462. overflow-y: auto;
  463. padding-right: 8px;
  464. min-height: 200px;
  465. &::-webkit-scrollbar {
  466. width: 6px;
  467. }
  468. &::-webkit-scrollbar-thumb {
  469. background: #d9d9d9;
  470. border-radius: 3px;
  471. }
  472. &::-webkit-scrollbar-thumb:hover {
  473. background: #bfbfbf;
  474. }
  475. .time-slot {
  476. padding: 12px 8px !important;
  477. border: 1px solid #e8e8e8 !important;
  478. border-radius: 4px !important;
  479. text-align: center !important;
  480. font-size: 14px !important;
  481. color: #333 !important;
  482. cursor: pointer !important;
  483. transition: all 0.3s;
  484. background: #fff !important;
  485. min-height: 40px;
  486. display: flex !important;
  487. align-items: center !important;
  488. justify-content: center !important;
  489. box-sizing: border-box;
  490. &:hover:not(.disabled) {
  491. // border-color: #8bc34a !important;
  492. // background: #f9fff9 !important;
  493. }
  494. &.active {
  495. background: #8bc34a !important;
  496. border-color: #8bc34a !important;
  497. color: #fff !important;
  498. }
  499. &.disabled {
  500. opacity: 0.4 !important;
  501. cursor: not-allowed !important;
  502. background: #f5f5f5 !important;
  503. color: #999 !important;
  504. border-color: #e8e8e8 !important;
  505. &:hover {
  506. border-color: #e8e8e8 !important;
  507. background: #f5f5f5 !important;
  508. }
  509. }
  510. }
  511. .time-grid-empty {
  512. grid-column: 1 / -1;
  513. text-align: center;
  514. padding: 40px;
  515. color: #999;
  516. }
  517. }
  518. }
  519. }
  520. }
  521. .date-picker-content {
  522. padding: 20px 0;
  523. }
  524. </style>
  525. <style lang="scss">
  526. .date-picker-modal {
  527. .ant-modal-body {
  528. padding: 24px;
  529. }
  530. }
  531. </style>