index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. {{ 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. function normalizeOrderItem(item) {
  154. const tags = item?.tags ?? [];
  155. const tagArr = Array.isArray(tags) ? tags : [];
  156. const images = item?.images ?? item?.image;
  157. const imageUrl = Array.isArray(images) ? images[0] : images;
  158. return {
  159. id: item?.id ?? item?.cuisineId,
  160. name: item?.cuisineName ?? item?.name ?? '',
  161. price: item?.totalPrice ?? item?.unitPrice ?? item?.price ?? 0,
  162. image: imageUrl ?? item?.image ?? item?.cuisineImage ?? '',
  163. quantity: item?.quantity ?? 1,
  164. tags: tagArr.map((t) => (typeof t === 'string' ? { text: t } : { text: t?.text ?? t?.tagName ?? '' }))
  165. };
  166. }
  167. const fetchOrderDetail = async () => {
  168. const id = orderId.value || '';
  169. if (!id) return;
  170. try {
  171. const res = await diningApi.GetOrderInfo(id);
  172. const raw = res?.data ?? res ?? {};
  173. orderInfo.value.orderNo = raw?.orderNo ?? raw?.orderId ?? '';
  174. orderInfo.value.storeId = raw?.storeId ?? raw?.store_id ?? '';
  175. orderInfo.value.tableId = raw?.tableId ?? raw?.tableNumber ?? '';
  176. orderInfo.value.tableNumber = raw?.tableNumber ?? raw?.tableNo ?? '';
  177. orderInfo.value.diners = raw?.dinerCount ?? '';
  178. orderInfo.value.contactPhone = raw?.contactPhone ?? raw?.phone ?? '';
  179. orderInfo.value.remark = raw?.remark ?? '';
  180. const total = Number(raw?.totalAmount ?? raw?.orderAmount ?? raw?.foodAmount ?? 0) || 0;
  181. orderInfo.value.totalAmount = total;
  182. orderInfo.value.dishTotal = raw?.dishTotal != null ? Number(raw.dishTotal) : null;
  183. orderInfo.value.utensilFee = Number(raw?.tablewareFee ?? raw?.tablewareAmount ?? 0) || 0;
  184. orderInfo.value.couponId = raw?.couponId ?? null;
  185. orderInfo.value.couponName = raw?.couponName ?? '';
  186. orderInfo.value.couponType = raw?.couponType ?? null;
  187. orderInfo.value.discountRate = raw?.discountRate ?? null;
  188. orderInfo.value.nominalValue = raw?.nominalValue ?? null;
  189. orderInfo.value.discountAmount = Number(raw?.discountAmount ?? raw?.couponAmount ?? raw?.couponDiscount ?? 0) || 0;
  190. orderInfo.value.payAmount = Number(raw?.payAmount ?? raw?.totalAmount ?? raw?.totalPrice ?? 0) || 0;
  191. const list = raw?.orderItemList ?? raw?.orderItems ?? raw?.items ?? raw?.detailList ?? [];
  192. foodList.value = (Array.isArray(list) ? list : []).map(normalizeOrderItem);
  193. } catch (err) {
  194. console.error('获取订单详情失败:', err);
  195. uni.showToast({ title: '加载失败', icon: 'none' });
  196. }
  197. };
  198. const handleConfirmPay = async () => {
  199. const id = orderId.value || '';
  200. if (!id) {
  201. uni.showToast({ title: '缺少订单ID', icon: 'none' });
  202. return;
  203. }
  204. const userStore = useUserStore();
  205. const openid = userStore.getOpenId || '';
  206. if (!openid) {
  207. uni.showToast({ title: '请先登录', icon: 'none' });
  208. return;
  209. }
  210. const priceAmount = Number(orderInfo.value.payAmount ?? 0) || 0;
  211. if (priceAmount <= 0) {
  212. uni.showToast({ title: '订单金额异常', icon: 'none' });
  213. return;
  214. }
  215. const orderNo = orderInfo.value.orderNo || '';
  216. if (!orderNo) {
  217. uni.showToast({ title: '缺少订单号', icon: 'none' });
  218. return;
  219. }
  220. const price = Math.round(priceAmount * 100);
  221. uni.showLoading({ title: '拉起支付...' });
  222. try {
  223. const storeId = orderInfo.value.storeId || uni.getStorageSync('currentStoreId') || '';
  224. const res = await diningApi.PostOrderPay({
  225. orderNo,
  226. payer: openid,
  227. price,
  228. subject: '订单支付',
  229. storeId: storeId || undefined
  230. });
  231. uni.hideLoading();
  232. uni.requestPayment({
  233. provider: 'wxpay',
  234. timeStamp: res.timestamp,
  235. nonceStr: res.nonce,
  236. package: res.prepayId,
  237. signType: res.signType,
  238. paySign: res.sign,
  239. success: () => {
  240. uni.showToast({ title: '支付成功', icon: 'success' });
  241. const oid = orderId.value || '';
  242. if (oid) {
  243. diningApi.PostOrderSettlementUnlock({ orderId: oid }).catch((e) => console.warn('解锁订单失败:', e));
  244. }
  245. const payType = 'wechatPayMininProgram';
  246. const transactionId = orderNo;
  247. const sid = orderInfo.value.storeId || uni.getStorageSync('currentStoreId') || '';
  248. const q = [`id=${encodeURIComponent(oid)}`, `payType=${encodeURIComponent(payType)}`, `transactionId=${encodeURIComponent(transactionId)}`];
  249. if (sid) q.push(`storeId=${encodeURIComponent(sid)}`);
  250. setTimeout(() => go(`/pages/paymentSuccess/index?${q.join('&')}`), 1500);
  251. },
  252. fail: (err) => {
  253. const msg = err?.errMsg ?? err?.message ?? '支付失败';
  254. if (String(msg).includes('cancel')) {
  255. uni.showToast({ title: '已取消支付', icon: 'none' });
  256. } else {
  257. uni.showToast({ title: msg || '支付失败', icon: 'none' });
  258. }
  259. }
  260. });
  261. } catch (e) {
  262. uni.hideLoading();
  263. uni.showToast({ title: e?.message || '获取支付参数失败', icon: 'none' });
  264. }
  265. };
  266. onLoad(async (options) => {
  267. const id = options?.orderId ?? options?.id ?? '';
  268. orderId.value = id;
  269. if (options?.orderNo) orderInfo.value.orderNo = options.orderNo;
  270. if (options?.tableId) orderInfo.value.tableId = options.tableId;
  271. if (options?.tableNumber) orderInfo.value.tableNumber = options.tableNumber;
  272. if (options?.diners) orderInfo.value.diners = options.diners;
  273. if (options?.remark != null && options?.remark !== '') orderInfo.value.remark = decodeURIComponent(options.remark);
  274. if (options?.totalAmount != null && options?.totalAmount !== '') {
  275. orderInfo.value.payAmount = Number(options.totalAmount) || 0;
  276. }
  277. if (id) {
  278. await fetchOrderDetail();
  279. }
  280. });
  281. onShow(() => {
  282. const id = orderId.value || '';
  283. if (id) {
  284. diningApi.PostOrderSettlementLock({ orderId: id }).catch((e) => console.warn('锁定订单失败:', e));
  285. }
  286. });
  287. onUnload(() => {
  288. const id = orderId.value || '';
  289. if (id) {
  290. diningApi.PostOrderSettlementUnlock({ orderId: id }).catch((e) => console.warn('解锁订单失败:', e));
  291. }
  292. });
  293. </script>
  294. <style lang="scss" scoped>
  295. .content {
  296. padding: 0 30rpx 300rpx;
  297. }
  298. .card {
  299. background-color: #fff;
  300. border-radius: 24rpx;
  301. padding: 30rpx 0;
  302. margin-top: 20rpx;
  303. .card-header {
  304. display: flex;
  305. align-items: center;
  306. padding: 0 30rpx;
  307. height: 40rpx;
  308. position: relative;
  309. font-size: 27rpx;
  310. color: #151515;
  311. font-weight: bold;
  312. }
  313. .tag {
  314. width: 10rpx;
  315. height: 42rpx;
  316. background: linear-gradient(35deg, #FCB73F 0%, #FC733D 100%);
  317. border-radius: 0;
  318. position: absolute;
  319. left: 0;
  320. top: 0;
  321. }
  322. .card-content {
  323. padding: 0 30rpx;
  324. }
  325. .info-item {
  326. display: flex;
  327. justify-content: space-between;
  328. align-items: center;
  329. margin-top: 20rpx;
  330. font-size: 27rpx;
  331. .info-item-label {
  332. color: #666666;
  333. }
  334. .info-item-value {
  335. color: #151515;
  336. &.remark-text {
  337. flex: 1;
  338. text-align: right;
  339. word-break: break-all;
  340. }
  341. }
  342. &--coupon .coupon-value {
  343. display: flex;
  344. align-items: center;
  345. gap: 8rpx;
  346. }
  347. .coupon-amount {
  348. color: #E61F19;
  349. }
  350. .coupon-placeholder {
  351. color: #999999;
  352. }
  353. }
  354. .price-line {
  355. display: flex;
  356. justify-content: space-between;
  357. align-items: center;
  358. margin-top: 24rpx;
  359. padding-top: 24rpx;
  360. border-top: 1rpx solid #f0f0f0;
  361. font-size: 28rpx;
  362. font-weight: bold;
  363. .price-line-label {
  364. color: #151515;
  365. }
  366. .price-line-value {
  367. color: #E61F19;
  368. }
  369. }
  370. .info-food {
  371. .food-item {
  372. display: flex;
  373. align-items: center;
  374. padding: 20rpx 0;
  375. border-bottom: 1rpx solid #f5f5f5;
  376. &:last-child {
  377. border-bottom: none;
  378. }
  379. &__image {
  380. width: 120rpx;
  381. height: 120rpx;
  382. border-radius: 12rpx;
  383. flex-shrink: 0;
  384. background: #f5f5f5;
  385. }
  386. &__info {
  387. flex: 1;
  388. margin-left: 24rpx;
  389. min-width: 0;
  390. }
  391. &__name {
  392. font-size: 28rpx;
  393. color: #151515;
  394. font-weight: 500;
  395. }
  396. &__desc {
  397. font-size: 22rpx;
  398. color: #999;
  399. margin-top: 6rpx;
  400. }
  401. &__tag {
  402. margin-right: 4rpx;
  403. }
  404. &__right {
  405. flex-shrink: 0;
  406. text-align: right;
  407. }
  408. &__price .price-main {
  409. font-size: 28rpx;
  410. color: #151515;
  411. font-weight: 600;
  412. }
  413. &__quantity {
  414. font-size: 24rpx;
  415. color: #999;
  416. margin-top: 6rpx;
  417. }
  418. }
  419. }
  420. }
  421. .bottom-button {
  422. position: fixed;
  423. left: 0;
  424. right: 0;
  425. bottom: 0;
  426. padding: 20rpx 30rpx;
  427. padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
  428. background: #fff;
  429. box-shadow: 0 -2rpx 12rpx rgba(0, 0, 0, 0.06);
  430. .bottom-button-text {
  431. height: 88rpx;
  432. line-height: 88rpx;
  433. text-align: center;
  434. background: linear-gradient(90deg, #FCB73F 0%, #FC733D 100%);
  435. color: #fff;
  436. font-size: 32rpx;
  437. font-weight: bold;
  438. border-radius: 44rpx;
  439. }
  440. .hover-active {
  441. opacity: 0.9;
  442. }
  443. }
  444. </style>