微信公众号授权获取codequ

getCode() {
this.code = ''
let origin = 'httpxxxxxxxxxxxxx/'
let urlNow = encodeURIComponent(origin); ///netbar/api/wechat/v1/get_open_id
let scope = "snsapi_base"; //snsapi_userinfo //静默授权 用户无感知
let appid = "wx4198xxxxxxxxxxxxx";
this.code = this.getUrlCode().code;
console.log(this.code);
console.log(this.getUrlCode())
sessionStorage.setItem( "code", this.getUrlCode().code);
// 截取code
if (this.code == null || this.code === '') { //未授权qu授权
let url =
`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${urlNow}&response_type=code&scope=${scope}&state=123#wechat_redirect`;
sessionStorage.setItem("routers", "routers");
window.location.href = url;
console.log(url)
console.log(window.location.href)
} else {
this.$router.push({
name: "Shopcar"
})
}

getUrlCode() { // 截取url中的code方法
var url = location.search
this.winUrl = url
var theRequest = new Object()
if (url.indexOf("?") != -1) {
var str = url.substr(1)
var strs = str.split("&")
for (var i = 0; i < strs.length; i++) {
theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1])
}
}
return theRequest
},

原文地址:https://www.cnblogs.com/xzhce/p/13277611.html