index.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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. <!-- 本版本不参与服务费
  77. <view class="info-item">
  78. <view class="info-item-label">服务费</view>
  79. <view class="info-item-value">¥{{ formatPrice(orderInfo.serviceFee ?? 0) }}</view>
  80. </view>
  81. -->
  82. <view class="price-line">
  83. <view class="price-line-label">应付金额</view>
  84. <view class="price-line-value">¥{{ formatPrice(orderInfo.payAmount ?? orderInfo.totalAmount) }}</view>
  85. </view>
  86. </view>
  87. </view>
  88. <!-- 底部按钮:确认下单(去结算请走 pages/checkout/index) -->
  89. <view class="bottom-button">
  90. <view class="bottom-button-text" hover-class="hover-active" @click="handleConfirmOrder">确认下单</view>
  91. </view>
  92. </view>
  93. </template>
  94. <script setup>
  95. import { onLoad, onShow, onUnload } from "@dcloudio/uni-app";
  96. import { ref, computed } from "vue";
  97. import { go } from "@/utils/utils.js";
  98. import { getFileUrl } from "@/utils/file.js";
  99. import { useUserStore } from "@/store/user.js";
  100. import * as diningApi from "@/api/dining.js";
  101. // 订单信息(从购物车带过来或 URL 参数)
  102. const orderInfo = ref({
  103. orderNo: '',
  104. tableId: '',
  105. tableNumber: '',
  106. diners: '',
  107. contactPhone: '',
  108. remark: '',
  109. totalAmount: 0,
  110. dishTotal: null,
  111. serviceFee: 0,
  112. discountAmount: 0,
  113. payAmount: 0,
  114. couponId: null
  115. });
  116. // 菜品列表(从购物车带过来)
  117. const foodList = ref([]);
  118. // 菜品清单展示:排除特殊占位项(id/cuisineId === -1)
  119. const displayFoodList = computed(() => {
  120. return (foodList.value ?? []).filter((it) => Number(it?.id ?? it?.cuisineId) !== -1);
  121. });
  122. // 菜品成交价小计(元,不含服务费):与点餐页购物车、服务费估算 goodsSubtotal 一致
  123. const dishTotal = computed(() => {
  124. const list = displayFoodList.value ?? [];
  125. const sum = list.reduce((s, it) => {
  126. if (it?.subtotalAmount != null && it.subtotalAmount !== '') {
  127. return s + (Number(it.subtotalAmount) || 0);
  128. }
  129. const qty = Number(it?.quantity) || 0;
  130. const unitPrice =
  131. Number(it?.unitPrice ?? it?.price ?? it?.salePrice ?? it?.totalPrice ?? 0) || 0;
  132. return s + qty * unitPrice;
  133. }, 0);
  134. return Math.max(0, Math.round(sum * 100) / 100);
  135. });
  136. function getItemImage(item) {
  137. const raw = item?.cuisineImage ?? item?.image ?? item?.imageUrl ?? '';
  138. const url = typeof raw === 'string' ? raw.split(',')[0].trim() : raw;
  139. return url ? getFileUrl(url) : '';
  140. }
  141. // 单品单价:列表展示用 unitPrice/price
  142. function getItemPrice(item) {
  143. const p = item?.unitPrice ?? item?.price ?? item?.totalPrice ?? item?.salePrice ?? 0;
  144. return Number(p) || 0;
  145. }
  146. function formatPrice(price) {
  147. const num = Number(price);
  148. return Number.isNaN(num) ? '0.00' : num.toFixed(2);
  149. }
  150. // 应付金额 = 菜品总价(本版不含服务费;优惠券在结算页选择)
  151. const updatePayAmount = () => {
  152. const dish = dishTotal.value;
  153. // const fee = Number(orderInfo.value.serviceFee) || 0;
  154. const fee = 0;
  155. orderInfo.value.payAmount = Math.max(0, dish + fee);
  156. orderInfo.value.totalAmount = orderInfo.value.payAmount;
  157. };
  158. /** 解析 /service-fee/estimate 的 data:顶层 serviceFee 或 items[].amount 汇总(与结算页接口结构一致) */
  159. function parseServiceFeeFromEstimate(res) {
  160. if (res == null) return 0;
  161. if (typeof res === 'number' && Number.isFinite(res)) return Math.max(0, res);
  162. if (typeof res !== 'object') return 0;
  163. const top = Number(
  164. res.serviceFee ?? res.estimatedServiceFee ?? res.fee ?? res.amount ?? NaN
  165. );
  166. if (Number.isFinite(top) && top > 0) return top;
  167. const items = Array.isArray(res.items) ? res.items : [];
  168. const sumItems = items.reduce(
  169. (s, it) => s + (Number(it?.amount ?? it?.fee ?? it?.serviceFee ?? 0) || 0),
  170. 0
  171. );
  172. if (sumItems > 0) return sumItems;
  173. if (Number.isFinite(top)) return Math.max(0, top);
  174. return 0;
  175. }
  176. /** 服务费估算(本版本关闭) */
  177. async function fetchServiceFeeEstimate(storeIdFromCart) {
  178. orderInfo.value.serviceFee = 0;
  179. updatePayAmount();
  180. /* 原 GetServiceFeeEstimate 逻辑(恢复时取消注释)
  181. const storeId =
  182. (storeIdFromCart != null && String(storeIdFromCart).trim() !== '' ? String(storeIdFromCart).trim() : '') ||
  183. uni.getStorageSync('currentStoreId') ||
  184. '';
  185. const tableId =
  186. (orderInfo.value.tableId != null && String(orderInfo.value.tableId).trim() !== ''
  187. ? String(orderInfo.value.tableId).trim()
  188. : '') || String(uni.getStorageSync('currentTableId') || '').trim();
  189. if (!storeId || !tableId) return;
  190. const rawDiners = Number(orderInfo.value.diners);
  191. const dinerCount = Number.isFinite(rawDiners) && rawDiners > 0 ? Math.floor(rawDiners) : 1;
  192. const goodsSubtotal = Number(dishTotal.value.toFixed(2));
  193. try {
  194. const res = await diningApi.GetServiceFeeEstimate({
  195. storeId,
  196. tableId,
  197. dinerCount,
  198. goodsSubtotal
  199. });
  200. const fee = parseServiceFeeFromEstimate(res);
  201. orderInfo.value.serviceFee = fee;
  202. updatePayAmount();
  203. } catch (e) {
  204. console.warn('服务费估算失败:', e);
  205. }
  206. */
  207. }
  208. // 将 tags 统一为 [{ text, type }] 格式
  209. function normalizeTags(raw) {
  210. if (raw == null) return [];
  211. let arr = [];
  212. if (Array.isArray(raw)) arr = raw;
  213. else if (typeof raw === 'string') {
  214. const t = raw.trim();
  215. if (t.startsWith('[')) { try { arr = JSON.parse(t); if (!Array.isArray(arr)) arr = []; } catch { arr = t ? [t] : []; } }
  216. else arr = t ? t.split(/[,,、\s]+/).map(s => s.trim()).filter(Boolean) : [];
  217. } else if (raw && typeof raw === 'object') arr = Array.isArray(raw.list) ? raw.list : Array.isArray(raw.items) ? raw.items : [];
  218. return arr.map((it) => {
  219. if (it == null) return { text: '', type: '' };
  220. if (typeof it === 'string') return { text: it, type: '' };
  221. if (typeof it === 'number') return { text: String(it), type: '' };
  222. return { text: it.text ?? it.tagName ?? it.name ?? it.label ?? it.title ?? '', type: it.type ?? it.tagType ?? '' };
  223. }).filter((t) => t.text !== '' && t.text != null);
  224. }
  225. // 将存储的购物车项转为确认页展示格式(兼容 cuisineName/cuisineImage/unitPrice/tags 等)
  226. function normalizeCartItem(item) {
  227. const image = item?.cuisineImage ?? item?.image ?? item?.imageUrl ?? '';
  228. const url = typeof image === 'string' ? image.split(',')[0].trim() : image;
  229. const rawTags = item?.tags ?? item?.tagList ?? item?.tagNames ?? item?.labels ?? item?.tag;
  230. return {
  231. id: item?.id ?? item?.cuisineId,
  232. name: item?.name ?? item?.cuisineName,
  233. cuisineName: item?.cuisineName,
  234. price: item?.unitPrice ?? item?.price,
  235. image: url,
  236. cuisineImage: item?.cuisineImage,
  237. quantity: Number(item?.quantity) || 0,
  238. tags: normalizeTags(rawTags),
  239. subtotalAmount: item?.subtotalAmount,
  240. remark: item?.remark
  241. };
  242. }
  243. const handleConfirmOrder = async () => {
  244. const tableId =
  245. (orderInfo.value.tableId != null && String(orderInfo.value.tableId).trim() !== ''
  246. ? String(orderInfo.value.tableId).trim()
  247. : '') || String(uni.getStorageSync('currentTableId') || '').trim();
  248. const contactPhone = orderInfo.value.contactPhone ?? '';
  249. const dinerCount = Number(orderInfo.value.diners) || 0;
  250. if (!tableId) {
  251. uni.showToast({ title: '请先选择桌号', icon: 'none' });
  252. return;
  253. }
  254. if (!contactPhone) {
  255. uni.showToast({ title: '请先填写联系电话', icon: 'none' });
  256. return;
  257. }
  258. const remark = (orderInfo.value.remark ?? '').trim().slice(0, 30);
  259. uni.showLoading({ title: '提交中...' });
  260. try {
  261. // const serviceFeeVal = Number(orderInfo.value.serviceFee) || 0;
  262. const serviceFeeVal = 0; // 本版本不参与服务费
  263. const payAmount = Number((Number(orderInfo.value.payAmount) ?? 0).toFixed(2));
  264. const totalAmount = Number((Number(dishTotal.value) ?? 0).toFixed(2));
  265. const createParams = {
  266. tableId,
  267. contactPhone,
  268. totalAmount,
  269. tablewareFee: 0,
  270. serviceFee: serviceFeeVal,
  271. payAmount,
  272. dinerCount: dinerCount || undefined,
  273. immediatePay: 0,
  274. remark: remark || undefined
  275. };
  276. const res = await diningApi.PostOrderCreate(createParams);
  277. uni.hideLoading();
  278. const orderId = res?.id ?? res?.orderId ?? res?.data?.id ?? res?.data?.orderId;
  279. const orderNo = res?.orderNo ?? res?.data?.orderNo ?? orderId ?? '';
  280. const total = orderInfo.value.payAmount ?? orderInfo.value.totalAmount ?? 0;
  281. // 结果页「去结算」需带 orderId/orderNo/金额/备注,用于确认支付
  282. uni.setStorageSync('lastPlaceOrderInfo', JSON.stringify({
  283. orderId: orderId ?? undefined,
  284. orderNo: orderNo ?? undefined,
  285. tableId: orderInfo.value.tableId,
  286. tableNumber: orderInfo.value.tableNumber,
  287. diners: orderInfo.value.diners,
  288. totalAmount: total,
  289. remark: (orderInfo.value.remark ?? '').trim().slice(0, 30)
  290. }));
  291. // 确认下单成功后即将跳转,此时页面不会触发 onUnload,需主动调用解锁
  292. const unlockFn = diningApi.PostOrderUnlock || diningApi.postOrderUnlock;
  293. if (typeof unlockFn === 'function' && tableId) {
  294. unlockFn({ tableId }).catch((err) => console.warn('解锁订单失败:', err));
  295. }
  296. if (orderId != null) {
  297. go(`/pages/result/index?id=${encodeURIComponent(orderId)}`);
  298. } else {
  299. go('/pages/result/index');
  300. }
  301. } catch (e) {
  302. uni.hideLoading();
  303. uni.showToast({ title: e?.message || '下单失败', icon: 'none' });
  304. }
  305. };
  306. onLoad((options) => {
  307. const userStore = useUserStore();
  308. const contactPhone = userStore.getUserInfo?.phone ?? userStore.getUserInfo?.contactPhone ?? userStore.getUserInfo?.mobile ?? '';
  309. orderInfo.value.contactPhone = contactPhone;
  310. let cartStoreId = '';
  311. const raw = uni.getStorageSync('placeOrderCart');
  312. if (raw) {
  313. try {
  314. const data = JSON.parse(raw);
  315. const list = Array.isArray(data.list) ? data.list : [];
  316. foodList.value = list.map(normalizeCartItem).filter((item) => (item.quantity || 0) > 0);
  317. if (data.tableId != null) orderInfo.value.tableId = data.tableId;
  318. if (data.tableNumber != null) orderInfo.value.tableNumber = data.tableNumber;
  319. if (data.diners != null) orderInfo.value.diners = data.diners;
  320. if (data.remark != null) orderInfo.value.remark = data.remark;
  321. if (data.storeId != null && data.storeId !== '') cartStoreId = String(data.storeId);
  322. const total = Number(data.totalAmount) || 0;
  323. orderInfo.value.totalAmount = total;
  324. if (data.dishTotal != null && data.dishTotal !== '') orderInfo.value.dishTotal = Number(data.dishTotal) || 0;
  325. // orderInfo.value.serviceFee =
  326. // Number(data.serviceFee ?? data.serviceCharge ?? data.utensilFee ?? data.tablewareFee ?? 0) || 0;
  327. orderInfo.value.serviceFee = 0;
  328. updatePayAmount();
  329. } catch (e) {
  330. console.error('解析购物车数据失败:', e);
  331. orderInfo.value.serviceFee = 0;
  332. }
  333. } else {
  334. orderInfo.value.serviceFee = 0;
  335. }
  336. if (orderInfo.value.tableId) uni.setStorageSync('currentTableId', String(orderInfo.value.tableId));
  337. fetchServiceFeeEstimate(cartStoreId); // 本版 no-op,不参与服务费
  338. // 不再主动调用 userOwnedByStore,优惠券在点击「优惠券」行打开弹窗时再拉取
  339. });
  340. // 每次进入页面(含从其他页返回)都调用锁定订单接口
  341. onShow(() => {
  342. const tableId = orderInfo.value?.tableId;
  343. if (tableId) {
  344. (diningApi.PostOrderLock || diningApi.postOrderLock)({ tableId }).catch((err) => {
  345. console.warn('锁定订单失败:', err);
  346. });
  347. }
  348. });
  349. // 离开页面时调用解锁订单接口
  350. onUnload(() => {
  351. const tableId = orderInfo.value?.tableId;
  352. if (tableId && typeof (diningApi.PostOrderUnlock || diningApi.postOrderUnlock) === 'function') {
  353. (diningApi.PostOrderUnlock || diningApi.postOrderUnlock)({ tableId }).catch((err) => {
  354. console.warn('解锁订单失败:', err);
  355. });
  356. }
  357. });
  358. </script>
  359. <style lang="scss" scoped>
  360. .content {
  361. padding: 0 30rpx 300rpx;
  362. }
  363. .card {
  364. background-color: #fff;
  365. border-radius: 24rpx;
  366. padding: 30rpx 0;
  367. margin-top: 20rpx;
  368. .card-header {
  369. display: flex;
  370. justify-content: space-between;
  371. align-items: center;
  372. padding: 0 30rpx;
  373. height: 40rpx;
  374. position: relative;
  375. font-size: 27rpx;
  376. color: #151515;
  377. font-weight: bold;
  378. }
  379. .tag {
  380. width: 10rpx;
  381. height: 42rpx;
  382. background: linear-gradient(35deg, #FCB73F 0%, #FC733D 100%);
  383. border-radius: 0rpx 0rpx 0rpx 0rpx;
  384. position: absolute;
  385. left: 0;
  386. top: 0;
  387. }
  388. .card-content {
  389. padding: 0 30rpx;
  390. }
  391. .info-item {
  392. display: flex;
  393. justify-content: space-between;
  394. align-items: center;
  395. margin-top: 20rpx;
  396. font-size: 27rpx;
  397. .info-item-label {
  398. color: #666666;
  399. }
  400. .info-item-value {
  401. color: #151515;
  402. }
  403. &--clickable {
  404. cursor: pointer;
  405. }
  406. &--coupon .coupon-value {
  407. display: flex;
  408. align-items: center;
  409. gap: 8rpx;
  410. }
  411. .coupon-amount {
  412. color: #E61F19;
  413. }
  414. .coupon-placeholder {
  415. color: #999999;
  416. }
  417. .coupon-arrow {
  418. color: #999999;
  419. font-size: 32rpx;
  420. }
  421. }
  422. .info-item-textarea {
  423. width: 100%;
  424. height: 115rpx;
  425. border-radius: 8rpx;
  426. border: 1rpx solid #F2F2F2;
  427. margin-top: 20rpx;
  428. font-size: 23rpx;
  429. color: #AAAAAA;
  430. box-sizing: border-box;
  431. padding: 20rpx;
  432. }
  433. .info-food {
  434. margin-top: 20rpx;
  435. }
  436. .food-item {
  437. display: flex;
  438. align-items: center;
  439. padding: 20rpx 0;
  440. &:last-child {
  441. border-bottom: none;
  442. }
  443. &__image {
  444. width: 118rpx;
  445. height: 118rpx;
  446. border-radius: 8rpx;
  447. flex-shrink: 0;
  448. background-color: #f5f5f5;
  449. }
  450. &__info {
  451. flex: 1;
  452. margin-left: 20rpx;
  453. display: flex;
  454. flex-direction: column;
  455. justify-content: center;
  456. }
  457. &__name {
  458. font-size: 28rpx;
  459. font-weight: bold;
  460. color: #151515;
  461. margin-bottom: 8rpx;
  462. }
  463. &__desc {
  464. font-size: 24rpx;
  465. color: #666666;
  466. line-height: 1.5;
  467. }
  468. &__tag {
  469. font-size: 24rpx;
  470. color: #666666;
  471. }
  472. &__right {
  473. display: flex;
  474. flex-direction: column;
  475. align-items: flex-end;
  476. justify-content: center;
  477. margin-left: 20rpx;
  478. }
  479. &__price {
  480. display: flex;
  481. align-items: baseline;
  482. color: #151515;
  483. font-weight: bold;
  484. margin-bottom: 8rpx;
  485. .price-symbol {
  486. font-size: 20rpx;
  487. margin-right: 2rpx;
  488. }
  489. .price-main {
  490. font-size: 28rpx;
  491. }
  492. .price-decimal {
  493. font-size: 24rpx;
  494. }
  495. }
  496. &__quantity {
  497. font-size: 24rpx;
  498. color: #797979;
  499. }
  500. }
  501. .price-line {
  502. display: flex;
  503. justify-content: space-between;
  504. align-items: center;
  505. margin-top: 20rpx;
  506. font-size: 27rpx;
  507. color: #151515;
  508. border-top: 1rpx solid rgba(170, 170, 170, 0.15);
  509. font-weight: bold;
  510. padding-top: 20rpx;
  511. }
  512. }
  513. .bottom-button {
  514. width: 100%;
  515. background-color: #fff;
  516. padding: 20rpx 30rpx;
  517. box-sizing: border-box;
  518. position: fixed;
  519. bottom: 0;
  520. left: 0;
  521. right: 0;
  522. z-index: 999;
  523. padding-bottom: env(safe-area-inset-bottom);
  524. box-shadow: 0rpx -11rpx 46rpx 0rpx rgba(0, 0, 0, 0.05);
  525. .bottom-button-text {
  526. font-size: 32rpx;
  527. font-weight: bold;
  528. color: #fff;
  529. width: 695rpx;
  530. height: 80rpx;
  531. background: linear-gradient(90deg, #FCB73F 0%, #FC743D 100%);
  532. border-radius: 23rpx 23rpx 23rpx 23rpx;
  533. display: flex;
  534. align-items: center;
  535. justify-content: center;
  536. }
  537. }
  538. </style>