app.js 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. data.forEach((it, inx) => {
  18. let found = false
  19. for (let i = 0; i < cp.length; i += 1) {
  20. if (cp[i].MenuId === it.FatherId) {
  21. found = true
  22. cp[i].children = [...(cp[i].children || []), cp[inx]]
  23. break
  24. }
  25. }
  26. if (!found) {
  27. res.push(cp[inx])
  28. }
  29. })
  30. return res
  31. }
  32. export default {
  33. state: {
  34. menusRaw: [],
  35. menus: [],
  36. breadcrumb: [],
  37. cases: [],
  38. roles: [],
  39. user: {}
  40. },
  41. mutations: {
  42. init (state, data) { // 这里的state对应着上面这个state
  43. state.cases = data.cases
  44. state.menusRaw = data.menus
  45. state.roles = data.roles
  46. state.user = data.user
  47. state.menus = packChildren(data.menus)
  48. },
  49. updateBread (state, { routerData, Url }) {
  50. // 扁平 routerData
  51. // 同时构造出 含有 title 的 parent 数据
  52. const routeList = flatten([], routerData[1].children, state.menusRaw)
  53. console.log(state, routerData[1].children, routeList)
  54. // 过滤出与 name 相同的 route 节点
  55. const currentRoute = routeList.filter(x => x.Url === Url)[0]
  56. // 获取 面包屑
  57. if (currentRoute) state.breadcrumb = currentRoute.parent.slice(1).concat(currentRoute)
  58. }
  59. },
  60. actions: {
  61. updateSystemInfo (context) { // 这里的context和我们使用的$store拥有相同的对象和方法
  62. this.$ajax(this.$api.system.init.url, {
  63. method: this.$api.system.init.method
  64. })
  65. .then(res => {
  66. context.commit('init', res)
  67. })
  68. }
  69. }
  70. }