wangrong
2026-08-30 761e5c5fa871959f60e15c971c8d83f2b53d0af5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { defineStore } from 'pinia'
import { store } from '@/store'
import { queryTreeApi, queryBusiTreeApi } from '@/api/visualization/dataVisualization'
import { getDatasetTree } from '@/api/dataset'
import { listDatasources } from '@/api/datasource'
import type { BusiTreeRequest, BusiTreeNode } from '@/models/tree/TreeNode'
import { pathValid } from '@/store/modules/permission'
import { useCache } from '@/hooks/web/useCache'
import { useAppStoreWithOut } from '@/store/modules/app'
const appStore = useAppStoreWithOut()
const { wsCache } = useCache()
export interface InnerInteractive {
  rootManage: boolean
  anyManage: boolean
  treeNodes: BusiTreeNode[]
  leafNodeCount: number
  menuAuth: boolean
}
 
interface InteractiveState {
  data: Record<number, InnerInteractive>
}
 
const apiMap = [queryTreeApi, queryTreeApi, getDatasetTree, listDatasources]
 
const busiFlagMap = ['dashboard', 'dataV', 'dataset', 'datasource']
 
export const interactiveStore = defineStore('interactive', {
  state: (): InteractiveState => ({
    data: {}
  }),
  getters: {
    getPanel(): InnerInteractive {
      return this.data[0]
    },
    getScreen(): InnerInteractive {
      return this.data[1]
    },
    getDataset(): InnerInteractive {
      return this.data[2]
    },
    getDatasource(): InnerInteractive {
      return this.data[3]
    },
    getData(): InteractiveState {
      return this.data
    }
  },
  actions: {
    async setInteractive(param: BusiTreeRequest, resParam?: object) {
      const flag = busiFlagMap.findIndex(item => item === param.busiFlag)
      if (!hasMenuAuth(flag) && !window.DataEaseBi && !appStore.getIsIframe) {
        const tempData: InnerInteractive = {
          rootManage: false,
          anyManage: false,
          treeNodes: [],
          leafNodeCount: 0,
          menuAuth: false
        }
        this.data[flag] = tempData
        if (flag === 0) {
          wsCache.set('panel-weight', {})
        }
        if (flag === 1) {
          wsCache.set('screen-weight', {})
        }
        return []
      }
      let res = resParam
      if (!resParam) {
        const method = apiMap[flag]
        res = await method(param)
      }
      this.data[flag] = convertInteractive(res)
      if (flag === 0) {
        wsCache.set('panel-weight', convertLocalStorage(this.data[flag]))
      }
      if (flag === 1) {
        wsCache.set('screen-weight', convertLocalStorage(this.data[flag]))
      }
      return res
    },
    async initInteractive(refresh?: boolean) {
      if (refresh) {
        await this.loadBusiInteractive()
        return
      }
      let index = 4
      while (index--) {
        if (!this.data[index] || refresh) {
          const param: BusiTreeRequest = {
            busiFlag: busiFlagMap[index]
          }
          await this.setInteractive(param)
        }
      }
    },
    async loadBusiInteractive() {
      const param = {}
      for (let i = 0; i < busiFlagMap.length; i++) {
        const key = busiFlagMap[i]
        if (window.DataEaseBi || appStore.getIsIframe || hasMenuAuth(i)) {
          param[key] = { busiFlag: key }
        }
      }
      const data = await queryBusiTreeApi(param)
      for (const busiKey in data) {
        const res = data[busiKey]
        this.setInteractive(param[busiKey], res)
      }
    },
    clear() {
      this.data = {}
      wsCache.set('panel-weight', {})
      wsCache.set('screen-weight', {})
    }
  }
})
 
export const interactiveStoreWithOut = () => interactiveStore(store)
 
const convertInteractive = (list): InnerInteractive => {
  const result: InnerInteractive = {
    rootManage: list[0]['weight'] >= 7,
    anyManage: false,
    treeNodes: (list as unknown as BusiTreeNode[]) || [],
    leafNodeCount: 0,
    menuAuth: true
  }
  const stack = [...list]
  let leafNodeCount = 0
  while (stack.length) {
    const node = stack.pop()
    if (!node['leaf'] && node['weight'] >= 7) {
      result.anyManage = true
      // break
    }
    if (node['leaf'] && node['weight']) {
      ++leafNodeCount
    }
    if (node?.children?.length) {
      node.children.forEach(kid => stack.push(kid))
    }
  }
  result.leafNodeCount = leafNodeCount
  return result
}
 
const hasMenuAuth = (flag: number): boolean => {
  let path = '/panel/index'
  if (flag === 1) {
    path = '/screen/index'
  } else if (flag === 2) {
    path = '/data/dataset'
  } else if (flag === 3) {
    path = '/data/datasource'
  }
  const valid = pathValid(path)
  return valid
}
 
const convertLocalStorage = (data?: InnerInteractive) => {
  if (!data?.leafNodeCount) {
    return {}
  }
  const result = {}
  const treeNodes = data.treeNodes
  const stack = [...treeNodes]
  while (stack.length) {
    const node = stack.pop()
    if (node.leaf) {
      const { id, weight } = node
      result[id] = weight
    }
    if (node.children?.length) {
      node.children.forEach(kid => stack.push(kid))
    }
  }
  return result
}