254 lines
8.5 KiB
JavaScript
254 lines
8.5 KiB
JavaScript
import Dialog from 'tdesign-miniprogram/dialog/index';
|
||
import Toast from 'tdesign-miniprogram/toast/index';
|
||
|
||
import { dispatchCommitPay } from '../../../services/order/orderConfirm';
|
||
import { config } from '../../../config/index';
|
||
|
||
// 真实的提交支付
|
||
export const commitPay = (params) => {
|
||
console.log('=== pay.js commitPay 调试 ===');
|
||
console.log('接收到的params:', params);
|
||
console.log('params.couponList:', params.couponList);
|
||
if (params.couponList && params.couponList.length > 0) {
|
||
console.log('第一张优惠券详情:', params.couponList[0]);
|
||
console.log('优惠券ID字段检查:', {
|
||
id: params.couponList[0].id,
|
||
couponId: params.couponList[0].couponId,
|
||
userCouponId: params.couponList[0].userCouponId,
|
||
allFields: Object.keys(params.couponList[0])
|
||
});
|
||
}
|
||
|
||
return dispatchCommitPay({
|
||
goodsRequestList: params.goodsRequestList, // 待结算的商品集合
|
||
invoiceRequest: params.invoiceRequest, // 发票信息
|
||
// isIgnore: params.isIgnore || false, // 删掉 是否忽视库存不足和商品失效,继续结算,true=继续结算 购物车请赋值false
|
||
userAddressReq: params.userAddressReq, // 地址信息(用户在购物选择更换地址)
|
||
currency: params.currency || 'CNY', // 支付货币: 人民币=CNY,美元=USD
|
||
logisticsType: params.logisticsType || 1, // 配送方式 0=无需配送 1=快递 2=商家 3=同城 4=自提
|
||
// orderMark: params.orderMark, // 下单备注
|
||
orderType: params.orderType || 0, // 订单类型 0=普通订单 1=虚拟订单
|
||
payType: params.payType || 1, // 支付类型(0=线上、1=线下)
|
||
totalAmount: params.totalAmount, // 新增字段"totalAmount"总的支付金额
|
||
userName: params.userName, // 用户名
|
||
payWay: 1,
|
||
authorizationCode: '', //loginCode, // 登录凭证
|
||
storeInfoList: params.storeInfoList, //备注信息列表
|
||
couponList: params.couponList,
|
||
groupInfo: params.groupInfo,
|
||
});
|
||
};
|
||
|
||
export const paySuccess = (payOrderInfo, pageContext = null) => {
|
||
const { payAmt, tradeNo, groupId, promotionId } = payOrderInfo;
|
||
// 支付成功
|
||
console.log('支付成功,准备跳转到支付结果页面', payOrderInfo);
|
||
|
||
// 显示成功提示,如果有页面上下文则使用,否则使用全局提示
|
||
if (pageContext) {
|
||
Toast({
|
||
context: pageContext,
|
||
selector: '#t-toast',
|
||
message: '支付成功',
|
||
duration: 1500,
|
||
icon: 'check-circle',
|
||
});
|
||
} else {
|
||
wx.showToast({
|
||
title: '支付成功',
|
||
icon: 'success',
|
||
duration: 1500
|
||
});
|
||
}
|
||
|
||
const params = {
|
||
totalPaid: payAmt,
|
||
orderNo: tradeNo,
|
||
};
|
||
if (groupId) {
|
||
params.groupId = groupId;
|
||
}
|
||
if (promotionId) {
|
||
params.promotionId = promotionId;
|
||
}
|
||
const paramsStr = Object.keys(params)
|
||
.map((k) => `${k}=${params[k]}`)
|
||
.join('&');
|
||
|
||
// 确保提示显示完成后再跳转
|
||
setTimeout(() => {
|
||
console.log('准备跳转到支付结果页面', `/pages/order/pay-result/index?${paramsStr}`);
|
||
|
||
// 直接使用redirectTo进行页面跳转
|
||
wx.redirectTo({
|
||
url: `/pages/order/pay-result/index?${paramsStr}`,
|
||
success: () => {
|
||
console.log('跳转到支付结果页面成功');
|
||
},
|
||
fail: (err) => {
|
||
console.error('跳转到支付结果页面失败', err);
|
||
// 如果redirectTo失败,尝试使用navigateTo
|
||
wx.navigateTo({
|
||
url: `/pages/order/pay-result/index?${paramsStr}`,
|
||
fail: (navErr) => {
|
||
console.error('navigateTo也失败了', navErr);
|
||
// 最后尝试使用reLaunch
|
||
wx.reLaunch({
|
||
url: `/pages/order/pay-result/index?${paramsStr}`,
|
||
fail: (relaunchErr) => {
|
||
console.error('所有跳转方式都失败了', relaunchErr);
|
||
}
|
||
});
|
||
}
|
||
});
|
||
}
|
||
});
|
||
}, 1600); // 延迟1.6秒后跳转,确保Toast显示完成
|
||
};
|
||
|
||
export const payFail = (payOrderInfo, resultMsg) => {
|
||
if (resultMsg === 'requestPayment:fail cancel') {
|
||
if (payOrderInfo.dialogOnCancel) {
|
||
//结算页,取消付款,dialog提示
|
||
Dialog.confirm({
|
||
title: '是否放弃付款',
|
||
content: '商品可能很快就会被抢空哦,是否放弃付款?',
|
||
confirmBtn: '放弃',
|
||
cancelBtn: '继续付款',
|
||
}).then(() => {
|
||
wx.redirectTo({ url: '/pages/order/order-list/index' });
|
||
});
|
||
} else {
|
||
//订单列表页,订单详情页,取消付款,toast提示
|
||
Toast({
|
||
context: this,
|
||
selector: '#t-toast',
|
||
message: '支付取消',
|
||
duration: 2000,
|
||
icon: 'close-circle',
|
||
});
|
||
}
|
||
} else {
|
||
Toast({
|
||
context: this,
|
||
selector: '#t-toast',
|
||
message: `支付失败:${resultMsg}`,
|
||
duration: 2000,
|
||
icon: 'close-circle',
|
||
});
|
||
setTimeout(() => {
|
||
wx.redirectTo({ url: '/pages/order/order-list/index' });
|
||
}, 2000);
|
||
}
|
||
};
|
||
|
||
// 微信支付方式
|
||
export const wechatPayOrder = (payOrderInfo, pageContext = null) => {
|
||
console.log('开始微信支付流程', payOrderInfo);
|
||
|
||
return new Promise((resolve, reject) => {
|
||
// 获取token
|
||
const token = wx.getStorageSync('token');
|
||
if (!token) {
|
||
wx.showModal({
|
||
title: '提示',
|
||
content: '请先登录后再进行支付',
|
||
showCancel: true,
|
||
cancelText: '取消',
|
||
confirmText: '去登录',
|
||
success: (res) => {
|
||
if (res.confirm) {
|
||
wx.navigateTo({
|
||
url: '/pages/login/index'
|
||
});
|
||
}
|
||
}
|
||
});
|
||
reject(new Error('未登录'));
|
||
return;
|
||
}
|
||
|
||
// 显示支付中提示
|
||
wx.showLoading({ title: '创建支付订单...' });
|
||
|
||
// 调用后端创建支付订单API
|
||
wx.request({
|
||
url: `${config.apiBase}/payment/create`,
|
||
method: 'POST',
|
||
header: {
|
||
'Authorization': `Bearer ${token}`,
|
||
'Content-Type': 'application/json'
|
||
},
|
||
data: {
|
||
orderNo: payOrderInfo.tradeNo,
|
||
paymentMethod: 'wechat'
|
||
},
|
||
success: (res) => {
|
||
console.log('创建支付订单API响应:', res);
|
||
|
||
if (res.statusCode === 200 && res.data.code === 200) {
|
||
const paymentData = res.data.data;
|
||
|
||
// 如果返回了微信支付参数,调用微信支付SDK
|
||
if (paymentData && paymentData.payInfo) {
|
||
wx.hideLoading();
|
||
wx.showLoading({ title: '调起微信支付...' });
|
||
|
||
try {
|
||
const payInfo = typeof paymentData.payInfo === 'string'
|
||
? JSON.parse(paymentData.payInfo)
|
||
: paymentData.payInfo;
|
||
|
||
console.log('微信支付参数:', payInfo);
|
||
|
||
wx.requestPayment({
|
||
timeStamp: payInfo.timeStamp,
|
||
nonceStr: payInfo.nonceStr,
|
||
package: payInfo.package,
|
||
signType: payInfo.signType || 'RSA',
|
||
paySign: payInfo.paySign,
|
||
success: function (payRes) {
|
||
wx.hideLoading();
|
||
console.log('微信支付成功:', payRes);
|
||
paySuccess(payOrderInfo, pageContext);
|
||
resolve();
|
||
},
|
||
fail: function (payErr) {
|
||
wx.hideLoading();
|
||
console.error('微信支付失败:', payErr);
|
||
payFail(payOrderInfo, payErr.errMsg);
|
||
reject(payErr);
|
||
}
|
||
});
|
||
} catch (parseErr) {
|
||
wx.hideLoading();
|
||
console.error('解析支付参数失败:', parseErr);
|
||
payFail(payOrderInfo, '支付参数解析失败');
|
||
reject(parseErr);
|
||
}
|
||
} else {
|
||
// 如果没有返回微信支付参数,可能是模拟支付成功
|
||
wx.hideLoading();
|
||
console.log('支付处理完成,调用支付成功回调');
|
||
paySuccess(payOrderInfo, pageContext);
|
||
resolve();
|
||
}
|
||
} else {
|
||
wx.hideLoading();
|
||
console.error('创建支付订单失败:', res.data.message);
|
||
const errorMsg = res.data.message || '创建支付订单失败';
|
||
payFail(payOrderInfo, errorMsg);
|
||
reject(new Error(errorMsg));
|
||
}
|
||
},
|
||
fail: (err) => {
|
||
wx.hideLoading();
|
||
console.error('创建支付订单请求失败:', err);
|
||
const errorMsg = '网络错误,支付失败';
|
||
payFail(payOrderInfo, errorMsg);
|
||
reject(err);
|
||
}
|
||
});
|
||
});
|
||
};
|