diff --git a/src/permission.js b/src/permission.js index 85e10ad..fe9746e 100644 --- a/src/permission.js +++ b/src/permission.js @@ -1,126 +1,165 @@ -import router from './router' -import store from './store' -import NProgress from 'nprogress' // progress bar 进度条 -import 'nprogress/nprogress.css' // progress bar style 进度条样式 +import router from "./router"; +import store from "./store"; +import NProgress from "nprogress"; // progress bar 进度条 +import "nprogress/nprogress.css"; // progress bar style 进度条样式 // import { Message } from 'element-ui' -import { getToken, getUser, setUser } from '@/utils/auth' // getToken from cookie -import { loginByToken, isSubSystem, sysCode, loginByUsername } from './settings' -import Vue from 'vue' -import { setToken, removeToken } from '@/utils/auth' -import { RSAencrypt } from '@/utils/security' -import { uerLogin } from '@/api/overview/wellOverview' +import { getToken, getUser, setUser } from "@/utils/auth"; // getToken from cookie +import { + loginByToken, + isSubSystem, + sysCode, + loginByUsername, +} from "./settings"; +import Vue from "vue"; +import { setToken, removeToken } from "@/utils/auth"; +import { RSAencrypt } from "@/utils/security"; +import { uerLogin } from "@/api/overview/wellOverview"; +import { extractURLParameters } from "@/utils/validate"; -NProgress.configure({ showSpinner: false })// NProgress configuration +NProgress.configure({ showSpinner: false }); // NProgress configuration -const whiteList = ['/login', 'config/baseconfig', '/overview3d'] // 不重定向白名单 +const whiteList = ["/login", "config/baseconfig", "/overview3d"]; // 不重定向白名单 // 全局钩子 router.beforeEach((to, from, next) => { - NProgress.start() // 加载进度条 + NProgress.start(); // 加载进度条 // 如果有token - const oldToken = getToken() // 获取原token - const newToken = to.query.token - console.log('form other system token:' + to.query.token) + const oldToken = getToken(); // 获取原token + const newToken = to.query.token; + console.log("原Token:" + oldToken); + console.log("新Token:" + newToken); + console.log("form other system token:" + to.query.token); // 允许使用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没有权限页面 - }) + console.log('允许使用token登录并且新旧token不一致,使用新token重新登录,调用自动登录接口,否则跳转到登录页'); + store + .dispatch("TokenLogin", newToken) + .then(() => { + console.log("自动登录成功"); + console.log(to.path); + next({ ...to, replace: true }); // 登录成功继续 + }) + .catch(() => { + next("/401"); // 否则全部重定向到401没有权限页面 + }); } else { // 如果cookie中已有token,直接进行下一步 if (oldToken) { // 当进入非登陆页时 - if (store.getters.roleList && store.getters.roleList.length === 0) { // 判断当前用户是否已拉取完user_info信息 + if (store.getters.roleList && store.getters.roleList.length === 0) { + // 判断当前用户是否已拉取完user_info信息 try { - store.dispatch('GetInfo').then(() => { + 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 - }) + 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(() => { + store.dispatch("FedLogOut").then(() => { // Message.error(err || '权限验证失败,请重新登录系统') - next({ path: '/' }) - }) + next({ path: "/" }); + }); } - } else { // 角色不为空 - next() + } else { + // 角色不为空 + next(); } - } - else { + } else { // has no token // 免登录白名单,直接进入 - if(to.path === '/overview3d') { + if (to.path === "/overview3d") { // console.log(to.query) - const userCode = to.query.userCode?to.query.userCode:to.query.usercode - uerLogin(userCode).then(response => { + const userCode = to.query.userCode + ? to.query.userCode + : to.query.usercode; + uerLogin(userCode).then((response) => { if (response.code === 200) { - setToken(response.data.token) - store.dispatch('TokenLogin', response.data.token).then(() => { + setToken(response.data.token); + store.dispatch("TokenLogin", response.data.token).then(() => { try { - store.dispatch('GetInfo').then(() => { + 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 - }) + 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) {} - }) + }); } - }) + }); } else if (whiteList.indexOf(to.path) !== -1) { - next() + next(); // next({ ...to, replace: true }) // 登录成功继续 } 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 - } + const token = to.query.token; + console.log("form other system token:" + to.query.token); + let zfToken = ""; + // 携带token 需要去掉 + const hrefIdx = window.location.href.indexOf("ZF-TOKEN"); // url中是否携带token + if (hrefIdx > -1) { + console.log("hrefIdx", extractURLParameters(window.location.href)); + const params = extractURLParameters(window.location.href); + zfToken = params["ZF-TOKEN"]; } - NProgress.done() + console.log("zfToken" + zfToken); + if (zfToken) { + console.log("跳转"); + } else { + if (loginByToken && token) { + // 调用自动登录接口,否则跳转到登录页 + console.log("调用自动登录接口,否则跳转到登录页"); + 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(); + } } } } -}) +}); router.afterEach(() => { - NProgress.done() // 结束Progress -}) + NProgress.done(); // 结束Progress +}); diff --git a/src/permission.js b/src/permission.js index 85e10ad..fe9746e 100644 --- a/src/permission.js +++ b/src/permission.js @@ -1,126 +1,165 @@ -import router from './router' -import store from './store' -import NProgress from 'nprogress' // progress bar 进度条 -import 'nprogress/nprogress.css' // progress bar style 进度条样式 +import router from "./router"; +import store from "./store"; +import NProgress from "nprogress"; // progress bar 进度条 +import "nprogress/nprogress.css"; // progress bar style 进度条样式 // import { Message } from 'element-ui' -import { getToken, getUser, setUser } from '@/utils/auth' // getToken from cookie -import { loginByToken, isSubSystem, sysCode, loginByUsername } from './settings' -import Vue from 'vue' -import { setToken, removeToken } from '@/utils/auth' -import { RSAencrypt } from '@/utils/security' -import { uerLogin } from '@/api/overview/wellOverview' +import { getToken, getUser, setUser } from "@/utils/auth"; // getToken from cookie +import { + loginByToken, + isSubSystem, + sysCode, + loginByUsername, +} from "./settings"; +import Vue from "vue"; +import { setToken, removeToken } from "@/utils/auth"; +import { RSAencrypt } from "@/utils/security"; +import { uerLogin } from "@/api/overview/wellOverview"; +import { extractURLParameters } from "@/utils/validate"; -NProgress.configure({ showSpinner: false })// NProgress configuration +NProgress.configure({ showSpinner: false }); // NProgress configuration -const whiteList = ['/login', 'config/baseconfig', '/overview3d'] // 不重定向白名单 +const whiteList = ["/login", "config/baseconfig", "/overview3d"]; // 不重定向白名单 // 全局钩子 router.beforeEach((to, from, next) => { - NProgress.start() // 加载进度条 + NProgress.start(); // 加载进度条 // 如果有token - const oldToken = getToken() // 获取原token - const newToken = to.query.token - console.log('form other system token:' + to.query.token) + const oldToken = getToken(); // 获取原token + const newToken = to.query.token; + console.log("原Token:" + oldToken); + console.log("新Token:" + newToken); + console.log("form other system token:" + to.query.token); // 允许使用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没有权限页面 - }) + console.log('允许使用token登录并且新旧token不一致,使用新token重新登录,调用自动登录接口,否则跳转到登录页'); + store + .dispatch("TokenLogin", newToken) + .then(() => { + console.log("自动登录成功"); + console.log(to.path); + next({ ...to, replace: true }); // 登录成功继续 + }) + .catch(() => { + next("/401"); // 否则全部重定向到401没有权限页面 + }); } else { // 如果cookie中已有token,直接进行下一步 if (oldToken) { // 当进入非登陆页时 - if (store.getters.roleList && store.getters.roleList.length === 0) { // 判断当前用户是否已拉取完user_info信息 + if (store.getters.roleList && store.getters.roleList.length === 0) { + // 判断当前用户是否已拉取完user_info信息 try { - store.dispatch('GetInfo').then(() => { + 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 - }) + 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(() => { + store.dispatch("FedLogOut").then(() => { // Message.error(err || '权限验证失败,请重新登录系统') - next({ path: '/' }) - }) + next({ path: "/" }); + }); } - } else { // 角色不为空 - next() + } else { + // 角色不为空 + next(); } - } - else { + } else { // has no token // 免登录白名单,直接进入 - if(to.path === '/overview3d') { + if (to.path === "/overview3d") { // console.log(to.query) - const userCode = to.query.userCode?to.query.userCode:to.query.usercode - uerLogin(userCode).then(response => { + const userCode = to.query.userCode + ? to.query.userCode + : to.query.usercode; + uerLogin(userCode).then((response) => { if (response.code === 200) { - setToken(response.data.token) - store.dispatch('TokenLogin', response.data.token).then(() => { + setToken(response.data.token); + store.dispatch("TokenLogin", response.data.token).then(() => { try { - store.dispatch('GetInfo').then(() => { + 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 - }) + 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) {} - }) + }); } - }) + }); } else if (whiteList.indexOf(to.path) !== -1) { - next() + next(); // next({ ...to, replace: true }) // 登录成功继续 } 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 - } + const token = to.query.token; + console.log("form other system token:" + to.query.token); + let zfToken = ""; + // 携带token 需要去掉 + const hrefIdx = window.location.href.indexOf("ZF-TOKEN"); // url中是否携带token + if (hrefIdx > -1) { + console.log("hrefIdx", extractURLParameters(window.location.href)); + const params = extractURLParameters(window.location.href); + zfToken = params["ZF-TOKEN"]; } - NProgress.done() + console.log("zfToken" + zfToken); + if (zfToken) { + console.log("跳转"); + } else { + if (loginByToken && token) { + // 调用自动登录接口,否则跳转到登录页 + console.log("调用自动登录接口,否则跳转到登录页"); + 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(); + } } } } -}) +}); router.afterEach(() => { - NProgress.done() // 结束Progress -}) + NProgress.done(); // 结束Progress +}); diff --git a/src/utils/validate.js b/src/utils/validate.js index d6d56eb..35f1d33 100644 --- a/src/utils/validate.js +++ b/src/utils/validate.js @@ -7,35 +7,36 @@ * 是否是外部地址 */ export function isExternal(path) { - return /^(https?:|mailto:|tel:)/.test(path) + return /^(https?:|mailto:|tel:)/.test(path); } /* 合法uri*/ export function validateURL(textval) { - const urlregex = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/ - return urlregex.test(textval) + const urlregex = + /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/; + return urlregex.test(textval); } /* 合法uri*/ export function validateNull(textval) { - return !!textval + return !!textval; } /* 验证小写字母*/ export function validateLowerCase(str) { - const reg = /^[a-z]+$/ - return reg.test(str) + const reg = /^[a-z]+$/; + return reg.test(str); } /* 验证大写字母*/ export function validateUpperCase(str) { - const reg = /^[A-Z]+$/ - return reg.test(str) + const reg = /^[A-Z]+$/; + return reg.test(str); } /* 验证大小写字母*/ export function validateAlphabets(str) { - const reg = /^[A-Za-z]+$/ - return reg.test(str) + const reg = /^[A-Za-z]+$/; + return reg.test(str); } /** @@ -44,8 +45,9 @@ * @returns {boolean} */ export function validateEmail(email) { - const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ - return re.test(email) + const re = + /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; + return re.test(email); } /** @@ -54,16 +56,16 @@ * @returns {boolean} */ export function validatePhone(phone) { - const re = /^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/ - return re.test(phone) + const re = /^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/; + return re.test(phone); } /** -* @description 验证手机号码 -* @param phone 被验证手机号 -* @return Boolean -*/ + * @description 验证手机号码 + * @param phone 被验证手机号 + * @return Boolean + */ export function validateisPhone(phone) { - const reg = /^[1][3-9]\d{9}$/ + const reg = /^[1][3-9]\d{9}$/; return reg.test(phone); } @@ -71,30 +73,30 @@ * 验证一定范围内的非负整数 */ export function validateIntPlus(value, min, max) { - const re = /^\d+$/ + const re = /^\d+$/; if (re.test(value)) { - const data = parseInt(value) + const data = parseInt(value); if (data > max || data < min) { - return false + return false; } } else { - return true + return true; } } /** * 验证一定范围内的非负小数 */ export function validateFloatPlus(value, min, max) { - const re = /^\d+(\.\d+)?$/ + const re = /^\d+(\.\d+)?$/; if (re.test(value)) { - const data = parseFloat(value) + const data = parseFloat(value); if (data > max || data < min) { - return false + return false; } else { - return true + return true; } } else { - return false + return false; } } @@ -102,6 +104,14 @@ * 常用正则表达式 */ export const regularReg = { - password: '', // 密码 - mobile: /^1[34578]\d{9}$/ // 手机号码 + password: "", // 密码 + mobile: /^1[34578]\d{9}$/, // 手机号码 +}; + +export function extractURLParameters(url) { + const params = new URLSearchParams(url.split("?")[1]); + return Array.from(params.entries()).reduce((acc, [key, value]) => { + acc[key] = value; + return acc; + }, {}); } diff --git a/src/permission.js b/src/permission.js index 85e10ad..fe9746e 100644 --- a/src/permission.js +++ b/src/permission.js @@ -1,126 +1,165 @@ -import router from './router' -import store from './store' -import NProgress from 'nprogress' // progress bar 进度条 -import 'nprogress/nprogress.css' // progress bar style 进度条样式 +import router from "./router"; +import store from "./store"; +import NProgress from "nprogress"; // progress bar 进度条 +import "nprogress/nprogress.css"; // progress bar style 进度条样式 // import { Message } from 'element-ui' -import { getToken, getUser, setUser } from '@/utils/auth' // getToken from cookie -import { loginByToken, isSubSystem, sysCode, loginByUsername } from './settings' -import Vue from 'vue' -import { setToken, removeToken } from '@/utils/auth' -import { RSAencrypt } from '@/utils/security' -import { uerLogin } from '@/api/overview/wellOverview' +import { getToken, getUser, setUser } from "@/utils/auth"; // getToken from cookie +import { + loginByToken, + isSubSystem, + sysCode, + loginByUsername, +} from "./settings"; +import Vue from "vue"; +import { setToken, removeToken } from "@/utils/auth"; +import { RSAencrypt } from "@/utils/security"; +import { uerLogin } from "@/api/overview/wellOverview"; +import { extractURLParameters } from "@/utils/validate"; -NProgress.configure({ showSpinner: false })// NProgress configuration +NProgress.configure({ showSpinner: false }); // NProgress configuration -const whiteList = ['/login', 'config/baseconfig', '/overview3d'] // 不重定向白名单 +const whiteList = ["/login", "config/baseconfig", "/overview3d"]; // 不重定向白名单 // 全局钩子 router.beforeEach((to, from, next) => { - NProgress.start() // 加载进度条 + NProgress.start(); // 加载进度条 // 如果有token - const oldToken = getToken() // 获取原token - const newToken = to.query.token - console.log('form other system token:' + to.query.token) + const oldToken = getToken(); // 获取原token + const newToken = to.query.token; + console.log("原Token:" + oldToken); + console.log("新Token:" + newToken); + console.log("form other system token:" + to.query.token); // 允许使用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没有权限页面 - }) + console.log('允许使用token登录并且新旧token不一致,使用新token重新登录,调用自动登录接口,否则跳转到登录页'); + store + .dispatch("TokenLogin", newToken) + .then(() => { + console.log("自动登录成功"); + console.log(to.path); + next({ ...to, replace: true }); // 登录成功继续 + }) + .catch(() => { + next("/401"); // 否则全部重定向到401没有权限页面 + }); } else { // 如果cookie中已有token,直接进行下一步 if (oldToken) { // 当进入非登陆页时 - if (store.getters.roleList && store.getters.roleList.length === 0) { // 判断当前用户是否已拉取完user_info信息 + if (store.getters.roleList && store.getters.roleList.length === 0) { + // 判断当前用户是否已拉取完user_info信息 try { - store.dispatch('GetInfo').then(() => { + 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 - }) + 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(() => { + store.dispatch("FedLogOut").then(() => { // Message.error(err || '权限验证失败,请重新登录系统') - next({ path: '/' }) - }) + next({ path: "/" }); + }); } - } else { // 角色不为空 - next() + } else { + // 角色不为空 + next(); } - } - else { + } else { // has no token // 免登录白名单,直接进入 - if(to.path === '/overview3d') { + if (to.path === "/overview3d") { // console.log(to.query) - const userCode = to.query.userCode?to.query.userCode:to.query.usercode - uerLogin(userCode).then(response => { + const userCode = to.query.userCode + ? to.query.userCode + : to.query.usercode; + uerLogin(userCode).then((response) => { if (response.code === 200) { - setToken(response.data.token) - store.dispatch('TokenLogin', response.data.token).then(() => { + setToken(response.data.token); + store.dispatch("TokenLogin", response.data.token).then(() => { try { - store.dispatch('GetInfo').then(() => { + 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 - }) + 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) {} - }) + }); } - }) + }); } else if (whiteList.indexOf(to.path) !== -1) { - next() + next(); // next({ ...to, replace: true }) // 登录成功继续 } 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 - } + const token = to.query.token; + console.log("form other system token:" + to.query.token); + let zfToken = ""; + // 携带token 需要去掉 + const hrefIdx = window.location.href.indexOf("ZF-TOKEN"); // url中是否携带token + if (hrefIdx > -1) { + console.log("hrefIdx", extractURLParameters(window.location.href)); + const params = extractURLParameters(window.location.href); + zfToken = params["ZF-TOKEN"]; } - NProgress.done() + console.log("zfToken" + zfToken); + if (zfToken) { + console.log("跳转"); + } else { + if (loginByToken && token) { + // 调用自动登录接口,否则跳转到登录页 + console.log("调用自动登录接口,否则跳转到登录页"); + 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(); + } } } } -}) +}); router.afterEach(() => { - NProgress.done() // 结束Progress -}) + NProgress.done(); // 结束Progress +}); diff --git a/src/utils/validate.js b/src/utils/validate.js index d6d56eb..35f1d33 100644 --- a/src/utils/validate.js +++ b/src/utils/validate.js @@ -7,35 +7,36 @@ * 是否是外部地址 */ export function isExternal(path) { - return /^(https?:|mailto:|tel:)/.test(path) + return /^(https?:|mailto:|tel:)/.test(path); } /* 合法uri*/ export function validateURL(textval) { - const urlregex = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/ - return urlregex.test(textval) + const urlregex = + /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/; + return urlregex.test(textval); } /* 合法uri*/ export function validateNull(textval) { - return !!textval + return !!textval; } /* 验证小写字母*/ export function validateLowerCase(str) { - const reg = /^[a-z]+$/ - return reg.test(str) + const reg = /^[a-z]+$/; + return reg.test(str); } /* 验证大写字母*/ export function validateUpperCase(str) { - const reg = /^[A-Z]+$/ - return reg.test(str) + const reg = /^[A-Z]+$/; + return reg.test(str); } /* 验证大小写字母*/ export function validateAlphabets(str) { - const reg = /^[A-Za-z]+$/ - return reg.test(str) + const reg = /^[A-Za-z]+$/; + return reg.test(str); } /** @@ -44,8 +45,9 @@ * @returns {boolean} */ export function validateEmail(email) { - const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ - return re.test(email) + const re = + /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; + return re.test(email); } /** @@ -54,16 +56,16 @@ * @returns {boolean} */ export function validatePhone(phone) { - const re = /^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/ - return re.test(phone) + const re = /^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/; + return re.test(phone); } /** -* @description 验证手机号码 -* @param phone 被验证手机号 -* @return Boolean -*/ + * @description 验证手机号码 + * @param phone 被验证手机号 + * @return Boolean + */ export function validateisPhone(phone) { - const reg = /^[1][3-9]\d{9}$/ + const reg = /^[1][3-9]\d{9}$/; return reg.test(phone); } @@ -71,30 +73,30 @@ * 验证一定范围内的非负整数 */ export function validateIntPlus(value, min, max) { - const re = /^\d+$/ + const re = /^\d+$/; if (re.test(value)) { - const data = parseInt(value) + const data = parseInt(value); if (data > max || data < min) { - return false + return false; } } else { - return true + return true; } } /** * 验证一定范围内的非负小数 */ export function validateFloatPlus(value, min, max) { - const re = /^\d+(\.\d+)?$/ + const re = /^\d+(\.\d+)?$/; if (re.test(value)) { - const data = parseFloat(value) + const data = parseFloat(value); if (data > max || data < min) { - return false + return false; } else { - return true + return true; } } else { - return false + return false; } } @@ -102,6 +104,14 @@ * 常用正则表达式 */ export const regularReg = { - password: '', // 密码 - mobile: /^1[34578]\d{9}$/ // 手机号码 + password: "", // 密码 + mobile: /^1[34578]\d{9}$/, // 手机号码 +}; + +export function extractURLParameters(url) { + const params = new URLSearchParams(url.split("?")[1]); + return Array.from(params.entries()).reduce((acc, [key, value]) => { + acc[key] = value; + return acc; + }, {}); } diff --git a/vue.config.js b/vue.config.js index ef9db0e..7ba8abc 100644 --- a/vue.config.js +++ b/vue.config.js @@ -22,6 +22,11 @@ filenameHashing: true, // 文件名哈希 // 它支持webPack-dev-server的所有选项 devServer: { + client: { + overlay: { + runtimeErrors: false, // 当出现编译错误或警告时,在浏览器中是否显示全屏覆盖。 + }, + }, // historyApiFallback: true, historyApiFallback: { index: '/index.html'