index.vue 31 KB

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