index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. <template>
  2. <!-- 下单页面 -->
  3. <view class="content">
  4. <!-- 订单卡片 -->
  5. <view class="card">
  6. <view class="card-header">
  7. <view class='tag'></view>
  8. <view class="card-header-title">订单信息</view>
  9. </view>
  10. <view class="card-content">
  11. <view class="info-item">
  12. <view class="info-item-label">就餐桌号</view>
  13. <view class="info-item-value">{{ orderInfo.tableNumber || orderInfo.tableId || '—' }}</view>
  14. </view>
  15. <view class="info-item">
  16. <view class="info-item-label">用餐人数</view>
  17. <view class="info-item-value">{{ orderInfo.diners || '—' }}人</view>
  18. </view>
  19. <view class="info-item">
  20. <view class="info-item-label">联系电话</view>
  21. <view class="info-item-value">{{ orderInfo.contactPhone || '—' }}</view>
  22. </view>
  23. <view class="info-item">
  24. <view class="info-item-label">备注信息</view>
  25. </view>
  26. <textarea
  27. v-model="orderInfo.remark"
  28. placeholder="请输入特殊需求,如少辣、不要香菜等(最多30字)"
  29. class="info-item-textarea"
  30. maxlength="30"
  31. ></textarea>
  32. </view>
  33. </view>
  34. <!-- 菜品清单 -->
  35. <view class="card">
  36. <view class="card-header">
  37. <view class='tag'></view>
  38. <view class="card-header-title">菜品清单</view>
  39. </view>
  40. <view class="card-content">
  41. <view class="info-food">
  42. <view v-for="(item, index) in foodList" :key="item.id || item.cuisineId || index" class="food-item">
  43. <!-- 菜品图片:兼容 cuisineImage/image,逗号分隔取首图 -->
  44. <image :src="getItemImage(item)" mode="aspectFill" class="food-item__image"></image>
  45. <!-- 菜品信息 -->
  46. <view class="food-item__info">
  47. <view class="food-item__name">{{ item.name || item.cuisineName }}</view>
  48. <view class="food-item__desc" v-if="item.tags && item.tags.length > 0">
  49. <text v-for="(tag, tagIndex) in item.tags" :key="tagIndex" class="food-item__tag"
  50. :class="tag.type">{{ tag.text }}<text v-if="tagIndex < item.tags.length - 1">,</text>
  51. </text>
  52. </view>
  53. </view>
  54. <!-- 价格和数量 -->
  55. <view class="food-item__right">
  56. <view class="food-item__price">
  57. <text class="price-main">¥{{ formatPrice(getItemPrice(item)) }}</text>
  58. </view>
  59. <view class="food-item__quantity">{{ item.quantity || 1 }}份</view>
  60. </view>
  61. </view>
  62. </view>
  63. </view>
  64. </view>
  65. <!-- 总价 -->
  66. <view class="card">
  67. <view class="card-header">
  68. <view class='tag'></view>
  69. <view class="card-header-title">价格明细</view>
  70. </view>
  71. <view class="card-content">
  72. <view class="info-item">
  73. <view class="info-item-label">菜品总价</view>
  74. <view class="info-item-value">¥{{ formatPrice(dishTotal) }}</view>
  75. </view>
  76. <view class="info-item">
  77. <view class="info-item-label">餐具费</view>
  78. <view class="info-item-value">¥{{ formatPrice(orderInfo.utensilFee ?? 0) }}</view>
  79. </view>
  80. <view class="price-line">
  81. <view class="price-line-label">应付金额</view>
  82. <view class="price-line-value">¥{{ formatPrice(orderInfo.payAmount ?? orderInfo.totalAmount) }}</view>
  83. </view>
  84. </view>
  85. </view>
  86. <!-- 底部按钮:确认下单(去结算请走 pages/checkout/index) -->
  87. <view class="bottom-button">
  88. <view class="bottom-button-text" hover-class="hover-active" @click="handleConfirmOrder">确认下单</view>
  89. </view>
  90. </view>
  91. </template>
  92. <script setup>
  93. import { onLoad, onShow, onUnload } from "@dcloudio/uni-app";
  94. import { ref, computed } from "vue";
  95. import { go } from "@/utils/utils.js";
  96. import { getFileUrl } from "@/utils/file.js";
  97. import { useUserStore } from "@/store/user.js";
  98. import * as diningApi from "@/api/dining.js";
  99. // 订单信息(从购物车带过来或 URL 参数)
  100. const orderInfo = ref({
  101. orderNo: '',
  102. tableId: '',
  103. tableNumber: '',
  104. diners: '',
  105. contactPhone: '',
  106. remark: '',
  107. totalAmount: 0,
  108. dishTotal: null,
  109. utensilFee: 0,
  110. discountAmount: 0,
  111. payAmount: 0,
  112. couponId: null
  113. });
  114. // 菜品列表(从购物车带过来)
  115. const foodList = ref([]);
  116. // 菜品清单展示:排除餐具(id/cuisineId === -1)
  117. const displayFoodList = computed(() => {
  118. return (foodList.value ?? []).filter((it) => Number(it?.id ?? it?.cuisineId) !== -1);
  119. });
  120. // 菜品总价(不含餐具费):仅按菜品清单中非餐具项汇总,不包含餐具费
  121. const dishTotal = computed(() => {
  122. const list = displayFoodList.value ?? [];
  123. const sum = list.reduce((s, it) => {
  124. const qty = Number(it?.quantity) || 0;
  125. const price = Number(it?.unitPrice ?? it?.price ?? it?.totalPrice ?? it?.salePrice ?? 0) || 0;
  126. return s + qty * price;
  127. }, 0);
  128. return Math.max(0, sum);
  129. });
  130. // 菜品图片:餐具(id/cuisineId=-1)固定用本地 /static/utensilFee.png,其他取接口图
  131. function getItemImage(item) {
  132. const id = item?.id ?? item?.cuisineId;
  133. if (Number(id) === -1) return '/static/utensilFee.png';
  134. const raw = item?.cuisineImage ?? item?.image ?? item?.imageUrl ?? '';
  135. const url = typeof raw === 'string' ? raw.split(',')[0].trim() : raw;
  136. return url ? getFileUrl(url) : '';
  137. }
  138. // 单品单价:列表展示用 unitPrice/price
  139. function getItemPrice(item) {
  140. const p = item?.unitPrice ?? item?.price ?? item?.totalPrice ?? item?.salePrice ?? 0;
  141. return Number(p) || 0;
  142. }
  143. function formatPrice(price) {
  144. const num = Number(price);
  145. return Number.isNaN(num) ? '0.00' : num.toFixed(2);
  146. }
  147. // 根据菜品总价(不含餐具)、餐具费计算应付金额(优惠券在结算页选择,确认订单页不选券)
  148. const updatePayAmount = () => {
  149. const dish = dishTotal.value;
  150. const utensil = Number(orderInfo.value.utensilFee) || 0;
  151. orderInfo.value.payAmount = Math.max(0, dish + utensil);
  152. };
  153. // 将 tags 统一为 [{ text, type }] 格式
  154. function normalizeTags(raw) {
  155. if (raw == null) return [];
  156. let arr = [];
  157. if (Array.isArray(raw)) arr = raw;
  158. else if (typeof raw === 'string') {
  159. const t = raw.trim();
  160. if (t.startsWith('[')) { try { arr = JSON.parse(t); if (!Array.isArray(arr)) arr = []; } catch { arr = t ? [t] : []; } }
  161. else arr = t ? t.split(/[,,、\s]+/).map(s => s.trim()).filter(Boolean) : [];
  162. } else if (raw && typeof raw === 'object') arr = Array.isArray(raw.list) ? raw.list : Array.isArray(raw.items) ? raw.items : [];
  163. return arr.map((it) => {
  164. if (it == null) return { text: '', type: '' };
  165. if (typeof it === 'string') return { text: it, type: '' };
  166. if (typeof it === 'number') return { text: String(it), type: '' };
  167. return { text: it.text ?? it.tagName ?? it.name ?? it.label ?? it.title ?? '', type: it.type ?? it.tagType ?? '' };
  168. }).filter((t) => t.text !== '' && t.text != null);
  169. }
  170. // 将存储的购物车项转为确认页展示格式(兼容 cuisineName/cuisineImage/unitPrice/tags 等)
  171. function normalizeCartItem(item) {
  172. const image = item?.cuisineImage ?? item?.image ?? item?.imageUrl ?? '';
  173. const url = typeof image === 'string' ? image.split(',')[0].trim() : image;
  174. const rawTags = item?.tags ?? item?.tagList ?? item?.tagNames ?? item?.labels ?? item?.tag;
  175. return {
  176. id: item?.id ?? item?.cuisineId,
  177. name: item?.name ?? item?.cuisineName,
  178. cuisineName: item?.cuisineName,
  179. price: item?.unitPrice ?? item?.price,
  180. image: url,
  181. cuisineImage: item?.cuisineImage,
  182. quantity: Number(item?.quantity) || 0,
  183. tags: normalizeTags(rawTags),
  184. subtotalAmount: item?.subtotalAmount,
  185. remark: item?.remark
  186. };
  187. }
  188. const handleConfirmOrder = async () => {
  189. const tableId = orderInfo.value.tableId;
  190. const contactPhone = orderInfo.value.contactPhone ?? '';
  191. const dinerCount = Number(orderInfo.value.diners) || 0;
  192. if (!tableId) {
  193. uni.showToast({ title: '请先选择桌号', icon: 'none' });
  194. return;
  195. }
  196. if (!contactPhone) {
  197. uni.showToast({ title: '请先填写联系电话', icon: 'none' });
  198. return;
  199. }
  200. const remark = (orderInfo.value.remark ?? '').trim().slice(0, 30);
  201. uni.showLoading({ title: '提交中...' });
  202. try {
  203. const tablewareFee = Number(orderInfo.value.utensilFee) || 0;
  204. const payAmount = Number((Number(orderInfo.value.payAmount) ?? 0).toFixed(2));
  205. const totalAmount = Number((Number(dishTotal.value) ?? 0).toFixed(2));
  206. const createParams = {
  207. tableId,
  208. contactPhone,
  209. totalAmount,
  210. tablewareFee,
  211. payAmount,
  212. dinerCount: dinerCount || undefined,
  213. immediatePay: 0,
  214. remark: remark || undefined
  215. };
  216. const res = await diningApi.PostOrderCreate(createParams);
  217. uni.hideLoading();
  218. const orderId = res?.id ?? res?.orderId ?? res?.data?.id ?? res?.data?.orderId;
  219. const orderNo = res?.orderNo ?? res?.data?.orderNo ?? orderId ?? '';
  220. const total = orderInfo.value.payAmount ?? orderInfo.value.totalAmount ?? 0;
  221. // 结果页「去结算」需带 orderId/orderNo/金额/备注,用于确认支付
  222. uni.setStorageSync('lastPlaceOrderInfo', JSON.stringify({
  223. orderId: orderId ?? undefined,
  224. orderNo: orderNo ?? undefined,
  225. tableId: orderInfo.value.tableId,
  226. tableNumber: orderInfo.value.tableNumber,
  227. diners: orderInfo.value.diners,
  228. totalAmount: total,
  229. remark: (orderInfo.value.remark ?? '').trim().slice(0, 30)
  230. }));
  231. // 确认下单成功后即将跳转,此时页面不会触发 onUnload,需主动调用解锁
  232. const unlockFn = diningApi.PostOrderUnlock || diningApi.postOrderUnlock;
  233. if (typeof unlockFn === 'function' && tableId) {
  234. unlockFn({ tableId }).catch((err) => console.warn('解锁订单失败:', err));
  235. }
  236. if (orderId != null) {
  237. go(`/pages/result/index?id=${encodeURIComponent(orderId)}`);
  238. } else {
  239. go('/pages/result/index');
  240. }
  241. } catch (e) {
  242. uni.hideLoading();
  243. uni.showToast({ title: e?.message || '下单失败', icon: 'none' });
  244. }
  245. };
  246. onLoad((options) => {
  247. const userStore = useUserStore();
  248. const contactPhone = userStore.getUserInfo?.phone ?? userStore.getUserInfo?.contactPhone ?? userStore.getUserInfo?.mobile ?? '';
  249. orderInfo.value.contactPhone = contactPhone;
  250. const raw = uni.getStorageSync('placeOrderCart');
  251. if (raw) {
  252. try {
  253. const data = JSON.parse(raw);
  254. const list = Array.isArray(data.list) ? data.list : [];
  255. foodList.value = list.map(normalizeCartItem).filter((item) => (item.quantity || 0) > 0);
  256. if (data.tableId != null) orderInfo.value.tableId = data.tableId;
  257. if (data.tableNumber != null) orderInfo.value.tableNumber = data.tableNumber;
  258. if (data.diners != null) orderInfo.value.diners = data.diners;
  259. if (data.remark != null) orderInfo.value.remark = data.remark;
  260. const total = Number(data.totalAmount) || 0;
  261. orderInfo.value.totalAmount = total;
  262. if (data.dishTotal != null && data.dishTotal !== '') orderInfo.value.dishTotal = Number(data.dishTotal) || 0;
  263. const fee = data.utensilFee ?? data.tablewareFee;
  264. if (fee != null && fee !== '') orderInfo.value.utensilFee = Number(fee) || 0;
  265. updatePayAmount();
  266. } catch (e) {
  267. console.error('解析购物车数据失败:', e);
  268. }
  269. }
  270. if (orderInfo.value.tableId) uni.setStorageSync('currentTableId', String(orderInfo.value.tableId));
  271. // 不再主动调用 userOwnedByStore,优惠券在点击「优惠券」行打开弹窗时再拉取
  272. });
  273. // 每次进入页面(含从其他页返回)都调用锁定订单接口
  274. onShow(() => {
  275. const tableId = orderInfo.value?.tableId;
  276. if (tableId) {
  277. (diningApi.PostOrderLock || diningApi.postOrderLock)({ tableId }).catch((err) => {
  278. console.warn('锁定订单失败:', err);
  279. });
  280. }
  281. });
  282. // 离开页面时调用解锁订单接口
  283. onUnload(() => {
  284. const tableId = orderInfo.value?.tableId;
  285. if (tableId && typeof (diningApi.PostOrderUnlock || diningApi.postOrderUnlock) === 'function') {
  286. (diningApi.PostOrderUnlock || diningApi.postOrderUnlock)({ tableId }).catch((err) => {
  287. console.warn('解锁订单失败:', err);
  288. });
  289. }
  290. });
  291. </script>
  292. <style lang="scss" scoped>
  293. .content {
  294. padding: 0 30rpx 300rpx;
  295. }
  296. .card {
  297. background-color: #fff;
  298. border-radius: 24rpx;
  299. padding: 30rpx 0;
  300. margin-top: 20rpx;
  301. .card-header {
  302. display: flex;
  303. justify-content: space-between;
  304. align-items: center;
  305. padding: 0 30rpx;
  306. height: 40rpx;
  307. position: relative;
  308. font-size: 27rpx;
  309. color: #151515;
  310. font-weight: bold;
  311. }
  312. .tag {
  313. width: 10rpx;
  314. height: 42rpx;
  315. background: linear-gradient(35deg, #FCB73F 0%, #FC733D 100%);
  316. border-radius: 0rpx 0rpx 0rpx 0rpx;
  317. position: absolute;
  318. left: 0;
  319. top: 0;
  320. }
  321. .card-content {
  322. padding: 0 30rpx;
  323. }
  324. .info-item {
  325. display: flex;
  326. justify-content: space-between;
  327. align-items: center;
  328. margin-top: 20rpx;
  329. font-size: 27rpx;
  330. .info-item-label {
  331. color: #666666;
  332. }
  333. .info-item-value {
  334. color: #151515;
  335. }
  336. &--clickable {
  337. cursor: pointer;
  338. }
  339. &--coupon .coupon-value {
  340. display: flex;
  341. align-items: center;
  342. gap: 8rpx;
  343. }
  344. .coupon-amount {
  345. color: #E61F19;
  346. }
  347. .coupon-placeholder {
  348. color: #999999;
  349. }
  350. .coupon-arrow {
  351. color: #999999;
  352. font-size: 32rpx;
  353. }
  354. }
  355. .info-item-textarea {
  356. width: 100%;
  357. height: 115rpx;
  358. border-radius: 8rpx;
  359. border: 1rpx solid #F2F2F2;
  360. margin-top: 20rpx;
  361. font-size: 23rpx;
  362. color: #AAAAAA;
  363. box-sizing: border-box;
  364. padding: 20rpx;
  365. }
  366. .info-food {
  367. margin-top: 20rpx;
  368. }
  369. .food-item {
  370. display: flex;
  371. align-items: center;
  372. padding: 20rpx 0;
  373. &:last-child {
  374. border-bottom: none;
  375. }
  376. &__image {
  377. width: 118rpx;
  378. height: 118rpx;
  379. border-radius: 8rpx;
  380. flex-shrink: 0;
  381. background-color: #f5f5f5;
  382. }
  383. &__info {
  384. flex: 1;
  385. margin-left: 20rpx;
  386. display: flex;
  387. flex-direction: column;
  388. justify-content: center;
  389. }
  390. &__name {
  391. font-size: 28rpx;
  392. font-weight: bold;
  393. color: #151515;
  394. margin-bottom: 8rpx;
  395. }
  396. &__desc {
  397. font-size: 24rpx;
  398. color: #666666;
  399. line-height: 1.5;
  400. }
  401. &__tag {
  402. font-size: 24rpx;
  403. color: #666666;
  404. }
  405. &__right {
  406. display: flex;
  407. flex-direction: column;
  408. align-items: flex-end;
  409. justify-content: center;
  410. margin-left: 20rpx;
  411. }
  412. &__price {
  413. display: flex;
  414. align-items: baseline;
  415. color: #151515;
  416. font-weight: bold;
  417. margin-bottom: 8rpx;
  418. .price-symbol {
  419. font-size: 20rpx;
  420. margin-right: 2rpx;
  421. }
  422. .price-main {
  423. font-size: 28rpx;
  424. }
  425. .price-decimal {
  426. font-size: 24rpx;
  427. }
  428. }
  429. &__quantity {
  430. font-size: 24rpx;
  431. color: #797979;
  432. }
  433. }
  434. .price-line {
  435. display: flex;
  436. justify-content: space-between;
  437. align-items: center;
  438. margin-top: 20rpx;
  439. font-size: 27rpx;
  440. color: #151515;
  441. border-top: 1rpx solid rgba(170, 170, 170, 0.15);
  442. font-weight: bold;
  443. padding-top: 20rpx;
  444. }
  445. }
  446. .bottom-button {
  447. width: 100%;
  448. background-color: #fff;
  449. padding: 20rpx 30rpx;
  450. box-sizing: border-box;
  451. position: fixed;
  452. bottom: 0;
  453. left: 0;
  454. right: 0;
  455. z-index: 999;
  456. padding-bottom: env(safe-area-inset-bottom);
  457. box-shadow: 0rpx -11rpx 46rpx 0rpx rgba(0, 0, 0, 0.05);
  458. .bottom-button-text {
  459. font-size: 32rpx;
  460. font-weight: bold;
  461. color: #fff;
  462. width: 695rpx;
  463. height: 80rpx;
  464. background: linear-gradient(90deg, #FCB73F 0%, #FC743D 100%);
  465. border-radius: 23rpx 23rpx 23rpx 23rpx;
  466. display: flex;
  467. align-items: center;
  468. justify-content: center;
  469. }
  470. }
  471. </style>