学企来从pc学习端点击消息直接跳转管理端审核——token一样

在这里插入图片描述

pc端跳转

// 各个环境的地址
let url = location.protocol.includes("https")
  ? "https://***.cn"
  : location.origin.includes("test.***.cn")
  ? "http://***:4000"
  : "http://***"; //http://192.168.2.89:80 本地
let token = this.$store.state.token.replace("Bearer ", "");
location.href = url + "/qualityTrain/questionBank?token=" + token;

管理端 permission.js中路由前置守卫自动登录

import store from './store'
router.beforeEach((to, from, next) => { 
  // pc端消息跳转管理端自动登录(以下三个页面,参数携带token)   
  if ((['/auditManagement/trainAudit', '/auditManagement/systemAudit', '/answer/question', '/qualityTrain/templateAudit'].includes(to.path)) && to.query.token) {
    store.dispatch("autoLogin", to.query.token).then(() => {
      next()
    })
  }
})

vuex

// 自动登录
autoLogin({ commit }, token) {
  return new Promise((resolve, reject) => {
    setToken(token);
    commit("SET_TOKEN", token);
    setExpiresIn(720);
    commit("SET_EXPIRES_IN", 720);
    resolve();
  });
},

完整代码

 messageDetails(item) {
      this.$refs.messageRef.doClose();
      if (item.type == 0) {
        this.$axios
          .get("/other/record/updateType?recordId=" + item.recordId)
          .then((res) => {});
      }
      if ([4, 5, 6, 10, 11].includes(item.messageType * 1)) {
        console.log(location);
        let url = location.protocol.includes("https")
          ? "https://***.cn"
          : location.origin.includes("test.***.cn")
          ? "http://***:4000"
          : "http://***"; //http://192.168.2.89:80 本地
        let token = this.$store.state.token.replace("Bearer ", "");
        if (item.messageType == 10) {
          location.href =
            url +
            "/qualityTrain/questionBank?examineLibraryId=" +
            item.questionId +
            "&token=" +
            token;
        } else if (item.messageType == 11) {
          location.href =
            url + "/qualityTrain/templateAudit?token=" + token;
        } else {
          location.href = [
            url + "/auditManagement/trainAudit?token=" + token,
            url + "/auditManagement/systemAudit?token=" + token,
            url +
              "/answer/question?questionId=" +
              item.questionId +
              "&token=" +
              token,
          ][item.messageType * 1 - 4];
        }
      } else {
        this.$router.push(
          [
            "/course",
            "/xql_exam/exam",
            "/xql_my/feedback",
            "",
            "",
            "",
            "/quality_train/knowledge",
            "/quality_train/myExam",
            "/quality_train/errors?tab=2",
            "",
          ][item.messageType * 1 - 1]
        );
      }
    },
import router from './router'
import store from './store'
import { Message } from 'element-ui'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { getToken } from '@/utils/auth'
import { isRelogin } from '@/utils/request'

NProgress.configure({ showSpinner: false })

const whiteList = ['/login', '/auth-redirect', '/bind', '/register']

router.beforeEach((to, from, next) => {
  NProgress.start()
  // pc端消息跳转管理端自动登录(以下三个页面,参数携带token) 
  if ((['/auditManagement/trainAudit', '/auditManagement/systemAudit', '/answer/question', '/qualityTrain/templateAudit'].includes(to.path)) && to.query.token) {
    store.dispatch("autoLogin", to.query.token).then(() => {
      next()
    })
  }

  if (getToken()) {
    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) {
        isRelogin.show = true
        // 判断当前用户是否已拉取完user_info信息
        store.dispatch('GetInfo').then(() => {
          isRelogin.show = false
          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()
    }
  }
})

router.afterEach(() => {
  NProgress.done()
})


actions: {
    // 自动登录
    autoLogin({ commit }, token) {
      return new Promise((resolve, reject) => {
        setToken(token);
        commit("SET_TOKEN", token);
        setExpiresIn(720);
        commit("SET_EXPIRES_IN", 720);
        resolve();
      });
    },
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim();
      const password = userInfo.password;
      const code = userInfo.code;
      const uuid = userInfo.uuid;
      return new Promise((resolve, reject) => {
        login(username, password, code, uuid)
          .then((res) => {
            let data = res.data;
            setToken(data.access_token);
            commit("SET_TOKEN", data.access_token);
            setExpiresIn(data.expires_in);
            commit("SET_EXPIRES_IN", data.expires_in);
            resolve();
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小曲曲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值