const query = this.$route.query; //查询信息在我的路由参数里
let str = window.location.href
let result = str.substring(str.indexOf("?") + 1, str.indexOf("#"));
this.location = result
this.str = result
let token = sessionStorage.getItem('Token')
// 如果有token直接查询业务接口
if (token) {
this.getData(query.vehicleNo)
} else if (result) {
// 提取code
const params = new URLSearchParams(result);
let myObject = {};
for(let param of params) {
myObject[param[0]] = param[1];
}
if (myObject.code) {
// 拿到code调用登录 获取token
login(myObject.code).then(res => {
// 存储token到sessionStorage并在请求拦截器里加入token
sessionStorage.setItem('Token', res.data)
this.token = res.data
// 查询业务接口
this.getData(query.vehicleNo)
})
} else {
// 没有code进行微信重定向到当前页面
let appid = "xxxxxx";
// 根据文档对重定向的页面做encode操作
let url = encodeURIComponent(window.location.href)
// scope有静默和用户授权两种形式,根据需求而定
let wx_url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${url}&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect`
window.location.href = wx_url;
}
}
在create里进行上述操作