index.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. <script setup lang="ts">
  2. // 1. 导入 Vue3 核心 API 和 uni-app 生命周期
  3. import { computed, nextTick, ref } from 'vue'
  4. import { onLoad } from '@dcloudio/uni-app'
  5. // 2. 导入原组件和模拟数据
  6. import { getArrayFieldMax, isWithin45Minutes } from '../utils'
  7. import canvasSeatmap from './components/pages/CanvasSeatmap.vue'
  8. import { StaticUrl } from '@/config'
  9. import router from '@/router'
  10. definePage({
  11. name: 'film-choose-seat',
  12. islogin: true,
  13. style: {
  14. navigationBarTitleText: '座次选择',
  15. backgroundColorBottom: '#fff',
  16. },
  17. })
  18. // ************************ 核心:定义所有缺失的接口(解决类型报错的关键) ************************
  19. // /**
  20. // * 影院信息接口
  21. // */
  22. // interface CinemaInfo {
  23. // cinema_name: string
  24. // [key: string]: any // 兼容其他未知属性,避免后续扩展报错
  25. // }
  26. /**
  27. * 区域列表项接口
  28. */
  29. interface AreaItem {
  30. area: string | number
  31. areaId: string | number
  32. market_price: number | string
  33. strokeStyle: string
  34. [key: string]: any // 兼容组件所需的其他未知属性
  35. }
  36. // /**
  37. // * 场次信息接口
  38. // */
  39. // interface ScheduleInfo {
  40. // schedule_id: string | number
  41. // hall_name: string
  42. // cinema: CinemaInfo | null
  43. // schedule_area: AreaItem[]
  44. // [key: string]: any // 兼容其他未知属性
  45. // }
  46. /**
  47. * 座位信息接口
  48. */
  49. interface SeatInfo {
  50. seatName: string
  51. marketPrice: number | string
  52. [key: string]: any // 兼容组件所需的其他未知属性
  53. }
  54. /**
  55. * 座位图配置项接口
  56. */
  57. interface SeatmapOptions {
  58. title: string
  59. hallName: string
  60. areaList: AreaItem[]
  61. footerHeight: number
  62. maxSelectNum?: number // 可选属性,对应注释的配置
  63. isolateSeats?: boolean // 可选属性,对应注释的配置
  64. [key: string]: any // 兼容组件所需的其他未知配置
  65. }
  66. /**
  67. * 座位点击回调事件接口
  68. */
  69. interface SeatTapEvent {
  70. chooseSeatList: SeatInfo[]
  71. }
  72. /**
  73. * 座位图组件实例接口(定义组件的公开方法,解决 ref 调用方法的类型报错)
  74. */
  75. interface CanvasSeatmapInstance {
  76. init: (params: { seatList: SeatInfo[], options: SeatmapOptions }) => void
  77. validateIsolates: () => boolean
  78. cancelSeat: (seat: SeatInfo) => void
  79. }
  80. const isLoging = ref(false)
  81. const loading = ref<boolean>(false)
  82. const seatInfo = ref<Api.filmMovieSeat>({})
  83. const areaList = ref<AreaItem[]>([])
  84. const seatList = ref<SeatInfo[]>([])
  85. const chooseSeatList = ref<SeatInfo[]>([])
  86. const options = ref<SeatmapOptions>({
  87. title: '',
  88. hallName: '',
  89. areaList: [],
  90. footerHeight: 0,
  91. })
  92. const areaArr = ref(['贵宾区', '普通区', '特价区1', '特价区2', '特价区3', '特价区4', '特价区5', '特价区6'])
  93. const { userInfo } = storeToRefs(useUserStore())
  94. const phone = ref('')
  95. const query = ref({
  96. memberId: unref(userInfo).id,
  97. channelId: unref(userInfo).channelId,
  98. shopId: '1',
  99. channelName: unref(userInfo).channelName,
  100. cinemaName: '',
  101. cinemaCode: '',
  102. movieCode: '',
  103. hallName: '',
  104. orderPayMode: '1',
  105. originPrice: 0,
  106. seatNames: '',
  107. sessionBeginTime: '',
  108. switchSeat: false,
  109. movieName: '',
  110. postImageUrl: '',
  111. planType: '',
  112. })
  113. const active = ref('')
  114. const showTime = ref(false)
  115. const screenList = ref()
  116. // ************************ 非响应式常量(类型自动推断,无需额外注解) ************************
  117. const colorList = [
  118. '#449DFE', // 浅黄色
  119. '#FFB639', // 浅蓝色
  120. '#FF4D3A', // 干枯玫瑰
  121. '#b6e67f', // 浅绿色
  122. '#ffb0bc', // 浅粉色
  123. ] as const // as const 锁定数组元素,禁止修改,类型更严格
  124. const totalPrice = computed(() => {
  125. let total = 0
  126. const scale = 10000
  127. if (!seatInfo.value.showTime)
  128. return 0
  129. const key = isWithin45Minutes(new Date(), seatInfo.value.showTime) ? 'fastPrice' : 'ticketPrice'
  130. const price = getArrayFieldMax(chooseSeatList.value, key) || 0
  131. total = (price * scale) * chooseSeatList.value.length / scale
  132. return total
  133. })
  134. // ************************ 组件 ref(添加明确实例类型,解决调用方法的类型报错) ************************
  135. const seatmapRef = ref<CanvasSeatmapInstance | null>(null)
  136. // ************************ 所有方法(添加参数/返回值类型注解,消除隐式 any) ************************
  137. // /**
  138. // * 金额转换(分转元,保留 2 位小数)
  139. // * @param total 分单位金额
  140. // * @returns 元单位金额(字符串,保留 2 位小数)
  141. // */
  142. // function priceConversion(total: number | string): string {
  143. // const numTotal = Number(total)
  144. // // 处理 NaN 情况,避免返回 NaN.toFixed(2) 报错
  145. // if (Number.isNaN(numTotal))
  146. // return '0.00'
  147. // return (numTotal / 100).toFixed(2)
  148. // }
  149. async function getScreenList(cinemaId: string, movieId: string) {
  150. const res = await Apis.film.getFilmMovieList({ data: { cinemaId, movieId } })
  151. res.data.data.movieShows?.forEach((i: any) => {
  152. if (i.shows.length) {
  153. i.shows.forEach((it: any, idx: number) => {
  154. if (active.value.includes(it.showDate)) {
  155. screenList.value = i.shows[idx].sessions
  156. }
  157. })
  158. }
  159. })
  160. console.log(2222222, screenList.value)
  161. }
  162. /**
  163. * 获取座位详情
  164. */
  165. async function getData(sessionId: string, movieId: string, cinemaId: string): Promise<void> {
  166. uni.showLoading({ title: '加载中...' })
  167. const res = await Apis.film.getFilmMovieSeat({ data: { sessionId, movieId, cinemaId } })
  168. uni.hideLoading()
  169. getScreenList(res.data.cinemaId, res.data.movieId)
  170. active.value = res.data.showTime
  171. seatInfo.value = res.data
  172. // seatInfo.value.seatSection = []
  173. query.value.hallName = res.data.hall
  174. query.value.cinemaCode = res.data.cinemaCode
  175. query.value.movieCode = res.data.movieCode
  176. query.value.sessionBeginTime = res.data.showTime
  177. query.value.cinemaName = res.data.cinemaName
  178. query.value.movieName = res.data.movieName
  179. query.value.postImageUrl = res.data.postImageUrl
  180. areaList.value = res.data.areaPriceList
  181. areaList.value.sort((a, b) => Number(b.originPrice) - Number(a.originPrice))
  182. areaList.value.forEach((item: AreaItem, index: number) => {
  183. item.strokeStyle = colorList[index % colorList.length] || '#ccc'
  184. item.areaName = areaArr.value[index]
  185. })
  186. for (let index = 0; index < res.data.seatSection.seatRows.length; index++) {
  187. const item = res.data.seatSection.seatRows[index]
  188. for (let i = 0; i < item.columns.length; i++) {
  189. const seat = item.columns[i]
  190. let price = 0
  191. let marketPrice = 0
  192. let fastPrice = 0
  193. for (let d = 0; d < res.data.areaPriceList.length; d++) {
  194. const el = res.data.areaPriceList[d]
  195. if (el.areaId === seat.areaId) {
  196. // 价格
  197. price = el.price
  198. marketPrice = el.originPrice
  199. fastPrice = el.fastPrice
  200. }
  201. }
  202. // 桌椅状态
  203. let status = 0
  204. if (seat.state === 0 || seat.state === 4) {
  205. status = -2
  206. }
  207. else if (seat.state === 1) {
  208. status = 1
  209. }
  210. else if (seat.state === 2) {
  211. status = 0
  212. }
  213. else if (seat.state === 3) {
  214. status = 10
  215. }
  216. else if (seat.state === 10) {
  217. status = 1
  218. }
  219. else if (seat.state === 11) {
  220. status = 1
  221. }
  222. // 座位类型
  223. let seatType = 0
  224. if (seat.seatType === 2) {
  225. seatType = 1
  226. }
  227. else if (seat.seatType === 3) {
  228. seatType = 3
  229. }
  230. seatList.value.push({
  231. status,
  232. seatType,
  233. seatName: seat.seatName,
  234. seatId: seat.originSeatID,
  235. columnId: seat.colNum,
  236. rowId: item.rowsNum,
  237. // columnId: i,
  238. // rowId: index,
  239. areaId: seat.areaId,
  240. marketPrice,
  241. ticketPrice: price,
  242. fastPrice,
  243. serviceFee: 0,
  244. })
  245. }
  246. }
  247. console.log(2222, areaList.value)
  248. // 赋值座位图配置项(严格符合 SeatmapOptions 接口)
  249. options.value = {
  250. title: '',
  251. hallName: seatInfo.value.hall as string,
  252. areaList: areaList.value,
  253. footerHeight: 470,
  254. // maxSelectNum: 4, // 可选属性,开启后无类型报错
  255. // isolateSeats: true, // 可选属性,开启后无类型报错
  256. }
  257. // 可选链访问,避免 cinema 为 null 时报错
  258. // const cinemaName = schedule.value.cinema?.cinema_name || '默认影院'
  259. // uni.setNavigationBarTitle({ title: cinemaName })
  260. }
  261. /**
  262. * 选定并购买座位
  263. */
  264. async function buySeat() {
  265. if (loading.value)
  266. return false
  267. if (chooseSeatList.value.length === 0) {
  268. return uni.showToast({
  269. title: '请选择座位',
  270. icon: 'none',
  271. duration: 2000,
  272. })
  273. }
  274. // 可选链调用,避免组件实例为 null 时报错
  275. if (seatmapRef.value?.validateIsolates()) {
  276. uni.showToast({
  277. title: '左右座位请不要留空!',
  278. icon: 'none',
  279. duration: 2000,
  280. })
  281. return false
  282. }
  283. query.value.seatNames = chooseSeatList.value.map(item => item.seatName).join(',')
  284. query.value.planType = seatInfo.value?.language as string + seatInfo.value?.planType
  285. uni.showToast({
  286. title: '开始锁座',
  287. icon: 'none',
  288. duration: 2000,
  289. })
  290. uni.setStorageSync('film-info', { showTime: active.value, endTime: seatInfo.value.endTime, cinemaName: seatInfo.value.cinemaName, chooseSeatList: chooseSeatList.value, language: seatInfo.value.language, planType: seatInfo.value.planType, totalPrice: totalPrice.value, phone: phone.value })
  291. router.push({ name: 'film-submit-order', params: { query: JSON.stringify(query.value) } })
  292. console.log(query.value)
  293. }
  294. /**
  295. * 取消选定座位
  296. * @param seat 要取消的座位信息
  297. */
  298. function cancelSeat(seat: SeatInfo): void {
  299. seatmapRef.value?.cancelSeat(seat)
  300. }
  301. /**
  302. * 座位图错误回调
  303. * @param e 错误事件对象
  304. */
  305. function onError(e: { message: string }): void {
  306. uni.showToast({
  307. title: e.message || '座位图加载失败',
  308. icon: 'none',
  309. duration: 2000,
  310. })
  311. }
  312. /**
  313. * 座位点击回调
  314. * @param event 座位点击事件对象
  315. */
  316. function onSeatTap(event: SeatTapEvent): void {
  317. chooseSeatList.value = event.chooseSeatList
  318. console.log('chooseSeatList', event)
  319. console.log('zuoshsjd', chooseSeatList.value)
  320. // 遍历已选座位列表,累加价格(严格类型访问)
  321. let price = 0
  322. for (const item of chooseSeatList.value) {
  323. if (item.marketPrice as number > price) {
  324. price = item.marketPrice as number
  325. }
  326. }
  327. query.value.originPrice = price
  328. }
  329. // ************************ 页面生命周期(严格类型,无报错) ************************
  330. onLoad(async (opt): Promise<void> => {
  331. phone.value = opt?.phone
  332. await getData(opt?.sessionId, opt?.movieId, opt?.cinemaId)
  333. // 等待 DOM 渲染完成后初始化座位图
  334. await nextTick()
  335. // 可选链 + 类型守卫,确保调用安全无报错
  336. if (seatmapRef.value) {
  337. seatmapRef.value.init({
  338. seatList: seatList.value,
  339. options: options.value,
  340. })
  341. }
  342. })
  343. function handleTime(time: string, sessionId: string) {
  344. if (time === active.value) {
  345. return
  346. }
  347. active.value = time
  348. router.push({ name: 'film-choose-seat', params: { sessionId, movieId: seatInfo.value.movieId as string, cinemaId: seatInfo.value.cinemaId } })
  349. }
  350. function handleSwitch() {
  351. console.log(11111)
  352. showTime.value = !showTime.value
  353. }
  354. // 获取时间
  355. function judgeNearDaysSimple(time: any) {
  356. const today = new Date(new Date().setHours(0, 0, 0, 0)).getTime()
  357. const target = new Date(new Date(time).setHours(0, 0, 0, 0)).getTime()
  358. const dayDiff = Math.floor((target - today) / 86400000)
  359. return { 0: '今天', 1: '明天', 2: '后天' }[dayDiff] || ''
  360. }
  361. </script>
  362. <template>
  363. <view class="view-page">
  364. <!-- 顶部价格区域 -->
  365. <view v-if="seatInfo.cinemaId" class="areaList">
  366. <view class="area-item">
  367. <view class="area" style="background-color:#AAAAAA" />
  368. <view class="area-price">
  369. 已售
  370. </view>
  371. </view>
  372. <view v-for="(item, index) in areaList" :key="index" class="area-item">
  373. <view class="area" :style="`border:1px solid ${item.strokeStyle}`" />
  374. <view class="area-price">
  375. {{ `${item.areaName} ${isWithin45Minutes(new Date(), seatInfo.showTime) ? item.fastPrice : item.price}` }}
  376. </view>
  377. </view>
  378. </view>
  379. <canvasSeatmap ref="seatmapRef" @error="onError" @on-seat-tap="onSeatTap" />
  380. <!-- 底部信息 -->
  381. <view v-if="seatInfo.cinemaId" class="footer">
  382. <view class="schedule bg-white">
  383. <view class="name-box">
  384. <view class="film-name">
  385. {{ seatInfo.movieName }}
  386. </view>
  387. <view class="change-box" @click="handleSwitch">
  388. <view>切换场次</view>
  389. <image class="arrow" :src="`${StaticUrl}/film-black-arrow.png`" mode="scaleToFill" />
  390. </view>
  391. </view>
  392. <view class="film-info">
  393. <text style="color:red">
  394. {{ judgeNearDaysSimple(seatInfo.showTime) || seatInfo.showTime?.split('T')[0] }}
  395. </text>
  396. {{ seatInfo.showTime?.split('T')[1] }} -
  397. {{ seatInfo.endTime?.split('T')[1] }}
  398. {{ seatInfo.planType }} {{ seatInfo.language }}
  399. </view>
  400. <!-- 切换场次 -->
  401. <view v-if="showTime" class="time-list">
  402. <view
  403. v-for="(item, index) in screenList" :key="index" class="time-item"
  404. :class="[active == item.showTime ? 'active' : '']" @click="handleTime(item.showTime, item.sessionId)"
  405. >
  406. <view class="time">
  407. {{ item.showTime.split('T')[1] }}
  408. </view>
  409. <view class="type">
  410. {{ item.language }} {{ item.planType }}
  411. </view>
  412. <view class="price">
  413. ¥{{ item.sellPrice }}起
  414. </view>
  415. </view>
  416. </view>
  417. <view v-if="chooseSeatList.length" class="seat-list">
  418. <view v-for="(seat, optindex) in chooseSeatList" :key="optindex" class="seat-item">
  419. <view class="seat">
  420. <view class="seat-name">
  421. {{ seat.seatName }}
  422. </view>
  423. <view class="seat-price">
  424. <!-- ¥{{ priceConversion(seat.marketPrice) }} -->
  425. ¥{{ isWithin45Minutes(new Date(), seatInfo.showTime) ? seat.fastPrice : seat.ticketPrice }}
  426. </view>
  427. </view>
  428. <view class="delete-seat" @click="cancelSeat(seat)">
  429. ×
  430. </view>
  431. </view>
  432. </view>
  433. </view>
  434. <view class="notice bg-white">
  435. 请尽快选座,座位可能被抢!
  436. </view>
  437. <wd-button
  438. custom-class="confirm" block :loading="isLoging" :class="{ unbtn: !chooseSeatList.length || loading }"
  439. @click="buySeat"
  440. >
  441. {{ chooseSeatList.length ? (`¥${totalPrice} 确认座位`) : '请选座位' }}
  442. </wd-button>
  443. </view>
  444. </view>
  445. </template>
  446. <style lang="scss" scoped>
  447. .view-page {
  448. background-color: #f7f9fc;
  449. }
  450. .bg-white {
  451. background-color: #fff;
  452. }
  453. /* 核心样式 */
  454. .areaList {
  455. display: flex;
  456. align-items: center;
  457. /* 上下居中 */
  458. /* 左右居中(可选) */
  459. height: 40px;
  460. white-space: nowrap;
  461. overflow-x: auto;
  462. scrollbar-width: none;
  463. /* Firefox */
  464. -ms-overflow-style: none;
  465. /* IE/Edge */
  466. }
  467. .areaList::-webkit-scrollbar {
  468. display: none;
  469. /* Chrome、Safari、微信小程序-web-view */
  470. }
  471. .area-item {
  472. display: inline-flex;
  473. align-items: center;
  474. justify-content: center;
  475. white-space: nowrap;
  476. margin: 0 8px;
  477. }
  478. .area {
  479. width: 20px;
  480. height: 20px;
  481. border-radius: 4px;
  482. }
  483. .area-price {
  484. margin-left: 10rpx;
  485. font-size: 20rpx;
  486. line-height: 20px;
  487. }
  488. .footer {
  489. position: fixed;
  490. left: 0;
  491. bottom: var(--window-bottom);
  492. padding: 0 15px 15px 15px;
  493. width: 100vw;
  494. box-sizing: border-box;
  495. border-radius: 5px 5px 0% 0%;
  496. z-index: 999999999;
  497. background: #FFFFFF;
  498. border-radius: 16rpx 16rpx 0rpx 0rpx;
  499. padding: 28rpx 24rpx;
  500. box-sizing: border-box;
  501. .notice{
  502. font-size: 24rpx;
  503. color: #FF4A39;
  504. margin-top: 24rpx;
  505. margin-bottom: 24rpx;
  506. }
  507. .schedule {
  508. .name-box {
  509. display: flex;
  510. justify-content: space-between;
  511. .film-name {
  512. font-weight: bold;
  513. font-size: 32rpx;
  514. color: #222222;
  515. }
  516. .change-box {
  517. display: flex;
  518. align-items: center;
  519. font-size: 24rpx;
  520. color: #222222;
  521. .arrow{
  522. width: 32rpx;
  523. height: 32rpx;
  524. transform: rotate(90deg);
  525. }
  526. }
  527. }
  528. .film-info {
  529. font-size: 24rpx;
  530. color: #AAAAAA;
  531. margin-top: 20rpx;
  532. }
  533. }
  534. }
  535. .time-list{
  536. display: flex;
  537. flex-wrap: nowrap;
  538. scrollbar-width: none;
  539. margin-top: 24rpx;
  540. overflow-x: auto;
  541. white-space: nowrap;
  542. /* Firefox */
  543. -ms-overflow-style: none;
  544. /* IE/Edge */
  545. .time-item{
  546. flex-shrink: 0;
  547. width: 160rpx;
  548. height: 114rpx;
  549. background: #F9F9F9;
  550. border-radius: 16rpx 16rpx 16rpx 16rpx;
  551. box-sizing: border-box;
  552. padding: 10rpx 0;
  553. text-align: center;
  554. margin-right: 20rpx;
  555. display: flex;
  556. flex-direction: column;
  557. align-items: center;
  558. justify-content: space-between;
  559. .time{
  560. font-size: 24rpx;
  561. color: #222222;
  562. }
  563. .type{
  564. font-size: 22rpx;
  565. color: #AAAAAA;
  566. }
  567. .price{
  568. font-size: 22rpx;
  569. color: #222222;
  570. }
  571. }
  572. .time-item.active {
  573. background: #FFE7E5;
  574. }
  575. }
  576. .seat-list {
  577. display: flex;
  578. align-items: center;
  579. justify-content: flex-start;
  580. overflow-x: auto;
  581. white-space: nowrap;
  582. margin-top: 5px;
  583. scrollbar-width: none;
  584. margin-top: 24rpx;
  585. /* Firefox */
  586. -ms-overflow-style: none;
  587. /* IE/Edge */
  588. }
  589. .seat-list::-webkit-scrollbar {
  590. display: none;
  591. /* Chrome、Safari、微信小程序-web-view */
  592. }
  593. .seat-item {
  594. display: flex;
  595. align-items: center;
  596. justify-content: center;
  597. height: 80rpx;
  598. background: #F9F9F9;
  599. border-radius: 16rpx 16rpx 16rpx 16rpx;
  600. box-sizing: border-box;
  601. padding: 10rpx 16rpx;
  602. margin-right: 20rpx;
  603. .seat-name {
  604. font-size: 24rpx;
  605. color: #222222;
  606. }
  607. .seat-price {
  608. font-size: 22rpx;
  609. color: #222222;
  610. margin-top: 10rpx;
  611. }
  612. .delete-seat {
  613. width: 20px;
  614. height: 20px;
  615. line-height: 20px;
  616. color: #727272;
  617. text-align: center;
  618. margin-left: 16rpx;
  619. }
  620. }
  621. .confirm {
  622. width: 100%;
  623. height: 80rpx;
  624. line-height: 80rpx;
  625. text-align: center;
  626. border-radius: 10px;
  627. font-size: 14px;
  628. color: #fff;
  629. background-color: #e54f4f;
  630. }
  631. .unbtn {
  632. background-color: #f69c9c;
  633. }
  634. </style>