diff --git a/src/permission.js b/src/permission.js index 96e5f73..ea80a31 100644 --- a/src/permission.js +++ b/src/permission.js @@ -3,9 +3,10 @@ import NProgress from 'nprogress' // progress bar 进度条 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 { getToken, getUser, setUser } from '@/utils/auth' // getToken from cookie +import { loginByToken, isSubSystem, sysCode, loginByUsername } from './settings' import Vue from 'vue' +import { RSAencrypt } from '@/utils/security' NProgress.configure({ showSpinner: false })// NProgress configuration @@ -27,70 +28,89 @@ }).catch(() => { next('/401') // 否则全部重定向到401没有权限页面 }) - } - // 如果cookie中已有token,直接进行下一步 - if (oldToken) { - // 登录后进入登录页 - if (to.path === '/login') { - next({ path: '/' }) - NProgress.done() // if current page is dashboard will not trigger afterEach hook, so manually handle it + } else { + // 如果链接中有用户名和密码 + const oldUser = getUser() + const username = to.query.username + const password = to.query.pwd + debugger + if (loginByUsername && username && password && !oldUser) { // 登陆后跳转 + const loginForm = { + username: username, + password: RSAencrypt(password) + } + store.dispatch('Login', loginForm).then(() => { + setUser(username) + next({ ...to, replace: true }) // 登录成功继续 + }).catch(() => { + next('/401') // 否则全部重定向到401没有权限页面 + }) } else { - // 当进入非登陆页时 - if (store.getters.roleList && store.getters.roleList.length === 0) { // 判断当前用户是否已拉取完user_info信息 - try { - store.dispatch('GetInfo').then(() => { - // 如果是子系统,参数为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 + // 如果cookie中已有token,直接进行下一步 + if (oldToken) { + // 登录后进入登录页 + if (to.path === '/login') { + next({ path: '/' }) + NProgress.done() // if current page is dashboard will not trigger afterEach hook, so manually handle it + } else { + // 当进入非登陆页时 + if (store.getters.roleList && store.getters.roleList.length === 0) { // 判断当前用户是否已拉取完user_info信息 + try { + store.dispatch('GetInfo').then(() => { + // 如果是子系统,参数为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 + }) + } }) - } 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 || '权限验证失败,请重新登录系统') + next({ path: '/' }) }) } - }) - } catch (err) { - store.dispatch('FedLogOut').then(() => { - // Message.error(err || '权限验证失败,请重新登录系统') - next({ path: '/' }) - }) + } else { // 角色不为空 + next() + } } - } else { // 角色不为空 - next() - } - } - } else { - // has no token - // 免登录白名单,直接进入 - if (whiteList.indexOf(to.path) !== -1) { - next() - } else { - // 如果允许通过链接上的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 + // has no token + // 免登录白名单,直接进入 + if (whiteList.indexOf(to.path) !== -1) { + next() + } else { + // 如果允许通过链接上的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() } } - NProgress.done() } } }) diff --git a/src/permission.js b/src/permission.js index 96e5f73..ea80a31 100644 --- a/src/permission.js +++ b/src/permission.js @@ -3,9 +3,10 @@ import NProgress from 'nprogress' // progress bar 进度条 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 { getToken, getUser, setUser } from '@/utils/auth' // getToken from cookie +import { loginByToken, isSubSystem, sysCode, loginByUsername } from './settings' import Vue from 'vue' +import { RSAencrypt } from '@/utils/security' NProgress.configure({ showSpinner: false })// NProgress configuration @@ -27,70 +28,89 @@ }).catch(() => { next('/401') // 否则全部重定向到401没有权限页面 }) - } - // 如果cookie中已有token,直接进行下一步 - if (oldToken) { - // 登录后进入登录页 - if (to.path === '/login') { - next({ path: '/' }) - NProgress.done() // if current page is dashboard will not trigger afterEach hook, so manually handle it + } else { + // 如果链接中有用户名和密码 + const oldUser = getUser() + const username = to.query.username + const password = to.query.pwd + debugger + if (loginByUsername && username && password && !oldUser) { // 登陆后跳转 + const loginForm = { + username: username, + password: RSAencrypt(password) + } + store.dispatch('Login', loginForm).then(() => { + setUser(username) + next({ ...to, replace: true }) // 登录成功继续 + }).catch(() => { + next('/401') // 否则全部重定向到401没有权限页面 + }) } else { - // 当进入非登陆页时 - if (store.getters.roleList && store.getters.roleList.length === 0) { // 判断当前用户是否已拉取完user_info信息 - try { - store.dispatch('GetInfo').then(() => { - // 如果是子系统,参数为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 + // 如果cookie中已有token,直接进行下一步 + if (oldToken) { + // 登录后进入登录页 + if (to.path === '/login') { + next({ path: '/' }) + NProgress.done() // if current page is dashboard will not trigger afterEach hook, so manually handle it + } else { + // 当进入非登陆页时 + if (store.getters.roleList && store.getters.roleList.length === 0) { // 判断当前用户是否已拉取完user_info信息 + try { + store.dispatch('GetInfo').then(() => { + // 如果是子系统,参数为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 + }) + } }) - } 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 || '权限验证失败,请重新登录系统') + next({ path: '/' }) }) } - }) - } catch (err) { - store.dispatch('FedLogOut').then(() => { - // Message.error(err || '权限验证失败,请重新登录系统') - next({ path: '/' }) - }) + } else { // 角色不为空 + next() + } } - } else { // 角色不为空 - next() - } - } - } else { - // has no token - // 免登录白名单,直接进入 - if (whiteList.indexOf(to.path) !== -1) { - next() - } else { - // 如果允许通过链接上的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 + // has no token + // 免登录白名单,直接进入 + if (whiteList.indexOf(to.path) !== -1) { + next() + } else { + // 如果允许通过链接上的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() } } - NProgress.done() } } }) diff --git a/src/settings.js b/src/settings.js index 58b4117..e639076 100644 --- a/src/settings.js +++ b/src/settings.js @@ -40,7 +40,12 @@ * @type {Boolean} * @description 是否允许token登录 */ - loginByToken: false, + loginByToken: true, + /** + * @type {Boolean} + * @description 是否允许用户名+密码登录 + */ + loginByUsername: true, /** * @type {Boolean}, * @description 是否是某个子系统 diff --git a/src/permission.js b/src/permission.js index 96e5f73..ea80a31 100644 --- a/src/permission.js +++ b/src/permission.js @@ -3,9 +3,10 @@ import NProgress from 'nprogress' // progress bar 进度条 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 { getToken, getUser, setUser } from '@/utils/auth' // getToken from cookie +import { loginByToken, isSubSystem, sysCode, loginByUsername } from './settings' import Vue from 'vue' +import { RSAencrypt } from '@/utils/security' NProgress.configure({ showSpinner: false })// NProgress configuration @@ -27,70 +28,89 @@ }).catch(() => { next('/401') // 否则全部重定向到401没有权限页面 }) - } - // 如果cookie中已有token,直接进行下一步 - if (oldToken) { - // 登录后进入登录页 - if (to.path === '/login') { - next({ path: '/' }) - NProgress.done() // if current page is dashboard will not trigger afterEach hook, so manually handle it + } else { + // 如果链接中有用户名和密码 + const oldUser = getUser() + const username = to.query.username + const password = to.query.pwd + debugger + if (loginByUsername && username && password && !oldUser) { // 登陆后跳转 + const loginForm = { + username: username, + password: RSAencrypt(password) + } + store.dispatch('Login', loginForm).then(() => { + setUser(username) + next({ ...to, replace: true }) // 登录成功继续 + }).catch(() => { + next('/401') // 否则全部重定向到401没有权限页面 + }) } else { - // 当进入非登陆页时 - if (store.getters.roleList && store.getters.roleList.length === 0) { // 判断当前用户是否已拉取完user_info信息 - try { - store.dispatch('GetInfo').then(() => { - // 如果是子系统,参数为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 + // 如果cookie中已有token,直接进行下一步 + if (oldToken) { + // 登录后进入登录页 + if (to.path === '/login') { + next({ path: '/' }) + NProgress.done() // if current page is dashboard will not trigger afterEach hook, so manually handle it + } else { + // 当进入非登陆页时 + if (store.getters.roleList && store.getters.roleList.length === 0) { // 判断当前用户是否已拉取完user_info信息 + try { + store.dispatch('GetInfo').then(() => { + // 如果是子系统,参数为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 + }) + } }) - } 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 || '权限验证失败,请重新登录系统') + next({ path: '/' }) }) } - }) - } catch (err) { - store.dispatch('FedLogOut').then(() => { - // Message.error(err || '权限验证失败,请重新登录系统') - next({ path: '/' }) - }) + } else { // 角色不为空 + next() + } } - } else { // 角色不为空 - next() - } - } - } else { - // has no token - // 免登录白名单,直接进入 - if (whiteList.indexOf(to.path) !== -1) { - next() - } else { - // 如果允许通过链接上的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 + // has no token + // 免登录白名单,直接进入 + if (whiteList.indexOf(to.path) !== -1) { + next() + } else { + // 如果允许通过链接上的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() } } - NProgress.done() } } }) diff --git a/src/settings.js b/src/settings.js index 58b4117..e639076 100644 --- a/src/settings.js +++ b/src/settings.js @@ -40,7 +40,12 @@ * @type {Boolean} * @description 是否允许token登录 */ - loginByToken: false, + loginByToken: true, + /** + * @type {Boolean} + * @description 是否允许用户名+密码登录 + */ + loginByUsername: true, /** * @type {Boolean}, * @description 是否是某个子系统 diff --git a/src/utils/auth.js b/src/utils/auth.js index 65691f6..28fee48 100644 --- a/src/utils/auth.js +++ b/src/utils/auth.js @@ -1,6 +1,7 @@ import Cookies from 'js-cookie' const TokenKey = 'token' // token:cookie中 +const UserKey = 'user' // token:cookie中 const PublicKey = 'public_key' // 公钥:localStorage中 const CurrentSystem = 'current_system' // 当前系统,未必每个项目都有用:localStorage中 const WsUrl = 'ws_url' @@ -17,6 +18,18 @@ return Cookies.remove(TokenKey) } +export function getUser() { + return Cookies.get(UserKey) +} + +export function setUser(user) { + return Cookies.set(UserKey, user) +} + +export function removeUser() { + return Cookies.remove(UserKey) +} + export function getPublicKey() { return window.localStorage.getItem(PublicKey) }