h5微信授权登录

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里进行上述操作

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值