分支自 SuZhouGuanHong/TaiYuanTaiZhong

PCS
dengjunjie
2023-12-13 113d1d4262d8f9e78a9d92123713c41669ad6c87
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
<template>
  <div class="com-el-tree">
    <div class="m-title"><i class="el-icon-warning-outline"></i>角色列表</div>
    <el-scrollbar style="height: 100%; width: 210px" class="el-role-tree">
      <el-tree
        :data="tree"
        @node-click="nodeClick"
        node-key="id"
        :default-expanded-keys="openKeys"
        :expand-on-click-node="false"
        style="padding: 5px 0"
      >
        <template #default="{ node, data }">
          <div class="action-group">
            <div
              class="action-text"
              :class="{
                actived: data.id == selectId,
                'node-text': data.parentId !== 0,
              }"
              :style="{ width: (4 - data.lv) * 18 + 150 + 'px' }"
            >
              {{ data.roleName }}
            </div>
          </div>
        </template>
      </el-tree>
    </el-scrollbar>
  </div>
</template>
 
<script>
export default {
  props: {
    onChange: {
      type: Function,
      default: (treeId) => {},
    },
  },
  data() {
    return {
      selectId: -1,
      checked: false,
      openKeys: [],
      data: [],
      tree: [],
    };
  },
  created() {
    this.load();
  },
  methods: {
    load() {
      this.http.post("/api/role/getUserChildRoles", {}, true).then((result) => {
        if (!result.status) return this.$message.error(result.message);
        this.data.splice(0);
        this.data = result.data;
        this.data.forEach((x) => {
          if (x.parentId == 0) {
            x.lv = 1;
            x.children = [];
            this.tree.push(x);
            this.getTree(x.id, x);
          }
        });
        this.openKeys.push(this.tree[0].id);
        this.selectId = this.openKeys[0];
        //默认加载第一个树形菜单下面的数据
        this.$store.getters.data().treeDemo1.treeId = this.selectId;
        this.onChange(this.selectId);
      });
    },
    getTree(id, data) {
      this.data.forEach((x) => {
        if (x.parentId == id) {
          x.lv = data.lv + 1;
          if (!data.children) data.children = [];
          data.children.push(x);
          this.getTree(x.id, x);
        }
      });
    },
    nodeClick(node, selected) {
      console.log(node);
      this.selectId = node.id;
      //缓存当前选中的节点
      this.$store.getters.data().treeDemo1.treeId = node.id;
      this.onChange(node.id);
    },
  },
};
</script>
<style lang="less" scoped>
.com-el-tree {
  display: inline-block;
  width: 210px;
  .el-role-tree {
    position: absolute;
    padding: 5px 0px;
    top: 45px;
    bottom: 0;
    border-right: 1px solid #eee;
  }
  .actived {
    color: #5884ff;
    font-size: 15px;
  }
  .action-text {
    font-size: 14px;
  }
}
.role-list {
  color: white;
  line-height: 44px;
  background: #1a89ff;
  padding: 0 13px;
  font-size: 16px;
  position: absolute;
  top: 2px;
  width: 210px;
}
.m-title {
  line-height: 40px;
  font-size: 15px;
  background: #66b1ff0f;
  font-weight: bold;
  padding: 6px 16px;
    border: 1px solid #ececec;
  i {
    padding-right: 5px;
  }
}
.com-el-tree ::v-deep(.el-tree-node) {
  padding: 3px 0;
}
.com-el-tree ::v-deep(.el-scrollbar .el-scrollbar__thumb) {
  width: 0 !important;
}
</style>