permission.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 } = 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. {
  135. 'code': "dashboard",
  136. 'moduleName': "首页",
  137. 'type': 2,
  138. 'url': '/dashboard',
  139. 'children': children
  140. }
  141. )
  142. }
  143. if (code == 'msg') {
  144. let children = route.children;
  145. route.children = [];
  146. route.children.push(
  147. {
  148. 'code': "notice_index",
  149. 'moduleName': "消息列表",
  150. 'type': 2,
  151. 'url': '/index',
  152. 'children': children
  153. }
  154. )
  155. item.alwaysShow = false;
  156. }
  157. item.children = []
  158. if ((route.hasOwnProperty('children') && type === 1) || code == 'wxcustomer' || code == 'settle_manage' || code == 'delivery_manage' || code == 'stockpile_manage' || code == 'msg' || code == 'index') {
  159. for (var child of route.children) {
  160. item.children.push(buildRoute(child, url))
  161. }
  162. }
  163. return item
  164. }
  165. function buildRoute(route, p_url) {
  166. const { url, moduleName, icon, moduleId, code, type, hidden } = route
  167. var item = {}
  168. if(url.substr(0, 1) == '/') {
  169. item.path = url.substr(1)
  170. }else {
  171. item.path = url
  172. }
  173. try {
  174. if(code == 'dashboard') {
  175. // item.component = _import(`${url}/index`)
  176. item.component = (resolve) => require(["@/views"+`${url}/index`], resolve)
  177. }
  178. else {
  179. item.component = _import(`${p_url}${url}`)
  180. }
  181. } catch (e) {
  182. console.log(e)
  183. }
  184. item.name = code
  185. item.meta = {
  186. title: moduleName,
  187. icon: icon
  188. }
  189. if((route.children && type == 2)) {
  190. let roles = [];
  191. for(var role of route.children) {
  192. roles.push(role.code)
  193. }
  194. item.meta.roles = roles;
  195. }
  196. item.hidden = hidden
  197. item.children = []
  198. if (route.hasOwnProperty('children') && type === 1) {
  199. for (var child of route.children) {
  200. item.children.push(buildThirdRoute(child, `${p_url}${url}`))
  201. }
  202. }
  203. return item
  204. }
  205. function buildThirdRoute(route, p_url) {
  206. const { url, moduleName, icon, moduleId, code, hidden, type } = route
  207. var item = {}
  208. if(url.substr(0, 1) == '/') {
  209. item.path = url.substr(1)
  210. }else {
  211. tem.path = url
  212. }
  213. try {
  214. item.component = _import(`${p_url}${url}`)
  215. } catch (e) {
  216. console.log(e)
  217. }
  218. item.name = code
  219. item.meta = {
  220. title: moduleName,
  221. icon: icon
  222. }
  223. if(route.children && type == 2) {
  224. let roles = [];
  225. for(var role of route.children) {
  226. roles.push(role.code)
  227. }
  228. item.meta.roles = roles;
  229. }
  230. item.hidden = hidden
  231. item.children = []
  232. return item
  233. }