feat:新增华为支付

This commit is contained in:
Zhukj
2025-12-06 08:41:52 +08:00
parent 03aa6e099f
commit bf09d8ad26
2 changed files with 175 additions and 286 deletions

View File

@@ -56,6 +56,9 @@ import {
} from 'common/js/wx-jssdk.js'; } from 'common/js/wx-jssdk.js';
// #endif // #endif
// 引入统一支付工具类
import { invokeWechatPay, invokeAlipay, invokeHuaweiPay } from '../../common/js/payCore.js';
export default { export default {
name: 'payment', name: 'payment',
components: { components: {
@@ -87,6 +90,13 @@ export default {
type: 'wechatpay' type: 'wechatpay'
}, },
// #endif // #endif
// #ifdef H5 || QUICKAPP-HUAWEI
{
name: '华为支付',
icon: 'icon-zhekou',
type: 'huaweipay'
},
// #endif
{ {
name: '线下支付', name: '线下支付',
icon: 'icondiy icon-yuezhifu', icon: 'icondiy icon-yuezhifu',
@@ -254,120 +264,87 @@ export default {
// #ifdef H5 // #ifdef H5
pay() { pay() {
var payType = this.payTypeList[this.payIndex]; var payType = this.payTypeList[this.payIndex];
var return_url = ''; if (!payType || payType.type === 'offlinepay') {
if (this.payInfo.event == 'BlindboxGoodsOrderPayNotify') { // 线下支付仍使用原有逻辑
return_url = '/pages_promotion/blindbox/index?outTradeNo='; this.payOffline();
} else { return;
return_url = '/pages_tool/pay/result?code=';
} }
this.$api.sendRequest({
url: '/api/pay/pay',
data: {
out_trade_no: this.payInfo.out_trade_no,
pay_type: payType ? payType.type : '',
return_url: encodeURIComponent(this.$config.h5Domain + return_url + this.payInfo.out_trade_no),
is_balance: this.isBalance
},
success: res => {
uni.hideLoading();
if (res.code >= 0) {
if (res.data.pay_success) {
this.paySuccess();
return;
}
switch (payType.type) {
case 'alipay':
if (this.$util.isWeiXin()) {
var wx_alipay = encodeURIComponent(res.data.data);
this.$util.redirectTo('/pages_tool/pay/wx_pay', {
wx_alipay: wx_alipay,
out_trade_no: this.payInfo.out_trade_no
}, '', 'redirectTo');
this.repeatFlag = false;
} else {
this.repeatFlag = false;
location.href = res.data.data;
this.checkPayStatus();
}
break;
case 'wechatpay':
if (this.$util.isWeiXin()) {
if (uni.getSystemInfoSync().platform == 'ios') {
var url = uni.getStorageSync('initUrl');
} else {
var url = location.href;
}
// 获取jssdk配置
this.$api.sendRequest({
url: '/wechat/api/wechat/jssdkconfig',
data: {
url: url
},
success: jssdkRes => {
var wxJS = new Weixin(),
payData = res.data.data;
wxJS.init(jssdkRes.data);
wxJS.pay({
timestamp: payData.timestamp ? payData.timestamp : payData.timeStamp,
nonceStr: payData.nonceStr,
package: payData.package,
signType: payData.signType,
paySign: payData.paySign
},
res => {
if (res.errMsg == 'chooseWXPay:ok') {
this.paySuccess();
this.repeatFlag = false;
} else {
this.$util.showToast({
title: res.errMsg
});
setTimeout(() => {
this.close();
this.repeatFlag = false;
}, 1500)
}
},
res => {
this.$util.showToast({
title: '您已取消支付'
});
this.resetpay();
this.repeatFlag = false;
}
);
}
});
} else {
this.repeatFlag = false;
location.href = res.data.url;
this.checkPayStatus();
}
break;
case 'offlinepay':
this.$util.redirectTo('/pages_tool/pay/offlinepay', {
outTradeNo: this.payInfo.out_trade_no
});
this.repeatFlag = false;
break;
} // 使用统一支付工具类进行支付
} else { this.invokeUnifiedPay(payType.type);
this.$util.showToast({ },
title: res.message
}); /**
this.repeatFlag = false; * 统一支付调用方法(使用新的支付工具类)
} */
}, async invokeUnifiedPay(payType) {
fail: res => { try {
uni.hideLoading(); let payResult;
this.$util.showToast({ const amount = this.payMoney;
title: 'request:fail' const outTradeNo = this.payInfo.out_trade_no;
}); const subject = '订单支付';
this.repeatFlag = false;
// 根据支付类型调用对应的支付方法
switch (payType) {
case 'wechatpay':
payResult = await invokeWechatPay(outTradeNo, amount, subject);
break;
case 'alipay':
payResult = await invokeAlipay(outTradeNo, amount, subject);
break;
case 'huaweipay':
payResult = await invokeHuaweiPay(outTradeNo, amount, subject);
break;
default:
throw new Error('不支持的支付方式');
} }
// 处理H5支付跳转
if (payResult.code === 0 && payResult.data?.payUrl) {
// H5端直接跳转其他端需要在WebView中显示
const systemInfo = uni.getSystemInfoSync();
if (systemInfo.platform === 'web') {
window.location.href = payResult.data.payUrl;
this.checkPayStatus();
} else {
// 微信小程序/华为快应用中显示WebView
this.showPayWebView(payResult.data.payUrl);
}
} else if (payResult.code === 0) {
// 原生支付成功
this.paySuccess();
} else {
throw new Error(payResult.msg || '支付失败');
}
} catch (error) {
this.repeatFlag = false;
this.$util.showToast({
title: error.message || '支付失败'
});
}
},
/**
* 显示支付WebView用于微信小程序/华为快应用的H5支付
*/
showPayWebView(payUrl) {
// 这里可以打开一个新的WebView页面来显示支付
// 或者使用现有的跳转逻辑
this.$util.redirectTo('/pages/Pay/Pay', {
payUrl: payUrl,
outTradeNo: this.payInfo.out_trade_no
}); });
}, },
/**
* 线下支付处理(保留原有逻辑)
*/
payOffline() {
this.$util.redirectTo('/pages_tool/pay/offlinepay', {
outTradeNo: this.payInfo.out_trade_no
});
this.repeatFlag = false;
},
checkPayStatus() { checkPayStatus() {
this.timer = setInterval(() => { this.timer = setInterval(() => {
this.$api.sendRequest({ this.$api.sendRequest({
@@ -392,110 +369,17 @@ export default {
// #ifdef MP // #ifdef MP
pay() { pay() {
var payType = this.payTypeList[this.payIndex]; var payType = this.payTypeList[this.payIndex];
this.$api.sendRequest({ if (!payType || payType.type === 'offlinepay') {
url: '/api/pay/pay', // 线下支付仍使用原有逻辑
data: { this.$util.redirectTo('/pages_tool/pay/offlinepay', {
out_trade_no: this.payInfo.out_trade_no, outTradeNo: this.payInfo.out_trade_no
pay_type: payType ? payType.type : '', });
scene: uni.getStorageSync('is_test') ? 1175 : wx.getLaunchOptionsSync().scene, this.repeatFlag = false;
is_balance: this.isBalance return;
}, }
success: res => {
uni.hideLoading();
if (res.code >= 0) {
if (res.data.pay_success) {
this.paySuccess();
this.repeatFlag = false;
return;
}
if (payType.type == 'offlinepay') {
this.$util.redirectTo('/pages_tool/pay/offlinepay', {
outTradeNo: this.payInfo.out_trade_no
});
this.repeatFlag = false;
} else {
var payData = res.data.data;
// #ifdef MP-WEIXIN // 小程序端也使用统一支付工具类
var scene = uni.getStorageSync('is_test') ? 1175 : wx.getLaunchOptionsSync().scene; this.invokeUnifiedPay(payType.type);
if ([1175, 1176, 1177, 1191, 1195].indexOf(scene) != -1) {
uni.requestOrderPayment({
timeStamp: payData.timeStamp,
nonceStr: payData.nonceStr,
package: payData.package,
signType: payData.signType,
paySign: payData.paySign,
success: res => {
this.paySuccess();
this.repeatFlag = false;
},
fail: res => {
this.flag = false;
if (res.errMsg == 'requestOrderPayment:fail cancel') {
this.$util.showToast({
title: '您已取消支付'
});
this.resetpay();
this.repeatFlag = false;
} else {
uni.showModal({
content: '支付失败,失败原因: ' + res.errMsg,
showCancel: false
});
setTimeout(() => {
this.close();
this.repeatFlag = false;
}, 1500)
}
}
});
return
}
// #endif
uni.requestPayment({
provider: payType.provider,
...payData,
success: res => {
this.paySuccess();
this.repeatFlag = false;
},
fail: res => {
this.flag = false;
if (res.errMsg == 'requestPayment:fail cancel') {
this.$util.showToast({
title: '您已取消支付'
});
this.resetpay();
this.repeatFlag = false;
} else {
uni.showModal({
content: '支付失败,失败原因: ' + res.errMsg,
showCancel: false
});
setTimeout(() => {
this.close();
this.repeatFlag = false;
}, 1500)
}
}
});
}
} else {
this.$util.showToast({
title: res.message
});
this.repeatFlag = false;
}
},
fail: res => {
uni.hideLoading();
this.$util.showToast({
title: 'request:fail'
});
this.repeatFlag = false;
}
});
}, },
// #endif // #endif
/** /**
@@ -644,6 +528,8 @@ export default {
color: #00a0e9; color: #00a0e9;
} }
.icon-checkboxblank { .icon-checkboxblank {
font-size: 40rpx; font-size: 40rpx;
color: $color-line; color: $color-line;

View File

@@ -1,75 +1,78 @@
{ {
"description": "项目配置文件", "description": "项目配置文件",
"packOptions": { "packOptions": {
"ignore": [] "ignore": [],
}, "include": []
"setting": { },
"urlCheck": true, "setting": {
"es6": true, "urlCheck": true,
"enhance": false, "es6": true,
"postcss": true, "enhance": false,
"preloadBackgroundData": false, "postcss": true,
"minified": true, "preloadBackgroundData": false,
"newFeature": false, "minified": true,
"coverView": true, "newFeature": false,
"nodeModules": false, "coverView": true,
"autoAudits": false, "nodeModules": false,
"showShadowRootInWxmlPanel": true, "autoAudits": false,
"scopeDataCheck": false, "showShadowRootInWxmlPanel": true,
"uglifyFileName": false, "scopeDataCheck": false,
"checkInvalidKey": true, "uglifyFileName": false,
"checkSiteMap": true, "checkInvalidKey": true,
"uploadWithSourceMap": true, "checkSiteMap": true,
"compileHotReLoad": false, "uploadWithSourceMap": true,
"useMultiFrameRuntime": true, "compileHotReLoad": false,
"useApiHook": true, "useMultiFrameRuntime": true,
"useApiHostProcess": true, "useApiHook": true,
"babelSetting": { "useApiHostProcess": true,
"ignore": [], "babelSetting": {
"disablePlugins": [], "ignore": [],
"outputPath": "" "disablePlugins": [],
}, "outputPath": ""
"enableEngineNative": false, },
"bundle": false, "enableEngineNative": false,
"useIsolateContext": true, "bundle": false,
"useCompilerModule": true, "useIsolateContext": true,
"userConfirmedUseCompilerModuleSwitch": false, "useCompilerModule": true,
"userConfirmedBundleSwitch": false, "userConfirmedUseCompilerModuleSwitch": false,
"packNpmManually": false, "userConfirmedBundleSwitch": false,
"packNpmRelationList": [], "packNpmManually": false,
"minifyWXSS": true "packNpmRelationList": [],
}, "minifyWXSS": true,
"compileType": "miniprogram", "compileWorklet": false,
"libVersion": "2.16.1", "minifyWXML": true,
"appid": "wx29215aa1bd97bbd6", "localPlugins": false,
"projectname": "niushop_b2c_v4_uniapp", "disableUseStrict": false,
"debugOptions": { "useCompilerPlugins": false,
"hidedInDevtools": [] "condition": false,
}, "swc": false,
"scripts": {}, "disableSWC": true
"staticServerOptions": { },
"baseURL": "", "compileType": "miniprogram",
"servePath": "" "libVersion": "2.16.1",
}, "appid": "wx29215aa1bd97bbd6",
"isGameTourist": false, "projectname": "niushop_b2c_v4_uniapp",
"condition": { "isGameTourist": false,
"search": { "condition": {
"list": [] "search": {
}, "list": []
"conversation": { },
"list": [] "conversation": {
}, "list": []
"game": { },
"list": [] "game": {
}, "list": []
"plugin": { },
"list": [] "plugin": {
}, "list": []
"gamePlugin": { },
"list": [] "gamePlugin": {
}, "list": []
"miniprogram": { },
"list": [] "miniprogram": {
} "list": []
} }
},
"simulatorPluginLibVersion": {},
"editorSetting": {}
} }