获取 openId
wxReload() {
let _this = this;
let wxInfo = this.getUrlCode();
let userInfo = _this.$store.state.userInfo;
//截取code
let code = wxInfo.code;
//判断是否在缓存中存储了用户信息
if (!_this.$tool.isObjNull(userInfo) && userInfo.userId != null && userInfo.userId != "") {
return;
}
_this.overlayShow = true;
//如果没有code,则去请求
if (code == undefined || code == '' || code == null) {
let url = "https://open.weixin.qq.com/connect/oauth2/authorize";
url += "?appid=" + uni.$wxAppid;
//回调地址就是前端地址
url += "&redirect_uri=" + uni.$frontUrl;
url += "&response_type=code";
url += "&scope=snsapi_userinfo";
url += "&state=1";
url += "#wechat_redirect";
window.location.href = url;
}
//通过code去获取openId
else {
_this.overlayShow = false;
_this.updateWxInfo(wxInfo);
_this.$request("", {
"code": code
}, "wx/getAppId").then(res => {
let wxBody = res.response.body;
let wxHead = res.response.head;
let ackCode = wxHead.ackCode;
if (ackCode == "100.1") {
} else {
uni.showToast({
title: wxHead.ackMessage,
icon: 'none',
duration: 1500
});
}
});
}
},
支付代码
pay() {
let _this = this;
if (_this.$tool.isWechat()) {
_this.$request("", {
"openId": _this.$store.state.userInfo.userId
}, "wx/payment").then(res => {
let response = res.response;
let ackCode = response.head.ackCode;
let msg = response.head.ackMessage;
if (ackCode == "100.1") {
let data = response.body;
WeixinJSBridge.invoke('getBrandWCPayRequest', data,
function(res) {
if (res.err_msg == "get_brand_wcpay_request:ok") {
alter("支付成功")
// 使用以上方式判断前端返回,微信团队郑重提示:
//res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
}
});
} else {
uni.showModal({
title: msg,
icon: 'none',
showCancel: false,
success: function(res) {}
});
}
});
}
},
JAVA 后端代码链接:JAVA + JSAPIv3