index.vue 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. <template>
  2. <!-- 确认支付页面:订单信息、菜品清单、价格明细、确认支付 -->
  3. <view class="content">
  4. <view class="card">
  5. <view class="card-header">
  6. <view class="tag"></view>
  7. <view class="card-header-title">订单信息</view>
  8. </view>
  9. <view class="card-content">
  10. <view class="info-item">
  11. <view class="info-item-label">就餐桌号</view>
  12. <view class="info-item-value">{{ orderInfo.tableNumber || orderInfo.tableId || '—' }}</view>
  13. </view>
  14. <view class="info-item info-item--diners">
  15. <view class="info-item-label">用餐人数</view>
  16. <view class="diner-stepper">
  17. <view
  18. class="diner-stepper__btn"
  19. :class="{ 'diner-stepper__btn--disabled': dinerCount <= 1 }"
  20. hover-class="diner-stepper__btn--active"
  21. @tap.stop="adjustDiners(-1)"
  22. >
  23. <view class="diner-stepper__icon diner-stepper__icon--minus" />
  24. </view>
  25. <text class="diner-stepper__value">{{ dinerCount }}</text>
  26. <view
  27. class="diner-stepper__btn"
  28. :class="{ 'diner-stepper__btn--disabled': dinerCount >= MAX_DINERS }"
  29. hover-class="diner-stepper__btn--active"
  30. @tap.stop="adjustDiners(1)"
  31. >
  32. <view class="diner-stepper__icon diner-stepper__icon--plus" />
  33. </view>
  34. </view>
  35. </view>
  36. <view class="info-item">
  37. <view class="info-item-label">联系电话</view>
  38. <view class="info-item-value">{{ orderInfo.contactPhone || '—' }}</view>
  39. </view>
  40. <view class="info-item">
  41. <view class="info-item-label">备注信息</view>
  42. <view class="info-item-value remark-text">{{ orderInfo.remark || '—' }}</view>
  43. </view>
  44. </view>
  45. </view>
  46. <view class="card" v-if="displayFoodList.length > 0">
  47. <view class="card-header">
  48. <view class="tag"></view>
  49. <view class="card-header-title">菜品清单</view>
  50. </view>
  51. <view class="card-content">
  52. <view class="info-food">
  53. <view v-for="(item, index) in displayFoodList" :key="item.id || index" class="food-item">
  54. <image :src="getItemImage(item)" mode="aspectFill" class="food-item__image"></image>
  55. <view class="food-item__info">
  56. <view class="food-item__name">{{ item.name }}</view>
  57. <view class="food-item__desc" v-if="item.tags && item.tags.length > 0">
  58. <text v-for="(tag, tagIndex) in item.tags" :key="tagIndex" class="food-item__tag"
  59. :class="tag.type">{{ tag.text }}<text v-if="tagIndex < item.tags.length - 1">,</text>
  60. </text>
  61. </view>
  62. </view>
  63. <view class="food-item__right">
  64. <view class="food-item__price">
  65. <text class="price-main">¥{{ formatPrice(item.price) }}</text>
  66. </view>
  67. <view class="food-item__quantity">{{ item.quantity || 1 }}份</view>
  68. </view>
  69. </view>
  70. </view>
  71. </view>
  72. </view>
  73. <view class="card">
  74. <view class="card-header">
  75. <view class="tag"></view>
  76. <view class="card-header-title">价格明细</view>
  77. </view>
  78. <view class="card-content">
  79. <view class="info-item">
  80. <view class="info-item-label">菜品总价</view>
  81. <view class="info-item-value">¥{{ formatPrice(foodSubtotalForDisplay) }}</view>
  82. </view>
  83. <!-- 本版本不参与服务费
  84. <view class="info-item">
  85. <view class="info-item-label">服务费</view>
  86. <view class="info-item-value">¥{{ formatPrice(orderInfo.serviceFee ?? 0) }}</view>
  87. </view>
  88. -->
  89. <view class="info-item info-item--coupon info-item--clickable" @click="onCouponRowClick">
  90. <view class="info-item-label">优惠券</view>
  91. <view class="info-item-value coupon-value">
  92. <text v-if="(orderInfo.discountAmount ?? 0) > 0" class="coupon-amount">{{ couponDisplayText }}</text>
  93. <text v-else class="coupon-placeholder">请选择</text>
  94. <text class="coupon-arrow">›</text>
  95. </view>
  96. </view>
  97. <view v-if="(orderInfo.discountAmount ?? 0) > 0" class="info-item info-item--coupon">
  98. <view class="info-item-label">优惠金额</view>
  99. <view class="info-item-value coupon-value">
  100. <text class="coupon-amount">-¥{{ formatPrice(orderInfo.discountAmount) }}</text>
  101. </view>
  102. </view>
  103. <view class="price-line">
  104. <view class="price-line-label">应付金额</view>
  105. <view class="price-line-value">¥{{ formatPrice(orderInfo.payAmount ?? 0) }}</view>
  106. </view>
  107. </view>
  108. </view>
  109. <view class="bottom-button">
  110. <view class="bottom-button-text" hover-class="hover-active" @click="handleConfirmPay">确认支付 ¥{{ formatPrice(orderInfo.payAmount ?? 0) }}</view>
  111. </view>
  112. <!-- 选择优惠券弹窗 -->
  113. <view class="coupon-modal-wrapper">
  114. <SelectCouponModal
  115. v-model:open="couponModalOpen"
  116. :coupon-list="couponList"
  117. :selected-coupon-id="selectedCouponId"
  118. :view-only="false"
  119. @select="handleCouponSelect"
  120. @close="couponModalOpen = false"
  121. />
  122. </view>
  123. </view>
  124. </template>
  125. <script setup>
  126. import { onLoad, onShow, onUnload } from '@dcloudio/uni-app';
  127. import { ref, computed } from 'vue';
  128. import { go } from '@/utils/utils.js';
  129. import { getFileUrl } from '@/utils/file.js';
  130. import { useUserStore } from '@/store/user.js';
  131. import * as diningApi from '@/api/dining.js';
  132. import SelectCouponModal from '@/pages/orderFood/components/SelectCouponModal.vue';
  133. const orderId = ref('');
  134. /** 用餐人数上限(与选人数页「查看更多」一致) */
  135. const MAX_DINERS = 16;
  136. const MIN_DINERS = 1;
  137. const orderInfo = ref({
  138. orderNo: '',
  139. storeId: '',
  140. tableId: '',
  141. tableNumber: '',
  142. diners: '',
  143. contactPhone: '',
  144. remark: '',
  145. totalAmount: 0,
  146. dishTotal: null,
  147. couponId: null,
  148. couponName: '',
  149. couponType: null, // 1 满减 2 折扣
  150. discountRate: null, // 折扣券力度,如 5.5 表示 5.5折
  151. nominalValue: null, // 满减券面额
  152. discountAmount: 0,
  153. payAmount: 0,
  154. serviceFee: 0
  155. });
  156. const foodList = ref([]);
  157. /** 服务费估算接口返回的 feeType;为 1 表示按人数计费,修改人数时需重新估算 */
  158. const estimateFeeType = ref(null);
  159. // 优惠券选择(结算页)
  160. const couponModalOpen = ref(false);
  161. const couponList = ref([]);
  162. const selectedCouponId = ref(null);
  163. // 菜品清单展示(排除特殊占位 id=-1)
  164. const displayFoodList = computed(() =>
  165. (foodList.value ?? []).filter((it) => Number(it?.id ?? it?.cuisineId) !== -1)
  166. );
  167. function parseDinerCount(val) {
  168. const n = Number(val);
  169. if (!Number.isFinite(n) || n < MIN_DINERS) return MIN_DINERS;
  170. return Math.min(MAX_DINERS, Math.floor(n));
  171. }
  172. /** 当前用餐人数(数字,用于步进器展示) */
  173. const dinerCount = computed(() => parseDinerCount(orderInfo.value.diners));
  174. function adjustDiners(delta) {
  175. const cur = parseDinerCount(orderInfo.value.diners);
  176. const next = cur + delta;
  177. if (next < MIN_DINERS || next > MAX_DINERS) return;
  178. orderInfo.value.diners = String(next);
  179. uni.setStorageSync('currentDiners', String(next));
  180. // 本版本不参与服务费(恢复按人数重算时取消注释)
  181. // if (Number(estimateFeeType.value) === 1) {
  182. // fetchServiceFeeEstimate().catch((err) => console.warn('按人数重算服务费失败:', err));
  183. // }
  184. }
  185. // 展示用菜品小计:有明细时与行成交价一致;否则用接口 dishTotal 或 totalAmount − 服务费
  186. const foodSubtotalForDisplay = computed(() => {
  187. const list = displayFoodList.value;
  188. if (list.length > 0) {
  189. const sum = list.reduce((s, it) => s + (Number(it.lineSubtotal) || 0), 0);
  190. return Math.max(0, Math.round(sum * 100) / 100);
  191. }
  192. const dt = orderInfo.value.dishTotal;
  193. if (dt != null && dt !== '') {
  194. const d = Number(dt);
  195. if (!Number.isNaN(d)) return Math.max(0, d);
  196. }
  197. const total = Number(orderInfo.value.totalAmount) || 0;
  198. const fee = Number(orderInfo.value.serviceFee) || 0;
  199. return Math.max(0, total - fee);
  200. });
  201. // 优惠券展示:满减券显示 nominalValue+元,折扣券显示 discountRate+折,否则显示 couponName 或 已使用优惠券
  202. const couponDisplayText = computed(() => {
  203. const o = orderInfo.value;
  204. if ((o.discountAmount ?? 0) <= 0) return '';
  205. const type = Number(o.couponType);
  206. if (type === 1 && (o.nominalValue != null && o.nominalValue !== '')) {
  207. const val = Number(o.nominalValue);
  208. return Number.isNaN(val) ? (o.couponName || '已使用优惠券') : val + '元';
  209. }
  210. if (type === 2 && (o.discountRate != null && o.discountRate !== '')) {
  211. const rate = Number(o.discountRate);
  212. return Number.isNaN(rate) ? (o.couponName || '已使用优惠券') : rate + '折';
  213. }
  214. return o.couponName || '已使用优惠券';
  215. });
  216. // 规范化优惠券项(供 SelectCouponModal 使用)
  217. function normalizeCouponItem(item) {
  218. if (!item || typeof item !== 'object') return null;
  219. const raw = item;
  220. const couponType = Number(raw.couponType) || 0;
  221. const nominalValue = Number(raw.nominalValue ?? raw.amount ?? 0) || 0;
  222. const discountRate = ((Number(raw.discountRate) || 0) / 10) || 0;
  223. const minAmount = Number(raw.minimumSpendingAmount ?? raw.minAmount ?? 0) || 0;
  224. let amountDisplay = nominalValue + '元';
  225. if (couponType === 1) amountDisplay = nominalValue + '元';
  226. else if (couponType === 2 && discountRate > 0) amountDisplay = (discountRate % 1 === 0 ? discountRate : discountRate.toFixed(1)) + '折';
  227. return {
  228. id: raw.userCouponId ?? raw.id ?? raw.couponId ?? '',
  229. couponId: raw.couponId ?? raw.id ?? raw.userCouponId ?? '',
  230. amount: nominalValue,
  231. minAmount,
  232. name: raw.name ?? raw.title ?? raw.couponName ?? '',
  233. expireDate: raw.expirationTime ?? raw.endGetDate ?? raw.expireDate ?? '',
  234. couponType,
  235. discountRate,
  236. amountDisplay
  237. };
  238. }
  239. // 点击优惠券行:打开选择弹窗
  240. const onCouponRowClick = () => {
  241. openCouponModal();
  242. };
  243. // 打开优惠券弹窗并拉取用户可用券
  244. const openCouponModal = async () => {
  245. const storeId = orderInfo.value.storeId || uni.getStorageSync('currentStoreId') || '';
  246. couponModalOpen.value = false;
  247. if (!storeId) {
  248. uni.showToast({ title: '暂无门店信息', icon: 'none' });
  249. return;
  250. }
  251. try {
  252. const res = await diningApi.GetUserOwnedCouponList({ storeId });
  253. const list = Array.isArray(res) ? res : (res?.data ?? res?.records ?? res?.list ?? []);
  254. const normalized = (Array.isArray(list) ? list : []).map(normalizeCouponItem).filter(Boolean);
  255. couponList.value = normalized;
  256. couponModalOpen.value = true;
  257. } catch (err) {
  258. console.error('获取优惠券失败:', err);
  259. uni.showToast({ title: '获取优惠券失败', icon: 'none' });
  260. }
  261. };
  262. // 选择优惠券后更新订单优惠与应付金额
  263. const handleCouponSelect = ({ coupon, selectedId }) => {
  264. const hasSelected = selectedId != null && selectedId !== '';
  265. selectedCouponId.value = hasSelected ? String(selectedId) : null;
  266. orderInfo.value.couponId = hasSelected ? (coupon?.couponId ?? coupon?.id ?? selectedCouponId.value) : null;
  267. if (!hasSelected) {
  268. orderInfo.value.discountAmount = 0;
  269. orderInfo.value.couponName = '';
  270. orderInfo.value.couponType = null;
  271. orderInfo.value.discountRate = null;
  272. orderInfo.value.nominalValue = null;
  273. } else if (coupon) {
  274. orderInfo.value.couponName = coupon.name ?? '';
  275. orderInfo.value.couponType = Number(coupon.couponType) ?? null;
  276. orderInfo.value.discountRate = coupon.discountRate != null ? coupon.discountRate : null;
  277. orderInfo.value.nominalValue = coupon.amount != null ? coupon.amount : null;
  278. const food = foodSubtotalForDisplay.value;
  279. const fee = Number(orderInfo.value.serviceFee) || 0;
  280. const baseForDiscount = food + fee;
  281. const couponType = Number(coupon.couponType) || 0;
  282. if (couponType === 2 && coupon.discountRate != null && baseForDiscount > 0) {
  283. const rate = Number(coupon.discountRate) || 0;
  284. orderInfo.value.discountAmount = Math.round(baseForDiscount * (1 - rate / 10) * 100) / 100;
  285. } else {
  286. orderInfo.value.discountAmount = Number(coupon.amount) || 0;
  287. }
  288. }
  289. updateCheckoutPayAmount();
  290. couponModalOpen.value = false;
  291. };
  292. // 应付金额 = 菜品总价 + 服务费 − 优惠金额
  293. const updateCheckoutPayAmount = () => {
  294. const food = Number(foodSubtotalForDisplay.value) || 0;
  295. const fee = Number(orderInfo.value.serviceFee) || 0;
  296. const discount = Number(orderInfo.value.discountAmount) || 0;
  297. orderInfo.value.payAmount = Math.max(0, Math.round((food + fee - discount) * 100) / 100);
  298. };
  299. /** 服务费变化后:折扣券(按菜品+服务费为基数)需重算优惠金额 */
  300. function recalcDiscountAfterServiceFeeChange() {
  301. const ct = Number(orderInfo.value.couponType) || 0;
  302. if (ct !== 2 || orderInfo.value.discountRate == null || !orderInfo.value.couponId) return;
  303. const food = Number(foodSubtotalForDisplay.value) || 0;
  304. const fee = Number(orderInfo.value.serviceFee) || 0;
  305. const base = food + fee;
  306. const rate = Number(orderInfo.value.discountRate) || 0;
  307. if (base <= 0) return;
  308. orderInfo.value.discountAmount = Math.round(base * (1 - rate / 10) * 100) / 100;
  309. }
  310. /**
  311. * 从估算接口 data 解析 feeType:后端可能放在顶层,也可能在 items[].feeType(如按人数规则)
  312. * 任一为 1 则视为按人数计费,修改人数需重新请求 estimate
  313. */
  314. function parseEstimateFeeType(res) {
  315. if (res == null || typeof res !== 'object') return null;
  316. const top = res.feeType != null && res.feeType !== '' ? Number(res.feeType) : NaN;
  317. if (top === 1) return 1;
  318. const items = Array.isArray(res.items) ? res.items : [];
  319. for (const it of items) {
  320. const ft = it?.feeType != null && it.feeType !== '' ? Number(it.feeType) : NaN;
  321. if (ft === 1) return 1;
  322. }
  323. if (!Number.isNaN(top)) return top;
  324. for (const it of items) {
  325. const ft = it?.feeType != null && it.feeType !== '' ? Number(it.feeType) : NaN;
  326. if (!Number.isNaN(ft)) return ft;
  327. }
  328. return null;
  329. }
  330. /**
  331. * 服务费估算(本版本关闭:不参与服务费)
  332. */
  333. async function fetchServiceFeeEstimate() {
  334. orderInfo.value.serviceFee = 0;
  335. estimateFeeType.value = null;
  336. recalcDiscountAfterServiceFeeChange();
  337. updateCheckoutPayAmount();
  338. /* 原 /store/dining/service-fee/estimate 逻辑(恢复时整段移出注释)
  339. const storeId = orderInfo.value.storeId || uni.getStorageSync('currentStoreId') || '';
  340. const tableId = getTableIdForServiceFeeEstimate();
  341. if (!storeId || !tableId) return;
  342. const dinerCount = parseDinerCount(orderInfo.value.diners);
  343. const goodsSubtotal = Math.max(0, Math.round((Number(foodSubtotalForDisplay.value) || 0) * 100) / 100);
  344. try {
  345. const res = await diningApi.GetServiceFeeEstimate({
  346. storeId: String(storeId),
  347. tableId,
  348. dinerCount,
  349. goodsSubtotal
  350. });
  351. estimateFeeType.value = parseEstimateFeeType(res);
  352. const fee =
  353. Number(
  354. res?.serviceFee ??
  355. res?.estimatedServiceFee ??
  356. res?.fee ??
  357. res?.amount ??
  358. (typeof res === 'number' ? res : 0)
  359. ) || 0;
  360. orderInfo.value.serviceFee = fee;
  361. recalcDiscountAfterServiceFeeChange();
  362. updateCheckoutPayAmount();
  363. } catch (e) {
  364. console.warn('结算页服务费估算失败:', e);
  365. }
  366. */
  367. }
  368. function formatPrice(price) {
  369. const num = Number(price);
  370. return Number.isNaN(num) ? '0.00' : num.toFixed(2);
  371. }
  372. // 取第一张图:逗号分隔取首段,数组取首项,对象取 url/path/src(与 orderDetail 一致)
  373. function firstImage(val) {
  374. if (val == null || val === '') return '';
  375. if (Array.isArray(val)) {
  376. const first = val[0];
  377. if (first != null && first !== '') {
  378. if (typeof first === 'object' && first !== null) return first.url ?? first.path ?? first.src ?? first.link ?? '';
  379. return String(first).split(/[,,]/)[0].trim();
  380. }
  381. return '';
  382. }
  383. if (typeof val === 'object') return val.url ?? val.path ?? val.src ?? val.link ?? '';
  384. const str = String(val).trim();
  385. return str ? str.split(/[,,]/)[0].trim() : '';
  386. }
  387. function getItemImage(item) {
  388. const raw = item?.image ?? item?.cuisineImage ?? item?.imageUrl ?? item?.images ?? item?.pic ?? item?.cover ?? '';
  389. const url = firstImage(raw) || (typeof raw === 'string' ? raw.split(/[,,]/)[0]?.trim() : '');
  390. if (url && typeof url === 'string' && (url.startsWith('http') || url.startsWith('//'))) return url;
  391. return getFileUrl(url || 'img/icon/shop.png');
  392. }
  393. // 将 tags 统一为 [{ text, type }] 格式
  394. function normalizeTags(raw) {
  395. if (raw == null) return [];
  396. let arr = [];
  397. if (Array.isArray(raw)) arr = raw;
  398. else if (typeof raw === 'string') {
  399. const t = raw.trim();
  400. if (t.startsWith('[')) { try { arr = JSON.parse(t); if (!Array.isArray(arr)) arr = []; } catch { arr = t ? [t] : []; } }
  401. else arr = t ? t.split(/[,,、\s]+/).map(s => s.trim()).filter(Boolean) : [];
  402. } else if (raw && typeof raw === 'object') arr = Array.isArray(raw.list) ? raw.list : Array.isArray(raw.items) ? raw.items : [];
  403. return arr.map((it) => {
  404. if (it == null) return { text: '', type: '' };
  405. if (typeof it === 'string') return { text: it, type: '' };
  406. if (typeof it === 'number') return { text: String(it), type: '' };
  407. return { text: it.text ?? it.tagName ?? it.name ?? it.label ?? it.title ?? '', type: it.type ?? it.tagType ?? '' };
  408. }).filter((t) => t.text !== '' && t.text != null);
  409. }
  410. // 接口订单项转列表项:图片取首张(逗号/数组/对象与 orderDetail 一致)
  411. function normalizeOrderItem(item) {
  412. const rawTags = item?.tags ?? item?.tagList ?? item?.tagNames ?? item?.labels ?? item?.tag;
  413. const rawImg = item?.images ?? item?.image ?? item?.cuisineImage ?? item?.imageUrl ?? item?.pic ?? item?.cover ?? '';
  414. const imageUrl = firstImage(rawImg) || (typeof rawImg === 'string' ? rawImg.split(/[,,]/)[0]?.trim() : '') || 'img/icon/shop.png';
  415. const qty = Number(item?.quantity ?? 1) || 1;
  416. /** 行成交价小计(元):与购物车/服务费估算 goodsSubtotal 一致 */
  417. let lineSubtotal = 0;
  418. if (item?.subtotalAmount != null && item.subtotalAmount !== '') {
  419. lineSubtotal = Number(item.subtotalAmount) || 0;
  420. } else if (item?.totalPrice != null && item.totalPrice !== '') {
  421. lineSubtotal = Number(item.totalPrice) || 0;
  422. } else {
  423. const unit = Number(item?.unitPrice ?? item?.price ?? 0) || 0;
  424. lineSubtotal = unit * qty;
  425. }
  426. const unitForDisplay =
  427. Number(item?.unitPrice ?? item?.price ?? 0) ||
  428. (qty > 0 ? lineSubtotal / qty : 0);
  429. return {
  430. id: item?.id ?? item?.cuisineId,
  431. name: item?.cuisineName ?? item?.name ?? '',
  432. price: unitForDisplay,
  433. lineSubtotal,
  434. image: imageUrl,
  435. quantity: qty,
  436. tags: normalizeTags(rawTags)
  437. };
  438. }
  439. /** 桌台主键:store_table.id,不可用桌号展示文案代替 */
  440. function getTableIdForServiceFeeEstimate() {
  441. const t = orderInfo.value.tableId;
  442. if (t != null && String(t).trim() !== '') return String(t).trim();
  443. return String(uni.getStorageSync('currentTableId') || '').trim();
  444. }
  445. const fetchOrderDetail = async () => {
  446. const id = orderId.value || '';
  447. if (!id) return;
  448. try {
  449. const res = await diningApi.GetOrderInfo(id);
  450. const raw = res?.data ?? res ?? {};
  451. orderInfo.value.orderNo = raw?.orderNo ?? raw?.orderId ?? '';
  452. orderInfo.value.storeId = raw?.storeId ?? raw?.store_id ?? '';
  453. // tableId 须为门店桌台表主键(store_table.id),勿用 tableNumber(展示用桌号)冒充
  454. orderInfo.value.tableId =
  455. raw?.tableId ??
  456. raw?.storeTableId ??
  457. raw?.store_table_id ??
  458. raw?.diningTableId ??
  459. '';
  460. orderInfo.value.tableNumber = raw?.tableNumber ?? raw?.tableNo ?? '';
  461. const dc = raw?.dinerCount ?? raw?.diners ?? '';
  462. orderInfo.value.diners =
  463. dc !== '' && dc != null ? String(parseDinerCount(dc)) : String(MIN_DINERS);
  464. orderInfo.value.contactPhone = raw?.contactPhone ?? raw?.phone ?? '';
  465. orderInfo.value.remark = raw?.remark ?? '';
  466. const total = Number(raw?.totalAmount ?? raw?.orderAmount ?? raw?.foodAmount ?? 0) || 0;
  467. orderInfo.value.totalAmount = total;
  468. orderInfo.value.dishTotal = raw?.dishTotal != null ? Number(raw.dishTotal) : null;
  469. orderInfo.value.couponId = raw?.couponId ?? null;
  470. orderInfo.value.couponName = raw?.couponName ?? '';
  471. orderInfo.value.couponType = raw?.couponType ?? null;
  472. const rawRate = raw?.discountRate;
  473. orderInfo.value.discountRate = rawRate != null && rawRate !== '' ? (Number(rawRate) || 0) / 10 : null;
  474. orderInfo.value.nominalValue = raw?.nominalValue ?? null;
  475. orderInfo.value.discountAmount = Number(raw?.discountAmount ?? raw?.couponAmount ?? raw?.couponDiscount ?? 0) || 0;
  476. orderInfo.value.payAmount = Number(raw?.payAmount ?? raw?.totalAmount ?? raw?.totalPrice ?? 0) || 0;
  477. // 本版本不参与服务费:不从订单拉取服务费
  478. // orderInfo.value.serviceFee =
  479. // Number(raw?.serviceFee ?? raw?.serviceCharge ?? raw?.tablewareFee ?? 0) || 0;
  480. orderInfo.value.serviceFee = 0;
  481. const list = raw?.orderItemList ?? raw?.orderItems ?? raw?.items ?? raw?.detailList ?? [];
  482. foodList.value = (Array.isArray(list) ? list : []).map(normalizeOrderItem);
  483. selectedCouponId.value = orderInfo.value.couponId != null && orderInfo.value.couponId !== '' ? String(orderInfo.value.couponId) : null;
  484. updateCheckoutPayAmount();
  485. await fetchServiceFeeEstimate(); // 本版为 no-op(不参与服务费)
  486. } catch (err) {
  487. console.error('获取订单详情失败:', err);
  488. uni.showToast({ title: '加载失败', icon: 'none' });
  489. }
  490. };
  491. const handleConfirmPay = async () => {
  492. const id = orderId.value || '';
  493. if (!id) {
  494. uni.showToast({ title: '缺少订单ID', icon: 'none' });
  495. return;
  496. }
  497. const userStore = useUserStore();
  498. const openid = userStore.getOpenId || '';
  499. if (!openid) {
  500. uni.showToast({ title: '请先登录', icon: 'none' });
  501. return;
  502. }
  503. const payAmountVal = Math.round((Number(orderInfo.value.payAmount ?? 0) || 0) * 100) / 100;
  504. if (payAmountVal <= 0) {
  505. uni.showToast({ title: '订单金额异常', icon: 'none' });
  506. return;
  507. }
  508. const orderNo = orderInfo.value.orderNo || '';
  509. if (!orderNo) {
  510. uni.showToast({ title: '缺少订单号', icon: 'none' });
  511. return;
  512. }
  513. const price = Math.round(payAmountVal * 100);
  514. uni.showLoading({ title: '拉起支付...' });
  515. try {
  516. const storeId = orderInfo.value.storeId || uni.getStorageSync('currentStoreId') || '';
  517. const u = userStore.getUserInfo || {};
  518. const payerId = u.id ?? u.userId ?? u.user_id ?? '';
  519. const couponIdVal =
  520. orderInfo.value.couponId != null && orderInfo.value.couponId !== ''
  521. ? String(orderInfo.value.couponId)
  522. : selectedCouponId.value != null && selectedCouponId.value !== ''
  523. ? String(selectedCouponId.value)
  524. : '';
  525. const discountAmountVal = Number(orderInfo.value.discountAmount) || 0;
  526. // const serviceFeeVal = Math.round((Number(orderInfo.value.serviceFee ?? 0) || 0) * 100) / 100;
  527. const res = await diningApi.PostOrderPay({
  528. orderNo,
  529. payer: openid,
  530. price,
  531. subject: '订单支付',
  532. storeId: storeId || undefined,
  533. couponId: couponIdVal || undefined,
  534. payerId: payerId ? String(payerId) : undefined,
  535. discountAmount: discountAmountVal,
  536. payAmount: payAmountVal
  537. // serviceFee: serviceFeeVal // 本版本不参与服务费
  538. });
  539. uni.hideLoading();
  540. uni.requestPayment({
  541. provider: 'wxpay',
  542. timeStamp: res.timestamp,
  543. nonceStr: res.nonce,
  544. package: res.prepayId,
  545. signType: res.signType,
  546. paySign: res.sign,
  547. success: () => {
  548. uni.showToast({ title: '支付成功', icon: 'success' });
  549. const oid = orderId.value || '';
  550. if (oid) {
  551. diningApi.PostOrderSettlementUnlock({ orderId: oid }).catch((e) => console.warn('解锁订单失败:', e));
  552. }
  553. const payType = 'wechatPayPartnerMininProgram';
  554. // 查询支付结果需用 prePay 返回的 transactionId,与订单号 orderNo 可能不同
  555. const prePayTid =
  556. res?.transactionId ??
  557. res?.transaction_id ??
  558. res?.outTradeNo ??
  559. orderNo;
  560. const sid = orderInfo.value.storeId || uni.getStorageSync('currentStoreId') || '';
  561. const q = [`id=${encodeURIComponent(oid)}`, `payType=${encodeURIComponent(payType)}`, `transactionId=${encodeURIComponent(String(prePayTid ?? ''))}`];
  562. if (sid) q.push(`storeId=${encodeURIComponent(sid)}`);
  563. setTimeout(() => go(`/pages/paymentSuccess/index?${q.join('&')}`), 1500);
  564. },
  565. fail: (err) => {
  566. const msg = err?.errMsg ?? err?.message ?? '支付失败';
  567. if (String(msg).includes('cancel')) {
  568. uni.showToast({ title: '已取消支付', icon: 'none' });
  569. } else {
  570. uni.showToast({ title: msg || '支付失败', icon: 'none' });
  571. }
  572. }
  573. });
  574. } catch (e) {
  575. uni.hideLoading();
  576. uni.showToast({ title: e?.message || '获取支付参数失败', icon: 'none' });
  577. }
  578. };
  579. onLoad(async (options) => {
  580. const id = options?.orderId ?? options?.id ?? '';
  581. orderId.value = id;
  582. if (options?.orderNo) orderInfo.value.orderNo = options.orderNo;
  583. if (options?.tableId) orderInfo.value.tableId = options.tableId;
  584. if (options?.tableNumber) orderInfo.value.tableNumber = options.tableNumber;
  585. if (options?.diners != null && options?.diners !== '') {
  586. orderInfo.value.diners = String(parseDinerCount(options.diners));
  587. uni.setStorageSync('currentDiners', orderInfo.value.diners);
  588. }
  589. if (options?.remark != null && options?.remark !== '') orderInfo.value.remark = decodeURIComponent(options.remark);
  590. if (options?.totalAmount != null && options?.totalAmount !== '') {
  591. orderInfo.value.payAmount = Number(options.totalAmount) || 0;
  592. }
  593. if (id) {
  594. await fetchOrderDetail();
  595. }
  596. });
  597. onShow(() => {
  598. const id = orderId.value || '';
  599. if (id) {
  600. diningApi.PostOrderSettlementLock({ orderId: id }).catch((e) => console.warn('锁定订单失败:', e));
  601. }
  602. });
  603. onUnload(() => {
  604. const id = orderId.value || '';
  605. if (id) {
  606. diningApi.PostOrderSettlementUnlock({ orderId: id }).catch((e) => console.warn('解锁订单失败:', e));
  607. }
  608. });
  609. </script>
  610. <style lang="scss" scoped>
  611. .content {
  612. padding: 0 30rpx 300rpx;
  613. }
  614. .card {
  615. background-color: #fff;
  616. border-radius: 24rpx;
  617. padding: 30rpx 0;
  618. margin-top: 20rpx;
  619. .card-header {
  620. display: flex;
  621. align-items: center;
  622. padding: 0 30rpx;
  623. height: 40rpx;
  624. position: relative;
  625. font-size: 27rpx;
  626. color: #151515;
  627. font-weight: bold;
  628. }
  629. .tag {
  630. width: 10rpx;
  631. height: 42rpx;
  632. background: linear-gradient(35deg, #FCB73F 0%, #FC733D 100%);
  633. border-radius: 0;
  634. position: absolute;
  635. left: 0;
  636. top: 0;
  637. }
  638. .card-content {
  639. padding: 0 30rpx;
  640. }
  641. .info-item {
  642. display: flex;
  643. justify-content: space-between;
  644. align-items: center;
  645. margin-top: 20rpx;
  646. font-size: 27rpx;
  647. .info-item-label {
  648. color: #666666;
  649. }
  650. .info-item-value {
  651. color: #151515;
  652. &.remark-text {
  653. flex: 1;
  654. text-align: right;
  655. word-break: break-all;
  656. }
  657. }
  658. &--coupon .coupon-value {
  659. display: flex;
  660. align-items: center;
  661. gap: 8rpx;
  662. }
  663. .coupon-amount {
  664. color: #E61F19;
  665. }
  666. .coupon-placeholder {
  667. color: #999999;
  668. }
  669. .coupon-arrow {
  670. color: #999;
  671. margin-left: 4rpx;
  672. }
  673. &--clickable {
  674. cursor: pointer;
  675. }
  676. &--diners {
  677. align-items: center;
  678. }
  679. }
  680. /* 用餐人数:胶囊步进器(与设计稿一致) */
  681. .diner-stepper {
  682. display: flex;
  683. flex-direction: row;
  684. align-items: center;
  685. justify-content: space-between;
  686. background: #f2f3f5;
  687. border-radius: 999rpx;
  688. padding: 3rpx 6rpx;
  689. min-width: 196rpx;
  690. box-sizing: border-box;
  691. }
  692. .diner-stepper__btn {
  693. width: 44rpx;
  694. height: 44rpx;
  695. border-radius: 50%;
  696. background: #ffffff;
  697. display: flex;
  698. align-items: center;
  699. justify-content: center;
  700. flex-shrink: 0;
  701. box-shadow: 0 1rpx 6rpx rgba(0, 0, 0, 0.06);
  702. }
  703. .diner-stepper__btn--active:active {
  704. opacity: 0.88;
  705. }
  706. .diner-stepper__btn--disabled {
  707. opacity: 0.35;
  708. pointer-events: none;
  709. }
  710. .diner-stepper__value {
  711. flex: 1;
  712. text-align: center;
  713. font-size: 26rpx;
  714. font-weight: 500;
  715. color: #151515;
  716. line-height: 44rpx;
  717. min-width: 36rpx;
  718. }
  719. .diner-stepper__icon--minus {
  720. width: 20rpx;
  721. height: 2rpx;
  722. background: #c8c8c8;
  723. border-radius: 2rpx;
  724. }
  725. .diner-stepper__icon--plus {
  726. position: relative;
  727. width: 18rpx;
  728. height: 18rpx;
  729. }
  730. .diner-stepper__icon--plus::before,
  731. .diner-stepper__icon--plus::after {
  732. content: '';
  733. position: absolute;
  734. left: 50%;
  735. top: 50%;
  736. background: #fc743d;
  737. border-radius: 2rpx;
  738. }
  739. .diner-stepper__icon--plus::before {
  740. width: 18rpx;
  741. height: 2rpx;
  742. transform: translate(-50%, -50%);
  743. }
  744. .diner-stepper__icon--plus::after {
  745. width: 2rpx;
  746. height: 18rpx;
  747. transform: translate(-50%, -50%);
  748. }
  749. .price-line {
  750. display: flex;
  751. justify-content: space-between;
  752. align-items: center;
  753. margin-top: 24rpx;
  754. padding-top: 24rpx;
  755. border-top: 1rpx solid #f0f0f0;
  756. font-size: 28rpx;
  757. font-weight: bold;
  758. .price-line-label {
  759. color: #151515;
  760. }
  761. .price-line-value {
  762. color: #E61F19;
  763. }
  764. }
  765. .info-food {
  766. .food-item {
  767. display: flex;
  768. align-items: center;
  769. padding: 20rpx 0;
  770. border-bottom: 1rpx solid #f5f5f5;
  771. &:last-child {
  772. border-bottom: none;
  773. }
  774. &__image {
  775. width: 120rpx;
  776. height: 120rpx;
  777. border-radius: 12rpx;
  778. flex-shrink: 0;
  779. background: #f5f5f5;
  780. }
  781. &__info {
  782. flex: 1;
  783. margin-left: 24rpx;
  784. min-width: 0;
  785. }
  786. &__name {
  787. font-size: 28rpx;
  788. color: #151515;
  789. font-weight: 500;
  790. }
  791. &__desc {
  792. font-size: 22rpx;
  793. color: #999;
  794. margin-top: 6rpx;
  795. }
  796. &__tag {
  797. margin-right: 4rpx;
  798. &.signature {
  799. color: #FC793D;
  800. }
  801. &.spicy {
  802. color: #2E2E2E;
  803. }
  804. }
  805. &__right {
  806. flex-shrink: 0;
  807. text-align: right;
  808. }
  809. &__price .price-main {
  810. font-size: 28rpx;
  811. color: #151515;
  812. font-weight: 600;
  813. }
  814. &__quantity {
  815. font-size: 24rpx;
  816. color: #999;
  817. margin-top: 6rpx;
  818. }
  819. }
  820. }
  821. }
  822. .bottom-button {
  823. position: fixed;
  824. left: 0;
  825. right: 0;
  826. bottom: 0;
  827. padding: 20rpx 30rpx;
  828. padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
  829. background: #fff;
  830. box-shadow: 0 -2rpx 12rpx rgba(0, 0, 0, 0.06);
  831. .bottom-button-text {
  832. height: 88rpx;
  833. line-height: 88rpx;
  834. text-align: center;
  835. background: linear-gradient(90deg, #FCB73F 0%, #FC733D 100%);
  836. color: #fff;
  837. font-size: 32rpx;
  838. font-weight: bold;
  839. border-radius: 44rpx;
  840. }
  841. .hover-active {
  842. opacity: 0.9;
  843. }
  844. }
  845. .coupon-modal-wrapper {
  846. position: relative;
  847. z-index: 99999;
  848. :deep(.uni-popup) {
  849. z-index: 99999 !important;
  850. top: 0 !important;
  851. left: 0 !important;
  852. right: 0 !important;
  853. bottom: 0 !important;
  854. }
  855. :deep(.uni-popup__wrapper) {
  856. z-index: 99999 !important;
  857. }
  858. :deep(.select-coupon-modal__list) {
  859. padding-bottom: calc(60rpx + env(safe-area-inset-bottom));
  860. }
  861. :deep(.select-coupon-modal__empty) {
  862. margin-bottom: 60rpx;
  863. }
  864. }
  865. </style>