|
|
@@ -110,14 +110,17 @@ import { ref } from "vue";
|
|
|
import { go } from "@/utils/utils.js";
|
|
|
import { getFileUrl } from "@/utils/file.js";
|
|
|
import { useUserStore } from "@/store/user.js";
|
|
|
-import { PostOrderCreate } from "@/api/dining.js";
|
|
|
+import { PostOrderCreate, PostOrderPay } from "@/api/dining.js";
|
|
|
|
|
|
const payType = ref('confirmOrder'); // confirmOrder 确认下单 confirmPay 确认支付
|
|
|
-/** 仅当从订单列表点击「去结算」进入时为 true,底部确认下单按钮才显示金额 */
|
|
|
+/** 仅当从订单列表/结果页等「去结算」进入时为 true,底部显示确认支付 */
|
|
|
const fromCheckout = ref(false);
|
|
|
+/** 待支付订单ID,确认支付时用于调起微信支付 */
|
|
|
+const payOrderId = ref('');
|
|
|
|
|
|
// 订单信息(从购物车带过来或 URL 参数)
|
|
|
const orderInfo = ref({
|
|
|
+ orderNo: '',
|
|
|
tableId: '',
|
|
|
diners: '',
|
|
|
contactPhone: '',
|
|
|
@@ -192,9 +195,12 @@ const handleConfirmOrder = async () => {
|
|
|
});
|
|
|
uni.hideLoading();
|
|
|
const orderId = res?.id ?? res?.orderId ?? res?.data?.id ?? res?.data?.orderId;
|
|
|
- // 结果页「去结算」需带金额和备注,保存本次下单的桌号、人数、金额、备注
|
|
|
+ const orderNo = res?.orderNo ?? res?.data?.orderNo ?? orderId ?? '';
|
|
|
const total = orderInfo.value.payAmount ?? orderInfo.value.totalAmount ?? 0;
|
|
|
+ // 结果页「去结算」需带 orderId/orderNo/金额/备注,用于确认支付
|
|
|
uni.setStorageSync('lastPlaceOrderInfo', JSON.stringify({
|
|
|
+ orderId: orderId ?? undefined,
|
|
|
+ orderNo: orderNo ?? undefined,
|
|
|
tableId: orderInfo.value.tableId,
|
|
|
diners: orderInfo.value.diners,
|
|
|
totalAmount: total,
|
|
|
@@ -211,8 +217,65 @@ const handleConfirmOrder = async () => {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
-const handleConfirmPay = () => {
|
|
|
- go('/pages/result/index?payType=confirmPay');
|
|
|
+// 确认支付:调起微信支付
|
|
|
+const handleConfirmPay = async () => {
|
|
|
+ const orderId = payOrderId.value || '';
|
|
|
+ if (!orderId) {
|
|
|
+ uni.showToast({ title: '缺少订单ID', icon: 'none' });
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ const userStore = useUserStore();
|
|
|
+ const openid = userStore.getOpenId || '';
|
|
|
+ if (!openid) {
|
|
|
+ uni.showToast({ title: '请先登录', icon: 'none' });
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ const priceAmount = Number(orderInfo.value.payAmount ?? orderInfo.value.totalAmount ?? 0) || 0;
|
|
|
+ if (priceAmount <= 0) {
|
|
|
+ uni.showToast({ title: '订单金额异常', icon: 'none' });
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ const orderNo = orderInfo.value.orderNo || '';
|
|
|
+ if (!orderNo) {
|
|
|
+ uni.showToast({ title: '缺少订单号', icon: 'none' });
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 后端要求金额乘 100 传入(分)
|
|
|
+ const price = Math.round(priceAmount * 100);
|
|
|
+ uni.showLoading({ title: '拉起支付...' });
|
|
|
+ try {
|
|
|
+ const res = await PostOrderPay({
|
|
|
+ orderNo,
|
|
|
+ payer: openid,
|
|
|
+ price,
|
|
|
+ subject: '订单支付'
|
|
|
+ });
|
|
|
+ uni.hideLoading();
|
|
|
+ console.log(res)
|
|
|
+ uni.requestPayment({
|
|
|
+ provider: 'wxpay',
|
|
|
+ timeStamp:res.timestamp,
|
|
|
+ nonceStr:res.nonce,
|
|
|
+ package: res.prepayId,
|
|
|
+ signType:res.signType,
|
|
|
+ paySign:res.sign,
|
|
|
+ success: () => {
|
|
|
+ uni.showToast({ title: '支付成功', icon: 'success' });
|
|
|
+ setTimeout(() => go(`/pages/result/index?id=${encodeURIComponent(orderId)}`), 1500);
|
|
|
+ },
|
|
|
+ fail: (err) => {
|
|
|
+ const msg = err?.errMsg ?? err?.message ?? '支付失败';
|
|
|
+ if (String(msg).includes('cancel')) {
|
|
|
+ uni.showToast({ title: '已取消支付', icon: 'none' });
|
|
|
+ } else {
|
|
|
+ uni.showToast({ title: msg || '支付失败', icon: 'none' });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } catch (e) {
|
|
|
+ uni.hideLoading();
|
|
|
+ uni.showToast({ title: e?.message ?? '获取支付参数失败', icon: 'none' });
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
onLoad((options) => {
|
|
|
@@ -220,6 +283,8 @@ onLoad((options) => {
|
|
|
const userStore = useUserStore();
|
|
|
const contactPhone = userStore.getUserInfo?.phone ?? userStore.getUserInfo?.contactPhone ?? userStore.getUserInfo?.mobile ?? '';
|
|
|
orderInfo.value.contactPhone = contactPhone;
|
|
|
+ if (options?.orderId != null && options?.orderId !== '') payOrderId.value = options.orderId;
|
|
|
+ if (options?.orderNo != null && options?.orderNo !== '') orderInfo.value.orderNo = options.orderNo;
|
|
|
if (options?.tableId) orderInfo.value.tableId = options.tableId;
|
|
|
if (options?.diners) orderInfo.value.diners = options.diners;
|
|
|
if (options?.remark != null && options?.remark !== '') orderInfo.value.remark = decodeURIComponent(options.remark);
|