|
@@ -2,44 +2,62 @@ import router from "@/router/index.js";
|
|
|
import store from "@/store";
|
|
|
import NProgress from "nprogress"; // progress bar
|
|
|
import "./progress.css";
|
|
|
-import {getQueryObject} from "../utils/data-utils.js";
|
|
|
-import {getToken, getUserName} from "@/utils/auth"; // getToken from cookie
|
|
|
+import { getToken, getUserName } from "@/utils/auth"; // getToken from cookie
|
|
|
import _ from "lodash";
|
|
|
|
|
|
-const {initSettings, getSetting, getSettings, getSettingBool} = window.systemParamsUtils;
|
|
|
+const { initSettings, getSetting, getSettings, getSettingBool } = window.systemParamsUtils;
|
|
|
|
|
|
-NProgress.configure({showSpinner: false}); // NProgress Configuration
|
|
|
+NProgress.configure({ showSpinner: false }); // NProgress Configuration
|
|
|
|
|
|
-// permission judge function
|
|
|
-function hasPermission(userPermissions, routePermissions) {
|
|
|
- if (!routePermissions || (routePermissions && routePermissions.length === 0)) {
|
|
|
- return true;
|
|
|
- }
|
|
|
- return userPermissions.some(userPermission => {
|
|
|
- return routePermissions.indexOf(userPermission) >= 0;
|
|
|
- });
|
|
|
+// 合并白名单列表
|
|
|
+const WHITE_LIST = getSetting("WHITE_LIST");
|
|
|
+let whiteList = ["/login", "/auth-redirect"]; // no redirect whitelist
|
|
|
+if (WHITE_LIST instanceof Array) {
|
|
|
+ whiteList = whiteList.concat(WHITE_LIST);
|
|
|
}
|
|
|
|
|
|
-const whiteList = ["/login", "/auth-redirect"]; // no redirect whitelist
|
|
|
-
|
|
|
router.beforeEach((to, from, next) => {
|
|
|
NProgress.start(); // start progress bar
|
|
|
let queryObject = getQueryObject();
|
|
|
if (_.isEmpty(getSettings())) {
|
|
|
- //如果配置信息为空,则先加载配置信息
|
|
|
- initSettings().then(() => location.reload());
|
|
|
- } else if (!getSettingBool("NEED_LOGIN")) {
|
|
|
- // 如果项目不需要登录,则不需要获取用户信息和权限
|
|
|
- noNeedLogin(to, from, next);
|
|
|
+ initSettings().then(() => location.reload()); // 如果配置信息为空,则先加载配置信息
|
|
|
+ } else if (!getSettingBool("NEED_LOGIN") || whiteList.indexOf(to.path) !== -1) {
|
|
|
+ noNeedLogin(to, from, next); // 如果项目不需要登录或页面url在白名单中,则不需要获取用户信息和权限
|
|
|
} else if (getSettingBool("IS_OAUTH2") && queryObject.code) {
|
|
|
- //如果使用oauth2登录并且地址栏中有code参数,则去请求token
|
|
|
- getAccessToken(queryObject);
|
|
|
+ getAccessToken(queryObject); // 如果使用oauth2登录并且地址栏中有code参数,则去请求token
|
|
|
} else {
|
|
|
- //检查是否登录
|
|
|
- checkLogin(to, next);
|
|
|
+ checkLogin(to, next); // 检查是否登录
|
|
|
}
|
|
|
});
|
|
|
|
|
|
+/**
|
|
|
+ * 将url的参数转为对象
|
|
|
+ * 不传默认获取当前浏览器地址的参数
|
|
|
+ */
|
|
|
+function getQueryObject(url = window.location.href) {
|
|
|
+ const search = url.substring(url.lastIndexOf("?") + 1);
|
|
|
+ const obj = {};
|
|
|
+ const reg = /([^?&=]+)=([^?&=]*)/g;
|
|
|
+ search.replace(reg, (rs, $1, $2) => {
|
|
|
+ const name = decodeURIComponent($1);
|
|
|
+ let val = decodeURIComponent($2);
|
|
|
+ val = String(val);
|
|
|
+ obj[name] = val;
|
|
|
+ return rs;
|
|
|
+ });
|
|
|
+ return obj;
|
|
|
+}
|
|
|
+
|
|
|
+// permission judge function
|
|
|
+function hasPermission(userPermissions = [], routePermissions = []) {
|
|
|
+ if (routePermissions && routePermissions.length === 0) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return userPermissions.some(userPermission => {
|
|
|
+ return routePermissions.indexOf(userPermission) >= 0;
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
router.afterEach(() => {
|
|
|
NProgress.done(); // finish progress bar
|
|
|
});
|
|
@@ -62,49 +80,19 @@ function noNeedLogin(to, from, next) {
|
|
|
* 如果是Oauth2登录并且地址栏上有code字段,则去请求access_token
|
|
|
* @param queryObject 地址栏上的参数
|
|
|
*/
|
|
|
-function getAccessToken(queryObject) {
|
|
|
+async function getAccessToken(queryObject) {
|
|
|
let code = queryObject.code.split("#/")[0];
|
|
|
- store
|
|
|
- .dispatch("GetOAuthToken", { code })
|
|
|
- .then(code => {
|
|
|
- //如果登录成功了,清除掉url中的code,刷新页面
|
|
|
- if (getToken()) {
|
|
|
- window.location.href = delParam("code");
|
|
|
- }
|
|
|
- })
|
|
|
- .catch(reason => {
|
|
|
- store.dispatch("FedLogout").then(() => {
|
|
|
- location.reload();
|
|
|
- });
|
|
|
- });
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * 去掉url上的指定参数
|
|
|
- * @param paramKey 要删除的key
|
|
|
- */
|
|
|
-function delParam(paramKey) {
|
|
|
- let url = window.location.href; //页面url
|
|
|
- let urlParam = window.location.search.substr(1); //页面参数
|
|
|
- let beforeUrl = url.substr(0, url.indexOf("?")); //页面主地址(参数之前地址)
|
|
|
- let nextUrl = "";
|
|
|
- let arr = [];
|
|
|
- if (urlParam !== "") {
|
|
|
- let urlParamArr = urlParam.split("&"); //将参数按照&符分成数组
|
|
|
- for (let i = 0; i < urlParamArr.length; i++) {
|
|
|
- let paramArr = urlParamArr[i].split("="); //将参数键,值拆开
|
|
|
- //如果键雨要删除的不一致,则加入到参数中
|
|
|
- if (paramArr[0] !== paramKey) {
|
|
|
- arr.push(urlParamArr[i]);
|
|
|
- }
|
|
|
+ try {
|
|
|
+ await store.dispatch("GetOAuthToken", { code });
|
|
|
+ if (getToken()) {
|
|
|
+ window.location.href = "http://" + window.location.host + decodeURIComponent(window.location.pathname);
|
|
|
}
|
|
|
- if (arr.length > 0) {
|
|
|
- nextUrl = "?" + arr.join("&");
|
|
|
- }
|
|
|
- url = beforeUrl + nextUrl;
|
|
|
+ } catch (reason) {
|
|
|
+ console.error(reason);
|
|
|
+ store.dispatch("FedLogout").then(() => {
|
|
|
+ location.reload();
|
|
|
+ });
|
|
|
}
|
|
|
- url = "http://" + window.location.host + decodeURIComponent(window.location.pathname);
|
|
|
- return url;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -115,8 +103,7 @@ function delParam(paramKey) {
|
|
|
*/
|
|
|
function checkIsInWhiteList(to, next, link, result) {
|
|
|
if (whiteList.indexOf(to.path) !== -1) {
|
|
|
- // 在免登录白名单,直接进入
|
|
|
- next();
|
|
|
+ next(); // 在免登录白名单,直接进入
|
|
|
} else if (result instanceof Promise) {
|
|
|
result.then(result => {
|
|
|
if (result && result.status === 403 && result.link) {
|
|
@@ -153,29 +140,36 @@ function generateRoutes(permissions, next, to) {
|
|
|
}
|
|
|
|
|
|
function checkLogin(to, next) {
|
|
|
+ console.log(getUserName());
|
|
|
if (getUserName()) {
|
|
|
- debugger;
|
|
|
if (to.path === "/login") {
|
|
|
next({ path: "/" });
|
|
|
NProgress.done(); // if current page is dashboard will not trigger afterEach hook, so manually handle it
|
|
|
} else {
|
|
|
let permissionsLength = store.getters.permissions.length;
|
|
|
if (!store.getters.name && !permissionsLength) {
|
|
|
- store
|
|
|
- .dispatch("GetUserInfo")
|
|
|
- .then(res => {
|
|
|
+ try {
|
|
|
+ // 获取用户信息
|
|
|
+ // await ;
|
|
|
+ store.dispatch("GetUserInfo").then(() => {
|
|
|
let permissions = store.getters.permissions;
|
|
|
generateRoutes(permissions, next, to);
|
|
|
- })
|
|
|
- .catch(err => {
|
|
|
- store.dispatch("FedLogout").then(() => {
|
|
|
- if (!err.link) {
|
|
|
- next(`/login?redirect=${to.path}`); // 否则全部重定向到登录页
|
|
|
- } else {
|
|
|
- next({ path: "/" });
|
|
|
- }
|
|
|
- });
|
|
|
});
|
|
|
+ } catch (err) {
|
|
|
+ store.dispatch("FedLogout").then(() => {
|
|
|
+ if (!err.link) {
|
|
|
+ next(`/login?redirect=${to.path}`); // 否则全部重定向到登录页
|
|
|
+ } else {
|
|
|
+ next({ path: "/" });
|
|
|
+ }
|
|
|
+ });
|
|
|
+ // await store.dispatch("FedLogout") // 如果获取用户信息失败,则将本地的用户信息相关的东西清空,并登出
|
|
|
+ // if (!err.link) {
|
|
|
+ // next(`/login?redirect=${to.path}`); // 否则全部重定向到登录页
|
|
|
+ // } else {
|
|
|
+ // next({ path: "/" });
|
|
|
+ // }
|
|
|
+ }
|
|
|
} else {
|
|
|
// 没有动态改变权限的需求可直接next() 删除下方权限判断 ↓
|
|
|
// if (hasPermission(store.getters.roles, to.meta.roles)) {
|
|
@@ -192,20 +186,20 @@ function checkLogin(to, next) {
|
|
|
}
|
|
|
} else {
|
|
|
if (getSetting("BASE_API") === "/easy-mock") {
|
|
|
- //检查是否在白名单中
|
|
|
- checkIsInWhiteList(to, next);
|
|
|
+ checkIsInWhiteList(to, next); // 检查是否在白名单中
|
|
|
} else {
|
|
|
- store
|
|
|
- .dispatch("GetUserInfo")
|
|
|
- .then(res => {
|
|
|
+ try {
|
|
|
+ store.dispatch("GetUserInfo").then(() => {
|
|
|
window.location.reload();
|
|
|
- })
|
|
|
- .catch(result => {
|
|
|
- let link;
|
|
|
- result && (link = result.link);
|
|
|
- //检查是否在白名单中
|
|
|
- checkIsInWhiteList(to, next, link, result);
|
|
|
});
|
|
|
+ // await store.dispatch("GetUserInfo")
|
|
|
+ // window.location.reload();
|
|
|
+ } catch (result) {
|
|
|
+ let link;
|
|
|
+ result && (link = result.link);
|
|
|
+
|
|
|
+ checkIsInWhiteList(to, next, link, result); // 检查是否在白名单中
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|