onLoad() {
console.log("token=>", localStorage.getItem('token'))
var ua = navigator.userAgent.toLowerCase();
var isWeixin = ua.indexOf('micromessenger') != -1;
if (isWeixin) {
console.log("微信浏览器内打开")
this.getWXCode()
} else {
console.log("其他浏览器内打开")
}
},
// 获取code并登录
getWXCode() {
let appid = '' //公众号的唯一标识
let code = this.getUrlCode().code; //是否存在code
let url = '' //授权后重定向的回调链接地址
if (code == null || code === "") {
window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appid +
'&redirect_uri=' + encodeURIComponent(url) +
'&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect'
} else {
this.code = code;
console.log(this.code)
//此处调用后端提供的接口,传入获取到的code换取access_token
}
},
// 截取url中的code方法
getUrlCode() {
// 截取url中的code方法
var url = location.search;
console.log(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;
},
08-05
596
10-28
1327
12-29