permission.js 7.3 KB

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