田源
2025-01-07 daa3d9edea915514e28c568b7288e6415f02fef4
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<template>
  <el-container>
    <el-aside>
      <basic-container>
        <div ref="TreeBox" style="height: calc(100vh - 154px);!important;">
          <!-- 左侧树         -->
          <div style="height:  calc(100vh - 195px);">
            <avue-tree :data="treeData" :option="treeOption" @node-click="nodeClick">
              <span slot-scope="{ node, data }" class="el-tree-node__label">
               <span>
                  <i class="el-icon-user-solid"></i>
                    {{ (node || {}).label }}
                </span>
              </span>
            </avue-tree>
          </div>
        </div>
      </basic-container>
    </el-aside>
 
    <el-main>
      <basic-container v-loading="treeLoading">
        <h3 style="margin: 0 0 10px 0">功能权限配置</h3>
        <div>
          <el-button v-if="permissionList.rightBtn" class="button-custom-icon" plain size="small" type="primary" @click="saveHandler">
            <icon-show :name="permissionList.rightBtn.source"></icon-show>
            授权
          </el-button>
          <el-button v-if="permissionList.resetBtn" class="button-custom-icon" plain size="small" style="margin-right: 40px;" type="primary"
                     @click="clearValue">
            <icon-show :name="permissionList.resetBtn.source"></icon-show>
            重置
          </el-button>
        </div>
        <div style="height:  calc(100vh - 232px);margin-top: 10px;">
          <avue-tree ref="uiTree" :data="uiTreeData" :node-key="id" :option="uiTreeOption">
            <span slot-scope="{ node, data }" class="el-tree-node__label">
               <span>
                  <i :class="data.icon"></i>
                    {{ (node || {}).label }}
                </span>
            </span>
          </avue-tree>
        </div>
      </basic-container>
    </el-main>
 
  </el-container>
</template>
 
<script>
import {gridRoles} from "@/api/system/role/api";
import {getSysModelAuthTreeMenuByPID, getSysModelAuth, saveRoleRight} from "@/api/authority/functionView/api"
import {mapGetters} from "vuex";
 
export default {
  name: "index",
  data() {
    return {
      treeLoading: false,
      type: '',//业务类型
      context: '',//UI上下文code
      treeOption: {
        menu: false,
        addBtn: false,
        props: {
          label: 'name',
          value: 'oid',
          children: 'children'
        }
      },
      nodeRow: {},
      treeData: [],
      defaultExpandKeys: [],
      uiTreeOption: {
        defaultExpandedKeys: this.defaultExpandKeys,
        multiple: true,
        menu: false,
        addBtn: false,
        filter: false,
        props: {
          label: 'name',
          value: 'id',
          children: 'children'
        }
      },
      uiTreeData: [],
      typeData: [],
      contextData: []
    }
  },
  computed:{
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        rightBtn: this.vaildData(this.permission[this.$route.query.id].RIGHT, false),
        resetBtn: this.vaildData(this.permission[this.$route.query.id].RESET, false),
      };
    },
  },
  created() {
    this.getTreeList();
    this.getUITree();
  },
  methods: {
    getTreeList() {
      const loading = this.$loading({});
      gridRoles().then(res => {
        this.treeData = res.data.data;
        loading.close();
      }).catch(error => {
        loading.close();
      })
    },
    // 角色点击
    nodeClick(row) {
      this.nodeRow = row;
      this.treeLoading = true;
      getSysModelAuth({roleId: row.oid}).then(res => {
        const data = res.data.data;
        // console.log(data);
        this.$refs.uiTree.setCheckedKeys(data);
        this.treeLoading = false;
      })
    },
    getUITree() {
      this.treeLoading = true;
      const params = {
        // 'conditionMap[roleId]': this.nodeRow.oid,
        'conditionMap[type]': this.type,
        'conditionMap[context]': this.context,
        'conditionMap[showCheckbox]': true
      }
      this.defaultExpandKeys = ['root'];
      getSysModelAuthTreeMenuByPID(params).then(res => {
        console.log(res);
        let data= [{
          attributes: {},
          checked: false,
          expanded: true,
          data: "root",
          childType: 1,
          icon: 'el-icon-s-home',
          id: 'root',
          name:'功能模块',
          children: res.data.data
        }];
        this.processChildren(data[0]); // 处理每个节点
        this.uiTreeOption.defaultExpandedKeys = this.defaultExpandKeys;
        this.uiTreeData = data;
        this.treeLoading = false;
      }).catch(error => {
        this.treeLoading = false;
      })
    },
    //处理树
    processChildren(item) {
      if (item.children && item.children.length > 0) {
        item.children = item.children.map(child => {
          if (!child.childType) {
            // 默认树节点展开,最子级不展开
            this.defaultExpandKeys.push(child.id)
          }
          child.icon = 'el-icon-s-promotion';
          if (child.childType == 1) {
            child.icon = 'el-icon-document';
          } else if (child.childType == 2) {
            child.icon = 'el-icon-s-tools';
          }
          this.processChildren(child); // 递归处理每个子节点
          return child; // 只返回子节点的 attributes
        });
      }
    },
    saveHandler() {
      const selectTreeList = this.$refs.uiTree.getCheckedNodes();
      const selectParentList = this.$refs.uiTree.getHalfCheckedNodes();
 
      if (selectTreeList.length == 0) {
        this.$message.error("请选择功能模块");
        return;
      }
      const formData = selectTreeList.map(item => {
        return {
          parentId: item.childType === 2 ? item.funcId : item.parentId,
          id: item.id,
          type: (!item.childType || item.childType === 1) ? 1 : 2,
          number: item.childType === 2 ? item.sort : ''
        }
      })
      const parentData = selectParentList.map(item => {
        return {
          parentId: item.parentId,
          id: item.id,
          type: (!item.childType || item.childType === 1) ? 1 : 2,
          number: ''
        }
      })
      const data = [...parentData,...formData];
      data.shift();
      const params = {
        roleId: this.nodeRow.oid,
        roleData: data
      }
      saveRoleRight(params).then(res => {
        if (res.data.code === 200) {
          this.$message.success("授权成功");
        }
      });
    },
    clearValue() {
      this.$refs.uiTree.setCheckedNodes([]);
    }
  }
}
</script>
 
<style lang="scss" scoped>
::v-deep {
  .el-scrollbar__wrap {
    overflow: auto !important;
  }
 
  .headerCon {
    .el-button {
      width: 82px;
    }
  }
}
 
.headerCon {
  display: flex;
  flex-wrap: wrap;
  margin-bottom: 5px;
 
  .el-button + .el-button {
    margin-left: 5px;
  }
 
  .el-button {
    margin-top: 5px;
  }
}
 
.headerCon > .el-button:nth-child(4) {
  margin-left: 0;
}
 
.headerCon > .el-button:nth-child(7) {
  margin-left: 0;
}
 
 
.smallBtn {
  width: 82px;
  text-align: center;
  padding-left: 4.5px;
}
 
</style>