index.vue 31 KB

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