hejianhao
2025-04-16 dab2d210ca06c1faa514c6388fbd5de1ab355360
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
<template>
  <div class="role_add">
    <v-header title="编辑权限"></v-header>
    <section class="sec">
      <p class="label">角色名称:</p>
      <article>
        <el-input
          maxlength="20"
          show-word-limit
          size="small"
          v-model="name"
        ></el-input>
      </article>
    </section>
    <section class="sec">
      <p class="label">角色权限:</p>
      <article><b>请勾选改角色用户的权限</b></article>
    </section>
    <h5>社区平台权限配置</h5>
    <el-checkbox v-model="all" class="pad">全选</el-checkbox>
    <div class="tree">
      <el-tree
        :data="item"
        ref="treeDom"
        :props="defaultProps"
        empty-text="无"
        check-on-click-node
        node-key="menuId"
        default-expand-all
        show-checkbox
        :expand-on-click-node="false"
        @node-click="handleNodeClick"
        @check="onCheck"
        :default-expanded-keys="pid"
        :default-checked-keys="id"
      >
      </el-tree>
    </div>
    <div class="btn">
      <el-button type="primary" size="small" @click="sub">确认</el-button>
      <el-button size="small" @click="close">取消</el-button>
    </div>
  </div>
</template>
 
<script>
export default {
  props: {},
  components: {},
  data() {
    return {
      item: [],
      defaultProps: {
        children: "sysMenuVOList",
        label: "menuName",
      },
      id: [],
      pid: [],
      now: {},
      name: "",
      isT: 0,
      all: false,
      rand: 0,
      eid: "",
    };
  },
  watch: {
    rand(n) {
      if (n && this.id.length && this.item.length) {
        let ts = this.id;
        let r = this.item
          .map((k) => {
            return ts.indexOf(k.menuId) >= 0;
          })
          .filter((k) => {
            return !k;
          });
        if (!r.length) {
          this.all = true;
        } else {
          this.all = false;
        }
      }
    },
    all(n) {
      if (n) {
        this.pid = this.id = this.item.map((j) => {
          return j.menuId;
        });
      } else {
        // this.id = this.pid = [];
      }
      this.$refs.treeDom.setCheckedKeys(this.id);
    },
  },
  methods: {
    sub() {
      if (this.name === "") {
        demo.toast("请输入角色名称");
        return 0;
      }
      if (!this.$refs.treeDom.getCheckedKeys().length) {
        demo.toast("请选择权限菜单");
        return 0;
      }
      // getCheckedKeys
      // console.log(this.id, this.$refs.treeDom.getCheckedKeys());
      // return 0;
      this.$api.put(
        "systemmanagement/menu",
        {
          roleName: this.name,
          menuIds: [
            ...this.$refs.treeDom.getHalfCheckedKeys(),
            ...this.$refs.treeDom.getCheckedKeys(),
          ],
          isAll: this.all ? "1" : "0",
          roleId: this.eid,
        },
        () => {
          demo.toast("编辑成功");
          this.$store.dispatch("setLevel", Date.now());
          this.$nextTick(() => {
            this.$router.go(-1);
          });
        }
      );
    },
    close() {
      this.id = this.pid = [];
      this.name = "";
      this.$refs.treeDom.setCheckedKeys(this.id);
      this.$nextTick(() => {
        this.$router.go(-1);
      });
    },
    onCheck(v) {
      // this.handleNodeClick(v);
      if (this.isT !== 2) {
        this.handleNodeClick(v);
        this.isT = 1;
      }
    },
    handleNodeClick(v) {
      this.isT = 2;
      this.$nextTick(() => {
        this.isT = 3;
      });
    },
    init() {
      this.name = this.$route.params.name;
      this.eid = this.$route.params.id;
      // 获取所有权限
      this.$api.get("systemmanagement/listmenu", {}, (e) => {
        this.item = e;
        this.rand++;
      });
      this.$api.get(
        "systemmanagement/listmenurole",
        { roleId: this.eid },
        (e) => {
          this.id = this.pid = e.map((k) => {
            return k;
          });
          this.rand++;
        }
      );
    },
  },
  mounted() {
    this.init();
  },
};
</script>
<style lang='less' scoped>
.role_add {
  overflow-y: auto;
  font-size: 14px;
  color: #333;
  .pad {
    padding-left: 24px;
    margin-bottom: 10px;
  }
  h5 {
    margin-bottom: 8px;
  }
  .btn {
    margin: 10px 0;
  }
  .sec {
    .label {
      text-align: left;
    }
    article {
      max-width: 300px;
      b {
        line-height: 32px;
        font-size: 12px;
        color: #999;
      }
    }
  }
}
</style>