2021-12-20

var url = location.search;

console.log(location);

var theRequest = new Object();

if (location.search.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];

  }

}

router.beforeEach((to, from, next) => {

//   //to 即将要访问的页面

//   //from 从哪个页面跳转过来的

//   //next 是一个函数 表示放行

//   // next()  放行  next('/login')强制跳转

  if (to.path === '/login'){

    return next()

  } else if (to.path === '/h5/login'){

    return next()

  }

  if(to.path.includes('/h5/')){

    const tokenStr = window.localStorage.getItem('h5_user_token')

     if (!tokenStr){

      if(theRequest.code){

        return next('/h5/login?code='+theRequest.code)

      }else{

        return next('/h5/login')

      }

    }

  }else{

    const tokenStr = window.localStorage.getItem('user_token')

    if (!tokenStr){

      return next('/login')

    }

  }

  next()

})




created() {

    //判断当前用户是否登陆

    if (localStorage.getItem("h5_user_token")) {

      this.$router.push("/h5/home");

      retrun;

    }

    let code = this.$route.query.code;

    if (code) {

      let data = { code: code };

      getUserInfo(data).then((res) => {

        if (res.errorCode === 200) {

          if (res.data.is_bind == "1") {

            localStorage.setItem(

              "h5_user_token",

              `${res.data.token_type} ${res.data.access_token}`

            );

            this.$router.push("/h5/home");

          }

        }

      });

    }

  },

你可能感兴趣的:(2021-12-20)