cart.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. <template>
  2. <view class="hotel-customer-theme cart-page">
  3. <!-- 暂停接单提示横幅 -->
  4. <view v-if="isPaused" class="c-pause-banner">
  5. <text class="c-pause-icon">⏸️</text>
  6. <text class="c-pause-text">餐厅已暂停接单,暂时无法结算</text>
  7. </view>
  8. <!-- 送至信息 -->
  9. <view class="c-delivery-info">
  10. <text>🏨 送至:{{ roomNo }} {{ roomTypeName }}</text>
  11. </view>
  12. <!-- 购物车列表 -->
  13. <view v-if="cartItems.length === 0" class="c-empty">
  14. <text class="c-empty-icon">🛒</text>
  15. <text class="c-empty-text">购物车是空的</text>
  16. <view class="c-empty-btn" @click="goToMenu">去点餐</view>
  17. </view>
  18. <view v-else class="c-list">
  19. <view v-for="(item, index) in cartItems" :key="index" class="c-item">
  20. <view class="c-item-img">
  21. <image
  22. v-if="showImage(item)"
  23. :src="getImageUrl(item)"
  24. mode="aspectFill"
  25. class="c-item-img-real"
  26. @error="onImageError(item)"
  27. />
  28. <text v-else class="c-item-img-placeholder">🍽️</text>
  29. </view>
  30. <view class="c-item-info">
  31. <view class="c-item-name">{{ item.dishName }}</view>
  32. <view class="c-item-spec" v-if="item.specs?.length || item.additions?.length">
  33. {{ formatSpecs(item) }}
  34. </view>
  35. <view class="c-item-row">
  36. <view class="c-qty-ctrl">
  37. <view class="c-qty-btn" @click="onMinus(index)">−</view>
  38. <text class="c-qty-num">{{ item.quantity }}</text>
  39. <view class="c-qty-btn c-qty-btn--add" @click="onPlus(index)">+</view>
  40. </view>
  41. <view class="c-item-price">¥{{ (item.unitPrice * item.quantity).toFixed(2) }}</view>
  42. </view>
  43. </view>
  44. </view>
  45. <!-- 备注 -->
  46. <view class="c-note-section">
  47. <text class="c-note-label">订单备注</text>
  48. <textarea
  49. class="c-note-input"
  50. v-model="orderNote"
  51. placeholder="特殊要求、忌口等..."
  52. :maxlength="200"
  53. />
  54. </view>
  55. <!-- 费用汇总 -->
  56. <view class="c-summary">
  57. <view class="c-summary-row">
  58. <text>菜品小计</text>
  59. <text>¥{{ cartTotal }}</text>
  60. </view>
  61. <view class="c-summary-row">
  62. <text>配送费</text>
  63. <text :style="{ color: deliveryFee > 0 ? 'var(--h-txt)' : 'var(--h-succ)' }">
  64. {{ deliveryFee > 0 ? '¥' + deliveryFee : '免配送费' }}
  65. </text>
  66. </view>
  67. <view class="c-summary-row c-summary-total">
  68. <text>合计</text>
  69. <text>¥{{ grandTotal }}</text>
  70. </view>
  71. </view>
  72. <!-- 结算按钮 -->
  73. <view class="c-checkout-btn" :class="{ 'c-checkout-btn--disabled': isPaused }" @click="goCheckout">{{ isPaused ? '餐厅已暂停接单' : '去结算 ¥' + grandTotal }}</view>
  74. </view>
  75. <!-- 底部 TabBar -->
  76. <HotelTabBar current="cart" />
  77. </view>
  78. </template>
  79. <script setup>
  80. import { computed, onMounted, ref } from 'vue'
  81. import { useHotelOrderStore } from '@/store'
  82. import HotelTabBar from '@/components/hotel/HotelTabBar.vue'
  83. import api from '@/api'
  84. import { getFileDownloadUrl } from '@/utils/file'
  85. const orderStore = useHotelOrderStore()
  86. // 加载失败的菜品图片,回退到占位图标
  87. const failedImages = ref(new Set())
  88. // 暂停接单状态
  89. const isPaused = ref(false)
  90. const roomNo = computed(() => orderStore.roomInfo?.roomNo || '')
  91. const roomTypeName = computed(() => orderStore.roomInfo?.roomTypeName || '')
  92. const cartItems = computed(() => orderStore.cartItems)
  93. const cartTotal = computed(() => orderStore.cartTotal.toFixed(2))
  94. const deliveryFee = computed(() => orderStore.deliveryFee)
  95. const grandTotal = computed(() => (orderStore.cartTotal + deliveryFee.value).toFixed(2))
  96. const orderNote = computed({
  97. get: () => orderStore.orderNote,
  98. set: v => orderStore.setOrderNote(v),
  99. })
  100. /**
  101. * 购物车里存的 image 是 fileId,不是可直接访问的地址,
  102. * 必须经 getFileDownloadUrl 转成 /api/file/download/{fileId},否则图片加载不出来。
  103. */
  104. function getImageUrl(item) {
  105. if (!item.image) return ''
  106. return getFileDownloadUrl(item.image)
  107. }
  108. /** 图片是否可用(有值且未加载失败) */
  109. function showImage(item) {
  110. return !!item.image && !failedImages.value.has(String(item.image))
  111. }
  112. /** 图片加载失败,标记后展示占位图标 */
  113. function onImageError(item) {
  114. failedImages.value.add(String(item.image))
  115. }
  116. function formatSpecs(item) {
  117. const parts = []
  118. if (item.specs?.length) parts.push(item.specs.map(s => s.optionName).join('·'))
  119. if (item.additions?.length) parts.push(item.additions.map(a => '+' + a.name).join('·'))
  120. return parts.join(' / ')
  121. }
  122. function onMinus(index) {
  123. const item = cartItems.value[index]
  124. orderStore.updateItemQuantity(index, item.quantity - 1)
  125. }
  126. function onPlus(index) {
  127. const item = cartItems.value[index]
  128. orderStore.updateItemQuantity(index, item.quantity + 1)
  129. }
  130. function goToMenu() {
  131. uni.redirectTo({ url: '/pages/hotel/customer/menu' })
  132. }
  133. function goCheckout() {
  134. if (cartItems.value.length === 0) {
  135. uni.showToast({ title: '购物车是空的', icon: 'none' })
  136. return
  137. }
  138. if (isPaused.value) {
  139. uni.showToast({ title: '餐厅已暂停接单,无法结算', icon: 'none' })
  140. return
  141. }
  142. uni.navigateTo({ url: '/pages/hotel/customer/order-confirm' })
  143. }
  144. onMounted(() => {
  145. loadPauseStatus()
  146. })
  147. /** 加载暂停接单状态 */
  148. async function loadPauseStatus() {
  149. if (!orderStore.tenantId) return
  150. try {
  151. var res = await api.hotelPauseStatus(orderStore.tenantId)
  152. isPaused.value = res?.data ?? res ?? false
  153. } catch (e) {
  154. console.log('暂停状态查询失败:', e?.message || e)
  155. isPaused.value = false
  156. }
  157. }
  158. </script>
  159. <style lang="scss" scoped>
  160. .cart-page {
  161. min-height: 100vh;
  162. background: var(--h-bg, #F7F7F7);
  163. padding: 24rpx 28rpx;
  164. padding-bottom: calc(140rpx + env(safe-area-inset-bottom));
  165. }
  166. .c-delivery-info {
  167. display: flex;
  168. align-items: center;
  169. gap: 8rpx;
  170. font-size: 24rpx;
  171. color: var(--h-txt-sub, #666);
  172. margin-bottom: 24rpx;
  173. }
  174. /* 空状态 */
  175. .c-empty {
  176. display: flex;
  177. flex-direction: column;
  178. align-items: center;
  179. padding: 160rpx 0;
  180. }
  181. .c-empty-icon {
  182. font-size: 96rpx;
  183. margin-bottom: 24rpx;
  184. }
  185. .c-empty-text {
  186. font-size: 28rpx;
  187. color: var(--h-txt-light, #999);
  188. margin-bottom: 32rpx;
  189. }
  190. .c-empty-btn {
  191. padding: 20rpx 64rpx;
  192. border-radius: 48rpx;
  193. background: var(--h-pri, #2D5016);
  194. color: #fff;
  195. font-size: 28rpx;
  196. font-weight: 600;
  197. }
  198. /* 购物车列表 */
  199. .c-list {
  200. display: flex;
  201. flex-direction: column;
  202. }
  203. .c-item {
  204. display: flex;
  205. gap: 20rpx;
  206. background: #fff;
  207. border-radius: var(--h-rad, 20rpx);
  208. padding: 24rpx;
  209. margin-bottom: 16rpx;
  210. box-shadow: var(--h-shd, 0 2rpx 8rpx rgba(0, 0, 0, 0.04));
  211. }
  212. .c-item-img {
  213. width: 140rpx;
  214. height: 140rpx;
  215. border-radius: var(--h-rad-sm, 12rpx);
  216. flex-shrink: 0;
  217. background: var(--h-bg, #F7F7F7);
  218. display: flex;
  219. align-items: center;
  220. justify-content: center;
  221. overflow: hidden;
  222. }
  223. .c-item-img-real {
  224. width: 100%;
  225. height: 100%;
  226. }
  227. .c-item-img-placeholder {
  228. font-size: 48rpx;
  229. opacity: 0.3;
  230. }
  231. .c-item-info {
  232. flex: 1;
  233. display: flex;
  234. flex-direction: column;
  235. justify-content: space-between;
  236. min-width: 0;
  237. }
  238. .c-item-name {
  239. font-size: 28rpx;
  240. font-weight: 600;
  241. color: var(--h-txt, #1A1A1A);
  242. }
  243. .c-item-spec {
  244. font-size: 22rpx;
  245. color: var(--h-txt-light, #999);
  246. margin-top: 4rpx;
  247. }
  248. .c-item-row {
  249. display: flex;
  250. align-items: center;
  251. justify-content: space-between;
  252. margin-top: 8rpx;
  253. }
  254. .c-qty-ctrl {
  255. display: flex;
  256. align-items: center;
  257. border-radius: 40rpx;
  258. border: 1rpx solid var(--h-bdr, #eee);
  259. overflow: hidden;
  260. }
  261. .c-qty-btn {
  262. width: 56rpx;
  263. height: 56rpx;
  264. display: flex;
  265. align-items: center;
  266. justify-content: center;
  267. font-size: 32rpx;
  268. color: var(--h-txt-sub, #666);
  269. background: var(--h-bg, #F7F7F7);
  270. }
  271. .c-qty-btn--add {
  272. color: var(--h-pri, #2D5016);
  273. font-weight: 700;
  274. }
  275. .c-qty-num {
  276. width: 64rpx;
  277. text-align: center;
  278. font-size: 28rpx;
  279. font-weight: 600;
  280. color: var(--h-txt, #1A1A1A);
  281. border-left: 1rpx solid var(--h-bdr, #eee);
  282. border-right: 1rpx solid var(--h-bdr, #eee);
  283. line-height: 56rpx;
  284. }
  285. .c-item-price {
  286. font-size: 32rpx;
  287. font-weight: 700;
  288. color: var(--h-warn, #D97706);
  289. }
  290. /* 备注 */
  291. .c-note-section {
  292. background: #fff;
  293. border-radius: var(--h-rad, 20rpx);
  294. padding: 24rpx;
  295. margin-top: 8rpx;
  296. margin-bottom: 16rpx;
  297. box-shadow: var(--h-shd, 0 2rpx 8rpx rgba(0, 0, 0, 0.04));
  298. }
  299. .c-note-label {
  300. font-size: 24rpx;
  301. font-weight: 600;
  302. color: var(--h-txt-sub, #666);
  303. margin-bottom: 12rpx;
  304. display: block;
  305. }
  306. .c-note-input {
  307. display: block;
  308. width: 100%;
  309. height: 140rpx;
  310. /* H5 无全局 border-box 重置,width:100% + padding + border 会把备注框撑出卡片右侧 */
  311. box-sizing: border-box;
  312. padding: 16rpx 20rpx;
  313. border: 1rpx solid var(--h-bdr, #eee);
  314. border-radius: 12rpx;
  315. font-size: 26rpx;
  316. line-height: 40rpx;
  317. resize: none;
  318. overflow: hidden;
  319. background: var(--h-bg, #F7F7F7);
  320. color: var(--h-txt, #1A1A1A);
  321. /* uni-app H5 的 textarea 会渲染成 uni-textarea > wrapper > textarea,
  322. 内层需要撑满外框,否则可输入区域和灰底高度不一致,文字看着被截断 */
  323. :deep(.uni-textarea-wrapper),
  324. :deep(.uni-textarea-textarea) {
  325. width: 100%;
  326. height: 100%;
  327. box-sizing: border-box;
  328. font-size: 26rpx;
  329. line-height: 40rpx;
  330. border: none;
  331. background: transparent;
  332. resize: none;
  333. }
  334. :deep(.uni-textarea-placeholder) {
  335. font-size: 26rpx;
  336. line-height: 40rpx;
  337. color: var(--h-txt-light, #999);
  338. }
  339. }
  340. /* 费用汇总 */
  341. .c-summary {
  342. background: #fff;
  343. border-radius: var(--h-rad, 20rpx);
  344. padding: 24rpx;
  345. margin-bottom: 24rpx;
  346. box-shadow: var(--h-shd, 0 2rpx 8rpx rgba(0, 0, 0, 0.04));
  347. }
  348. .c-summary-row {
  349. display: flex;
  350. justify-content: space-between;
  351. padding: 10rpx 0;
  352. font-size: 26rpx;
  353. color: var(--h-txt-sub, #666);
  354. }
  355. .c-summary-total {
  356. border-top: 1rpx solid var(--h-bdr, #eee);
  357. margin-top: 12rpx;
  358. padding-top: 16rpx;
  359. font-size: 32rpx;
  360. font-weight: 700;
  361. color: var(--h-pri, #2D5016);
  362. }
  363. /* 结算按钮 */
  364. .c-checkout-btn {
  365. width: 100%;
  366. box-sizing: border-box;
  367. padding: 28rpx;
  368. border-radius: 48rpx;
  369. background: var(--h-pri, #2D5016);
  370. color: #fff;
  371. font-size: 30rpx;
  372. font-weight: 700;
  373. text-align: center;
  374. box-shadow: 0 8rpx 24rpx rgba(45, 80, 22, 0.3);
  375. }
  376. .c-checkout-btn:active {
  377. opacity: 0.85;
  378. }
  379. .c-checkout-btn--disabled {
  380. opacity: 0.5;
  381. background: var(--h-txt-sub, #999);
  382. }
  383. /* 暂停接单横幅 */
  384. .c-pause-banner {
  385. display: flex;
  386. align-items: center;
  387. justify-content: center;
  388. gap: 8rpx;
  389. padding: 20rpx 28rpx;
  390. background: #FEF3C7;
  391. color: #92400E;
  392. font-size: 26rpx;
  393. font-weight: 600;
  394. }
  395. .c-pause-icon {
  396. font-size: 28rpx;
  397. }
  398. </style>