ke_junjie
2025-06-04 bb6e2230bb8ded3c5546bc4e4c282ee343754475
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<template>
  <div id="role-auth" class="global-content">
    <div class="width height overflow flex align-start justify-around">
      <div class="width20 height99 left-aside overflow">
        <div class="left-role-title" @click="refresh">请选择角色</div>
        <div class="width height-calc40 overflowy-auto">
          <template v-for="(item, index) in roleList">
            <div
              @click="activeclick(item, index)"
              class="pointer paddingtopbottom textindex2 hover"
              :class="activeindex == index ? 'color409EFF backgroundecf5ff' : ''"
              :key="index + 'role'"
            >
              {{ item.roleName }}
            </div>
          </template>
        </div>
      </div>
      <div class="width78 height overflow">
        <el-tabs v-model="currentTab" type="card" class="width height" @tab-click="handleClick">
          <el-tab-pane class="width height" label="角色权限设置" name="user">
            <div ref="el-main" class="height" style="padding: 0">
              <el-card class="box-card height99 overflow">
                <div slot="header" class="clearfix">
                  <span>角色权限设置 - {{ title }}</span>
                </div>
                <tree-table
                  ref="tree-table"
                  :data="menuList"
                  :expand-all="expandAll"
                  height="100%"
                  border
                  class="heightclacimport overflow"
                  tree-column-label="菜单名称"
                  @row-selected="rowSelected"
                >
                  <el-table-column label="权限点">
                    <template slot-scope="scope">
                      <el-checkbox
                        v-for="(item, index) in scope.row.auth"
                        :key="index"
                        v-model="item.value"
                        :true-label="1"
                        :false-label="0"
                        :checked="!!item.value"
                        @change="checkAuthNode(scope.row)"
                        >{{ item.label }}</el-checkbox
                      >
                    </template>
                  </el-table-column>
                </tree-table>
                <div class="tool absolute bottom7">
                  <el-button :loading="loading" type="primary" size="mini" @click="saveAuth">保存</el-button>
                </div>
              </el-card>
            </div>
          </el-tab-pane>
          <el-tab-pane class="width height" label="APP权限设置" name="app">
            <el-main ref="el-main" class="height" style="padding: 0">
              <el-card class="box-card height99 overflow">
                <div slot="header" class="clearfix">
                  <span>APP权限设置 - {{ title }}</span>
                </div>
                <tree-table
                  ref="tree-table"
                  :data="menuList"
                  :expand-all="expandAll"
                  height="100%"
                  class="heightclacimport overflow"
                  border
                  tree-column-label="菜单名称"
                  @row-selected="rowSelected"
                >
                  <el-table-column label="权限点">
                    <template slot-scope="scope">
                      <el-checkbox
                        v-for="(item, index) in scope.row.auth"
                        :key="index"
                        v-model="item.value"
                        :true-label="1"
                        :false-label="0"
                        :checked="!!item.value"
                        @change="checkAuthNode(scope.row)"
                        >{{ item.label }}</el-checkbox
                      >
                    </template>
                  </el-table-column>
                </tree-table>
                <div class="tool absolute bottom7">
                  <el-button :loading="loading" type="primary" size="mini" @click="saveAuth">保存</el-button>
                </div>
              </el-card>
            </el-main>
          </el-tab-pane>
        </el-tabs>
      </div>
    </div>
  </div>
</template>
 
