自学内容网 自学内容网

vue项目自动登录跳转页面

登录页面,页面加载前设置token

import {  setToken } from '@/utils/auth'

 beforeCreate(){
    // 自动登录重新设置token
    if (this.$route.query.token) {
      setToken('')
      setToken(this.$route.query.token)
      this.$store.commit('SET_TOKEN', '')
      this.$store.commit('SET_TOKEN', this.$route.query.token)
      this.$router.push({ path: '/IOTmanagement/IOTequipment' })
    }
  },
  created() {
    if (!this.$route.query.token) {
      this.getCode();
      this.getCookie();
    }
  },

permission.js加判断if (getToken()&&!to.query.token)


// 自动登录重新设置token
  if (getToken()&&!to.query.token) {
    to.meta.title && store.dispatch('settings/setTitle', to.meta.title)
    /* has token*/
    if (to.path === '/login') {
      next({ path: '/' })
      NProgress.done()
    } else {
      if (store.getters.roles.length === 0) {
        // 判断当前用户是否已拉取完user_info信息
        store.dispatch('GetInfo').then(() => {
          store.dispatch('GenerateRoutes').then(accessRoutes => {
            // 根据roles权限生成可访问的路由表
            router.addRoutes(accessRoutes) // 动态添加可访问路由表
            next({ ...to, replace: true }) // hack方法 确保addRoutes已完成
          })
        }).catch(err => {
            store.dispatch('LogOut').then(() => {
              Message.error(err)
              next({ path: '/' })
            })
          })
      } else {
        next()
      }
    }
  } else {
    // 没有token
    if (whiteList.indexOf(to.path) !== -1) {
      // 在免登录白名单,直接进入
      next()
    } else {
      next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
      NProgress.done()
    }
  }
})

原文地址:https://blog.csdn.net/weixin_38999134/article/details/136205960

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!