【订单支付结果页】

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 分页 // 获得商品 SPU 分页
export function getSpuPage(data) { 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 { import {
getWechatConfig,
wechatAuth wechatAuth
} from "@/api/public"; } from "@/api/public";
import * as WeiXinApi from '@/api/system/weixin.js';
import { import {
WX_AUTH, WX_AUTH,
STATE_KEY, STATE_KEY,
@ -21,47 +21,33 @@ import Cache from '@/utils/cache';
class AuthWechat { class AuthWechat {
constructor() { constructor() {
//微信实例化对象 this.instance = WechatJSSDK; // 微信实例化对象
this.instance = WechatJSSDK; this.status = false; // 是否实例化
//是否实例化
this.status = false;
this.initConfig = {}; 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() { wechat() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// if (this.status && !this.isAndroid()) return resolve(this.instance); // if (this.status) return resolve(this.instance);
getWechatConfig() WeiXinApi.createJsapiSignature(location.href).then(res => {
.then(res => { // debugger
this.instance.config(res.data); const jsapiTicket = res.data;
this.initConfig = res.data; jsapiTicket.jsApiList = ['chooseWXPay']; // TODO 芋艿:这里要设置下
this.status = true; jsapiTicket.debug = false;
this.instance.ready(() => { this.instance.config(jsapiTicket);
resolve(this.instance); this.initConfig = jsapiTicket;
}) this.status = true;
}).catch(err => { this.instance.ready(() => {
console.log('微信分享配置失败',err); resolve(this.instance);
this.status = false; })
reject(err); }).catch(err => {
}); console.log('WechatJSSDK 初始化失败 ',err);
this.status = false;
reject(err);
});
}); });
} }
@ -110,8 +96,8 @@ class AuthWechat {
reject(err); reject(err);
}) })
}); });
} }
// 使用微信内置地图查看位置接口; // 使用微信内置地图查看位置接口;
seeLocation(config){ seeLocation(config){
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -126,7 +112,7 @@ class AuthWechat {
}) })
}); });
} }
/** /**
* 微信支付 * 微信支付
* @param {Object} config * @param {Object} config
@ -144,7 +130,7 @@ class AuthWechat {
}); });
}); });
} }
toPromise(fn, config = {}) { toPromise(fn, config = {}) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fn({ fn({
@ -169,6 +155,10 @@ class AuthWechat {
* 自动去授权 * 自动去授权
*/ */
oAuth(snsapiBase,url) { oAuth(snsapiBase,url) {
// TODO 芋艿:先链式去掉这个逻辑;
if (true) {
return;
}
if (uni.getStorageSync(WX_AUTH) && store.state.app.token && snsapiBase == 'snsapi_base') return; if (uni.getStorageSync(WX_AUTH) && store.state.app.token && snsapiBase == 'snsapi_base') return;
const { const {
code code
@ -201,10 +191,6 @@ class AuthWechat {
// } // }
} }
clearAuthStatus() {
}
/** /**
* 授权登录获取token * 授权登录获取token
* @param {Object} code * @param {Object} code
@ -220,7 +206,7 @@ class AuthWechat {
Cache.clear(STATE_KEY); Cache.clear(STATE_KEY);
// Cache.clear('spread'); // Cache.clear('spread');
loginType && Cache.clear(LOGINTYPE); loginType && Cache.clear(LOGINTYPE);
}) })
.catch(reject); .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`; // 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" : { "h5" : {
"devServer" : { "devServer" : {
"https" : false "https" : false,
"disableHostCheck": true
}, },
"router" : { "router" : {
"mode" : "history", "mode" : "history",

View File

@ -107,7 +107,6 @@
title: '加载订单中' title: '加载订单中'
}); });
PayOrderApi.getOrder(this.orderId).then(res => { PayOrderApi.getOrder(this.orderId).then(res => {
// TODO
if (res.data.status === 10) { if (res.data.status === 10) {
uni.showToast({ uni.showToast({
title: '支付成功' title: '支付成功'
@ -166,6 +165,9 @@
channelCode: channelCode, channelCode: channelCode,
displayMode: 'url', // TODO displayMode: 'url', // TODO
returnUrl: this.getPayReturnUrl(), returnUrl: this.getPayReturnUrl(),
channelExtras: {
openid: "ockUAwIZ-0OeMZl9ogcZ4ILrGba0"
}
}).then(res => { }).then(res => {
this.handleSubmitOrderResult(res.data); this.handleSubmitOrderResult(res.data);
@ -245,29 +247,7 @@
}) })
// #endif // #endif
// #ifdef H5 // #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 // #endif
// #ifdef APP-PLUS // #ifdef APP-PLUS
uni.requestPayment({ uni.requestPayment({
@ -409,7 +389,52 @@
window.location = displayContent; window.location = displayContent;
return; 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