pc端跳转
let url = location.protocol.includes("https")
? "https://***.cn"
: location.origin.includes("test.***.cn")
? "http://***:4000"
: "http://***";
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) => {
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://***";
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()
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)
if (to.path === '/login') {
next({ path: '/' })
NProgress.done()
} else {
if (store.getters.roles.length === 0) {
isRelogin.show = true
store.dispatch('GetInfo').then(() => {
isRelogin.show = false
store.dispatch('GenerateRoutes').then(accessRoutes => {
router.addRoutes(accessRoutes)
next({ ...to, replace: true })
})
}).catch(err => {
store.dispatch('LogOut').then(() => {
Message.error(err)
next({ path: '/' })
})
})
} else {
next()
}
}
} else {
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);
});
});
},
}