【订单支付结果页】

1. 接入微信公众号 MP 支付
pull/1/MERGE
YunaiV 2023-07-06 19:10:53 +08:00
parent bff8bd8dd0
commit 6316405712
6 changed files with 95 additions and 73 deletions

View File

@ -9,7 +9,9 @@ export function getSpuList(recommendType) {
// 获得商品 SPU 分页
export function getSpuPage(data) {
return request.get('app-api/product/spu/page', data);
return request.get('app-api/product/spu/page', data, {
noAuth: true // TODO 芋艿:后续要做调整
});
}
// 查询商品

8
api/system/weixin.js Normal file
View File

@ -0,0 +1,8 @@
import request from "@/utils/request.js";
// 创建微信 JS SDK 初始化所需的签名
export function createJsapiSignature(url) {
return request.post("app-api/system/wx-mp/create-jsapi-signature?url=" + url, {}, {
noAuth: true // TODO 芋艿:后续要做调整
});
}

View File

@ -3,9 +3,9 @@ import WechatJSSDK from "@/plugin/jweixin-module/index.js";
import {
getWechatConfig,
wechatAuth
} from "@/api/public";
import * as WeiXinApi from '@/api/system/weixin.js';
import {
WX_AUTH,
STATE_KEY,
@ -21,47 +21,33 @@ import Cache from '@/utils/cache';
class AuthWechat {
constructor() {
//微信实例化对象
this.instance = WechatJSSDK;
//是否实例化
this.status = false;
this.instance = WechatJSSDK; // 微信实例化对象
this.status = false; // 是否实例化
this.initConfig = {};
}
isAndroid(){
let u = navigator.userAgent;
return u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;
}
signLink() {
if (typeof window.entryUrl === 'undefined' || window.entryUrl === '') {
window.entryUrl = location.href.split('#')[0]
}
return /(Android)/i.test(navigator.userAgent) ? location.href.split('#')[0] : window.entryUrl;
}
/**
* 初始化wechat(分享配置)
* 初始化 wechat(分享配置)
*/
wechat() {
return new Promise((resolve, reject) => {
// if (this.status && !this.isAndroid()) return resolve(this.instance);
getWechatConfig()
.then(res => {
this.instance.config(res.data);
this.initConfig = res.data;
this.status = true;
this.instance.ready(() => {
resolve(this.instance);
})
}).catch(err => {
console.log('微信分享配置失败',err);
this.status = false;
reject(err);
});
// if (this.status) return resolve(this.instance);
WeiXinApi.createJsapiSignature(location.href).then(res => {
// debugger
const jsapiTicket = res.data;
jsapiTicket.jsApiList = ['chooseWXPay']; // TODO 芋艿:这里要设置下
jsapiTicket.debug = false;
this.instance.config(jsapiTicket);
this.initConfig = jsapiTicket;
this.status = true;
this.instance.ready(() => {
resolve(this.instance);
})
}).catch(err => {
console.log('WechatJSSDK 初始化失败 ',err);
this.status = false;
reject(err);
});
});
}
@ -110,8 +96,8 @@ class AuthWechat {
reject(err);
})
});
}
}
// 使用微信内置地图查看位置接口;
seeLocation(config){
return new Promise((resolve, reject) => {
@ -126,7 +112,7 @@ class AuthWechat {
})
});
}
/**
* 微信支付
* @param {Object} config
@ -144,7 +130,7 @@ class AuthWechat {
});
});
}
toPromise(fn, config = {}) {
return new Promise((resolve, reject) => {
fn({
@ -169,6 +155,10 @@ class AuthWechat {
* 自动去授权
*/
oAuth(snsapiBase,url) {
// TODO 芋艿:先链式去掉这个逻辑;
if (true) {
return;
}
if (uni.getStorageSync(WX_AUTH) && store.state.app.token && snsapiBase == 'snsapi_base') return;
const {
code
@ -201,10 +191,6 @@ class AuthWechat {
// }
}
clearAuthStatus() {
}
/**
* 授权登录获取token
* @param {Object} code
@ -220,7 +206,7 @@ class AuthWechat {
Cache.clear(STATE_KEY);
// Cache.clear('spread');
loginType && Cache.clear(LOGINTYPE);
})
.catch(reject);
});
@ -259,7 +245,7 @@ class AuthWechat {
// return `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&state=${state}#wechat_redirect`;
// }
}
/**
* 跳转自动登录
*/

View File

@ -91,7 +91,8 @@
},
"h5" : {
"devServer" : {
"https" : false
"https" : false,
"disableHostCheck": true
},
"router" : {
"mode" : "history",

View File

@ -107,7 +107,6 @@
title: '加载订单中'
});
PayOrderApi.getOrder(this.orderId).then(res => {
// TODO
if (res.data.status === 10) {
uni.showToast({
title: '支付成功'
@ -166,6 +165,9 @@
channelCode: channelCode,
displayMode: 'url', // TODO
returnUrl: this.getPayReturnUrl(),
channelExtras: {
openid: "ockUAwIZ-0OeMZl9ogcZ4ILrGba0"
}
}).then(res => {
this.handleSubmitOrderResult(res.data);
@ -245,29 +247,7 @@
})
// #endif
// #ifdef H5
this.$wechat.pay(res.data.result.jsConfig).then(res => {
return that.$util.Tips({
title: '支付成功',
icon: 'success'
}, {
tab: 5,
url: goPages
});
}).catch(res => {
if (!this.$wechat.isWeixin()) {
uni.redirectTo({
url: goPages + '&msg=' + that.$t(`支付失败`) +
'&status=2'
// '&msg=&status=2'
})
}
if (res.errMsg == 'chooseWXPay:cancel') return that.$util.Tips({
title: '取消支付'
}, {
tab: 5,
url: goPages + '&status=2'
});
})
//
// #endif
// #ifdef APP-PLUS
uni.requestPayment({
@ -409,7 +389,52 @@
window.location = displayContent;
return;
}
// 2.2
// 2.2 CUSTOM
if (displayMode === 'custom') {
if (this.channelCode === 'wx_pub') {
this.handleSubmitOrderResultForWxPub(displayContent)
return;
}
}
},
/**
* 发起微信公众号支付
*/
handleSubmitOrderResultForWxPub(displayContent) {
const payConfig = JSON.parse(displayContent);
this.$wechat.pay({
timestamp: payConfig.timeStamp,
nonceStr: payConfig.nonceStr,
package: payConfig.packageValue,
signType: payConfig.signType,
paySign: payConfig.paySign,
}).then(res => {
//
if (res.errMsg === 'chooseWXPay:cancel') {
return this.$util.Tips({
title: '取消微信支付'
});
}
if (res.errMsg) {
return this.$util.Tips({
title: res.errMsg,
icon: 'error'
})
}
//
return this.$util.Tips({
title: '支付成功',
icon: 'success'
}, () => {
this.goReturnUrl('success');
});
}).catch(res => {
return this.$util.Tips({
title: '初始化微信支付失败,请重试或者选择其它支付方式',
icon: 'error'
})
})
},
/**

File diff suppressed because one or more lines are too long