我们用的是【免确认模式】https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter3_1_3.shtml
- 查询授权情况
后端调用返回给你
- 拿支付分的预授权 token
后端调用返回给你
- 去预授权
uni.navigateToMiniProgram({
appId: 'wxd8f3793ea3b935b8', // 固定就这么写
path: 'pages/use/enable', // 固定就这么写
extraData: {
apply_permissions_token: res.data // res.data是第2步给的token
},
success() {
// 此处是跳转支付分授权页面成功,不是授权成功
}
});
贴出我的相关代码:
// 查询支付分授权情况
queryPayCreditAuthorization() {
this.useElectricityBTNdisabled = true;
this.usePayCredit = true;
this.$request("/wechat/payScore/getPermissionsByOpenid", "POST", {
userId: uni.getStorageSync("openId")
}).then(res => {
console.log("授权情况:", res);
if (res.data.status === 200) {
// 未授权
if (res.data.returnStr.authorization_state === "UNAVAILABLE") {
this.authorizationPayCredit();
}
// 已授权
if (res.data.returnStr.authorization_state === "AVAILABLE") {
this.portISUsed();
}
};
// 无授权记录
if (res.data.status === 404) {
this.authorizationPayCredit();
}
if (res.data.status !== 200) {
uni.showToast({
title: res.data.returnStr.message,
icon: "none"
})
}
});
},
// 拿支付分的预授权 token
authorizationPayCredit() {
const that = this;
this.$request("/wechat/payScore/permissions", "POST", {
userId: uni.getStorageSync("openId")
}).then(res => {
console.log(res);
// 去预授权
uni.navigateToMiniProgram({
appId: 'wxd8f3793ea3b935b8', // 固定就这么写
path: 'pages/use/enable', // 固定就这么写
extraData: {
apply_permissions_token: res.data
},
success() {
that.useElectricityBTNdisabled = false;
}
});
})
},
剩下就是解除授权、退款之类的,由后端调用微信的公共API,然后返回给你:
查看支付分订单详情需要前端调:
uni.navigateToMiniProgram({
appId: 'wxd8f3793ea3b935b8', // 固定就这么写
path: 'pages/record/detail', // 固定就这么写
extraData: res.data // res.data是后端给的参数
});