From 7f8c4966aa31a629ffc02fe59c6812bf9d536fb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=AF=E4=B9=90=E5=8A=A0=E5=86=B0?= <11869599+addie66@user.noreply.gitee.com> Date: Wed, 23 Sep 2026 20:24:44 +0800 Subject: [PATCH] =?UTF-8?q?fix(pay):=20=E6=94=AF=E4=BB=98=E6=88=90?= =?UTF-8?q?=E5=8A=9F=E5=90=8E=E8=BD=AE=E8=AF=A2=E7=A1=AE=E8=AE=A4=E8=AE=A2?= =?UTF-8?q?=E5=8D=95=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 微信/余额等支付完成后进入 paying 状态轮询后端 - 延长轮询次数,避免回调延迟误判失败 Co-authored-by: Cursor --- .gitignore | 5 + pages/order/payment/result.vue | 291 +++++++++++++++++++++++++++++++++ shopro/pay/index.js | 282 ++++++++++++++++++++++++++++++++ 3 files changed, 578 insertions(+) create mode 100644 .gitignore create mode 100644 pages/order/payment/result.vue create mode 100644 shopro/pay/index.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..675f0dc --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +unpackage/ +node_modules/ +.hbuilderx/ +.DS_Store +*.log diff --git a/pages/order/payment/result.vue b/pages/order/payment/result.vue new file mode 100644 index 0000000..d083bf0 --- /dev/null +++ b/pages/order/payment/result.vue @@ -0,0 +1,291 @@ + + + + + + diff --git a/shopro/pay/index.js b/shopro/pay/index.js new file mode 100644 index 0000000..f21558d --- /dev/null +++ b/shopro/pay/index.js @@ -0,0 +1,282 @@ +import $http from '@/shopro/request/index' +// #ifdef H5 +import wxsdk from '@/shopro/wechat/sdk' +// #endif +import wechat from '@/shopro/wechat/wechat' +import { + router as $Router +} from '@/shopro/router/index.js' +import store from '@/shopro/store'; +import $platform from '@/shopro/platform'; + +/** + * 支付 + * + * @param {String} payment = ['wechat','alipay','wallet'] - 支付方式 + * @param {Object} order = {} - 订单详情 + * @param {String} orderType = ['goods','recharge'] - 订单类型 + */ + +export default class ShoproPay { + + + // wxOfficialAccount wxMiniProgram App H5 + // wechat 公众号JSSDK支付 小程序支付 微信开放平台支付 H5网页支付 + // alipay 复制网址 复制网址 支付宝开放平台支付 直接跳转链接 + // wallet v v v v + + + constructor(payment, order, orderType) { + this.payment = payment; + this.order = order; + this.orderType = orderType; + this.platform = $platform.get(); + let payMehod = this.getPayMethod(); + console.log(this.payment, this.payment, this.platform); + payMehod(); + } + + getPayMethod() { + var payMethod = { + 'wxOfficialAccount': { + 'wechat': () => { + this.wxOfficialAccountPay() + }, + 'alipay': () => { + this.copyPayLink() + }, + 'wallet': () => { + this.walletPay() + } + }, + 'wxMiniProgram': { + 'wechat': () => { + this.wxMiniProgramPay() + }, + 'group': () => { + this.wxMiniProgramPay() + }, + 'alipay': () => { + this.copyPayLink() + }, + 'wallet': () => { + this.walletPay() + } + }, + 'App': { + 'wechat': () => { + this.wechatPay() + }, + 'group': () => { + this.wechatPay() + }, + 'alipay': () => { + this.aliPay() + }, + 'wallet': () => { + this.walletPay() + }, + }, + 'H5': { + 'wechat': () => { + this.wechatWapPay() + }, + 'alipay': () => { + this.goToPayLink() + }, + 'wallet': () => { + this.walletPay() + }, + }, + } + return payMethod[this.platform][this.payment]; + } + + // 预支付 + prepay() { + let that = this; + return new Promise((resolve, reject) => { + let that = this; + let params = { + order_id: that.order.id, + order_sn: that.order.order_sn, + payment: that.payment + } + if (uni.getStorageSync('openid')) { + params.openid = uni.getStorageSync('openid'); + } + + $http('money.prepay', params, '支付中').then(res => { + if (res.code === 1) { + res.data === 'no_openid' ? + uni.showModal({ + title: '微信支付', + content: '请先绑定微信再使用微信支付', + confirmText: '去绑定', + success: function(res) { + if (res.confirm) { + // 跳转到个人中心绑定信息 + uni.navigateTo({ + url: '/pages/user/info' + }); + + // wechat.bind(); + } + }, + }) : + resolve(res); + } + }) + }); + } + + // 微信H5支付 + async wxOfficialAccountPay() { + let that = this; + let result = await this.prepay(); + wxsdk.wxpay(result.data.pay_data, (res) => { + res.errMsg == "chooseWXPay:ok" ? that.payResult('paying') : that.payResult('fail') + }); + } + + // 浏览器微信支付 + async wechatWapPay() { + let that = this; + let result = await this.prepay(); + if (result.code === 1) { + var url = result.data.pay_data.match(/url\=\'(\S*)\'/); + let reg = new RegExp('&', 'g') //g代表全部 + let newUrl = url[1].replace(reg, '&'); + let domain = store.getters.initShop.domain; //域名需要https + let params = encodeURIComponent( + `${domain}pages/order/payment/result?orderId=${that.order.id}&type=${that.payment}&orderType=${that.orderType}` + ) + window.location.href = newUrl + '&redirect_url=' + params; + } + } + + // 微信小程序支付 + async wxMiniProgramPay() { + let that = this; + let result = await this.prepay(); + uni.requestPayment({ + provider: 'wxpay', + ...result.data.pay_data, + success: res => { + that.payResult('paying') + }, + fail: err => { + console.log('支付取消或者失败:', err); + err.errMsg !== "requestPayment:fail cancel" && that.payResult('fail') + } + }); + } + + // 余额支付 + async walletPay() { + let that = this; + let result = await this.prepay(); + result.code === 1 && that.payResult('paying') + } + + // 支付宝复制链接支付 + async copyPayLink() { + let that = this; + let result = await this.prepay(); + if (result.code === 1) { + //引入showModal 点击确认 复制链接; + uni.showModal({ + title: '支付宝支付', + content: '复制链接到外部浏览器', + confirmText: '复制链接', + success: (res) => { + if (res.confirm) { + uni.setClipboardData({ + data: result.data.pay_data, + success: function(data) { + that.$u.toast('已复制到剪切板'); + } + }); + } + } + }) + } + } + + // 支付链接 + async goToPayLink() { + let that = this; + let result = await this.prepay(); + if (result.code === 1) { + window.location = result.data.pay_data; + } + } + + // 支付宝支付 + async aliPay() { + let that = this; + let result = await this.prepay(); + + if (result.code === 1) { + uni.requestPayment({ + provider: 'alipay', + orderInfo: result.data.pay_data, // 支付宝订单数据 + success: res => { + that.payResult('paying') + }, + fail: err => { + console.log('支付取消或者失败:', err); + err.errMsg !== "requestPayment:fail cancel" && that.payResult('fail') + } + }); + } + } + + // 微信app支付 + async wechatPay() { + let that = this; + let result = await this.prepay(); + + if (result.code === 1) { + uni.requestPayment({ + provider: 'wxpay', + orderInfo: JSON.parse(result.data.pay_data), // 微信订单数据(官方说是string。实测为object) + // orderInfo: result.data.pay_data, + success: res => { + that.payResult('paying') + }, + fail: err => { + console.log('取消支付或支付失败:', err); + err.errMsg !== "requestPayment:fail cancel" && that.payResult( + 'fail'); + return; + uni.showModal({ + title: '温馨提示', + content: `支付取消或者失败。errMsg:${err.errMsg}。errCode:${err.errCode}。code:${err.code}`, + showCancel: false, + success: ret => { + console.log('取消支付或支付失败:', err); + err.errMsg !== "requestPayment:fail cancel" && that.payResult( + 'fail') + } + }) + } + }); + } + } + + // 支付结果跳转,success:成功,fail:失败 + payResult(resultType) { + const that = this; + $Router.replace({ + path: '/pages/order/payment/result', + query: { + orderId: that.order.id, + type: that.payment, //重新支付的时候使用 + payState: resultType, + orderType: that.orderType + } + }); + } + +} \ No newline at end of file