Browse Source

[FE]优化部门列表数据结构算法

luoxu5 4 years ago
parent
commit
3a19daa5a1

+ 1 - 29
security-protection-platform/src/api/common/index.js

@ -1,8 +1,4 @@
1 1
import http from '@/http'
2
import { keysMappingReverse } from '@/utils/keysMapping'
3
// const mapping = {
4
//   id: 'code'
5
// }
6 2
const $default = http.$default
7 3
const api = {
8 4
  /**
@ -18,32 +14,8 @@ const api = {
18 14
    return $default.get('/sp/workEmployee/queryAllOrganize').then((resp) => {
19 15
      const data = resp.data.data
20 16
      if (!Array.isArray(data)) return resp
21
22
      // eslint-disable-next-line one-var
23
      // const newData = [],
24
      //   map = {},
25
      //   queue = []
26
      // console.log(data)
27
      // data.forEach((item) => {
28
      //   if (item.parentCode === '-1') {
29
      //     const dep = { id: item.code, name: item.name, departments: [] }
30
      //     map[item.code] = newData.length
31
      //     newData.push(dep)
32
      //   } else {
33
      //     queue.push(item)
34
      //   }
35
      // })
36
      // queue.forEach((item) => {
37
      //   if (!(item.parentCode in map)) return
38
      //   const dep = { id: item.code, name: item.name }
39
      //   newData[map[item.parentCode]].departments.push(dep)
40
      // })
41
      // resp.data = newData
42
      // console.log(newData)
43
      // console.log('queryAllOrganize', resp)
17
      // 将平铺的数组变为树形数组
44 18
      resp.data = nest(data)
45
      console.log(resp.data)
46
      // resp.data = keysMappingReverse(nest(data), mapping)
47 19
      return resp
48 20
    })
49 21
  },

+ 6 - 36
security-protection-platform/src/modules/system/attendance/components/AttendancePanel/index.vue

@ -234,7 +234,7 @@ export default {
234 234
            label: item.name,
235 235
            pid: item.parentCode
236 236
          })
237
          // 如果没有中值 默认选中一个值 这里是一般是公司的考勤
237
          // 如果没有中值 默认选中一个值 这里是默认展示根节点的考勤规则
238 238
          if (this.treeValue.length === 0) this.handleDeptChange([item.id + ''])
239 239
          this.nextDepartment(item.departments, arr)
240 240
        })
@ -245,46 +245,16 @@ export default {
245 245
      this.loadDepts = true
246 246
      commonapi.getDepartments().then(resp => {
247 247
        const data = []
248
        // resp.data.forEach(item => {
249
        //   const orgId = item.id + ''
250
        //   data.push({
251
        //     id: orgId,
252
        //     label: item.name
253
        //     // disabled: true
254
        //   })
255
        //   item.departments.forEach(dept => {
256
        //     const deptId = dept.id + ''
257
        //     data.push({
258
        //       id: deptId,
259
        //       label: dept.name,
260
        //       pid: orgId
261
        //     })
262
        //     if (this.treeValue.length === 0) this.handleDeptChange([deptId])
263
        //     if (dept.departments.length > 0) {
264
        //       dept.departments.forEach(child => {
265
        //         const childId = child.id + ''
266
        //         data.push({
267
        //           id: childId,
268
        //           label: child.name,
269
        //           pid: deptId
270
        //         })
271
        //         this.handleDeptChange([childId])
272
        //       })
273
        //     }
274
        //   })
275
        // })
276
        // 递归
248
        // 递归 实现tree组件所需部门数据结构
277 249
        this.nextDepartment(resp.data, data)
278
        // 删除pid为"-1"的的pid属性,否则tree渲染的时候没有根节点渲染不出来
250
        // 深拷贝data
251
        const data1 = JSON.parse(JSON.stringify(data))
252
        // 用部门id映射关系代替code与parentCode父子映射关系
279 253
        data.forEach(item => {
254
          // 删除pid为"-1"的的pid属性,否则tree渲染的时候没有根节点渲染不出来
280 255
          if (item.pid === '-1') {
281 256
            delete item.pid
282 257
          }
283
        })
284
        // 深拷贝data
285
        const data1 = JSON.parse(JSON.stringify(data))
286
        // 用部门真的id映射关系代替code与parentCode父子映射关系
287
        data.forEach(item => {
288 258
          item.id = item.orgId
289 259
          data1.some((item1) => {
290 260
            if (item.pid === item1.id) {