diff --git a/src/permission.js b/src/permission.js index dfd4e8c..ddd539f 100644 --- a/src/permission.js +++ b/src/permission.js @@ -4,6 +4,8 @@ import 'nprogress/nprogress.css' // progress bar style 进度条样式 import { Message } from 'element-ui' import { getToken } from '@/utils/auth' // getToken from cookie +import { loginByToken, isSubSystem, sysCode } from './settings' +import Vue from 'vue' NProgress.configure({ showSpinner: false })// NProgress configuration @@ -12,8 +14,23 @@ router.beforeEach((to, from, next) => { NProgress.start() // 加载进度条 // 如果有token - const hasToken = getToken() - if (hasToken) { + const oldToken = getToken() // 获取原token + const newToken = to.query.token + console.log('form other system token:' + to.query.token) + debugger + // 允许使用token登录并且新旧token不一致,使用新token重新登录 + if (loginByToken && newToken && oldToken !== newToken) { + // 调用自动登录接口,否则跳转到登录页 + store.dispatch('TokenLogin', newToken).then(() => { + console.log('自动登录成功') + console.log(to.path) + next({ ...to, replace: true }) // 登录成功继续 + }).catch(() => { + next('/401') // 否则全部重定向到401没有权限页面 + }) + } + // 如果cookie中已有token,直接进行下一步 + if (oldToken) { // 登录后进入登录页 if (to.path === '/login') { next({ path: '/' }) @@ -23,14 +40,22 @@ if (store.getters.roleList && store.getters.roleList.length === 0) { // 判断当前用户是否已拉取完user_info信息 try { store.dispatch('GetInfo').then(() => { - store.dispatch('GetMenus').then((accessRoute) => { - router.addRoutes(accessRoute) - next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record - }) + // 如果是子系统,参数为setting文件中的sysCode属性 + if (isSubSystem) { + store.dispatch('GetMenus', { code: sysCode }).then((accessRoute) => { + router.addRoutes(accessRoute) + next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record + }) + } else { // 否则直接获取全部菜单权限 + store.dispatch('GetMenus').then((accessRoute) => { + router.addRoutes(accessRoute) + next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record + }) + } }) } catch (err) { store.dispatch('FedLogOut').then(() => { - Message.error(err || '权限验证失败,请重新登录系统') + // Message.error(err || '权限验证失败,请重新登录系统') next({ path: '/' }) }) } @@ -40,10 +65,32 @@ } } else { // has no token - if (whiteList.indexOf(to.path) !== -1) { // 免登录白名单,直接进入 + // 免登录白名单,直接进入 + if (whiteList.indexOf(to.path) !== -1) { next() } else { - next(`/login?redirect=${to.path}`) // 否则全部重定向到登录页 + // 如果允许通过链接上的token登录 + // 获取链接里的token + const token = to.query.token + console.log('form other system token:' + to.query.token) + if (loginByToken && token) { + // 调用自动登录接口,否则跳转到登录页 + store.dispatch('TokenLogin', token).then(() => { + console.log('自动登录成功') + console.log(to.path) + next({ ...to, replace: true }) // 登录成功继续 + }).catch(() => { + next('/401') // 否则全部重定向到401没有权限页面 + }) + } else { + // 判断是独立运行的子系统还是和其他基础子系统一起的 + const config = Vue.prototype.baseConfig + if (config.singleSys) { + next(`/login`) // 重定向到本系统登录页 + } else { // 跳转到配置的主页 + window.location.href = config.mainPage + } + } NProgress.done() } } diff --git a/src/permission.js b/src/permission.js index dfd4e8c..ddd539f 100644 --- a/src/permission.js +++ b/src/permission.js @@ -4,6 +4,8 @@ import 'nprogress/nprogress.css' // progress bar style 进度条样式 import { Message } from 'element-ui' import { getToken } from '@/utils/auth' // getToken from cookie +import { loginByToken, isSubSystem, sysCode } from './settings' +import Vue from 'vue' NProgress.configure({ showSpinner: false })// NProgress configuration @@ -12,8 +14,23 @@ router.beforeEach((to, from, next) => { NProgress.start() // 加载进度条 // 如果有token - const hasToken = getToken() - if (hasToken) { + const oldToken = getToken() // 获取原token + const newToken = to.query.token + console.log('form other system token:' + to.query.token) + debugger + // 允许使用token登录并且新旧token不一致,使用新token重新登录 + if (loginByToken && newToken && oldToken !== newToken) { + // 调用自动登录接口,否则跳转到登录页 + store.dispatch('TokenLogin', newToken).then(() => { + console.log('自动登录成功') + console.log(to.path) + next({ ...to, replace: true }) // 登录成功继续 + }).catch(() => { + next('/401') // 否则全部重定向到401没有权限页面 + }) + } + // 如果cookie中已有token,直接进行下一步 + if (oldToken) { // 登录后进入登录页 if (to.path === '/login') { next({ path: '/' }) @@ -23,14 +40,22 @@ if (store.getters.roleList && store.getters.roleList.length === 0) { // 判断当前用户是否已拉取完user_info信息 try { store.dispatch('GetInfo').then(() => { - store.dispatch('GetMenus').then((accessRoute) => { - router.addRoutes(accessRoute) - next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record - }) + // 如果是子系统,参数为setting文件中的sysCode属性 + if (isSubSystem) { + store.dispatch('GetMenus', { code: sysCode }).then((accessRoute) => { + router.addRoutes(accessRoute) + next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record + }) + } else { // 否则直接获取全部菜单权限 + store.dispatch('GetMenus').then((accessRoute) => { + router.addRoutes(accessRoute) + next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record + }) + } }) } catch (err) { store.dispatch('FedLogOut').then(() => { - Message.error(err || '权限验证失败,请重新登录系统') + // Message.error(err || '权限验证失败,请重新登录系统') next({ path: '/' }) }) } @@ -40,10 +65,32 @@ } } else { // has no token - if (whiteList.indexOf(to.path) !== -1) { // 免登录白名单,直接进入 + // 免登录白名单,直接进入 + if (whiteList.indexOf(to.path) !== -1) { next() } else { - next(`/login?redirect=${to.path}`) // 否则全部重定向到登录页 + // 如果允许通过链接上的token登录 + // 获取链接里的token + const token = to.query.token + console.log('form other system token:' + to.query.token) + if (loginByToken && token) { + // 调用自动登录接口,否则跳转到登录页 + store.dispatch('TokenLogin', token).then(() => { + console.log('自动登录成功') + console.log(to.path) + next({ ...to, replace: true }) // 登录成功继续 + }).catch(() => { + next('/401') // 否则全部重定向到401没有权限页面 + }) + } else { + // 判断是独立运行的子系统还是和其他基础子系统一起的 + const config = Vue.prototype.baseConfig + if (config.singleSys) { + next(`/login`) // 重定向到本系统登录页 + } else { // 跳转到配置的主页 + window.location.href = config.mainPage + } + } NProgress.done() } } diff --git a/src/settings.js b/src/settings.js index 72192af..29c06d8 100644 --- a/src/settings.js +++ b/src/settings.js @@ -9,7 +9,7 @@ * @type {boolean} true | false * @description 是否显示tagsView */ - tagsView: true, + tagsView: false, /** * @type {boolean} true | false @@ -35,5 +35,20 @@ * The default is only used in the production env * If you want to also use it in dev, you can pass ['production', 'development'] */ - errorLog: 'production' + errorLog: 'production', + /** + * @type {Boolean} + * @description 是否允许token登录 + */ + loginByToken: false, + /** + * @type {Boolean}, + * @description 是否是某个子系统 + */ + isSubSystem: false, + /** + * @type {String}, + * @description 子系统编码,获取菜单权限用 + */ + sysCode: 'abc' } diff --git a/src/permission.js b/src/permission.js index dfd4e8c..ddd539f 100644 --- a/src/permission.js +++ b/src/permission.js @@ -4,6 +4,8 @@ import 'nprogress/nprogress.css' // progress bar style 进度条样式 import { Message } from 'element-ui' import { getToken } from '@/utils/auth' // getToken from cookie +import { loginByToken, isSubSystem, sysCode } from './settings' +import Vue from 'vue' NProgress.configure({ showSpinner: false })// NProgress configuration @@ -12,8 +14,23 @@ router.beforeEach((to, from, next) => { NProgress.start() // 加载进度条 // 如果有token - const hasToken = getToken() - if (hasToken) { + const oldToken = getToken() // 获取原token + const newToken = to.query.token + console.log('form other system token:' + to.query.token) + debugger + // 允许使用token登录并且新旧token不一致,使用新token重新登录 + if (loginByToken && newToken && oldToken !== newToken) { + // 调用自动登录接口,否则跳转到登录页 + store.dispatch('TokenLogin', newToken).then(() => { + console.log('自动登录成功') + console.log(to.path) + next({ ...to, replace: true }) // 登录成功继续 + }).catch(() => { + next('/401') // 否则全部重定向到401没有权限页面 + }) + } + // 如果cookie中已有token,直接进行下一步 + if (oldToken) { // 登录后进入登录页 if (to.path === '/login') { next({ path: '/' }) @@ -23,14 +40,22 @@ if (store.getters.roleList && store.getters.roleList.length === 0) { // 判断当前用户是否已拉取完user_info信息 try { store.dispatch('GetInfo').then(() => { - store.dispatch('GetMenus').then((accessRoute) => { - router.addRoutes(accessRoute) - next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record - }) + // 如果是子系统,参数为setting文件中的sysCode属性 + if (isSubSystem) { + store.dispatch('GetMenus', { code: sysCode }).then((accessRoute) => { + router.addRoutes(accessRoute) + next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record + }) + } else { // 否则直接获取全部菜单权限 + store.dispatch('GetMenus').then((accessRoute) => { + router.addRoutes(accessRoute) + next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record + }) + } }) } catch (err) { store.dispatch('FedLogOut').then(() => { - Message.error(err || '权限验证失败,请重新登录系统') + // Message.error(err || '权限验证失败,请重新登录系统') next({ path: '/' }) }) } @@ -40,10 +65,32 @@ } } else { // has no token - if (whiteList.indexOf(to.path) !== -1) { // 免登录白名单,直接进入 + // 免登录白名单,直接进入 + if (whiteList.indexOf(to.path) !== -1) { next() } else { - next(`/login?redirect=${to.path}`) // 否则全部重定向到登录页 + // 如果允许通过链接上的token登录 + // 获取链接里的token + const token = to.query.token + console.log('form other system token:' + to.query.token) + if (loginByToken && token) { + // 调用自动登录接口,否则跳转到登录页 + store.dispatch('TokenLogin', token).then(() => { + console.log('自动登录成功') + console.log(to.path) + next({ ...to, replace: true }) // 登录成功继续 + }).catch(() => { + next('/401') // 否则全部重定向到401没有权限页面 + }) + } else { + // 判断是独立运行的子系统还是和其他基础子系统一起的 + const config = Vue.prototype.baseConfig + if (config.singleSys) { + next(`/login`) // 重定向到本系统登录页 + } else { // 跳转到配置的主页 + window.location.href = config.mainPage + } + } NProgress.done() } } diff --git a/src/settings.js b/src/settings.js index 72192af..29c06d8 100644 --- a/src/settings.js +++ b/src/settings.js @@ -9,7 +9,7 @@ * @type {boolean} true | false * @description 是否显示tagsView */ - tagsView: true, + tagsView: false, /** * @type {boolean} true | false @@ -35,5 +35,20 @@ * The default is only used in the production env * If you want to also use it in dev, you can pass ['production', 'development'] */ - errorLog: 'production' + errorLog: 'production', + /** + * @type {Boolean} + * @description 是否允许token登录 + */ + loginByToken: false, + /** + * @type {Boolean}, + * @description 是否是某个子系统 + */ + isSubSystem: false, + /** + * @type {String}, + * @description 子系统编码,获取菜单权限用 + */ + sysCode: 'abc' } diff --git a/src/store/modules/user.js b/src/store/modules/user.js index dfbfaf4..d182b32 100644 --- a/src/store/modules/user.js +++ b/src/store/modules/user.js @@ -78,8 +78,8 @@ }) }) }, - // 自动登录接口 - AppLogin({ commit }, token) { + // 用token自动登录接口 + TokenLogin({ commit }, token) { return new Promise((resolve, reject) => { // 如果token存在直接setToken if (token) {