permission.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. import { asyncRoutes, constantRoutes } from '@/router'
  2. import { getSilder } from '@/api/silder'
  3. // import { stat } from 'fs'
  4. // import { removeToken } from '@/utils/auth'
  5. // import Layout from '@/layout'
  6. /**
  7. * 遍历接口菜单添加页面
  8. * @param asyncRoutes
  9. * @param getMenu
  10. */
  11. function generateAsyncRouter(asyncRoutes, data) {
  12. if (!data) {
  13. return []
  14. }
  15. data.forEach((item) => {
  16. item.component = asyncRoutes[item.component]
  17. if (item.children && item.children.length > 0) {
  18. generateAsyncRouter(asyncRoutes, item.children)
  19. }
  20. })
  21. return data
  22. }
  23. /**
  24. * 判断平台端添加首页
  25. * @param type
  26. */
  27. // const type = getters.type
  28. const state = {
  29. routes: [],
  30. addRoutes: [],
  31. type: '', // 登录的平台类型
  32. permission: [] // 权限
  33. }
  34. const mutations = {
  35. SET_ROUTES: (state, routes) => {
  36. state.addRoutes = routes
  37. state.routes = constantRoutes.concat(routes)
  38. },
  39. SET_PERMISSION: (state, permission) => {
  40. state.permission = permission
  41. }
  42. }
  43. function getFirstMenu(routes) {
  44. let firstMenu = null
  45. routes.forEach(item => {
  46. if (item.children?.length > 0 && !item.hidden) {
  47. firstMenu = pathErgodic(item)
  48. console.log(firstMenu)
  49. item.redirect = firstMenu
  50. }
  51. })
  52. return routes
  53. }
  54. function pathErgodic(item) {
  55. // console.log(item)
  56. let firstMenu = null
  57. item.children.forEach(i => {
  58. if (!firstMenu && i.children?.length > 0&&!i.hidden) {
  59. let isChildrenList = false;
  60. i.children.forEach(ii=>{
  61. if(!ii.hidden){
  62. isChildrenList = true
  63. }
  64. })
  65. if(isChildrenList){
  66. firstMenu = pathErgodic(i)
  67. }else{
  68. if (!firstMenu && checkPathUrl(i.path)) {
  69. firstMenu = i.path
  70. } else {
  71. if (!firstMenu && !i.hidden) {
  72. firstMenu = item.path + '/' + i.path
  73. }
  74. }
  75. }
  76. } else {
  77. if (!firstMenu && checkPathUrl(i.path)) {
  78. firstMenu = i.path
  79. } else {
  80. if (!firstMenu && !i.hidden) {
  81. firstMenu = item.path + '/' + i.path
  82. }
  83. }
  84. }
  85. })
  86. return firstMenu
  87. }
  88. // 判断path有没有带/,并且是第一个位置
  89. function checkPathUrl(path) {
  90. return path.indexOf('/') === 0 ? true : false
  91. }
  92. // 路由
  93. // 递归遍历数组
  94. function recursionRouter(arr) {
  95. // 这里来了
  96. if (arr.length > 0) {
  97. let newArr = [];
  98. for (let i = 0; i < arr.length; i++) {
  99. if (arr[i].type == 1) {
  100. continue
  101. }
  102. let obj = {};
  103. obj.component = arr[i].component;
  104. obj.name = arr[i].component;
  105. // if (item.type != '1' && item.component) {
  106. // if (!item.path.startsWith('/') && item.component != 'Layout') {
  107. // obj.names = item.name
  108. // }
  109. // }
  110. arr[i].hid == 0 ? obj.hidden = false : obj.hidden = true
  111. // console.log('高亮标签'+arr[i].parentPermission,'普通路径'+arr[i].path)
  112. obj.path = arr[i].path;
  113. obj.meta = { 'title': arr[i].name, 'icon': arr[i].icon, 'noCache': arr[i].keepAlive, 'activeMenu': arr[i].parentPermission, 'belongTopMenu': arr[i].belongTopMenu }
  114. if (arr[i].sysMenus && arr[i].sysMenus.length > 0) {
  115. obj.children = recursionRouter(arr[i].sysMenus);
  116. }
  117. newArr.push(obj)
  118. }
  119. return newArr
  120. }
  121. }
  122. // 设置 belongTopMenu, 顶部菜单
  123. function addTopMenu(arr) {
  124. if (arr.length > 0) {
  125. let newArr = [];
  126. for (let i = 0; i < arr.length; i++) {
  127. if (arr[i].type == 1) {
  128. continue
  129. }
  130. let obj = arr[i]
  131. obj.belongTopMenu = obj.path
  132. if (arr[i].sysMenus && arr[i].sysMenus.length > 0) {
  133. obj.sysMenus = setTopMenu(arr[i].sysMenus, obj);
  134. }
  135. newArr.push(obj)
  136. }
  137. return newArr
  138. }
  139. }
  140. function setTopMenu(arr, topParentArr) {
  141. let newArr = [];
  142. for (let i = 0; i < arr.length; i++) {
  143. let obj = arr[i]
  144. obj.belongTopMenu = topParentArr.path
  145. if (arr[i].sysMenus && arr[i].sysMenus.length > 0) {
  146. obj.sysMenus = setTopMenu(arr[i].sysMenus, topParentArr);
  147. }
  148. newArr.push(obj)
  149. }
  150. return newArr
  151. }
  152. // 权限
  153. // 递归遍历数组
  154. let tempArr = []
  155. function recursionPermission(arr) {
  156. arr.map(item => {
  157. tempArr.push(item.memo)
  158. if (item.sysMenus && item.sysMenus.length > 0) {
  159. recursionPermission(item.sysMenus)
  160. }
  161. })
  162. }
  163. function setDetailRoute(accessedRoutes) {
  164. accessedRoutes.forEach(route => {
  165. console.log(route.path)
  166. if (route.path == '/main') {
  167. route.children = route.children.concat([
  168. {
  169. name: '日程安排',
  170. path: 'scheduleDetail',
  171. component: () => import('@/views/main/teamSchedule/scheduleDetail'),
  172. hidden: true,
  173. meta: {
  174. noCache: '1',
  175. title: '日程安排',
  176. belongTopMenu: "/main",
  177. activeMenu: '/main/main'
  178. }
  179. },
  180. {
  181. name: '未在班级学员',
  182. path: 'notClassStudent',
  183. component: () => import('@/views/main/notClassStudent'),
  184. hidden: true,
  185. meta: {
  186. noCache: '1',
  187. title: '未在班级学员',
  188. belongTopMenu: "/main",
  189. activeMenu: '/main/main'
  190. }
  191. },
  192. //
  193. ])
  194. }
  195. if (route.path == '/business') {
  196. // import('@/views/resetTeaming/components/strudentPayInfo'),
  197. route.children = route.children.concat([
  198. {
  199. name: '学员缴费详情',
  200. path: 'strudentPayInfo',
  201. component: () => import('@/views/resetTeaming/components/strudentPayInfo'),
  202. hidden: true,
  203. meta: {
  204. noCache: '1',
  205. title: '学员缴费详情',
  206. belongTopMenu: "/business",
  207. activeMenu: '/teamList'
  208. }
  209. },
  210. {
  211. name: '乐团详情',
  212. path: 'resetTeaming',
  213. component: () => import('@/views/resetTeaming/index'),
  214. hidden: true,
  215. meta: {
  216. noCache: '1',
  217. title: '乐团详情',
  218. belongTopMenu: "/business",
  219. activeMenu: '/teamList'
  220. }
  221. },
  222. {
  223. name: '新建vip',
  224. path: 'buildVip',
  225. component: () => import('@/views/buildVip/index'),
  226. hidden: true,
  227. meta: {
  228. noCache: '1',
  229. title: 'VIP/乐理课申请',
  230. belongTopMenu: "/business",
  231. activeMenu: '/vipManager/vipList'
  232. }
  233. },
  234. {
  235. name: 'vip修改',
  236. path: 'vipReset',
  237. component: () => import('@/views/vipClass/vipReset'),
  238. hidden: true,
  239. meta: {
  240. noCache: '1',
  241. title: 'VIP/乐理课修改',
  242. belongTopMenu: "/business",
  243. activeMenu: '/vipManager/vipList'
  244. }
  245. },
  246. {
  247. name: 'vip详情',
  248. path: 'vipDetail',
  249. component: () => import('@/views/vipClass/vipDetail'),
  250. hidden: true,
  251. meta: {
  252. noCache: '1',
  253. title: 'vip详情',
  254. belongTopMenu: "/business",
  255. activeMenu: '/vipManager/vipList'
  256. }
  257. },
  258. {
  259. name: '网管课详情',
  260. path: 'accompanys',
  261. component: () => import('@/views/accompanyManager/accompanys'),
  262. hidden: true,
  263. meta: {
  264. noCache: '1',
  265. title: '网管课详情',
  266. belongTopMenu: "/business",
  267. activeMenu: '/accompanyManager/accompany'
  268. }
  269. },
  270. {
  271. name: '评价详情',
  272. path: 'evaluateDetail',
  273. component: () => import('@/views/evaluateManager/evaluateDetail'),
  274. hidden: true,
  275. meta: {
  276. noCache: '1',
  277. title: '评价详情',
  278. belongTopMenu: "/business",
  279. activeMenu: '/commentManager'
  280. }
  281. },
  282. {
  283. name: '课外训练详情',
  284. path: 'afterSchoolDetail',
  285. component: () => import('@/views/afterSchoolManager/afterSchoolDetail'),
  286. hidden: true,
  287. meta: {
  288. noCache: '1',
  289. title: 'VIP/乐理课详情',
  290. belongTopMenu: "/business",
  291. activeMenu: '/afterSchoolManager'
  292. }
  293. },
  294. {
  295. name: '学员详情',
  296. path: 'studentDetail',
  297. component: () => import('@/views/studentManager/index'),
  298. hidden: true,
  299. meta: {
  300. noCache: '1',
  301. title: '学员详情',
  302. belongTopMenu: "/business",
  303. activeMenu: '/studentManager/studentList'
  304. }
  305. },
  306. {
  307. name: '老师详情',
  308. path: 'teacherDetail',
  309. component: () => import('@/views/teacherManager/teacherDetail/index'),
  310. hidden: true,
  311. meta: {
  312. noCache: '1',
  313. title: '老师详情',
  314. belongTopMenu: "/business",
  315. activeMenu: '/teacherManager/teacherList'
  316. }
  317. },
  318. {
  319. name: '老师修改',
  320. path: 'teacherOperation',
  321. component: () => import('@/views/teacherManager/teacherOperation/index'),
  322. hidden: true,
  323. meta: {
  324. noCache: '1',
  325. title: '老师修改',
  326. belongTopMenu: "/business",
  327. activeMenu: '/teacherManager/teacherList'
  328. }
  329. },
  330. {
  331. name: '问答详情',
  332. path: 'answer',
  333. component: () => import('@/views/reaplceMusicPlayer/answerList'),
  334. hidden: true,
  335. meta: {
  336. noCache: '1',
  337. title: '问答详情',
  338. belongTopMenu: "/business",
  339. activeMenu: '/otherManager/reaplceMusicPlayer'
  340. }
  341. },
  342. // /otherManager/reaplceMusicPlayer /reaplceMusicPlayer/answer
  343. ])
  344. }
  345. if (route.path == '/operateManager') {
  346. route.children = route.children.concat([
  347. {
  348. name: '服务指标(详情)',
  349. path: 'serverIndexDetail',
  350. component: () => import('@/views/operateManager/serverIndexDetail'),
  351. hidden: true,
  352. meta: {
  353. noCache: '1',
  354. title: '服务指标(详情)',
  355. belongTopMenu: "/operateManager",
  356. activeMenu: '/serverIndexManager/serverIndexList'
  357. }
  358. },
  359. {
  360. name: '新建活动方案',
  361. path: 'vipNewActive',
  362. component: () => import('@/views/categroyManager/vipNewActive'),
  363. hidden: true,
  364. meta: {
  365. noCache: '1',
  366. title: '新建活动方案',
  367. belongTopMenu: "/operateManager",
  368. activeMenu: '/vipActiveManager/vipActiveList'
  369. }
  370. },
  371. {
  372. name: '添加分部活动',
  373. path: 'branchActiveOperationAdd',
  374. component: () => import('@/views/categroyManager/insideSetting/branchActiveOperation'),
  375. hidden: true,
  376. meta: {
  377. noCache: '1',
  378. title: '添加分部活动',
  379. belongTopMenu: "/operateManager",
  380. activeMenu: '/branchActiveManager/branchActive'
  381. }
  382. },
  383. {
  384. name: '修改分部活动',
  385. path: 'branchActiveOperation',
  386. component: () => import('@/views/categroyManager/insideSetting/branchActiveOperation'),
  387. hidden: true,
  388. meta: {
  389. noCache: '1',
  390. title: '修改分部活动',
  391. belongTopMenu: "/operateManager",
  392. activeMenu: '/branchActiveManager/branchActive'
  393. }
  394. },
  395. {
  396. name: '添加问卷',
  397. path: 'questionOperations',
  398. component: () => import('@/views/setQuestions/operation'),
  399. hidden: true,
  400. meta: {
  401. noCache: '1',
  402. title: '添加问卷',
  403. belongTopMenu: "/operateManager",
  404. activeMenu: '/operateManager/setQuestions'
  405. }
  406. },
  407. // /operateManager/setQuestions /questionOperation operateManager
  408. ])
  409. }
  410. if (route.path == '/financialManager') {
  411. route.children = route.children.concat([
  412. {
  413. name: '经营报表详情',
  414. path: 'businessStatementDetail',
  415. component: () => import('@/views/businessManager/orderManager/businessStatementDetail'),
  416. hidden: true,
  417. meta: {
  418. noCache: '1',
  419. title: '经营报表详情',
  420. belongTopMenu: "/financialManager",
  421. activeMenu: '/businessStatement'
  422. }
  423. },
  424. ])
  425. }
  426. if (route.path == '/contentManager') {
  427. route.children = route.children.concat([
  428. {
  429. name: '经营报表详情',
  430. path: 'helpCategory',
  431. component: () => import('@/views/helpCenter/helpCategory'),
  432. hidden: true,
  433. meta: {
  434. noCache: '1',
  435. title: '经营报表详情',
  436. belongTopMenu: "/contentManager",
  437. activeMenu: '/contentManager/helpContent'
  438. }
  439. },
  440. {
  441. name: '添加&修改内容管理',
  442. path: 'contentOperation',
  443. component: () => import('@/views/contentManager/contentOperation'),
  444. hidden: true,
  445. meta: {
  446. noCache: '1',
  447. title: '添加&修改内容管理',
  448. belongTopMenu: "/contentManager",
  449. activeMenu: '/contentManager/contentManager'
  450. }
  451. },
  452. ])
  453. }
  454. if (route.path == '/shopManager') {
  455. route.children = route.children.concat([
  456. {
  457. name: '进货清单',
  458. path: 'purchaseLlist',
  459. component: () => import('@/views/businessManager/shopManager/purchase-llist'),
  460. hidden: true,
  461. meta: {
  462. noCache: '1',
  463. title: '进货清单',
  464. belongTopMenu: "/shopManager",
  465. activeMenu: '/shopManager'
  466. }
  467. },
  468. {
  469. name: '添加修改商品',
  470. path: 'shopOperation',
  471. component: () => import('@/views/businessManager/shopManager/shopOperation'),
  472. hidden: true,
  473. meta: {
  474. noCache: '1',
  475. title: '添加修改商品',
  476. belongTopMenu: "/shopManager",
  477. activeMenu: '/shopManager'
  478. }
  479. },
  480. ])
  481. }
  482. if(route.path == '/systemManager'){
  483. route.children = route.children.concat([
  484. {
  485. name: '创建&修改汇付账号',
  486. path: 'adapayOperation',
  487. component: () => import('@/views/adapayAccount/form'),
  488. hidden: true,
  489. meta: {
  490. noCache: '1',
  491. title: '创建&修改汇付账号',
  492. belongTopMenu: "/systemManager",
  493. activeMenu: '/sysBasics/adapayManager'
  494. }
  495. },
  496. {
  497. name: '添加&查看时间充值活动',
  498. path: 'entryOperation',
  499. component: () => import('@/views/app/entryOperation'),
  500. hidden: true,
  501. meta: {
  502. noCache: '1',
  503. title: '添加&查看时间充值活动',
  504. belongTopMenu: "/systemManager",
  505. activeMenu: '/sysBasics/entryActivities'
  506. }
  507. },
  508. {
  509. name: '添加&修改系统权限',
  510. path: 'adminOperation',
  511. component: () => import('@/views/categroyManager/insideSetting/adminOperation'),
  512. hidden: true,
  513. meta: {
  514. noCache: '1',
  515. title: '添加&修改系统权限',
  516. belongTopMenu: "/systemManager",
  517. activeMenu: '/parameter/adminManager'
  518. }
  519. },
  520. // /parameter/adminManager adminOperation
  521. ])
  522. }
  523. })
  524. return accessedRoutes
  525. }
  526. const actions = {
  527. generateRoutes({ commit }) {
  528. return new Promise(resolve => {
  529. // 获取接口返回的权限菜单
  530. getSilder().then(res => {
  531. if (res.code == 200) {
  532. let result = addTopMenu(res.data)
  533. let newData = recursionRouter(result);
  534. newData = getFirstMenu(newData)
  535. recursionPermission(res.data)
  536. let accessedRoutes
  537. // 生成异步路由表
  538. accessedRoutes = generateAsyncRouter(asyncRoutes, newData)
  539. accessedRoutes = setDetailRoute(accessedRoutes)
  540. console.log('生成出来的异步路由', accessedRoutes)
  541. // var result = accessedRoutes.concat({ path: '*', redirect: '/404', hidden: true })
  542. commit('SET_ROUTES', accessedRoutes)
  543. // commit('SET_PERMISSION', recursionPermission(res.data).flat(Infinity))
  544. window.localStorage.removeItem('permission')
  545. window.localStorage.setItem('permission', tempArr)
  546. this.dispatch('app/setDotStatus')
  547. resolve(accessedRoutes)
  548. }
  549. })
  550. })
  551. },
  552. removePermission({ commit }) {
  553. window.localStorage.removeItem('permission')
  554. commit('SET_PERMISSION', [])
  555. }
  556. }
  557. export default {
  558. namespaced: true,
  559. state,
  560. mutations,
  561. actions
  562. }