app.js 2.8KB

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