<script>
import treeTable from '@/components/TreeTable';
import { getCache, setCache } from '@/utils/sessionStorage';
import { SearchPermission, AddOrUpdateRoleVsPermission, GetUserRoleAllPermission } from '@/api/role-auth';
import { RoleSearch } from '@/api/role';
export default {
  name: 'sys-permission-role-auth',
  components: { treeTable },
  data() {
    return {
      title: '[请选择左侧角色名称]',
      // 加载状态
      loading: false,
      expandAll: true,
      activeindex: -1,
      // 选中角色
      currentRoleInfo: {},
      // 左侧导航集合
      roleList: [],
      // 右侧表格树权限列表集合
      menuList: [],
      // 需要保存的角色权限集合
      saveMenuList: [],
      currentTab: 'user',
      // 右侧表格树APP权限列表集合
      menuAppList: [],
      // app选中角色
      currentAppRoleInfo: {},
      role_Id: null,
      types: 1,
      tablabel: ''
    };
  },
  mounted() {
    this.RoleSearch();
    // this.GetUserRoleAllPermission();
  },
  methods: {
    //刷新
    refresh() {
      this.activeindex = -1;
      this.GetUserRoleAllPermission();
    },
    // 左侧加载数据
    RoleSearch() {
      RoleSearch('1&onePageNum=999').then(res => {
        if (res.code == 0) {
          this.roleList = res.data;
        }
      });
    },
    // 左侧菜单点击事件
    activeclick(item, index) {
      this.activeindex = index;
      // var item = node.$attrs.item;
      // this.currentRoleInfo = item;
      this.$Loading(true);
      this.title = item.roleName;
 
      this.GetUserRoleAllPermission(item.roleName);
    },
    // 获得所有权限右侧表格树列表
    GetUserRoleAllPermission(e) {
      this.$Loading(true);
      this.menuList = [];
      GetUserRoleAllPermission({ roleName: this.title, type: this.types }).then(res => {
        if (res.code == 0) {
          if (e) {
            let data = res.data;
            let curcir = element => {
              element.forEach(item => {
                // item.isSelected = true;
                if (item.children) {
                  curcir(item.children);
                }
                // if (item.value == 0) {
                //   item.isSelected = false;
                // }
              });
            };
            let Selected = element => {
              element.forEach(item => {
                if (item.auth) {
                  let auth = item.auth;
                  auth.forEach(i => {
                    if (i.value == 1) {
                      item.isSelected = true;
                    }
                  });
                }
                if (item.children) {
                  Selected(item.children);
                }
              });
            };
            Selected(data);
            curcir(data);
            data.forEach(element => {
              if (element.children) {
                let item = element.children;
                item.forEach(i => {
                  if (i.isSelected) {
                    element.isSelected = true;
                  }
                });
              }
              if (element.menuName == '首页') {
                element.isSelected = true;
              }
            });
            console.log(data);
            this.menuList = data;
          } else {
            this.menuList = res.data;
          }
        }
        this.$Loading();
      });
    },
    // 权限点改变
    checkAuthNode(row) {
      row.auth.forEach(item => {
        if (item.value == 1) {
          row.isSelected = true;
          if (row.parent) {
            row.parent.isSelected = true;
          }
        }
      });
    },
    // 树前面复选框选中
    rowSelected(isSelected, row) {
      let circulation = element => {
        element.forEach(item => {
          if (row.parentId == item.menu_Id) {
            item.isSelected = row.isSelected;
          } else {
            if (item.children) {
              circulation(item.children);
            }
          }
        });
      };
      let setValue = authList => {
        if (authList) {
          authList.forEach(item => {
            item.value = isSelected ? 1 : 0;
          });
        }
      };
      let eachChildren = children => {
        if (children) {
          children.forEach(item => {
            item.isSelected = isSelected;
            setValue(item.auth);
            eachChildren(item.children);
            // 改变保存数据集合
            this.checkAuthNode(item);
          });
        }
      };
      setValue(row.auth);
      eachChildren(row.children);
    },
    // 保存数据
    saveAuth() {
      let list = [];
      //获取勾选的
      let savlist = element => {
        element.forEach(item => {
          if (item.isSelected) {
            list.push(item.menu_Id);
          }
          if (item.children) {
            savlist(item.children);
          }
          if (item.value == 1) {
            list.push(item.menu_Id);
          }
          if (item.auth) {
            savlist(item.auth);
          }
        });
      };
      savlist(this.menuList);
      if (list.length == 0) {
        this.$message.error('没有可保存的数据!');
        return;
      }
      const { roleName } = getCache('userInfo');
      if (this.title == '[请选择左侧角色名称]') {
        this.$message.error('请选择角色');
      } else {
        this.$Loading(true);
        AddOrUpdateRoleVsPermission({ roleName: this.title, permissionNameList: JSON.stringify(list) }).then(res => {
          if (res.code == 0) {
            this.$message({
              type: 'success',
              message: '修改成功'
            });
          } else {
            this.$message({
              type: 'warning',
              message: res.msg
            });
          }
          this.$Loading();
        });
      }
    },
    // 头部切换
    handleClick(tab, event) {
      this.activeindex = -1;
      this.title = '';
      this.tablabel = tab.label;
      if (tab.label === 'APP权限设置') {
        this.types = 2;
        this.GetUserRoleAllPermission();
      }
      if (tab.label === '角色权限设置') {
        this.types = 1;
        this.GetUserRoleAllPermission();
      }
    }
  }
};
</script>
 
<style lang="scss" scoped>
.left-aside {
  box-shadow: 0 0 4px #eaedef;
  .left-role-title {
    padding: 8px 10px;
    background-color: #66b1ff;
    color: white;
  }
}
.box-card {
  ::v-deep .el-card__body {
    padding: 10px;
    height: calc(100% - 75px);
    .el-checkbox:first-child {
      margin-left: 0px;
    }
    .el-checkbox + .el-checkbox {
      margin-left: 0px;
    }
    .el-checkbox__label {
      padding-left: 2px;
    }
  }
}
</style>
<style lang="scss">
#role-auth {
  .el-tabs__content {
    height: calc(100% - 60px) !important;
  }
}
</style>