田源
2024-08-22 b3042224aa7a288fc996cd2542e777362abbc9f6
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
<template>
  <el-dialog
    v-dialogDrag
    :close-on-click-modal="false"
    :visible.sync="isShowDialog"
    append-to-body
    class="avue-dialog avue-dialog--top"
    title="分类授权"
    top="-45px" @close="closeDialog">
    <el-table
      ref="dataTable"
      v-loading="isLoading"
      :data="classifyAuthData"
      :header-cell-style="{ background: '#FAFAFA', color: '#505050' }"
      :height="tableHeight"
      border
      class="cus-table"
      @select="handleSelection"
      @row-click="handleRowClick"
      @select-all="handleSelectionAll"
      @selection-change="handleSelectionChange"
      @sort-change="sortChange"
    >
      <el-table-column
        type="selection"
        width="55"
      ></el-table-column>
      <el-table-column
        v-for="(item, index) in classifyAuthHeader"
        v-if="classifyAuthHeader.length !== 0"
        :key="index"
        :label="item.label"
        :prop="item.prop"
        :show-overflow-tooltip="true"
        :width="item.width"
        align="center"
      >
        <template slot-scope="{ row }">
          <el-select
            v-if="item.type === 'select'"
            slot="prepend"
            v-model="row[item.prop]"
            @change="roleChange(row.roleData,row)"
          >
            <el-option
              v-for="optionItem in roleList"
              :key="optionItem.id"
              :label="optionItem.roleName"
              :value="optionItem.id"
            >
            </el-option>
          </el-select>
          <el-input
            v-if="item.type === 'text'"
            v-model="row[item.prop]"
            readonly
            type="text"
          >
          </el-input>
          <el-checkbox
            v-if="item.type === 'checkbox'"
            v-model="row[item.prop]"
            :disabled="row.authButton.allDisabled ? true : row.authButton[item.code]"
            @change="checkBoxToChangeTheEvent(row[item.prop],item.code,row,item.prop)">
          </el-checkbox>
        </template>
      </el-table-column>
    </el-table>
    <div slot="footer" class="dialog-footer">
      <el-button plain type="info" @click="selectAllButton">按钮全选</el-button>
      <el-button class="el-icon-plus" plain type="success" @click="addClassifyAuth"></el-button>
      <el-button class="el-icon-minus" plain type="warning" @click="subClassifyAuth"></el-button>
      <el-button plain type="primary" @click="submit">提 交</el-button>
      <el-button plain type="danger" @click="isShowDialog = false">关 闭</el-button>
    </div>
  </el-dialog>
</template>
 
<script>
import {getButtonByParentCode} from "@/api/system/menu"
import {getPage} from "@/api/system/role"
import {saveOrUpdate, getClassifyAuthList, getButtonsByRoleId} from "@/api/system/classifyAuth"
import {v4 as uuidv4} from 'uuid';
 
