index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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">
  15. <view class="info-item-label">用餐人数</view>
  16. <view class="info-item-value">{{ orderInfo.diners || '—' }}人</view>
  17. </view>
  18. <view class="info-item">
  19. <view class="info-item-label">联系电话</view>
  20. <view class="info-item-value">{{ orderInfo.contactPhone || '—' }}</view>
  21. </view>
  22. <view class="info-item">
  23. <view class="info-item-label">备注信息</view>
  24. <view class="info-item-value remark-text">{{ orderInfo.remark || '—' }}</view>
  25. </view>
  26. </view>
  27. </view>
  28. <view class="card" v-if="displayFoodList.length > 0">
  29. <view class="card-header">
  30. <view class="tag"></view>
  31. <view class="card-header-title">菜品清单</view>
  32. </view>
  33. <view class="card-content">
  34. <view class="info-food">
  35. <view v-for="(item, index) in displayFoodList" :key="item.id || index" class="food-item">
  36. <image :src="getItemImage(item)" mode="aspectFill" class="food-item__image"></image>
  37. <view class="food-item__info">
  38. <view class="food-item__name">{{ item.name }}</view>
  39. <view class="food-item__desc" v-if="item.tags && item.tags.length > 0">
  40. <text v-for="(tag, tagIndex) in item.tags" :key="tagIndex" class="food-item__tag"
  41. :class="tag.type">{{ tag.text }}<text v-if="tagIndex < item.tags.length - 1">,</text>
  42. </text>
  43. </view>
  44. </view>
  45. <view class="food-item__right">
  46. <view class="food-item__price">
  47. <text class="price-main">¥{{ formatPrice(item.price) }}</text>
  48. </view>
  49. <view class="food-item__quantity">{{ item.quantity || 1 }}份</view>
  50. </view>
  51. </view>
  52. </view>
  53. </view>
  54. </view>
  55. <view class="card">
  56. <view class="card-header">
  57. <view class="tag"></view>
  58. <view class="card-header-title">价格明细</view>
  59. </view>
  60. <view class="card-content">
  61. <view class="info-item">
  62. <view class="info-item-label">菜品总价</view>
  63. <view class="info-item-value">¥{{ formatPrice(dishTotal) }}</view>
  64. </view>
  65. <view class="info-item">
  66. <view class="info-item-label">餐具费</view>
  67. <view class="info-item-value">¥{{ formatPrice(orderInfo.utensilFee ?? 0) }}</view>
  68. </view>
  69. <view class="info-item info-item--coupon">
  70. <view class="info-item-label">优惠券</view>
  71. <view class="info-item-value coupon-value">
  72. <text v-if="(orderInfo.discountAmount ?? 0) > 0" class="coupon-amount">{{ couponDisplayText }}</text>
  73. <text v-else class="coupon-placeholder">—</text>
  74. </view>
  75. </view>
  76. <view v-if="(orderInfo.discountAmount ?? 0) > 0" class="info-item info-item--coupon">
  77. <view class="info-item-label">优惠金额</view>
  78. <view class="info-item-value coupon-value">
  79. <text class="coupon-amount">-¥{{ formatPrice(orderInfo.discountAmount) }}</text>
  80. </view>
  81. </view>
  82. <view class="price-line">
  83. <view class="price-line-label">应付金额</view>
  84. <view class="price-line-value">¥{{ formatPrice(orderInfo.payAmount ?? 0) }}</view>
  85. </view>
  86. </view>
  87. </view>
  88. <view class="bottom-button">
  89. <view class="bottom-button-text" hover-class="hover-active" @click="handleConfirmPay">确认支付 ¥{{ formatPrice(orderInfo.payAmount ?? 0) }}</view>
  90. </view>
  91. </view>
  92. </template>
  93. <script setup>
  94. import { onLoad, onShow, onUnload } from '@dcloudio/uni-app';
  95. import { ref, computed } from 'vue';
  96. import { go } from '@/utils/utils.js';
  97. import { getFileUrl } from '@/utils/file.js';
  98. import { useUserStore } from '@/store/user.js';
  99. import * as diningApi from '@/api/dining.js';
  100. const orderId = ref('');
  101. const orderInfo = ref({
  102. orderNo: '',
  103. storeId: '',
  104. tableId: '',
  105. tableNumber: '',
  106. diners: '',
  107. contactPhone: '',
  108. remark: '',
  109. totalAmount: 0,
  110. dishTotal: null,
  111. utensilFee: 0,
  112. couponId: null,
  113. couponName: '',
  114. couponType: null, // 1 满减 2 折扣
  115. discountRate: null, // 折扣券力度,如 5.5 表示 5.5折
  116. nominalValue: null, // 满减券面额
  117. discountAmount: 0,
  118. payAmount: 0
  119. });
  120. const foodList = ref([]);
  121. // 菜品清单展示:包含所有项(含餐具 id/cuisineId === -1)
  122. const displayFoodList = computed(() => foodList.value ?? []);
  123. // 菜品总价:绑定 totalAmount
  124. const dishTotal = computed(() => {
  125. const total = Number(orderInfo.value.totalAmount) || 0;
  126. return Math.max(0, total);
  127. });
  128. // 优惠券展示:满减券显示 nominalValue+元,折扣券显示 discountRate+折,否则显示 couponName 或 已使用优惠券
  129. const couponDisplayText = computed(() => {
  130. const o = orderInfo.value;
  131. if ((o.discountAmount ?? 0) <= 0) return '';
  132. const type = Number(o.couponType);
  133. if (type === 1 && (o.nominalValue != null && o.nominalValue !== '')) {
  134. const val = Number(o.nominalValue);
  135. return Number.isNaN(val) ? (o.couponName || '已使用优惠券') : val + '元';
  136. }
  137. if (type === 2 && (o.discountRate != null && o.discountRate !== '')) {
  138. const rate = Number(o.discountRate);
  139. return Number.isNaN(rate) ? (o.couponName || '已使用优惠券') : rate + '折';
  140. }
  141. return o.couponName || '已使用优惠券';
  142. });
  143. function formatPrice(price) {
  144. const num = Number(price);
  145. return Number.isNaN(num) ? '0.00' : num.toFixed(2);
  146. }
  147. function getItemImage(item) {
  148. if (Number(item?.id ?? item?.cuisineId) === -1) return '/static/utensilFee.png';
  149. const raw = item?.image ?? item?.cuisineImage ?? item?.imageUrl ?? '';
  150. if (typeof raw === 'string' && (raw.startsWith('http') || raw.startsWith('//'))) return raw;
  151. return getFileUrl(raw || 'img/icon/shop.png');
  152. }
  153. // 将 tags 统一为 [{ text, type }] 格式
  154. function normalizeTags(raw) {
  155. if (raw == null) return [];
  156. let arr = [];
  157. if (Array.isArray(raw)) arr = raw;
  158. else if (typeof raw === 'string') {
  159. const t = raw.trim();
  160. if (t.startsWith('[')) { try { arr = JSON.parse(t); if (!Array.isArray(arr)) arr = []; } catch { arr = t ? [t] : []; } }
  161. else arr = t ? t.split(/[,,、\s]+/).map(s => s.trim()).filter(Boolean) : [];
  162. } else if (raw && typeof raw === 'object') arr = Array.isArray(raw.list) ? raw.list : Array.isArray(raw.items) ? raw.items : [];
  163. return arr.map((it) => {
  164. if (it == null) return { text: '', type: '' };
  165. if (typeof it === 'string') return { text: it, type: '' };
  166. if (typeof it === 'number') return { text: String(it), type: '' };
  167. return { text: it.text ?? it.tagName ?? it.name ?? it.label ?? it.title ?? '', type: it.type ?? it.tagType ?? '' };
  168. }).filter((t) => t.text !== '' && t.text != null);
  169. }
  170. function normalizeOrderItem(item) {
  171. const rawTags = item?.tags ?? item?.tagList ?? item?.tagNames ?? item?.labels ?? item?.tag;
  172. const images = item?.images ?? item?.image;
  173. const imageUrl = Array.isArray(images) ? images[0] : images;
  174. return {
  175. id: item?.id ?? item?.cuisineId,
  176. name: item?.cuisineName ?? item?.name ?? '',
  177. price: item?.totalPrice ?? item?.unitPrice ?? item?.price ?? 0,
  178. image: imageUrl ?? item?.image ?? item?.cuisineImage ?? '',
  179. quantity: item?.quantity ?? 1,
  180. tags: normalizeTags(rawTags)
  181. };
  182. }
  183. const fetchOrderDetail = async () => {
  184. const id = orderId.value || '';
  185. if (!id) return;
  186. try {
  187. const res = await diningApi.GetOrderInfo(id);
  188. const raw = res?.data ?? res ?? {};
  189. orderInfo.value.orderNo = raw?.orderNo ?? raw?.orderId ?? '';
  190. orderInfo.value.storeId = raw?.storeId ?? raw?.store_id ?? '';
  191. orderInfo.value.tableId = raw?.tableId ?? raw?.tableNumber ?? '';
  192. orderInfo.value.tableNumber = raw?.tableNumber ?? raw?.tableNo ?? '';
  193. orderInfo.value.diners = raw?.dinerCount ?? '';
  194. orderInfo.value.contactPhone = raw?.contactPhone ?? raw?.phone ?? '';
  195. orderInfo.value.remark = raw?.remark ?? '';
  196. const total = Number(raw?.totalAmount ?? raw?.orderAmount ?? raw?.foodAmount ?? 0) || 0;
  197. orderInfo.value.totalAmount = total;
  198. orderInfo.value.dishTotal = raw?.dishTotal != null ? Number(raw.dishTotal) : null;
  199. orderInfo.value.utensilFee = Number(raw?.tablewareFee ?? raw?.tablewareAmount ?? 0) || 0;
  200. orderInfo.value.couponId = raw?.couponId ?? null;
  201. orderInfo.value.couponName = raw?.couponName ?? '';
  202. orderInfo.value.couponType = raw?.couponType ?? null;
  203. const rawRate = raw?.discountRate;
  204. orderInfo.value.discountRate = rawRate != null && rawRate !== '' ? (Number(rawRate) || 0) / 10 : null;
  205. orderInfo.value.nominalValue = raw?.nominalValue ?? null;
  206. orderInfo.value.discountAmount = Number(raw?.discountAmount ?? raw?.couponAmount ?? raw?.couponDiscount ?? 0) || 0;
  207. orderInfo.value.payAmount = Number(raw?.payAmount ?? raw?.totalAmount ?? raw?.totalPrice ?? 0) || 0;
  208. const list = raw?.orderItemList ?? raw?.orderItems ?? raw?.items ?? raw?.detailList ?? [];
  209. foodList.value = (Array.isArray(list) ? list : []).map(normalizeOrderItem);
  210. } catch (err) {
  211. console.error('获取订单详情失败:', err);
  212. uni.showToast({ title: '加载失败', icon: 'none' });
  213. }
  214. };
  215. const handleConfirmPay = async () => {
  216. const id = orderId.value || '';
  217. if (!id) {
  218. uni.showToast({ title: '缺少订单ID', icon: 'none' });
  219. return;
  220. }
  221. const userStore = useUserStore();
  222. const openid = userStore.getOpenId || '';
  223. if (!openid) {
  224. uni.showToast({ title: '请先登录', icon: 'none' });
  225. return;
  226. }
  227. const priceAmount = Number(orderInfo.value.payAmount ?? 0) || 0;
  228. if (priceAmount <= 0) {
  229. uni.showToast({ title: '订单金额异常', icon: 'none' });
  230. return;
  231. }
  232. const orderNo = orderInfo.value.orderNo || '';
  233. if (!orderNo) {
  234. uni.showToast({ title: '缺少订单号', icon: 'none' });
  235. return;
  236. }
  237. const price = Math.round(priceAmount * 100);
  238. uni.showLoading({ title: '拉起支付...' });
  239. try {
  240. const storeId = orderInfo.value.storeId || uni.getStorageSync('currentStoreId') || '';
  241. const res = await diningApi.PostOrderPay({
  242. orderNo,
  243. payer: openid,
  244. price,
  245. subject: '订单支付',
  246. storeId: storeId || undefined
  247. });
  248. uni.hideLoading();
  249. uni.requestPayment({
  250. provider: 'wxpay',
  251. timeStamp: res.timestamp,
  252. nonceStr: res.nonce,
  253. package: res.prepayId,
  254. signType: res.signType,
  255. paySign: res.sign,
  256. success: () => {
  257. uni.showToast({ title: '支付成功', icon: 'success' });
  258. const oid = orderId.value || '';
  259. if (oid) {
  260. diningApi.PostOrderSettlementUnlock({ orderId: oid }).catch((e) => console.warn('解锁订单失败:', e));
  261. }
  262. const payType = 'wechatPayMininProgram';
  263. const transactionId = orderNo;
  264. const sid = orderInfo.value.storeId || uni.getStorageSync('currentStoreId') || '';
  265. const q = [`id=${encodeURIComponent(oid)}`, `payType=${encodeURIComponent(payType)}`, `transactionId=${encodeURIComponent(transactionId)}`];
  266. if (sid) q.push(`storeId=${encodeURIComponent(sid)}`);
  267. setTimeout(() => go(`/pages/paymentSuccess/index?${q.join('&')}`), 1500);
  268. },
  269. fail: (err) => {
  270. const msg = err?.errMsg ?? err?.message ?? '支付失败';
  271. if (String(msg).includes('cancel')) {
  272. uni.showToast({ title: '已取消支付', icon: 'none' });
  273. } else {
  274. uni.showToast({ title: msg || '支付失败', icon: 'none' });
  275. }
  276. }
  277. });
  278. } catch (e) {
  279. uni.hideLoading();
  280. uni.showToast({ title: e?.message || '获取支付参数失败', icon: 'none' });
  281. }
  282. };
  283. onLoad(async (options) => {
  284. const id = options?.orderId ?? options?.id ?? '';
  285. orderId.value = id;
  286. if (options?.orderNo) orderInfo.value.orderNo = options.orderNo;
  287. if (options?.tableId) orderInfo.value.tableId = options.tableId;
  288. if (options?.tableNumber) orderInfo.value.tableNumber = options.tableNumber;
  289. if (options?.diners) orderInfo.value.diners = options.diners;
  290. if (options?.remark != null && options?.remark !== '') orderInfo.value.remark = decodeURIComponent(options.remark);
  291. if (options?.totalAmount != null && options?.totalAmount !== '') {
  292. orderInfo.value.payAmount = Number(options.totalAmount) || 0;
  293. }
  294. if (id) {
  295. await fetchOrderDetail();
  296. }
  297. });
  298. onShow(() => {
  299. const id = orderId.value || '';
  300. if (id) {
  301. diningApi.PostOrderSettlementLock({ orderId: id }).catch((e) => console.warn('锁定订单失败:', e));
  302. }
  303. });
  304. onUnload(() => {
  305. const id = orderId.value || '';
  306. if (id) {
  307. diningApi.PostOrderSettlementUnlock({ orderId: id }).catch((e) => console.warn('解锁订单失败:', e));
  308. }
  309. });
  310. </script>
  311. <style lang="scss" scoped>
  312. .content {
  313. padding: 0 30rpx 300rpx;
  314. }
  315. .card {
  316. background-color: #fff;
  317. border-radius: 24rpx;
  318. padding: 30rpx 0;
  319. margin-top: 20rpx;
  320. .card-header {
  321. display: flex;
  322. align-items: center;
  323. padding: 0 30rpx;
  324. height: 40rpx;
  325. position: relative;
  326. font-size: 27rpx;
  327. color: #151515;
  328. font-weight: bold;
  329. }
  330. .tag {
  331. width: 10rpx;
  332. height: 42rpx;
  333. background: linear-gradient(35deg, #FCB73F 0%, #FC733D 100%);
  334. border-radius: 0;
  335. position: absolute;
  336. left: 0;
  337. top: 0;
  338. }
  339. .card-content {
  340. padding: 0 30rpx;
  341. }
  342. .info-item {
  343. display: flex;
  344. justify-content: space-between;
  345. align-items: center;
  346. margin-top: 20rpx;
  347. font-size: 27rpx;
  348. .info-item-label {
  349. color: #666666;
  350. }
  351. .info-item-value {
  352. color: #151515;
  353. &.remark-text {
  354. flex: 1;
  355. text-align: right;
  356. word-break: break-all;
  357. }
  358. }
  359. &--coupon .coupon-value {
  360. display: flex;
  361. align-items: center;
  362. gap: 8rpx;
  363. }
  364. .coupon-amount {
  365. color: #E61F19;
  366. }
  367. .coupon-placeholder {
  368. color: #999999;
  369. }
  370. }
  371. .price-line {
  372. display: flex;
  373. justify-content: space-between;
  374. align-items: center;
  375. margin-top: 24rpx;
  376. padding-top: 24rpx;
  377. border-top: 1rpx solid #f0f0f0;
  378. font-size: 28rpx;
  379. font-weight: bold;
  380. .price-line-label {
  381. color: #151515;
  382. }
  383. .price-line-value {
  384. color: #E61F19;
  385. }
  386. }
  387. .info-food {
  388. .food-item {
  389. display: flex;
  390. align-items: center;
  391. padding: 20rpx 0;
  392. border-bottom: 1rpx solid #f5f5f5;
  393. &:last-child {
  394. border-bottom: none;
  395. }
  396. &__image {
  397. width: 120rpx;
  398. height: 120rpx;
  399. border-radius: 12rpx;
  400. flex-shrink: 0;
  401. background: #f5f5f5;
  402. }
  403. &__info {
  404. flex: 1;
  405. margin-left: 24rpx;
  406. min-width: 0;
  407. }
  408. &__name {
  409. font-size: 28rpx;
  410. color: #151515;
  411. font-weight: 500;
  412. }
  413. &__desc {
  414. font-size: 22rpx;
  415. color: #999;
  416. margin-top: 6rpx;
  417. }
  418. &__tag {
  419. margin-right: 4rpx;
  420. &.signature {
  421. color: #FC793D;
  422. }
  423. &.spicy {
  424. color: #2E2E2E;
  425. }
  426. }
  427. &__right {
  428. flex-shrink: 0;
  429. text-align: right;
  430. }
  431. &__price .price-main {
  432. font-size: 28rpx;
  433. color: #151515;
  434. font-weight: 600;
  435. }
  436. &__quantity {
  437. font-size: 24rpx;
  438. color: #999;
  439. margin-top: 6rpx;
  440. }
  441. }
  442. }
  443. }
  444. .bottom-button {
  445. position: fixed;
  446. left: 0;
  447. right: 0;
  448. bottom: 0;
  449. padding: 20rpx 30rpx;
  450. padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
  451. background: #fff;
  452. box-shadow: 0 -2rpx 12rpx rgba(0, 0, 0, 0.06);
  453. .bottom-button-text {
  454. height: 88rpx;
  455. line-height: 88rpx;
  456. text-align: center;
  457. background: linear-gradient(90deg, #FCB73F 0%, #FC733D 100%);
  458. color: #fff;
  459. font-size: 32rpx;
  460. font-weight: bold;
  461. border-radius: 44rpx;
  462. }
  463. .hover-active {
  464. opacity: 0.9;
  465. }
  466. }
  467. </style>