node+koa2实现QQ登陆,vue的回调地址不会触发重定向

最近网站接入了QQ快速登陆,请求都是用的node+koa2,点击QQ登陆后,回调地址只能第一次触发重定向,退出QQ登陆,这里的退出是删除localstorage里面的用户信息,在重新选择QQ登陆,不会触发mounted和created里面的this.$router.push()

async mounted() {
    let code = this.$route.query.code;
    var that = this;

    if (code) {
      var user_info = await axios.get(`${client_api.qq_login}?code=${code}`);

      if (user_info.data.code == 0) {
        that.$router.push("/message");
        if (process.browser) {
          console.log(1);

          localStorage.setItem(
            "user_info",
            JSON.stringify(user_info.data.message)
          );
        }
      }
    }
   
  },`
  
  请问大佬们改如何解决,不知道为什么回调地址不会触发路由的push
  一下代码是node+koa2的
  
  `login.get('/qq_login', function (ctx) {

    var authorization = 'https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=' + qqAppID + '&redirect_uri=' + encodeURIComponent(qqRedirect_uri) + '&state=233&scope=get_user_info,get_vip_info,get_vip_rich_info';
    ctx.body = {
        code: 0,
        message: authorization
    }

});
login.get('/qq/login', async (ctx) => {
    //拿到code
    var code = ctx.query.code;
    var redirect_url = ctx.query.redirect_url;

    //获取token
    var getTokenUrl = 'https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=' + qqAppID + '&client_secret=' + qqAppkey + '&code=' + code + '&redirect_uri=' + qqRedirect_uri;

    let token = await axios.get(getTokenUrl)

    let access_token = token.data.split('=')[1].split('&')[0];
    //获取openid
    let info = await axios.get(`https://graph.qq.com/oauth2.0/me?access_token=${access_token}`);
    console.log(access_token, info.data)
    var jsonStr = info.data.replace('callback( ', '');
    jsonStr = jsonStr.replace(' );', '');
    jsonStr = JSON.parse(jsonStr);
    var qqOpenid = jsonStr['openid'];
    console.log(qqOpenid)
    var qqClient_id = jsonStr['client_id'];

    let user_info_url = 'https://graph.qq.com/user/get_user_info?access_token=' + encodeURIComponent(access_token) + '&oauth_consumer_key=' + encodeURIComponent(qqAppID) + '&openid=' + encodeURIComponent(qqOpenid);

    //根据openid获取用户信息
    let user_info = await axios.get(user_info_url);
    let datas = user_info.data
    var obj = {};
    obj.nickname = datas.nickname
    obj.gender = datas.gender
    obj.province = datas.province
    obj.city = datas.city
    obj.year = datas.year
    obj.figureurl_qq_2 = datas.figureurl_qq_2
    obj.figureurl_qq = datas.figureurl_qq
    obj.qqOpenid = qqOpenid
    const create_qq = await qqModel.createQQ(obj)
    delete obj.qqOpenid
    ctx.body = {
        code: 0,
        message: obj
    }


});
待解决 悬赏分:30 - 离问题结束还有 341天5小时23分4秒
反对 0举报 0 收藏 0

我来回答

回答0