export default {
  name: "dataAuthDialog.vue",
  props: {
    // 当前点击节点的顶层节点
    classifyData: {
      type: "Object",
      default: "",
    },
    /**对话框显示隐藏控制*/
    visible: {
      type: "Boolean",
      default: false,
    },
    // 当前点击的节点
    TreeNode: {
      type: Object
    }
  },
  data() {
    return {
      // 对话框显示控制
      isShowDialog: this.visible,
      isLoading: true,
      tableHeight: 'calc(100vh - 550px)',
      classifyAuthData: [],
      //列头
      classifyAuthHeader: [],
      //按钮数据
      classifyAuthButton: [],
      //角色列表
      roleList: [],
      //当前选中的表格行
      selectList: [],
      itemKey: '',
      addIndex: Number,
      currentRow: {}
    };
  },
  watch: {
    // 监听父组件传的窗口显示隐藏的值
    visible() {
      if (this.visible) {
        this.isShowDialog = this.visible;
        this.loadAuthPage();
      }
    },
    classifyAuthData(newval) {
      this.addIndex = newval.length <= 0 ? 0 : newval.length - 1;
    }
  },
  computed: {},
  mounted() {
  },
  created() {
  },
  methods: {
    checkBoxToChangeTheEvent(check, code, row, id) {
      if (code === "classify_view") {
        row.checkTheStatus = check;
      } else if (!row.checkTheStatus) {
        this.$message.warning('清先勾选查看按钮!');
        // 在条件不满足时将多选框状态设置为false
        row[id] = false;
      }
 
      if (!row.checkTheStatus) {
        this.classifyAuthButton.forEach(key => {
          // 查看为false时将所有多选框状态设置为false
          row[key.id] = false;
        });
      }
    },
    // 关闭对话框
    closeDialog() {
      this.$emit('update:visible', false);
      this.classifyAuthData = [];
    },
    roleHandlerMethods(id, type, index) {
      if (this.classifyAuthData.length === 0) {
        return;
      }
      //filteredItems 未授权数组
      getButtonsByRoleId({roleId: id, code: 'classifyTree '}).then(res => {
        const filteredItems = this.classifyAuthButton.filter(item =>
          !res.data.data.some(x => x.id === item.id)
        );
 
        //和表格列进行对比
        this.classifyAuthHeader.forEach((item) => {
          // if (item.code !== 'classify_view') {
          //   this.$set(this.classifyAuthData[index].authButton, "allDisabled", true);
          // }
          const isMatched = filteredItems.some((x) => x.id === item.prop);
          this.$nextTick(() => {
            if (this.classifyAuthData[index]) {
              if (isMatched) {
                Object.keys(this.classifyAuthData[index]).forEach((key) => {
                  this.$set(this.classifyAuthData[index].authButton, item.code, true);
                  //添加 “查看全部禁用条件”
                  if (item.code === "classify_view") {
                    this.$set(this.classifyAuthData[index].authButton, "allDisabled", true);
                  }
                });
 
              } else {
                Object.keys(this.classifyAuthData[index]).forEach((key) => {
                  this.$set(this.classifyAuthData[index].authButton, item.code, false);
                });
                if (item.code === "classify_view") {
                  this.classifyAuthData[index].authButton.allDisabled = false;
                }
              }
            }
          });
        });
        // console.log(this.classifyAuthData)
        //强制刷新表格
        this.itemKey = uuidv4();
      });
    },
    // 角色改变时
    async roleChange(row, currentRow) {
      try {
        this.currentRow = currentRow;
        const res = await getButtonsByRoleId({roleId: row, code: 'classifyTree '});
 
        const filteredItems = this.classifyAuthButton.filter(item => {
          return !res.data.data.find(x => x.id === item.id);
        });
 
        this.classifyAuthHeader.forEach(item => {
          const isMatched = filteredItems.some(x => x.id === item.prop);
          this.$nextTick(() => {
            if (currentRow) {
              if (isMatched) {
                Object.keys(currentRow).forEach((key) => {
                  currentRow.authButton[item.code] = true;
                  if (!currentRow.authButton.classify_view) {
                    currentRow.authButton.allDisabled = false;
                  }
                });
              } else {
                Object.keys(currentRow).forEach((key) => {
                  currentRow.authButton[item.code] = false;
                  if (!currentRow.authButton.classify_view) {
                    currentRow.authButton.allDisabled = false;
                  }
                });
              }
            }
            this.itemKey = uuidv4(); // 强制刷新表格
          });
        });
      } catch (error) {
        console.error(error);
      }
    },
    // 增加行
    addClassifyAuth() {
      if (this.roleList.length <= 0) {
        this.$message.warning("当前租户不存在角色信息!");
        return;
      }
 
      let item = {
        roleData: this.roleList[0].id,
        classifyItem: this.classifyData.label,
        authButton: {},
        uuid: uuidv4(),//生成唯一的id,
        index: this.classifyAuthData.length -1,
      }
      //将按钮设置进去
      this.classifyAuthButton.forEach(data => {
        Vue.set(item, data.id, false);
      })
      //console.log(item)
      this.classifyAuthData.push(item)
      this.roleHandlerMethods(this.roleList[0].id, 'add', this.classifyAuthData.length -1)
    },
    // 删除行
    subClassifyAuth() {
      for (let item of this.selectList) {
        let index = this.classifyAuthData.findIndex(data => data.index === item.index);
        if (index !== -1) {
          this.classifyAuthData.splice(index, 1);
        }
      }
    },
    handleRowClick(row, column) {
      this.selectList.push(row);
      this.$refs.dataTable.toggleRowSelection(row);
    },
    // 多选
    handleSelectionChange(list) {
      this.selectList = list;
      //console.log("多选");
    },
    // 选择全部
    handleSelectionAll(list) {
      this.selectList = list;
    },
    // 保存分类授权信息
    async submit() {
      let form = {
        classifyAuthList: [],
        isCLear: false, //默认是清空
        classifyId: this.TreeNode.oid,
        authType: 'classify_auth',
      };
      console.log('classifyAuthData',this.classifyAuthData);
      let isRepeat = false;
      if (this.classifyAuthData.length > 0) {
        // 遍历数组,比较后面的对象的roleData是否与前面的对象相等
        for (let i = 0; i < this.classifyAuthData.length - 1; i++) {
          let currentRoleId = this.classifyAuthData[i].roleData;
          for (let j = i + 1; j < this.classifyAuthData.length; j++) {
            if (currentRoleId === this.classifyAuthData[j].roleData) {
              isRepeat = true;
              break;
            }
          }
          if (isRepeat) {
            break;
          }
        }
        if (isRepeat) {
          this.$message.warning("角色和分类已经存在,请重新配置!");
          return;
        }
        let flag = false;
        this.classifyAuthData.forEach(item => {
          let itemButtonList = [];
          //筛选出按钮勾选为true的列
          for (let key in item) {
            if (item[key] === true && key !== 'checkTheStatus') {
              itemButtonList.push(key);
            }
          }
          /**如果itemButtonList为空证明是,
           添加了角色但是没勾选任何的按钮*/
          if (itemButtonList.length <= 0) {
            flag = true;
            return;
          }
          let data = {
            oid: item.oid,
            roleId: item.roleData,
            classifyId: this.TreeNode.oid,
            buttonIds: itemButtonList.join(","),
            authType: "classify_auth",
          }
          form.classifyAuthList.push(data);
        });
        if (flag) {
          this.$message.warning('有未勾选操作的权限,不允许授权')
          return;
        }
        // console.log(form)
      } else {
        // 为空证明是清空授权列表
        form.isCLear = true;
      }
      // 调用保存分类授权的接口
      await saveOrUpdate(form).then(res => {
        this.$message({
          type: "success",
          message: res.data.msg,
        });
        // this.isShowDialog = false
      }, (error) => {
        window.console.log(error);
      })
      this.classifyAuthData = [];
      this.loadAuthPage();
    },
    // 全选按钮
    selectAllButton() {
      if (this.selectList.length !== 1) {
        this.$message.warning("请只选择一行需要全选的按钮的数据行!");
        return;
      }
      this.classifyAuthButton.forEach(item => {
        //console.log("item",item);
        Vue.set(this.selectList[0], item.id, true);
      });
      //console.log("this.selectList",this.selectList);
    },
    loadAuthPage() {
      return new Promise((resolve, reject) => {
        this.isLoading = true;
        getButtonByParentCode({code: 'classifyTree '}).then(res => {
          // 记录按钮数据
          this.classifyAuthButton = res.data.data;
          const list = res.data.data;
          let tempData = [];
          // 角色列
          tempData.push({
            label: "角色",
            prop: "roleData",
            type: "select",
            width: 150,
          });
          // 编码项分类授权
          tempData.push({
            label: "编码项数据授权",
            prop: "classifyItem",
            type: "text",
            width: 180,
          });
          list.forEach(item => {
            let columnItem = {
              label: item.name,
              prop: item.id,
              code: item.code,
              type: "checkbox",
              width: 180,
            };
            tempData.push(columnItem);
          })
          this.classifyAuthHeader = tempData
          resolve();
        }).catch(err => {
          reject(err)
        });
        // 获取角色列表
        getPage(1, -1, null).then(res => {
          this.roleList = res.data.data.records;
        });
        // 获取该分类下已授权的分类授权信息
        getClassifyAuthList({classifyId: this.TreeNode.oid, authType: "classify_auth"}).then(res => {
          //console.log(res.data.data);
          let authDatas = res.data.data;
          if (authDatas.length > 0) {
            authDatas.forEach((authData, index) => {
              let item = {
                oid: authData.oid,
                roleData: authData.roleId,
                classifyItem: this.classifyData.label,
                index: index,
                authButton: {},
                uuid: uuidv4(),//生成唯一的id
              }
              //将按钮设置进去
              authData.buttonIdList.forEach(data => {
                Vue.set(item, data, true);
              });
              this.classifyAuthData.push(item);
              this.roleHandlerMethods(authData.roleId, 'create', index)
            })
          }
        });
        this.isLoading = false;
      });
    },
  },
};
</script>
 
<style lang="scss" scoped>
 
// 滚动条样式修改
// 滚动条的宽度
/deep/ .el-table__body-wrapper::-webkit-scrollbar {
  height: 15px; // 纵向滚动条 必写
  background: white;
  border: white;
  width: 10px;
 
}
 
// 滚动条的滑块
/deep/ .el-table__body-wrapper::-webkit-scrollbar-thumb {
  background-color: #ececec;
  border-radius: 20px;
  border: #ececec;
}
 
/deep/ .el-table__body-wrapper {
  height: calc(100% - 50px) !important;
}
 
</style>