Blame view

HHECS.Web/src/views/systemPermission/components/bus_role.vue 10.8 KB
赖素文 authored
1
<template>
2
3
  <div class="sys-container">
    <div class="sys-pb-5">
赖素文 authored
4
5
      <el-button icon="el-icon-refresh-right" size="small" @click="btnRefresh">刷新</el-button>
      <el-button type="primary" size="small" icon="el-icon-plus" @click="editOrAddOpen(null, 'add')">
6
        新增
7
8
9
10
11
      </el-button>
    </div>

    <!-- table -->
    <div class="sys-table-border">
赖素文 authored
12
      <el-table :data="tableData" ref="multipleTable" highlight-current-row style="width: 100%" height="570">
13
14
        <el-table-column label="序号" width="60" type="index" />
        <el-table-column prop="id" label="id" v-if="false"></el-table-column>
15
16
        <el-table-column prop="roleCode" label="角色编码" v-if="false">
        </el-table-column>
17
18
19
        <el-table-column prop="roleName" label="角色名称"> </el-table-column>
        <el-table-column prop="remark" label="备注"> </el-table-column>
20
21
        <el-table-column prop="created" label="创建时间" width="180">
        </el-table-column>
22
23
24
        <el-table-column prop="createdBy" label="创建人"> </el-table-column>
        <el-table-column label="操作" width="120">
          <template slot-scope="scope">
赖素文 authored
25
26
            <el-button type="text" @click="editOrAddOpen(scope.row, 'edit')">编辑</el-button>
            <el-button style="color: red" type="text" @click="deleteAciton(scope.row)">删除</el-button>
27
          </template>
赖素文 authored
28
29
30
        </el-table-column>
      </el-table>
    </div>
31
32

    <!-- dialog 新增和编辑 -->
赖素文 authored
33
34
    <el-dialog :title="editOrAddOpt.title" v-if="editOrAddOpt.visible" :close-on-click-modal="false"
      :visible.sync="editOrAddOpt.visible" width="60%">
35
36
37
38
      <el-form :model="editOrAddOpt.form" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="名称">
赖素文 authored
39
40
              <el-input v-model="editOrAddOpt.form.roleCode" ref="roleCode" maxlength="20" show-word-limit
                autocomplete="off"></el-input>
41
42
43
44
45
46
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注">
赖素文 authored
47
              <el-input v-model="editOrAddOpt.form.remark" type="textarea" :rows="2" autocomplete="off">
48
49
50
51
52
53
54
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col>
            <el-form-item label="权限">
赖素文 authored
55
56
57
58
              <el-tree :data="treeOpt.data" show-checkbox node-key="id" :props="treeOpt.defaultProps"
                :default-expand-all="false" :check-strictly="true" :expand-on-click-node="false"
                :highlight-current="true" style="margin-top: 5px; height: 260px; overflow-y: auto"
                @node-click="roleNodeClick" ref="treePer" @check="handleNodeCheck">
59
60
61
62
                <!-- 自定义节点模板 -->
                <template #default="{ node, data }">
                  <span class="custom-tree-node">
                    <!-- 根据权限类型显示不同图标 -->
赖素文 authored
63
                    <i v-if="data.permissionType === 10" class="icon-module"></i>
64
65
                    <i v-if="data.permissionType === 1" class="icon-folder"></i>
                    <i v-if="data.permissionType === 5" class="icon-menu"></i>
66
                    <i v-if="data.permissionType === 15" class="icon-btn"></i>
67
68
69
70
                    <span>{{ node.label }}</span>
                  </span>
                </template>
              </el-tree>
71
72
73
74
75
76
77
78
79
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="userAddOrEditSave">确 定</el-button>
        <el-button @click="editOrAddOpt.visible = false">取 消</el-button>
      </span>
    </el-dialog>
赖素文 authored
80
81
82
  </div>
