index.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. import Vue from 'vue'
  2. import VueRouter from 'vue-router'
  3. // 进度条
  4. import NProgress from 'nprogress'
  5. import 'nprogress/nprogress.css'
  6. import store from '@/store/index'
  7. import util from '@/libs/util.js'
  8. // 路由数据
  9. import routes from './routes'
  10. // 固定菜单与路由
  11. import menuHeader from '@/menu/header'
  12. // import menuAside from '@/menu/aside'
  13. import utils from '@/static/js/utils.js'
  14. import { frameInRoutes } from '@/router/routes'
  15. // 路由与组件映射关系
  16. import routerMapComponents from '@/routerMapComponents'
  17. import sidebarrouter from '@/api/sidebar'
  18. Vue.use(VueRouter)
  19. // 导出路由 在 main.js 里使用
  20. const router = new VueRouter({
  21. routes
  22. })
  23. let permissionMenu = []
  24. let permissionRouter = []
  25. let permission = {
  26. functions: [],
  27. roles: [],
  28. interfaces: [],
  29. isAdmin: false
  30. }
  31. // 标记是否已经拉取权限信息
  32. let isFetchPermissionInfo = false
  33. let fetchPermissionInfo = async () => {
  34. // 处理动态添加的路由
  35. const formatRoutes = function (routes) {
  36. routes.forEach(route => {
  37. route.component = routerMapComponents[route.component]
  38. if (route.children) {
  39. formatRoutes(route.children)
  40. }
  41. })
  42. }
  43. // try {
  44. // let userPermissionInfo =
  45. // {
  46. // statusCode: 200,
  47. // msg: '',
  48. // data: {
  49. // user: 'MenuManager',
  50. // userRoles: [
  51. // 'R_MENUADMIN'
  52. // ],
  53. // userPermissions: [
  54. // 'p_menu_view',
  55. // 'p_menu_edit',
  56. // 'p_menu_menu',
  57. // 'p_issues_edit1'
  58. // ],
  59. // accessMenus: [
  60. // {
  61. // title: '系统',
  62. // path: '/system',
  63. // icon: 'cogs',
  64. // children: [
  65. // {
  66. // title: '系统设置',
  67. // icon: 'cogs',
  68. // children: [
  69. // {
  70. // title: '菜单管理',
  71. // path: '/system/menu',
  72. // icon: 'th-list'
  73. // }
  74. // ]
  75. // },
  76. // {
  77. // title: '组织架构',
  78. // icon: 'pie-chart',
  79. // children: [
  80. // {
  81. // title: '部门管理',
  82. // icon: 'html5'
  83. // },
  84. // {
  85. // title: '职位管理',
  86. // icon: 'opencart'
  87. // },
  88. // {
  89. // title: '编辑界面演示',
  90. // path: '/issues',
  91. // icon: 'th-list'
  92. // }
  93. // ]
  94. // }
  95. // ]
  96. // }
  97. // ],
  98. // accessRoutes: [
  99. // {
  100. // name: 'System',
  101. // path: '/system',
  102. // component: 'layoutHeaderAside',
  103. // componentPath: 'layout/header-aside/layout',
  104. // meta: {
  105. // title: '系统设置',
  106. // cache: true
  107. // },
  108. // children: [
  109. // {
  110. // name: 'MenuPage',
  111. // path: '/system/menu',
  112. // component: 'menu',
  113. // componentPath: 'pages/sys/menu/index',
  114. // meta: {
  115. // title: '菜单管理',
  116. // cache: true
  117. // }
  118. // },
  119. // {
  120. // name: 'RoutePage',
  121. // path: '/system/route',
  122. // component: 'route',
  123. // componentPath: 'pages/sys/route/index',
  124. // meta: {
  125. // title: '路由管理',
  126. // cache: true
  127. // }
  128. // },
  129. // {
  130. // name: 'RolePage',
  131. // path: '/system/role',
  132. // component: 'role',
  133. // componentPath: 'pages/sys/role/index',
  134. // meta: {
  135. // title: '角色管理',
  136. // cache: true
  137. // }
  138. // },
  139. // {
  140. // name: 'UserPage',
  141. // path: '/system/user',
  142. // component: 'user',
  143. // componentPath: 'pages/sys/user/index',
  144. // meta: {
  145. // title: '用户管理',
  146. // cache: true
  147. // }
  148. // },
  149. // {
  150. // name: 'InterfacePage',
  151. // path: '/system/interface',
  152. // component: 'interface',
  153. // meta: {
  154. // title: '接口管理'
  155. // }
  156. // },
  157. // {
  158. // name: 'issues',
  159. // path: '/issues',
  160. // component: 'issues',
  161. // meta: {
  162. // title: '编辑页面示例'
  163. // }
  164. // },
  165. // {
  166. // name: 'issues-edit',
  167. // path: '/issues/edit/:id',
  168. // component: 'issues-edit',
  169. // meta: {
  170. // title: '无缓存编辑'
  171. // }
  172. // },
  173. // {
  174. // name: 'issues-edit-cache-db',
  175. // path: '/issues/edit-cache-db/:id',
  176. // component: 'issues-edit-cache-db',
  177. // meta: {
  178. // title: '带缓存编辑 DB'
  179. // }
  180. // }
  181. // ]
  182. // }
  183. // ],
  184. // accessInterfaces: [
  185. // {
  186. // path: '/menu/:id',
  187. // method: 'get'
  188. // },
  189. // {
  190. // path: '/menu',
  191. // method: 'get'
  192. // },
  193. // {
  194. // path: '/menu/save',
  195. // method: 'post'
  196. // },
  197. // {
  198. // path: '/interface/paged',
  199. // method: 'get'
  200. // }
  201. // ],
  202. // isAdmin: 0,
  203. // avatarUrl: 'https://api.adorable.io/avatars/85/abott@adorable.png'
  204. // }
  205. // }
  206. try {
  207. let userPermissionInfo = {}
  208. // 后端需要返回的权限信息包括权限过滤后的角色编码集合,功能编码集合,接口信息集合,菜单列表,路由列表,以及是否系统管理员标识
  209. // 动态生成菜单
  210. let uuid = util.cookies.get('uuid')
  211. let params = {
  212. uuid: uuid
  213. }
  214. sidebarrouter
  215. .getAllbars(params)
  216. .then(res => {
  217. userPermissionInfo = res
  218. if (userPermissionInfo.access_menus) {
  219. permissionMenu = userPermissionInfo.access_menus
  220. permissionMenu = utils.toolfun_gettreejson(
  221. permissionMenu,
  222. 'id',
  223. 'parent_id',
  224. 'icon,path,title'
  225. )
  226. }
  227. // permissionRouter = userPermissionInfo.accessRoutes
  228. // permission.functions = userPermissionInfo.userPermissions
  229. // permission.roles = userPermissionInfo.userRoles
  230. // permission.interfaces = util.formatInterfaces(userPermissionInfo.accessInterfaces)
  231. // permission.isAdmin = userPermissionInfo.isAdmin === 1
  232. formatRoutes(permissionRouter)
  233. let allMenuAside = [...permissionMenu]
  234. // console.log(allMenuAside, '=============')
  235. // let allMenuAside = [...menuAside, ...permissionMenu]
  236. // let allMenuHeader = [...menuHeader, ...permissionMenu]
  237. let allMenuHeader = [...menuHeader]
  238. // 动态添加路由
  239. router.addRoutes(permissionRouter)
  240. // 处理路由 得到每一级的路由设置
  241. store.commit('d2admin/page/init', [
  242. ...frameInRoutes,
  243. ...permissionRouter
  244. ])
  245. // 设置顶栏菜单
  246. store.commit('d2admin/menu/headerSet', allMenuHeader)
  247. // 设置侧边栏菜单
  248. store.commit('d2admin/menu/fullAsideSet', allMenuAside)
  249. // 初始化菜单搜索功能
  250. store.commit('d2admin/search/init', allMenuHeader)
  251. // 设置权限信息
  252. store.commit('d2admin/permission/set', permission)
  253. // 加载上次退出时的多页列表 DB -> store 持久化数据加载上次退出时的多页列表
  254. store.dispatch('d2admin/page/openedLoad')
  255. })
  256. .catch(err => {
  257. console.error(err)
  258. })
  259. } catch (ex) {
  260. console.log(ex)
  261. }
  262. await Promise.resolve()
  263. }
  264. /**
  265. * 路由拦截
  266. * 权限验证
  267. */
  268. router.beforeEach(async (to, from, next) => {
  269. // 进度条
  270. NProgress.start()
  271. // 关闭搜索面板
  272. store.commit('d2admin/search/set', false)
  273. // 验证当前路由所有的匹配中是否需要有登录验证的
  274. if (to.matched.some(r => r.meta.auth)) {
  275. // 这里暂时将cookie里是否存有token作为验证是否登录的条件
  276. // 请根据自身业务需要修改
  277. const token = util.cookies.get('token')
  278. if (token && token !== 'undefined') {
  279. // 拉取权限信息
  280. if (!isFetchPermissionInfo) {
  281. await fetchPermissionInfo()
  282. isFetchPermissionInfo = true
  283. next(to.path, true)
  284. } else {
  285. next()
  286. }
  287. } else {
  288. // 没有登录的时候跳转到登录界面
  289. // 携带上登陆成功之后需要跳转的页面完整路径
  290. next({
  291. name: 'page1',
  292. query: {
  293. redirect: to.fullPath
  294. }
  295. })
  296. // https://github.com/d2-projects/d2-admin/issues/138
  297. NProgress.done()
  298. }
  299. } else {
  300. // 不需要身份校验 直接通过
  301. next()
  302. }
  303. })
  304. const originalPush = VueRouter.prototype.push
  305. VueRouter.prototype.push = function push (location, onResolve, onReject) {
  306. if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  307. return originalPush.call(this, location).catch(err => err)
  308. }
  309. router.afterEach(to => {
  310. // 进度条
  311. NProgress.done()
  312. // 多页控制 打开新的页面
  313. store.dispatch('d2admin/page/open', to)
  314. // 更改标题
  315. util.title(to.meta.title)
  316. })
  317. export default router