feat: 增加现金支付

This commit is contained in:
HongRun Liu
2026-08-05 10:51:29 +08:00
parent 1daec0121b
commit 25da673443
16 changed files with 1535 additions and 1230 deletions
+92 -51
View File
@@ -108,7 +108,12 @@ export default class SheepPay {
},
},
};
return payAction[sheep.$platform.name][this.payment]();
const action = payAction[sheep.$platform.name]?.[this.payment];
if (!action) {
sheep.$helper.toast('当前支付方式不可用');
return;
}
return action();
}
// 预支付
@@ -134,14 +139,13 @@ export default class SheepPay {
console.log('发起预支付 API 调用' + JSON.stringify(data));
// 发起预支付 API 调用
PayOrderApi.submitOrder(data).then((res) => {
// 成功时
res.code === 0 && resolve(res);
resolve(res);
// 失败时
if (res.code !== 0 && res.msg.indexOf('无效的openid') >= 0) {
if (res.code !== 0 && res.msg?.indexOf('无效的openid') >= 0) {
// 特殊逻辑:微信公众号、小程序支付时,必须传入 openid 不正确的情况
if (
res.msg.indexOf('无效的openid') >= 0 || // 获取的 openid 不正确时,或者随便输入了个 openid
res.msg.indexOf('下单账号与支付账号不一致') >= 0
res.msg?.indexOf('无效的openid') >= 0 || // 获取的 openid 不正确时,或者随便输入了个 openid
res.msg?.indexOf('下单账号与支付账号不一致') >= 0
) {
// https://developers.weixin.qq.com/community/develop/doc/00008c53c347804beec82aed051c00
this.bindWeixin();
@@ -181,8 +185,9 @@ export default class SheepPay {
async wechatWapPay() {
const { error, data } = await this.prepay();
if (error === 0) {
const redirect_url = `${getRootUrl()}pages/pay/result?id=${this.id}&payment=${this.payment
}&orderType=${this.orderType}`;
const redirect_url = `${getRootUrl()}pages/pay/result?id=${this.id}&payment=${
this.payment
}&orderType=${this.orderType}`;
location.href = `${data.pay_data.h5_url}&redirect_url=${encodeURIComponent(redirect_url)}`;
}
}
@@ -234,8 +239,44 @@ export default class SheepPay {
}
async cashPay() {
const { code } = await this.prepay('cash_payment');
code === 0 && this.payResult('success');
const result = await this.prepay('cash_payment');
const code = result?.code;
const data = result?.data;
if (code !== 0) {
return;
}
if (Number(data?.status) !== 40 || data?.displayContent !== 'cash_pending') {
sheep.$helper.toast('现金支付状态异常');
return;
}
const payOrderResult = await PayOrderApi.getOrder(this.id, false);
const payOrder = payOrderResult?.code === 0 ? payOrderResult.data : null;
uni.showModal({
title: '现金支付',
content: '现金支付申请已提交,等待工作人员确认',
showCancel: false,
success: () => this.redirectCashPendingOrder(payOrder),
});
}
redirectCashPendingOrder(payOrder = {}) {
const businessOrderId = payOrder?.merchantOrderId;
const orderType = Number(payOrder?.orderType);
if (!businessOrderId) {
uni.reLaunch({ url: '/pages/order/list' });
return;
}
const routes = {
1: `/pages/order/detail?id=${businessOrderId}`,
2: `/pages/AI/orderInfo?id=${businessOrderId}`,
3: `/pages/AI/giftOrderInfo?id=${businessOrderId}`,
};
const url = routes[orderType];
if (!url) {
uni.reLaunch({ url: '/pages/order/list' });
return;
}
uni.redirectTo({ url });
}
// 模拟支付
@@ -273,7 +314,7 @@ export default class SheepPay {
if (code !== 0) {
return;
}
console.log(data.displayContent)
console.log(data.displayContent);
uni.requestPayment({
provider: 'alipay',
orderInfo: data.displayContent, // 直接使用返回的支付参数
@@ -349,29 +390,31 @@ export default class SheepPay {
console.log('token:' + uni.getStorageSync('token'));
console.log('orderId:' + that.id);
// #ifdef APP-PLUS
plus.share.getServices(services => {
const weixinService = services.find(service => service.id === 'weixin');
if (weixinService) {
weixinService.launchMiniProgram({
// appid: 'wx23b0c321b5d74d63',
id: 'gh_c69ddc78e1cd', // 替换为实际小程序原始ID
type: 0, // 类型:// 版本:0-正式版 1-测试版 2-体验版
path: 'pages/pkgD/pay?token=' + uni.getStorageSync('token') + '&orderId=' + that.id, // 可选跳转路径
});
} else {
uni.showToast({ title: '未检测到微信客户端', icon: 'none' });
}
}, err => {
console.error('获取服务失败:', err);
});
plus.share.getServices(
(services) => {
const weixinService = services.find((service) => service.id === 'weixin');
if (weixinService) {
weixinService.launchMiniProgram({
// appid: 'wx23b0c321b5d74d63',
id: 'gh_c69ddc78e1cd', // 替换为实际小程序原始ID
type: 0, // 类型:// 版本:0-正式版 1-测试版 2-体验版
path: 'pages/pkgD/pay?token=' + uni.getStorageSync('token') + '&orderId=' + that.id, // 可选跳转路径
});
} else {
uni.showToast({ title: '未检测到微信客户端', icon: 'none' });
}
},
(err) => {
console.error('获取服务失败:', err);
},
);
// #endif
}
//检测code是否过期
verify_wechat_code(code) {
let appid = 'wx23b0c321b5d74d63'
let secret = 'aff28ef70e00c12245e77a9804dfd8a2'
let url = `https://api.weixin.qq.com/sns/oauth2/access_token?appid=${appid}&secret=${secret}&code=${code}&grant_type=authorization_code`
let appid = 'wx23b0c321b5d74d63';
let secret = 'aff28ef70e00c12245e77a9804dfd8a2';
let url = `https://api.weixin.qq.com/sns/oauth2/access_token?appid=${appid}&secret=${secret}&code=${code}&grant_type=authorization_code`;
return uni.request({
url: url,
method: 'GET',
@@ -382,11 +425,10 @@ export default class SheepPay {
fail(err) {
console.log('verify_wechat_code 失败:' + JSON.stringify(err));
},
})
});
}
//检测是否开通支付
is_open_wxPay(pay_info) {
uni.requestPayment({
provider: 'wxpay',
orderInfo: pay_info,
@@ -395,35 +437,36 @@ export default class SheepPay {
},
fail(err) {
console.error('开通支付失败:', err);
}
},
});
}
// 汇付支付宝支付
async hfAlipayPay() {
let { code, data } = await this.prepay('hf_alipay_app');
console.log(data)
if (code !== 0) return
console.log(data);
if (code !== 0) return;
if (data.displayMode.toLowerCase() == sheep.$platform.name.toLowerCase()) {
//拼接支付宝内部功能,直接打开支付页面
let url = `alipays://platformapi/startapp?saId=10000007&qrcode=${encodeURI(data.displayContent)}?_s=web-other`;
console.log(url)
let platform = uni.getSystemInfoSync().platform;//获取平台判断
let url = `alipays://platformapi/startapp?saId=10000007&qrcode=${encodeURI(
data.displayContent,
)}?_s=web-other`;
console.log(url);
let platform = uni.getSystemInfoSync().platform; //获取平台判断
if (platform == 'android') {
plus.runtime.openURL(url)
plus.runtime.openURL(url);
} else if (platform == 'ios') {
plus.runtime.launchApplication({
action: url
})
action: url,
});
}
} else {
sheep.$helper.toast(`请使用${data.displayMode}进行支付`);
}
}
}
export function getPayMethods(channels) {
channels = Array.isArray(channels) ? channels : [];
const payMethods1 = [
{
icon: '/static/pay/wechat.svg',
@@ -470,10 +513,9 @@ export function getPayMethods(channels) {
{
icon: '/static/pay/alipay.svg',
title: 'H支付宝支付',
value: "hf_alipay_app",
value: 'hf_alipay_app',
disabled: false,
},
];
let payMethods = [];
// 1. 处理【微信支付】
@@ -492,8 +534,7 @@ export function getPayMethods(channels) {
let hfAlipayMethod = null;
payMethods1.forEach((e) => {
const exists =
channels.indexOf(e.value) !== -1 ||
(e.value === 'cash' && channels.includes('cash_payment'));
channels.indexOf(e.value) !== -1 || (e.value === 'cash' && channels.includes('cash_payment'));
if (exists) {
payMethods.push(e);
if (e.value == 'wechat') {
@@ -520,7 +561,7 @@ export function getPayMethods(channels) {
(platform === 'WechatMiniProgram' && channels.includes('wx_lite')) ||
(platform === 'App' && channels.includes('wx_app'))
) {
wechatMethod.disabled = false;
if (wechatMethod) wechatMethod.disabled = false;
}
if (
@@ -529,10 +570,10 @@ export function getPayMethods(channels) {
(platform === 'WechatMiniProgram' && channels.includes('alipay_wap')) ||
(platform === 'App' && channels.includes('alipay_app'))
) {
alipayMethod.disabled = false;
if (alipayMethod) alipayMethod.disabled = false;
}
if (channels.includes('wallet')) {
walletMethod.disabled = false;
if (walletMethod) walletMethod.disabled = false;
}
if (channels.includes('cash_payment') && cashMethod) {
cashMethod.disabled = false;
@@ -558,7 +599,7 @@ export function goPayResult(id, orderType, resultType) {
// });
uni.redirectTo({
url: '/pages/pay/result?id=' + id + '&orderType=' + orderType + '&payState=' + resultType,
})
});
}
export function goBindWeixin() {