permission.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. import { asyncRoutes, constantRoutes } from '@/router'
  2. import router from '@/router'
  3. import { getSilder } from '@/api/silder'
  4. import store from "@/store";
  5. import { Message } from 'element-ui'
  6. // import { stat } from 'fs'
  7. // import { removeToken } from '@/utils/auth'
  8. // import Layout from '@/layout'
  9. /**
  10. * 遍历接口菜单添加页面
  11. * @param asyncRoutes
  12. * @param getMenu
  13. */
  14. function generateAsyncRouter(asyncRoutes, data) {
  15. if (!data) {
  16. return []
  17. }
  18. data.forEach((item) => {
  19. item.component = asyncRoutes[item.component]
  20. if (item.children && item.children.length > 0) {
  21. generateAsyncRouter(asyncRoutes, item.children)
  22. }
  23. })
  24. return data
  25. }
  26. /**
  27. * 判断平台端添加首页
  28. * @param type
  29. */
  30. // const type = getters.type
  31. const state = {
  32. routes: [],
  33. addRoutes: [],
  34. type: '', // 登录的平台类型
  35. permission: [] // 权限
  36. }
  37. const mutations = {
  38. SET_ROUTES: (state, routes) => {
  39. state.addRoutes = routes
  40. state.routes = constantRoutes.concat(routes)
  41. },
  42. SET_PERMISSION: (state, permission) => {
  43. state.permission = permission
  44. }
  45. }
  46. function getFirstMenu(routes) {
  47. let firstMenu = null
  48. routes.forEach(item => {
  49. if (item.children?.length > 0 && !item.hidden) {
  50. firstMenu = pathErgodic(item)
  51. // console.log(firstMenu)
  52. item.redirect = firstMenu
  53. }
  54. })
  55. return routes
  56. }
  57. function pathErgodic(item) {
  58. // console.log(item)
  59. let firstMenu = null
  60. item.children.forEach(i => {
  61. if (!firstMenu && i.children?.length > 0&&!i.hidden) {
  62. let isChildrenList = false;
  63. i.children.forEach(ii=>{
  64. if(!ii.hidden){
  65. isChildrenList = true
  66. }
  67. })
  68. if(isChildrenList){
  69. firstMenu = pathErgodic(i)
  70. }else{
  71. if (!firstMenu && checkPathUrl(i.path)) {
  72. firstMenu = i.path
  73. } else {
  74. if (!firstMenu && !i.hidden) {
  75. firstMenu = item.path + '/' + i.path
  76. }
  77. }
  78. }
  79. } else {
  80. if (!firstMenu && checkPathUrl(i.path)) {
  81. firstMenu = i.path
  82. } else {
  83. if (!firstMenu && !i.hidden) {
  84. firstMenu = item.path + '/' + i.path
  85. }
  86. }
  87. }
  88. })
  89. return firstMenu
  90. }
  91. // 判断path有没有带/,并且是第一个位置
  92. function checkPathUrl(path) {
  93. return path.indexOf('/') === 0 ? true : false
  94. }
  95. // 路由
  96. // 递归遍历数组
  97. function recursionRouter(arr) {
  98. // 这里来了
  99. if (arr.length > 0) {
  100. let newArr = [];
  101. for (let i = 0; i < arr.length; i++) {
  102. if (arr[i].type == 1) {
  103. continue
  104. }
  105. let obj = {};
  106. obj.component = arr[i].component;
  107. obj.name = arr[i].component;
  108. // if (item.type != '1' && item.component) {
  109. // if (!item.path.startsWith('/') && item.component != 'Layout') {
  110. // obj.names = item.name
  111. // }
  112. // }
  113. arr[i].hid == 0 ? obj.hidden = false : obj.hidden = true
  114. // console.log('高亮标签'+arr[i].parentPermission,'普通路径'+arr[i].path)
  115. obj.path = arr[i].path;
  116. obj.meta = { 'title': arr[i].name, 'icon': arr[i].icon, 'noCache': arr[i].keepAlive, 'activeMenu': arr[i].parentPermission, 'belongTopMenu': arr[i].belongTopMenu,'id':arr[i].id }
  117. if (arr[i].sysMenus && arr[i].sysMenus.length > 0) {
  118. obj.children = recursionRouter(arr[i].sysMenus);
  119. }
  120. newArr.push(obj)
  121. }
  122. return newArr
  123. }
  124. }
  125. // 设置 belongTopMenu, 顶部菜单
  126. function addTopMenu(arr) {
  127. if (arr.length > 0) {
  128. let newArr = [];
  129. for (let i = 0; i < arr.length; i++) {
  130. if (arr[i].type == 1) {
  131. continue
  132. }
  133. let obj = arr[i]
  134. obj.belongTopMenu = obj.path
  135. if (arr[i].sysMenus && arr[i].sysMenus.length > 0) {
  136. obj.sysMenus = setTopMenu(arr[i].sysMenus, obj);
  137. }
  138. newArr.push(obj)
  139. }
  140. return newArr
  141. }
  142. }
  143. function setTopMenu(arr, topParentArr) {
  144. let newArr = [];
  145. for (let i = 0; i < arr.length; i++) {
  146. let obj = arr[i]
  147. obj.belongTopMenu = topParentArr.path
  148. if (arr[i].sysMenus && arr[i].sysMenus.length > 0) {
  149. obj.sysMenus = setTopMenu(arr[i].sysMenus, topParentArr);
  150. }
  151. newArr.push(obj)
  152. }
  153. return newArr
  154. }
  155. // 权限
  156. // 递归遍历数组
  157. let tempArr = []
  158. function recursionPermission(arr) {
  159. arr.map(item => {
  160. tempArr.push(item.memo)
  161. if (item.sysMenus && item.sysMenus.length > 0) {
  162. recursionPermission(item.sysMenus)
  163. }
  164. })
  165. }
  166. function setDetailRoute(accessedRoutes) {
  167. // console.log(accessedRoutes)
  168. accessedRoutes.forEach(route => {
  169. // console.log(route.path)
  170. if (route.path == '/main') {
  171. route.children = route.children.concat([
  172. {
  173. name: '日程安排',
  174. path: 'scheduleDetail',
  175. component: () => import('@/views/main/teamSchedule/scheduleDetail'),
  176. hidden: true,
  177. meta: {
  178. noCache: '1',
  179. title: '日程安排',
  180. belongTopMenu: "/main",
  181. activeMenu: '/main/main',
  182. id:'xx1'
  183. }
  184. },
  185. {
  186. name: '未在班级学员',
  187. path: 'notClassStudent',
  188. component: () => import('@/views/main/notClassStudent'),
  189. hidden: true,
  190. meta: {
  191. noCache: '1',
  192. title: '未在班级学员',
  193. belongTopMenu: "/main",
  194. activeMenu: '/main/main',
  195. id:'xx2'
  196. }
  197. },
  198. {
  199. name: '学员请假列表',
  200. path: 'studentLeaveList',
  201. component: () => import('@/views/main/studentLeaveList'),
  202. hidden: true,
  203. meta: {
  204. noCache: '1',
  205. title: '学员请假列表',
  206. belongTopMenu: "/main",
  207. activeMenu: '/main/main'
  208. }
  209. },
  210. {
  211. name: '乐团会员列表',
  212. path: 'teamMemberList',
  213. component: () => import('@/views/studentManager/memberList'),
  214. hidden: true,
  215. meta: {
  216. noCache: '1',
  217. title: '乐团会员列表',
  218. belongTopMenu: "/main",
  219. activeMenu: '/main/main'
  220. }
  221. },
  222. {
  223. name: '乐团展演列表',
  224. path: 'teamShowList',
  225. component: () => import('@/views/main/teamShowList'),
  226. hidden: true,
  227. meta: {
  228. noCache: '1',
  229. title: '乐团展演列表',
  230. belongTopMenu: "/main",
  231. activeMenu: '/main/main'
  232. }
  233. },
  234. //
  235. ])
  236. }
  237. if (route.path == '/business') {
  238. // import('@/views/resetTeaming/components/strudentPayInfo'),
  239. route.children = route.children.concat([
  240. {
  241. name: '学员缴费详情',
  242. path: 'strudentPayInfo',
  243. component: () => import('@/views/resetTeaming/components/strudentPayInfo'),
  244. hidden: true,
  245. meta: {
  246. noCache: '1',
  247. title: '学员缴费详情',
  248. belongTopMenu: "/business",
  249. activeMenu: '/teamList',
  250. id:'xx3'
  251. }
  252. },
  253. {
  254. name: '乐团详情',
  255. path: 'resetTeaming',
  256. component: () => import('@/views/resetTeaming/index'),
  257. hidden: true,
  258. meta: {
  259. noCache: '1',
  260. title: '乐团详情',
  261. belongTopMenu: "/business",
  262. activeMenu: '/teamList',
  263. id:'xx4'
  264. }
  265. },
  266. {
  267. name: '乐团档案',
  268. path: 'musicArchices',
  269. component: () => import('@/views/resetTeaming/components/musicArchices'),
  270. hidden: true,
  271. meta: {
  272. noCache: '1',
  273. title: '乐团档案',
  274. belongTopMenu: "/business",
  275. activeMenu: '/teamList',
  276. id:'xx4'
  277. }
  278. },
  279. //musicArchices
  280. {
  281. name: '会员排课列表',
  282. path: 'memberClassList',
  283. component: () => import('@/views/teamDetail/components/memberClassList'),
  284. hidden: true,
  285. meta: {
  286. noCache: '1',
  287. title: '会员排课列表',
  288. belongTopMenu: "/business",
  289. activeMenu: '/teamList',
  290. id:'xx28'
  291. }
  292. },
  293. {
  294. name: '相册详情',
  295. path: 'photo-detail',
  296. component: () => import('@/views/photo-detail'),
  297. hidden: true,
  298. meta: {
  299. noCache: '1',
  300. title: '相册详情',
  301. belongTopMenu: "/business",
  302. activeMenu: '/teamList',
  303. id:'xxx28'
  304. }
  305. },
  306. // 相册详情
  307. {
  308. name: '全部证书',
  309. path: 'performance',
  310. component: () => import('@/views/photo-detail'),
  311. hidden: true,
  312. meta: {
  313. noCache: '1',
  314. title: '全部证书',
  315. belongTopMenu: "/business",
  316. activeMenu: '/teamList',
  317. id:'xxx28'
  318. }
  319. },
  320. // 全部证书
  321. {
  322. name: '新建vip',
  323. path: 'buildVip',
  324. component: () => import('@/views/buildVip/index'),
  325. hidden: true,
  326. meta: {
  327. noCache: '1',
  328. title: 'VIP/乐理课申请',
  329. belongTopMenu: "/business",
  330. activeMenu: '/vipManager/vipList',
  331. id:'xx5'
  332. }
  333. },
  334. {
  335. name: 'vip修改',
  336. path: 'vipReset',
  337. component: () => import('@/views/vipClass/vipReset'),
  338. hidden: true,
  339. meta: {
  340. noCache: '1',
  341. title: 'VIP/乐理课修改',
  342. belongTopMenu: "/business",
  343. activeMenu: '/vipManager/vipList',
  344. id:'xx6'
  345. }
  346. },
  347. {
  348. name: 'vip详情',
  349. path: 'vipDetail',
  350. component: () => import('@/views/vipClass/vipDetail'),
  351. hidden: true,
  352. meta: {
  353. noCache: '1',
  354. title: 'vip详情',
  355. belongTopMenu: "/business",
  356. activeMenu: '/vipManager/vipList',
  357. id:'xx7'
  358. }
  359. },
  360. {
  361. name: '网管课详情',
  362. path: 'accompanys',
  363. component: () => import('@/views/accompanyManager/accompanys'),
  364. hidden: true,
  365. meta: {
  366. noCache: '1',
  367. title: '网管课详情',
  368. belongTopMenu: "/business",
  369. activeMenu: '/accompanyManager/accompany',
  370. id:'xx8'
  371. }
  372. },
  373. {
  374. name: '评价详情',
  375. path: 'evaluateDetail',
  376. component: () => import('@/views/evaluateManager/evaluateDetail'),
  377. hidden: true,
  378. meta: {
  379. noCache: '1',
  380. title: '评价详情',
  381. belongTopMenu: "/business",
  382. activeMenu: '/commentManager',
  383. id:'xx9'
  384. }
  385. },
  386. {
  387. name: '课外训练详情',
  388. path: 'afterSchoolDetail',
  389. component: () => import('@/views/afterSchoolManager/afterSchoolDetail'),
  390. hidden: true,
  391. meta: {
  392. noCache: '1',
  393. title: 'VIP/乐理课详情',
  394. belongTopMenu: "/business",
  395. activeMenu: '/afterSchoolManager',
  396. id:'xx10'
  397. }
  398. },
  399. {
  400. name: '学员详情',
  401. path: 'studentDetail',
  402. component: () => import('@/views/studentManager/index'),
  403. hidden: true,
  404. meta: {
  405. noCache: '1',
  406. title: '学员详情',
  407. belongTopMenu: "/business",
  408. activeMenu: '/studentManager/studentList',
  409. id:'xx11'
  410. }
  411. },
  412. {
  413. name: '老师详情',
  414. path: 'teacherDetail',
  415. component: () => import('@/views/teacherManager/teacherDetail/index'),
  416. hidden: true,
  417. meta: {
  418. noCache: '1',
  419. title: '老师详情',
  420. belongTopMenu: "/business",
  421. activeMenu: '/teacherManager/teacherList',
  422. id:'xx12'
  423. }
  424. },
  425. {
  426. name: '老师修改',
  427. path: 'teacherOperation',
  428. component: () => import('@/views/teacherManager/teacherOperation/index'),
  429. hidden: true,
  430. meta: {
  431. noCache: '1',
  432. title: '老师修改',
  433. belongTopMenu: "/business",
  434. activeMenu: '/teacherManager/teacherList',
  435. id:'xx13'
  436. }
  437. },
  438. {
  439. name: '问答详情',
  440. path: 'answer',
  441. component: () => import('@/views/reaplceMusicPlayer/answerList'),
  442. hidden: true,
  443. meta: {
  444. noCache: '1',
  445. title: '问答详情',
  446. belongTopMenu: "/business",
  447. activeMenu: '/otherManager/reaplceMusicPlayer',
  448. id:'xx4'
  449. }
  450. },
  451. {
  452. name: '活动详情',
  453. path: 'childrensdayDetail',
  454. component: () => import('@/views/childrensDay/detail'),
  455. hidden: true,
  456. meta: {
  457. noCache: '1',
  458. title: '活动详情',
  459. belongTopMenu: "/business",
  460. activeMenu: '/childrensDay'
  461. }
  462. },
  463. // /otherManager/reaplceMusicPlayer /reaplceMusicPlayer/answer
  464. ])
  465. }
  466. if (route.path == '/operateManager') {
  467. route.children = route.children.concat([
  468. {
  469. name: '服务指标(详情)',
  470. path: 'serverIndexDetail',
  471. component: () => import('@/views/operateManager/serverIndexDetail'),
  472. hidden: true,
  473. meta: {
  474. noCache: '1',
  475. title: '服务指标(详情)',
  476. belongTopMenu: "/operateManager",
  477. activeMenu: '/serverIndexManager/serverIndexList',
  478. id:'xx15'
  479. }
  480. },
  481. {
  482. name: '新建活动方案',
  483. path: 'vipNewActive',
  484. component: () => import('@/views/categroyManager/vipNewActive'),
  485. hidden: true,
  486. meta: {
  487. noCache: '1',
  488. title: '新建活动方案',
  489. belongTopMenu: "/operateManager",
  490. activeMenu: '/vipActiveManager/vipActiveList',
  491. id:'xx16'
  492. }
  493. },
  494. {
  495. name: '添加分部活动',
  496. path: 'branchActiveOperationAdd',
  497. component: () => import('@/views/categroyManager/insideSetting/branchActiveOperation'),
  498. hidden: true,
  499. meta: {
  500. noCache: '1',
  501. title: '添加分部活动',
  502. belongTopMenu: "/operateManager",
  503. activeMenu: '/branchActiveManager/branchActive',
  504. id:'xx17'
  505. }
  506. },
  507. {
  508. name: '修改分部活动',
  509. path: 'branchActiveOperation',
  510. component: () => import('@/views/categroyManager/insideSetting/branchActiveOperation'),
  511. hidden: true,
  512. meta: {
  513. noCache: '1',
  514. title: '修改分部活动',
  515. belongTopMenu: "/operateManager",
  516. activeMenu: '/branchActiveManager/branchActive',
  517. id:'xx18'
  518. }
  519. },
  520. {
  521. name: '添加问卷',
  522. path: 'questionOperations',
  523. component: () => import('@/views/setQuestions/operation'),
  524. hidden: true,
  525. meta: {
  526. noCache: '1',
  527. title: '添加问卷',
  528. belongTopMenu: "/operateManager",
  529. activeMenu: '/operateManager/setQuestions',
  530. id:'xx19'
  531. }
  532. },
  533. // /operateManager/setQuestions /questionOperation operateManager
  534. ])
  535. }
  536. if (route.path == '/financialManager') {
  537. route.children = route.children.concat([
  538. {
  539. name: '经营报表详情',
  540. path: 'businessStatementDetail',
  541. component: () => import('@/views/businessManager/orderManager/businessStatementDetail'),
  542. hidden: true,
  543. meta: {
  544. noCache: '1',
  545. title: '经营报表详情',
  546. belongTopMenu: "/financialManager",
  547. activeMenu: '/businessStatement',
  548. id:'xx20'
  549. }
  550. },
  551. ])
  552. }
  553. if (route.path == '/contentManager') {
  554. route.children = route.children.concat([
  555. {
  556. name: '经营报表详情',
  557. path: 'helpCategory',
  558. component: () => import('@/views/helpCenter/helpCategory'),
  559. hidden: true,
  560. meta: {
  561. noCache: '1',
  562. title: '经营报表详情',
  563. belongTopMenu: "/contentManager",
  564. activeMenu: '/contentManager/helpContent',
  565. id:'xx21'
  566. }
  567. },
  568. {
  569. name: '添加&修改内容管理',
  570. path: 'contentOperation',
  571. component: () => import('@/views/contentManager/contentOperation'),
  572. hidden: true,
  573. meta: {
  574. noCache: '1',
  575. title: '添加&修改内容管理',
  576. belongTopMenu: "/contentManager",
  577. activeMenu: '/contentManager/contentManager',
  578. id:'xx22'
  579. }
  580. },
  581. ])
  582. }
  583. if (route.path == '/shopManager') {
  584. route.children = route.children.concat([
  585. {
  586. name: '进货清单',
  587. path: 'purchaseLlist',
  588. component: () => import('@/views/businessManager/shopManager/purchase-llist'),
  589. hidden: true,
  590. meta: {
  591. noCache: '1',
  592. title: '进货清单',
  593. belongTopMenu: "/shopManager",
  594. activeMenu: '/shopList',
  595. id:'xx23'
  596. }
  597. },
  598. {
  599. name: '添加修改商品',
  600. path: 'shopOperation',
  601. component: () => import('@/views/businessManager/shopManager/shopOperation'),
  602. hidden: true,
  603. meta: {
  604. noCache: '1',
  605. title: '添加修改商品',
  606. belongTopMenu: "/shopManager",
  607. activeMenu: '/shopList',
  608. id:'xx24'
  609. }
  610. },
  611. ])
  612. }
  613. if(route.path == '/systemManager'){
  614. route.children = route.children.concat([
  615. {
  616. name: '创建&修改汇付账号',
  617. path: 'adapayOperation',
  618. component: () => import('@/views/adapayAccount/form'),
  619. hidden: true,
  620. meta: {
  621. noCache: '1',
  622. title: '创建&修改汇付账号',
  623. belongTopMenu: "/systemManager",
  624. activeMenu: '/sysBasics/adapayManager',
  625. id:'xx25'
  626. }
  627. },
  628. {
  629. name: '添加&查看时间充值活动',
  630. path: 'entryOperation',
  631. component: () => import('@/views/app/entryOperation'),
  632. hidden: true,
  633. meta: {
  634. noCache: '1',
  635. title: '添加&查看时间充值活动',
  636. belongTopMenu: "/systemManager",
  637. activeMenu: '/sysBasics/entryActivities',
  638. id:'xx26'
  639. }
  640. },
  641. {
  642. name: '添加&修改系统权限',
  643. path: 'adminOperation',
  644. component: () => import('@/views/categroyManager/insideSetting/adminOperation'),
  645. hidden: true,
  646. meta: {
  647. noCache: '1',
  648. title: '添加&修改系统权限',
  649. belongTopMenu: "/systemManager",
  650. activeMenu: '/parameter/adminManager',
  651. id:'xx27'
  652. }
  653. },
  654. // /parameter/adminManager adminOperation
  655. ])
  656. }
  657. })
  658. return accessedRoutes
  659. }
  660. const actions = {
  661. generateRoutes({ commit }) {
  662. return new Promise(resolve => {
  663. // 获取接口返回的权限菜单
  664. getSilder().then(async res => {
  665. if (res.code == 200) {
  666. let result = addTopMenu(res.data)
  667. if(res.data?.length <1){
  668. // 一条权限都没有
  669. //退出 跳到登录页 提示'该账号无任何权限'
  670. // console.log(store.dispatch)
  671. await store.dispatch("user/logout");
  672. localStorage.removeItem("firstMenuUrl");
  673. // await this.$store.dispatch("permission/removePermission")
  674. Message.error('该用户无访问权限')
  675. router.push(`/login`);
  676. // window.location.reload();
  677. }
  678. let newData = recursionRouter(result);
  679. newData = getFirstMenu(newData)
  680. recursionPermission(res.data)
  681. let accessedRoutes
  682. // 生成异步路由表
  683. accessedRoutes = generateAsyncRouter(asyncRoutes, newData)
  684. accessedRoutes = setDetailRoute(accessedRoutes)
  685. // console.log('生成出来的异步路由', accessedRoutes)
  686. // var result = accessedRoutes.concat({ path: '*', redirect: '/404', hidden: true })
  687. commit('SET_ROUTES', accessedRoutes)
  688. // commit('SET_PERMISSION', recursionPermission(res.data).flat(Infinity))
  689. window.localStorage.removeItem('permission')
  690. window.localStorage.setItem('permission', tempArr)
  691. this.dispatch('app/setDotStatus')
  692. resolve(accessedRoutes)
  693. }
  694. })
  695. })
  696. },
  697. removePermission({ commit }) {
  698. window.localStorage.removeItem('permission')
  699. commit('SET_PERMISSION', [])
  700. }
  701. }
  702. export default {
  703. namespaced: true,
  704. state,
  705. mutations,
  706. actions
  707. }