permission.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. // console.log(route,1111);
  120. const { url, icon, moduleName, moduleId, code, type } = route;
  121. var item = {};
  122. item.path = url;
  123. item.component = Layout;
  124. item.name = code;
  125. item.meta = {
  126. title: moduleName,
  127. icon: icon,
  128. };
  129. if (code == "index") {
  130. item.path = "/";
  131. item.alwaysShow = false;
  132. let children = route.children;
  133. route.children = [];
  134. route.children.push({
  135. code: "dashboard",
  136. moduleName: "首页",
  137. type: 2,
  138. url: "/dashboard",
  139. children: children,
  140. });
  141. }
  142. if (code == "issue") {
  143. let children = route.children;
  144. route.children = [];
  145. route.children.push({
  146. code: "issue_index",
  147. moduleName: "文件下发",
  148. type: 2,
  149. url: "/index",
  150. children: children,
  151. });
  152. item.alwaysShow = false;
  153. }
  154. if (code == "notice") {
  155. let children = route.children;
  156. route.children = [];
  157. route.children.push({
  158. code: "notice_index",
  159. moduleName: "系统通知",
  160. type: 2,
  161. url: "/index",
  162. children: children,
  163. });
  164. item.alwaysShow = false;
  165. }
  166. item.children = [];
  167. if (
  168. (route.hasOwnProperty("children") && type === 1) ||
  169. code == "issue" ||
  170. code == "notice" ||
  171. code == "index"
  172. ) {
  173. for (var child of route.children) {
  174. item.children.push(buildRoute(child, url));
  175. }
  176. }
  177. return item;
  178. }
  179. function buildRoute(route, p_url) {
  180. // console.log(route,2222);
  181. const { url, moduleName, icon, moduleId, code, type, hidden,fullUrl } = route;
  182. var item = {};
  183. if (url.substr(0, 1) == "/") {
  184. item.path = url.substr(1);
  185. } else {
  186. item.path = url;
  187. }
  188. try {
  189. if (code == "dashboard") {
  190. // item.component = _import(`${url}/index`)
  191. item.component = (resolve) =>
  192. require(["@/views" + `${url}/index`], resolve);
  193. } else {
  194. if (fullUrl) {
  195. item.component = _import(`${fullUrl}`);
  196. }else{
  197. item.component = _import(`${p_url}${url}`);
  198. }
  199. }
  200. } catch (e) {
  201. console.log(e);
  202. }
  203. item.name = code;
  204. item.meta = {
  205. title: moduleName,
  206. icon: icon,
  207. };
  208. if (route.children && type == 2) {
  209. let roles = [];
  210. for (var role of route.children) {
  211. roles.push(role.code);
  212. }
  213. item.meta.roles = roles;
  214. }
  215. item.hidden = hidden;
  216. item.children = [];
  217. if (route.hasOwnProperty("children") && type === 1) {
  218. for (var child of route.children) {
  219. item.children.push(buildThirdRoute(child, `${p_url}${url}`));
  220. }
  221. }
  222. return item;
  223. }
  224. function buildThirdRoute(route, p_url) {
  225. const { url, moduleName, icon, moduleId, code, hidden, type ,fullUrl } = route;
  226. // console.log(fullUrl,'7878');
  227. var item = {};
  228. if (url.substr(0, 1) == "/") {
  229. // console.log(url.substr(1),p_url,url);
  230. item.path = url.substr(1);
  231. } else {
  232. tem.path = url;
  233. }
  234. try {
  235. if (fullUrl) {
  236. item.component = _import(`${fullUrl}`);
  237. }else{
  238. item.component = _import(`${p_url}${url}`);
  239. }
  240. } catch (e) {
  241. console.log(e);
  242. }
  243. item.name = code;
  244. item.meta = {
  245. title: moduleName,
  246. icon: icon,
  247. };
  248. if (route.children && type == 2) {
  249. let roles = [];
  250. for (var role of route.children) {
  251. roles.push(role.code);
  252. }
  253. item.meta.roles = roles;
  254. }
  255. item.hidden = hidden;
  256. item.children = [];
  257. return item;
  258. }