permission.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. import router from "./router";
  2. import { resetRouter } from "./router";
  3. import store from "./store";
  4. import { Message } from "element-ui";
  5. import NProgress from "nprogress"; // progress bar
  6. import "nprogress/nprogress.css"; // progress bar style
  7. import { getToken } from "@/utils/auth"; // get token from cookie
  8. import getPageTitle from "@/utils/get-page-title";
  9. import Layout from "@/layout";
  10. const _import = require("./router/_import_" + process.env.NODE_ENV); // 获取组件的方法
  11. NProgress.configure({ showSpinner: false }); // NProgress Configuration
  12. const whiteList = ["/login"]; // no redirect whitelist
  13. router.beforeEach(async (to, from, next) => {
  14. // start progress bar
  15. NProgress.start();
  16. // set page title
  17. document.title = getPageTitle(to.meta.title);
  18. // determine whether the user has logged in
  19. const hasToken = getToken();
  20. if (hasToken) {
  21. if (to.path === "/login") {
  22. // if is logged in, redirect to the home page
  23. next({ path: "/" });
  24. NProgress.done();
  25. } else {
  26. const hasGetUserInfo = store.getters.name;
  27. if (hasGetUserInfo) {
  28. next();
  29. } else {
  30. try {
  31. // get user info
  32. await store.dispatch("user/getInfo");
  33. // 请求获取路由表
  34. await store.dispatch("user/getRouter");
  35. if (store.getters.menus.length < 1) {
  36. global.antRouter = [];
  37. next();
  38. }
  39. // 设置路由
  40. var newRoutes = [];
  41. for (var route of store.getters.menus) {
  42. const item = buildRootRoute(route);
  43. newRoutes.push(item);
  44. }
  45. // newRoutes.shift();
  46. // 添加一项根目录重定向页面
  47. if (newRoutes[0].path != "/") {
  48. let path = newRoutes[0].path;
  49. if (newRoutes[0].children.length > 0) {
  50. path = `${path}/${newRoutes[0].children[0].path}`;
  51. }
  52. if (newRoutes[0].children[0].children.length > 0) {
  53. path = `${path}/${newRoutes[0].children[0].children[0].path}`;
  54. }
  55. newRoutes.unshift({
  56. path: "/",
  57. component: Layout,
  58. redirect: path,
  59. });
  60. } else {
  61. newRoutes[0].redirect = "/dashboard";
  62. }
  63. console.log(newRoutes);
  64. router.addRoutes(newRoutes); // 2.动态添加路由
  65. global.antRouter = newRoutes; // 3.将路由数据传递给全局变量,做侧边栏菜单渲染工作
  66. // const menus = filterAsyncRouter(store.getters.menus) // 1.过滤路由
  67. // console.log(menus);
  68. // router.addRoutes(menus) // 2.动态添加路由
  69. // global.antRouter = menus // 3.将路由数据传递给全局变量,做侧边栏菜单渲染工作
  70. next({
  71. ...to,
  72. replace: true,
  73. });
  74. // next()
  75. } catch (error) {
  76. // remove token and go to login page to re-login
  77. await store.dispatch("user/resetToken");
  78. Message.error(error || "Has Error");
  79. next(`/login?redirect=${to.path}`);
  80. NProgress.done();
  81. }
  82. }
  83. }
  84. } else {
  85. /* has no token*/
  86. if (whiteList.indexOf(to.path) !== -1) {
  87. // in the free login whitelist, go directly
  88. next();
  89. } else {
  90. // other pages that do not have permission to access are redirected to the login page.
  91. next(`/login?redirect=${to.path}`);
  92. NProgress.done();
  93. }
  94. }
  95. });
  96. router.afterEach(() => {
  97. // finish progress bar
  98. NProgress.done();
  99. });
  100. // 遍历后台传来的路由字符串,转换为组件对象
  101. // function filterAsyncRouter(asyncRouterMap) {
  102. // const accessedRouters = asyncRouterMap.filter((route) => {
  103. // console.log(route);
  104. // if (route.component) {
  105. // if (route.component === "Layout") {
  106. // route.component = Layout;
  107. // } else {
  108. // route.component = _import(route.component); // 导入组件
  109. // }
  110. // }
  111. // if (route.children && route.children.length) {
  112. // route.children = filterAsyncRouter(route.children);
  113. // }
  114. // return true;
  115. // });
  116. // return accessedRouters;
  117. // }
  118. function buildRootRoute(route) {
  119. const { url, icon, moduleName, moduleId, code, type,fullUrl } = route;
  120. var item = {};
  121. item.path = url;
  122. item.component = Layout;
  123. item.name = code;
  124. item.meta = {
  125. title: moduleName,
  126. icon: icon,
  127. };
  128. if (code == "index") {
  129. item.path = "/";
  130. item.alwaysShow = false;
  131. let children = route.children;
  132. route.children = [];
  133. route.children.push({
  134. code: "dashboard",
  135. moduleName: "首页",
  136. type: 2,
  137. url: "/dashboard",
  138. children: children,
  139. });
  140. }
  141. if (code == "issue") {
  142. let children = route.children;
  143. route.children = [];
  144. route.children.push({
  145. code: "issue_index",
  146. moduleName: "文件下发",
  147. type: 2,
  148. url: "/index",
  149. children: children,
  150. });
  151. item.alwaysShow = false;
  152. }
  153. if (code == "notice") {
  154. let children = route.children;
  155. route.children = [];
  156. route.children.push({
  157. code: "notice_index",
  158. moduleName: "系统通知",
  159. type: 2,
  160. url: "/index",
  161. children: children,
  162. });
  163. item.alwaysShow = false;
  164. }
  165. item.children = [];
  166. if (
  167. (route.hasOwnProperty("children") && type === 1) ||
  168. code == "issue" ||
  169. code == "notice" ||
  170. code == "index"
  171. ) {
  172. for (var child of route.children) {
  173. item.children.push(buildRoute(child, fullUrl));
  174. }
  175. }
  176. return item;
  177. }
  178. function buildRoute(route, p_url) {
  179. const { url, moduleName, icon, moduleId, code, type, hidden,fullUrl } = route;
  180. var item = {};
  181. if (url.substr(0, 1) == "/") {
  182. item.path = url.substr(1);
  183. } else {
  184. item.path = url;
  185. }
  186. try {
  187. if (code == "dashboard") {
  188. // item.component = _import(`${url}/index`)
  189. item.component = (resolve) =>
  190. require(["@/views" + `${url}/index`], resolve);
  191. } else {
  192. if (fullUrl) {
  193. item.component = _import(`${fullUrl}`);
  194. }else{
  195. item.component = _import(`${p_url}${url}`);
  196. }
  197. }
  198. } catch (e) {
  199. console.log(e);
  200. }
  201. item.name = code;
  202. item.meta = {
  203. title: moduleName,
  204. icon: icon,
  205. };
  206. if (route.children && type == 2) {
  207. let roles = [];
  208. for (var role of route.children) {
  209. roles.push(role.code);
  210. }
  211. item.meta.roles = roles;
  212. }
  213. item.hidden = hidden;
  214. item.children = [];
  215. if (route.hasOwnProperty("children") && type === 1) {
  216. for (var child of route.children) {
  217. item.children.push(buildThirdRoute(child, `${p_url}${url}`));
  218. }
  219. }
  220. return item;
  221. }
  222. function buildThirdRoute(route, p_url) {
  223. const { url, moduleName, icon, moduleId, code, hidden, type ,fullUrl } = route;
  224. var item = {};
  225. if (url.substr(0, 1) == "/") {
  226. item.path = url.substr(1);
  227. } else {
  228. tem.path = url;
  229. }
  230. try {
  231. if (fullUrl) {
  232. item.component = _import(`${fullUrl}`);
  233. }else{
  234. item.component = _import(`${p_url}${url}`);
  235. }
  236. } catch (e) {
  237. console.log(e);
  238. }
  239. item.name = code;
  240. item.meta = {
  241. title: moduleName,
  242. icon: icon,
  243. };
  244. if (route.children && type == 2) {
  245. let roles = [];
  246. for (var role of route.children) {
  247. roles.push(role.code);
  248. }
  249. item.meta.roles = roles;
  250. }
  251. item.hidden = hidden;
  252. item.children = [];
  253. return item;
  254. }