index.vue 14 KB

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