图书馆小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

704 lines
19 KiB

1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
  1. <template>
  2. <view class="booking-detail">
  3. <view class="section">
  4. <view class="calendar-wrap">
  5. <view class="calendar-top">
  6. <text class="calendar-title">选择入馆日期</text>
  7. <text class="calendar-month">{{ displayYear }}.{{ String(displayMonth).padStart(2,'0') }}</text>
  8. </view>
  9. <view class="calendar-days-grid">
  10. <view
  11. v-for="(day, index) in calendarDays"
  12. :key="index"
  13. class="day-card"
  14. :class="{
  15. 'card-selected': day.date === selectedDate,
  16. 'card-available': day.available && !day.tempClose && !day.userHasBooked,
  17. 'card-disabled': !day.available || day.tempClose,
  18. 'card-today': day.isToday,
  19. 'card-user-booked': day.userHasBooked,
  20. 'card-temp-close': day.tempClose
  21. }"
  22. @click="selectDate(day)"
  23. >
  24. <!-- 今天蓝色角标 -->
  25. <view v-if="day.isToday" class="corner-block-today"></view>
  26. <!-- 用户已预约 橙色角标 -->
  27. <view v-if="day.userHasBooked" class="corner-block-booked"></view>
  28. <text class="card-week">{{ day.isToday ? '今天' : day.weekText }}</text>
  29. <text class="card-num">{{ day.day }}</text>
  30. <text class="card-status">
  31. {{ day.tempClose ? '闭馆' : (day.userHasBooked ? '已预约' : (day.available ? '可预约' : '')) }}
  32. </text>
  33. </view>
  34. </view>
  35. </view>
  36. </view>
  37. <!-- 时间选择闭馆时整体禁用 -->
  38. <view class="section" :class="{disabled: isSelectCloseDay}">
  39. <view class="section-title">时间选择</view>
  40. <view class="time-slots">
  41. <view
  42. v-for="slot in timeSlots"
  43. :key="slot.id"
  44. class="time-item"
  45. :class="{
  46. 'selected': selectedTimeList.includes(slot.time),
  47. disabled: isSelectCloseDay,
  48. 'booked-item': slot.isUserBooked
  49. }"
  50. @click="selectTime(slot)"
  51. >
  52. <text class="time-text">{{ slot.time }}</text>
  53. <text v-if="slot.isUserBooked" class="booked-tag">已预约</text>
  54. <uni-icons
  55. v-if="selectedTimeList.includes(slot.time) && !slot.isUserBooked"
  56. style="position: absolute; right: 5px; top: 50%; transform: translateY(-50%);"
  57. custom-prefix="iconfont"
  58. type="icon-xuanze"
  59. size="20"
  60. color="#01a4fe"
  61. ></uni-icons>
  62. </view>
  63. </view>
  64. <view v-if="isSelectCloseDay" class="disable-tip">该日期闭馆不可选择时间段</view>
  65. </view>
  66. <!-- 闭馆公告板块仅选中闭馆日期显示 -->
  67. <view v-if="isSelectCloseDay" class="section notice-section">
  68. <view class="notice-title">闭馆公告</view>
  69. <view class="notice-content">{{ closeNoticeText }}</view>
  70. </view>
  71. <view class="section operate-desc">
  72. <view class="section-title" style="font-size: 14px;">预约说明</view>
  73. <view class="desc-item">
  74. <text class="color-blue">1<text>蓝色</text>日期 可预约</text>
  75. </view>
  76. <view class="desc-item">
  77. <text class="color-orange">2<text>橙色</text>日期 已有预约</text>
  78. </view>
  79. <view class="desc-item">
  80. <text class="color-gray">3<text>灰色</text>日期 超出可选范围不可点击预约</text>
  81. </view>
  82. <view class="desc-item">
  83. <text class="color-close">4<text>闭馆</text>日期 可查看公告但无法预约或入馆</text>
  84. </view>
  85. <view class="desc-item">
  86. <text>5已预约时间段仅可查看不可取消可新增其他时间段</text>
  87. </view>
  88. </view>
  89. <view class="bottom-bar">
  90. <button class="btn-primary" :disabled="isSelectCloseDay" @click="confirmBooking">确认预约</button>
  91. </view>
  92. </view>
  93. </template>
  94. <script>
  95. import { FetchInitLibraryReservationSettings,FetchReserve } from '@/api/reservation';
  96. import { getOpenId } from '@/utils/storage';
  97. import config from '@/utils/config';
  98. export default {
  99. data() {
  100. return {
  101. selectedDate: '',
  102. selectedTimeList: [], // 多选时间段数组
  103. calendarDays: [],
  104. timeSlots: [],
  105. reserveConfig: null,
  106. // 馆员临时闭馆日期列表,格式'2026-08-20',后台接口返回
  107. tempCloseDateList: [],
  108. // 闭馆公告映射 key:日期 value:公告内容
  109. tempCloseNoticeMap: {},
  110. // 用户已经预约过的日期列表,后台接口返回
  111. userBookedDateList: [],
  112. // key:日期字符串,value:已预约时间段,后端返回
  113. userBookedMap:{}
  114. };
  115. },
  116. computed: {
  117. // 是否选中闭馆日期
  118. isSelectCloseDay(){
  119. if(!this.selectedDate) return false
  120. return this.tempCloseDateList.includes(this.selectedDate)
  121. },
  122. // 获取当前闭馆公告文本
  123. closeNoticeText(){
  124. return this.tempCloseNoticeMap[this.selectedDate] || "本日场馆闭馆,暂不支持预约。"
  125. },
  126. displayYear() {
  127. let targetDateStr = this.selectedDate;
  128. if (!targetDateStr && this.calendarDays.length > 0) {
  129. const firstValid = this.calendarDays.find(d => d.available && !d.tempClose) || this.calendarDays[0];
  130. targetDateStr = firstValid.date;
  131. }
  132. if (!targetDateStr) return new Date().getFullYear();
  133. const d = new Date(targetDateStr);
  134. return d.getFullYear();
  135. },
  136. displayMonth() {
  137. let targetDateStr = this.selectedDate;
  138. if (!targetDateStr && this.calendarDays.length > 0) {
  139. const firstValid = this.calendarDays.find(d => d.available && !d.tempClose) || this.calendarDays[0];
  140. targetDateStr = firstValid.date;
  141. }
  142. if (!targetDateStr) return new Date().getMonth() + 1;
  143. const d = new Date(targetDateStr);
  144. return d.getMonth() + 1;
  145. }
  146. },
  147. async onLoad(options) {
  148. await this.initReservationSettings();
  149. if(this.reserveConfig){
  150. this.generateCalendar();
  151. }
  152. },
  153. methods: {
  154. transformReserveConfig(apiData){
  155. const { baseSetting, weekJson } = apiData;
  156. const weekKeyMap = {
  157. 1: 'monday',
  158. 2: 'tuesday',
  159. 3: 'wednesday',
  160. 4: 'thursday',
  161. 5: 'friday',
  162. 6: 'saturday',
  163. 7: 'sunday'
  164. };
  165. const weekDays = [];
  166. for(let wId=1;wId<=7;wId++){
  167. const propName = weekKeyMap[wId];
  168. const checked = !!baseSetting[propName];
  169. const timeArr = weekJson[wId] || [];
  170. // 把startTime[h,m], endTime[h,m]转 "HH:mm - HH:mm"
  171. const times = timeArr.map(item=>{
  172. const sh = String(item.startTime[0]).padStart(2,'0');
  173. const sm = String(item.startTime[1]).padStart(2,'0');
  174. const eh = String(item.endTime[0]).padStart(2,'0');
  175. const em = String(item.endTime[1]).padStart(2,'0');
  176. return `${sh}:${sm} - ${eh}:${em}`;
  177. })
  178. weekDays.push({
  179. id:wId,
  180. name:this.getWeekText(wId),
  181. checked: checked,
  182. times: times,
  183. showPicker: false,
  184. newTime: ""
  185. })
  186. }
  187. return {
  188. frequency: String(baseSetting.advanceBookingDays),
  189. weekDays
  190. }
  191. },
  192. // 初始化预约设置
  193. async initReservationSettings() {
  194. try {
  195. const res = await FetchInitLibraryReservationSettings({
  196. libcode: config.LIB_CODE
  197. })
  198. if (res && res.code === 200 && res.data) {
  199. // 转换为旧格式,原有业务逻辑完全不用改动
  200. this.reserveConfig = this.transformReserveConfig(res.data);
  201. }else{
  202. console.error('初始化预约设置失败',res)
  203. }
  204. } catch (e) {
  205. console.error('获取预约设置异常', e)
  206. }
  207. },
  208. getWeekNum(dateStr) {
  209. const d = new Date(dateStr);
  210. const day = d.getDay();
  211. return day === 0 ? 7 : day
  212. },
  213. getWeekText(weekNo){
  214. const map = {1:'周一',2:'周二',3:'周三',4:'周四',5:'周五',6:'周六',7:'周日'}
  215. return map[weekNo]
  216. },
  217. generateCalendar() {
  218. if(!this.reserveConfig) return;
  219. const today = new Date();
  220. const todayStr = `${today.getFullYear()}-${String(today.getMonth() + 1).padStart(2, '0')}-${String(today.getDate()).padStart(2, '0')}`;
  221. const freq = Number(this.reserveConfig.frequency);
  222. const allowDateList = [];
  223. for(let i = 0; i < freq; i++){
  224. const temp = new Date(today);
  225. temp.setDate(today.getDate() + i);
  226. const ds = `${temp.getFullYear()}-${String(temp.getMonth()+1).padStart(2,'0')}-${String(temp.getDate()).padStart(2,'0')}`
  227. allowDateList.push(ds)
  228. }
  229. const list = []
  230. allowDateList.forEach(dateStr=>{
  231. const weekNo = this.getWeekNum(dateStr)
  232. const weekCfg = this.reserveConfig.weekDays.find(w => w.id === weekNo)
  233. const available = !!weekCfg?.checked
  234. const d = new Date(dateStr)
  235. const tempClose = this.tempCloseDateList.includes(dateStr)
  236. const userHasBooked = this.userBookedDateList.includes(dateStr)
  237. list.push({
  238. day: d.getDate(),
  239. date: dateStr,
  240. weekText: this.getWeekText(weekNo),
  241. available: available,
  242. isToday: dateStr === todayStr,
  243. tempClose: tempClose,
  244. userHasBooked: userHasBooked
  245. })
  246. })
  247. this.calendarDays = list
  248. // 找第一个不是闭馆的有效日期作为初始选中
  249. const firstAvail = this.calendarDays.find(d => d.available && !d.tempClose);
  250. if(firstAvail){
  251. this.selectedDate = firstAvail.date
  252. this.refreshTimeSlots(this.selectedDate)
  253. }else{
  254. this.selectedDate = ''
  255. this.timeSlots = []
  256. }
  257. },
  258. refreshTimeSlots(dateStr){
  259. if(!dateStr) {
  260. this.timeSlots = []
  261. return
  262. }
  263. const weekNo = this.getWeekNum(dateStr)
  264. const weekCfg = this.reserveConfig.weekDays.find(w => w.id === weekNo)
  265. if(!weekCfg || !weekCfg.checked){
  266. this.timeSlots = []
  267. return
  268. }
  269. // 当前日期已经预约的时间段
  270. const bookedTimeArr = this.userBookedMap[dateStr] || []
  271. this.timeSlots = weekCfg.times.map((t, idx)=>{
  272. return {
  273. id: idx + 1,
  274. time: t,
  275. available: true,
  276. isUserBooked: bookedTimeArr.includes(t)
  277. }
  278. })
  279. // 回填历史已预约时间段,不能取消
  280. this.selectedTimeList = [...bookedTimeArr]
  281. },
  282. selectDate(day) {
  283. // 普通不可选(非临时闭馆):弹提示,禁止选中
  284. if (!day.available && !day.tempClose) {
  285. uni.showToast({
  286. title: '超出可选的时间范围,请选择其他日期',
  287. icon: 'none'
  288. })
  289. return;
  290. }
  291. // 临时闭馆 || 正常可预约:允许选中
  292. this.selectedDate = day.date;
  293. this.refreshTimeSlots(day.date);
  294. },
  295. selectTime(slot) {
  296. if(this.isSelectCloseDay) return
  297. if (!slot.available) return;
  298. // 已预约时间段禁止取消
  299. if(slot.isUserBooked){
  300. uni.showToast({
  301. title:"该时间段已预约,不可取消",
  302. icon:"none"
  303. })
  304. return
  305. }
  306. const idx = this.selectedTimeList.indexOf(slot.time)
  307. if(idx > -1){
  308. // 本次新增的,可以取消
  309. this.selectedTimeList.splice(idx,1)
  310. }else{
  311. this.selectedTimeList.push(slot.time)
  312. }
  313. },
  314. async confirmBooking() {
  315. if(this.isSelectCloseDay){
  316. uni.showToast({title:"该日期闭馆,无法预约",icon:"none"})
  317. return
  318. }
  319. if (!this.selectedDate) {
  320. uni.showToast({ title: '请选择日期', icon: 'none' });
  321. return;
  322. }
  323. const bookedTimeArr = this.userBookedMap[this.selectedDate] || []
  324. // 过滤:只取本次新增勾选的时间段
  325. const newSelectTimes = this.selectedTimeList.filter(item=> !bookedTimeArr.includes(item))
  326. if (!newSelectTimes || newSelectTimes.length === 0) {
  327. uni.showToast({ title: '请选择需要新增预约的时间段', icon: 'none' });
  328. return;
  329. }
  330. uni.showLoading({ title: '预约中...' });
  331. const openId = await getOpenId();
  332. if (!openId) {
  333. uni.hideLoading();
  334. uni.showToast({ title: '获取用户openid失败', icon: 'none' });
  335. return;
  336. }
  337. // 组装数组参数,后端接收对象数组
  338. const param = newSelectTimes.map(timeStr => {
  339. const [startPart, endPart] = timeStr.split(' - ');
  340. const startTime = startPart.trim() + ':00';
  341. const endTime = endPart.trim() + ':00';
  342. return {
  343. libcode: config.LIB_CODE,
  344. openid: openId,
  345. specificDate: this.selectedDate,
  346. startTime: startTime,
  347. endTime: endTime
  348. }
  349. })
  350. try {
  351. const res = await FetchReserve(param);
  352. uni.hideLoading();
  353. if(res.code === 200){
  354. // 更新本地预约状态
  355. if(!this.userBookedDateList.includes(this.selectedDate)){
  356. this.userBookedDateList.push(this.selectedDate)
  357. }
  358. const old = this.userBookedMap[this.selectedDate] || []
  359. this.userBookedMap[this.selectedDate] = [...new Set([...old,...newSelectTimes])]
  360. const timeText = newSelectTimes.join('\n');
  361. uni.showModal({
  362. title: '预约成功',
  363. content: `您已新增预约\n日期:${this.selectedDate}\n时间段:${timeText}`,
  364. showCancel: false,
  365. confirmText: '确定',
  366. success: () => {
  367. uni.navigateBack({ delta: 2 });
  368. }
  369. })
  370. }else{
  371. uni.showToast({ title: res.msg || '预约失败,请重试', icon: 'none' })
  372. }
  373. } catch (err) {
  374. uni.hideLoading();
  375. console.error('预约接口异常', err);
  376. uni.showToast({ title: '预约失败,请重试', icon: 'none' })
  377. }
  378. }
  379. }
  380. };
  381. </script>
  382. <style lang="scss" scoped>
  383. .booking-detail {
  384. min-height: 100vh;
  385. background-color: #f5f5f5;
  386. padding-bottom: 70px;
  387. }
  388. .section {
  389. background-color: #fff;
  390. margin: 10px;
  391. border-radius: 12px;
  392. padding: 16px;
  393. }
  394. // 闭馆公告板块
  395. .notice-section{
  396. background:#FCECE9;
  397. .notice-title{
  398. font-size:15px;
  399. font-weight:bold;
  400. color:#FD7359;
  401. margin-bottom:8px;
  402. }
  403. .notice-content{
  404. font-size:14px;
  405. color:#595959;
  406. line-height:1.6;
  407. }
  408. }
  409. // 时间板块整体置灰
  410. .section.disabled{
  411. opacity:0.55;
  412. pointer-events:none;
  413. }
  414. .disable-tip{
  415. margin-top:12px;
  416. font-size:13px;
  417. color:#999;
  418. text-align:center;
  419. }
  420. .calendar-wrap{
  421. background:#ffffff;
  422. border-radius:16px;
  423. }
  424. .calendar-top{
  425. display:flex;
  426. justify-content:space-between;
  427. align-items:center;
  428. margin-bottom:12px;
  429. }
  430. .calendar-title{
  431. font-size:17px;
  432. font-weight:bold;
  433. color:#222;
  434. }
  435. .calendar-month{
  436. font-size:16px;
  437. color:#333;
  438. font-weight:bold;
  439. }
  440. .calendar-days-grid{
  441. display:flex;
  442. flex-wrap:wrap;
  443. gap:8px;
  444. }
  445. .day-card{
  446. width: calc((100% - 48px) / 7);
  447. background:#f7f8fa;
  448. border-radius: 4px;
  449. padding: 4px 0;
  450. display:flex;
  451. flex-direction:column;
  452. align-items:center;
  453. justify-content:flex-start;
  454. position:relative;
  455. overflow: hidden;
  456. }
  457. /* 今天左上角蓝色三角 */
  458. .corner-block-today{
  459. position:absolute;
  460. top:0;
  461. left:0;
  462. width: 0;
  463. height: 0;
  464. border-top: 12px solid #01a4fe;
  465. border-right: 12px solid transparent;
  466. }
  467. /* 已预约 橙色三角角标 */
  468. .corner-block-booked{
  469. position:absolute;
  470. top:0;
  471. left:0;
  472. width: 0;
  473. height: 0;
  474. border-top: 12px solid #ff7800;
  475. border-right: 12px solid transparent;
  476. }
  477. .card-week{
  478. font-size:12px;
  479. color:#666;
  480. margin-bottom:4px;
  481. }
  482. .card-num{
  483. font-size:18px;
  484. font-weight:bold;
  485. color:#333;
  486. }
  487. .card-status{
  488. font-size:11px;
  489. margin-top:4px;
  490. }
  491. /* 可预约 */
  492. .day-card.card-available{
  493. background:#e8f4fd;
  494. }
  495. .day-card.card-available .card-status{
  496. color:#01a4fe;
  497. }
  498. /* 闭馆/不可选 */
  499. .day-card.card-disabled{
  500. background:#f7f8fa;
  501. }
  502. .day-card.card-disabled .card-week,
  503. .day-card.card-disabled .card-num,
  504. .day-card.card-disabled .card-status{
  505. color:#999;
  506. }
  507. /* 选中状态 */
  508. .day-card.card-selected{
  509. background:#01a4fe;
  510. }
  511. .day-card.card-selected .card-week,
  512. .day-card.card-selected .card-num,
  513. .day-card.card-selected .card-status{
  514. color:#ffffff;
  515. }
  516. .day-card.card-user-booked{
  517. background:#FFF3E5;
  518. }
  519. .day-card.card-user-booked .card-week,
  520. .day-card.card-user-booked .card-num,
  521. .day-card.card-user-booked .card-status{
  522. color:#ff7800;
  523. }
  524. .day-card.card-user-booked.card-selected{
  525. background:#ff7800;
  526. }
  527. .day-card.card-user-booked.card-selected .card-week,
  528. .day-card.card-user-booked.card-selected .card-num,
  529. .day-card.card-user-booked.card-selected .card-status{
  530. color:#fff;
  531. }
  532. .day-card.card-temp-close .card-status{
  533. color:#999;
  534. }
  535. .day-card.card-temp-close.card-selected {
  536. background:#888;
  537. }
  538. .day-card.card-temp-close.card-selected .card-week,
  539. .day-card.card-temp-close.card-selected .card-num,
  540. .day-card.card-temp-close.card-selected .card-status{
  541. color:#fff;
  542. }
  543. /* 时间选择 */
  544. .section-title {
  545. font-size: 16px;
  546. font-weight: bold;
  547. color: #333;
  548. margin-bottom: 16px;
  549. }
  550. .time-slots {
  551. display: flex;
  552. flex-wrap: wrap;
  553. gap: 10px;
  554. }
  555. .time-item {
  556. position: relative;
  557. width: calc(50% - 5px);
  558. display: flex;
  559. align-items: center;
  560. justify-content: space-between;
  561. padding: 12px;
  562. background-color: #f8f8f8;
  563. border-radius: 8px;
  564. border: 1px solid transparent;
  565. }
  566. .time-item.selected {
  567. background-color: #e8f4fd;
  568. border-color: #01a4fe;
  569. }
  570. .time-item.disabled{
  571. pointer-events:none;
  572. }
  573. .time-item.booked-item{
  574. background: #fff3e5;
  575. border:1px solid #ffb870;
  576. .time-text{
  577. color:#ff7800;
  578. }
  579. }
  580. .booked-tag{
  581. font-size:11px;
  582. color:#ff7800;
  583. }
  584. .time-text {
  585. font-size: 14px;
  586. color: #333;
  587. }
  588. .available-badge {
  589. font-size: 12px;
  590. color: #01a4fe;
  591. background-color: #e8f4fd;
  592. padding: 2px 8px;
  593. border-radius: 10px;
  594. }
  595. .unavailable-badge {
  596. font-size: 12px;
  597. color: #999;
  598. background-color: #f0f0f0;
  599. padding: 2px 8px;
  600. border-radius: 10px;
  601. }
  602. /* 底部按钮 */
  603. .bottom-bar {
  604. position: fixed;
  605. bottom: 0;
  606. left: 0;
  607. right: 0;
  608. padding: 10px;
  609. background-color: #fff;
  610. box-shadow: 0 -1px 5px rgba(0, 0, 0, 0.1);
  611. }
  612. .btn-primary {
  613. width: 100%;
  614. height: 44px;
  615. line-height: 44px;
  616. background-color: #01a4fe;
  617. color: #fff;
  618. border-radius: 22px;
  619. font-size: 15px;
  620. border: none;
  621. }
  622. .btn-primary::after {
  623. border: none;
  624. }
  625. .btn-primary[disabled]{
  626. background:#b8d8f5 !important;
  627. }
  628. .operate-desc{
  629. margin:10px;
  630. padding:16px;
  631. border-radius:12px;
  632. background:#fff;
  633. .section-title{
  634. margin-bottom:10px !important;
  635. }
  636. .desc-item{
  637. line-height:2;
  638. font-size:13px;
  639. }
  640. .color-blue{
  641. text{
  642. color:#01a4fe;
  643. }
  644. }
  645. .color-orange{
  646. text{
  647. color:#ff7800;
  648. }
  649. }
  650. .color-gray{
  651. text{
  652. color:#999;
  653. }
  654. }
  655. .color-close{
  656. text{
  657. color:#FD7359;
  658. font-weight:500;
  659. }
  660. }
  661. }
  662. </style>