</template>
<script>
83
import * as userApi from "@/api/systemPermission/userPermission";
赖素文 authored
84
85
export default {
  data() {
86
87
88
89
90
91
92
93
94
95
96
97
98
    const editOrAddData = {
      roleCode: "",
      roleName: "",
      remark: null,
      Disable: false,
      //角色拥有的权限数据
      PermissionsIds: [],

      Permissions: null,
      Created: null,
      CreatedBy: null,
      Updated: null,
      UpdatedBy: null,
99
      Id: 0,
100
    };
赖素文 authored
101
102
    return {
      tableData: [],
103
104
105
106
107
108
109

      editOrAddOpt: {
        visible: false,
        flag: "add",
        title: "新增",
        form: { ...editOrAddData },
        formDefaultData: JSON.parse(JSON.stringify(editOrAddData)),
赖素文 authored
110
      },
111
112
      //临时存储的数据
      treeOpt: {
113
        data: [], // 处理后的数据(展示用)
114
115
        originData: [], // 原始数据
        defaultProps: {
116
117
118
119
          children: "children",
          label: "label",
        },
      },
赖素文 authored
120
121
122
    };
  },
  methods: {
123
124
125
126
    /**
     * 查询或者搜索
     */
    btnRefresh() {
127
      this.getAllPermissionList();
128
      this.getData();
赖素文 authored
129
    },
130
131

    getData() {
132
      userApi.getUserRole().then((res) => {
赖素文 authored
133
134
        userApi.ajaxSuccessBefore(res, (res) => {
          this.tableData = res.data;
135
        });
赖素文 authored
136
137
      });
    },
138
139
140
141
142

    /**
     * 获取权限数据
     */
    getAllPermissionList() {
143
      userApi.getAllPermission().then((res) => {
赖素文 authored
144
145
146
        userApi.ajaxSuccessBefore(res, (res) => {
          this.treeOpt.data = userApi.transformToTreeData(res.data);
          this.treeOpt.originData = res.data;
赖素文 authored
147
          //console.log(res.data, "all");
148
149
        });
      });
赖素文 authored
150
    },
151
152
153
154
155

    /**
     * 新增或者编辑弹出框
     */
    editOrAddOpen(row, flag) {
156
      debugger;
157
      if (this.treeOpt.data.length == 0) {
158
159
160
        this.$message.error(
          "权限数据加载中...请稍后在操作,反复出现请联系管理员!"
        );
161
162
163
164
165
        return;
      }
      this.editOrAddOpt.flag = flag;
      this.editOrAddOpt.visible = true;
      if (flag == "add") {
166
167
168
169
        this.editOrAddOpt.form = JSON.parse(
          JSON.stringify(this.editOrAddOpt.formDefaultData)
        );
        this.editOrAddOpt.title = "新增";
赖素文 authored
170
      } else {
171
        this.editOrAddOpt.title = `编辑,用户名【${row.roleName}】`;
172
        this.editOrAddOpt.form = row;
173
174
175
        userApi.getPermissionByRoleId(row.id).then((res) => {
          if (res.code !== "Success") return;
          const keys = res.data.map((x) => x.id);
176
177
178
          this.$nextTick(() => {
            this.$refs.treePer.setCheckedKeys(keys);
          });
179
180
          console.log(keys, "keys");
181
182
        });
      }
183
      this.$nextTick(() => {
184
185
186
187
        debugger;
        const firstNode = this.$refs.treePer.root.childNodes[0];
        if (firstNode) firstNode.expand();
      });
188
189
190
191
192
193
    },

    /**
     * 新增或者编辑 保存操作
     */
    userAddOrEditSave() {
194
195
196
197
198
      debugger;
      if (
        this.editOrAddOpt.form.roleCode == "" ||
        this.editOrAddOpt.form.roleCode == null
      ) {
199
200
201
        this.$message.error("权限名称不能为!");
        this.$refs.roleCode.focus();
        return;
赖素文 authored
202
      }
203
      this.editOrAddOpt.form.roleName = this.editOrAddOpt.form.roleCode;
204
205
206
207
208
      var selectData = this.getSelectRoleVal();
      if (selectData.length == 0) return;
      this.editOrAddOpt.form.PermissionsIds = selectData;

      if (this.editOrAddOpt.flag == "edit") {
209
210
211
212
213
214
215
216
217
        userApi.editRoleAndPermissions(this.editOrAddOpt.form).then((res) => {
          userApi.ajaxSuccessBefore(
            res,
            (res) => {
              this.editOrAddOpt.visible = false;
              this.getData();
            },
            this
          );
218
        });
219
        return;
220
      }
221
222
223
224
225
226
227
228
229
      userApi.addRoleAndPermissions(this.editOrAddOpt.form).then((res) => {
        userApi.ajaxSuccessBefore(
          res,
          (res) => {
            this.editOrAddOpt.visible = false;
            this.getData();
          },
          this
        );
230
      });
赖素文 authored
231
    },
232
233
234
235

    //删除
    deleteAciton(row) {
      var title = `此操作将永久删除该数据,【${row.roleName}】,是否继续?`;
236
      this.$confirm(title, "提示", { type: "warning" })
237
        .then(() => {
238
239
240
241
242
243
244
245
246
          userApi.deleteRoleByIds([row.id]).then((res) => {
            userApi.ajaxSuccessBefore(
              res,
              (res) => {
                this.editOrAddOpt.visible = false;
                this.getData();
              },
              this
            );
247
          });
248
249
        })
        .catch();
赖素文 authored
250
    },
251
252
253

    setCurrent(row) {
      this.$refs.multipleTable.setCurrentRow(row);
赖素文 authored
254
    },
255
256
257
258
259
260
261
262

    //获取选中的权限
    getSelectRoleVal() {
      var result = this.$refs.treePer.getCheckedKeys();
      if (result.length == 0) {
        this.$message.error("请选择权限数据!");
        return [];
      }
263
      return result;
赖素文 authored
264
    },
265
    roleNodeClick(data, node, event) {
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
      console.log("selectRowData", data);
    },
    // 处理节点勾选事件
    handleNodeCheck(data, { checkedNodes }) {
      // 新增:获取当前节点自身的选中状态
      const isChecked = checkedNodes.some((node) => node.id === data.id);

      if (isChecked) {
        // 勾选时同步子节点
        if (data.children) {
          this.toggleChildren(data.children, true);
        }
      } else {
        // 取消勾选时同步子节点(关键修复)
        if (data.children) {
          const childKeys = this.getAllChildKeys(data);
          const currentChecked = this.$refs.treePer.getCheckedKeys();
          const newChecked = currentChecked.filter(
            (k) => !childKeys.includes(k)
          );
          this.$refs.treePer.setCheckedKeys(newChecked);
        }
      }
    }, // 新增方法:获取所有子节点键值
    getAllChildKeys(node) {
      return (
        node.children?.reduce((acc, child) => {
          acc.push(child.id);
          if (child.children) acc.push(...this.getAllChildKeys(child));
          return acc;
        }, []) || []
      );
    },

    // 修改后的子节点同步方法
    toggleChildren(children, checked) {
      children.forEach((child) => {
        this.$refs.treePer.setChecked(child.id, checked);
        if (child.children) this.toggleChildren(child.children, checked);
      });
306
    },
赖素文 authored
307
  },
308
309
310
311
  mounted() {
    this.getAllPermissionList();
    this.getData();
  },
赖素文 authored
312
313
314
};
</script>
<style scoped lang="scss">
315
.sys-container {
赖素文 authored
316
  width: 100%;
317
318
319
320
  .sys-pb-5 {
    padding-bottom: 5px !important;
  }
321
322
323
324
325
326
327
328
  .sys-form-item {
    display: flex;
    border-radius: 4px;
    min-height: 46px;
    margin-bottom: 10px;
    align-items: center;
  }
329
330
331
332
  .sys-table-border {
    border: 1px solid #e5e9f2;
  }
333
334
335
336
  .sys-pagination-container {
    text-align: center;
  }
赖素文 authored
337
}
338
339
340

.current-row {
  background-color: #ecf5ff;
赖素文 authored
341
}
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362

/* 图标样式 */
.custom-tree-node i[class^="icon-"] {
  display: inline-block;
  width: 16px;
  height: 16px;
  margin-right: 5px;
  vertical-align: middle;
}

.icon-module {
  background: url(~@/assets/images/module.png) no-repeat center/contain;
}

.icon-folder {
  background: url(~@/assets/images/folder.png) no-repeat center/contain;
}

.icon-menu {
  background: url(~@/assets/images/menu.png) no-repeat center/contain;
}
363
364
365
366

.icon-btn {
  background: url(~@/assets/images/btn.png) no-repeat center/contain;
}
赖素文 authored
367
</style>