123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. function flatten (parent, data = [], menusRaw = []) {
  2. return data.reduce((acc, { name, children }) => {
  3. // console.log('menusRaw:', menusRaw)
  4. const menu = (menusRaw || []).filter(x => x.Url === name)[0]
  5. if (!menu) return acc
  6. const MenuName = menu.MenuName
  7. const item = { Url: name, MenuName, parent }
  8. if (children && children.length) {
  9. const p = parent.concat({ Url: name, MenuName })
  10. return acc.concat(item, flatten(p, children, menusRaw))
  11. }
  12. return acc.concat(item)
  13. }, [])
  14. }
  15. function packChildren (data) {
  16. let cp = data.slice()
  17. const res = []
  18. cp.map(x => {
  19. x.id = x.MenuId
  20. x.label = x.MenuName
  21. return x
  22. })
  23. data.forEach((it, inx) => {
  24. let found = false
  25. for (let i = 0; i < cp.length; i += 1) {
  26. if (cp[i].MenuId === it.FatherId) {
  27. found = true
  28. if (it.IsShow === 1) {
  29. cp[i].children = [...(cp[i].children || []), cp[inx]]
  30. }
  31. break
  32. }
  33. }
  34. if (!found && cp[inx].IsShow === 1) {
  35. res.push(cp[inx])
  36. }
  37. })
  38. return res
  39. }
  40. export default {
  41. state: {
  42. menusRaw: [],
  43. menus: [],
  44. breadcrumb: [],
  45. cases: {
  46. list: [],
  47. default: '',
  48. belongCase: null,
  49. },
  50. roles: [],
  51. user: {},
  52. clientUrl: '',
  53. },
  54. mutations: {
  55. init (state, data) { // 这里的state对应着上面这个state
  56. state.menusRaw = data.menus
  57. state.roles = data.roles
  58. state.user = data.user
  59. state.menus = packChildren(data.menus || [])
  60. state.clientUrl = data.clienturl
  61. let defaultCase = ''
  62. for (let i = 0; i < data.cases.length; i++) {
  63. if (data.cases[i].IsBelong === 1) {
  64. defaultCase = data.cases[i].CaseId
  65. }
  66. }
  67. state.cases = {
  68. list: data.cases,
  69. default: defaultCase,
  70. belongCase: defaultCase,
  71. }
  72. },
  73. updateBread (state, { routerData, Url }) {
  74. // 扁平 routerData
  75. // 同时构造出 含有 title 的 parent 数据
  76. const routeList = flatten([], routerData[1].children, state.menusRaw)
  77. // console.log(routerData[1].children, state.menusRaw)
  78. // 过滤出与 name 相同的 route 节点
  79. const currentRoute = routeList.filter(x => x.Url === Url)[0]
  80. // console.log(currentRoute)
  81. // 获取 面包屑
  82. if (currentRoute) state.breadcrumb = currentRoute.parent.slice(1).concat(currentRoute)
  83. // console.log(state.breadcrumb)
  84. }
  85. },
  86. actions: {
  87. updateSystemInfo (context) { // 这里的context和我们使用的$store拥有相同的对象和方法
  88. return new Promise((resolve) => {
  89. this.$ajax(this.$api.system.init.url, {
  90. method: this.$api.system.init.method
  91. }).then(res => {
  92. context.commit('init', res)
  93. resolve()
  94. })
  95. })
  96. }
  97. }
  98. }