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';
// #endif
// 引入统一支付工具类
import { invokeWechatPay, invokeAlipay, invokeHuaweiPay } from '../../common/js/payCore.js';
export default {
name: 'payment',
components: {
@@ -87,6 +90,13 @@ export default {
type: 'wechatpay'
},
// #endif
// #ifdef H5 || QUICKAPP-HUAWEI
{
name: '华为支付',
icon: 'icon-zhekou',
type: 'huaweipay'
},
// #endif
{
name: '线下支付',
icon: 'icondiy icon-yuezhifu',
@@ -254,120 +264,87 @@ export default {
// #ifdef H5
pay() {
var payType = this.payTypeList[this.payIndex];
var return_url = '';
if (this.payInfo.event == 'BlindboxGoodsOrderPayNotify') {
return_url = '/pages_promotion/blindbox/index?outTradeNo=';
} else {
return_url = '/pages_tool/pay/result?code=';
if (!payType || payType.type === 'offlinepay') {
// 线下支付仍使用原有逻辑
this.payOffline();
return;
}
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.$util.showToast({
title: res.message
});
this.repeatFlag = false;
}
},
fail: res => {
uni.hideLoading();
this.$util.showToast({
title: 'request:fail'
});
this.repeatFlag = false;
// 使用统一支付工具类进行支付
this.invokeUnifiedPay(payType.type);
},
/**
* 统一支付调用方法(使用新的支付工具类)
*/
async invokeUnifiedPay(payType) {
try {
let payResult;
const amount = this.payMoney;
const outTradeNo = this.payInfo.out_trade_no;
const subject = '订单支付';
// 根据支付类型调用对应的支付方法
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() {
this.timer = setInterval(() => {
this.$api.sendRequest({
@@ -392,110 +369,17 @@ export default {
// #ifdef MP
pay() {
var payType = this.payTypeList[this.payIndex];
this.$api.sendRequest({
url: '/api/pay/pay',
data: {
out_trade_no: this.payInfo.out_trade_no,
pay_type: payType ? payType.type : '',
scene: uni.getStorageSync('is_test') ? 1175 : wx.getLaunchOptionsSync().scene,
is_balance: this.isBalance
},
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;
if (!payType || payType.type === 'offlinepay') {
// 线下支付仍使用原有逻辑
this.$util.redirectTo('/pages_tool/pay/offlinepay', {
outTradeNo: this.payInfo.out_trade_no
});
this.repeatFlag = false;
return;
}
// #ifdef MP-WEIXIN
var scene = uni.getStorageSync('is_test') ? 1175 : wx.getLaunchOptionsSync().scene;
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;
}
});
// 小程序端也使用统一支付工具类
this.invokeUnifiedPay(payType.type);
},
// #endif
/**
@@ -644,6 +528,8 @@ export default {
color: #00a0e9;
}
.icon-checkboxblank {
font-size: 40rpx;
color: $color-line;

View File

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