authority.vue 11.1 KB
Newer Older
1
<template>
2
  <div class="authority">
3
    <div class="button-box clearflex">
4
      <el-button @click="addAuthority('0')" type="primary">新增角色</el-button>
5
    </div>
K
klausY 已提交
6
    <el-table
Mr.奇淼('s avatar
Mr.奇淼( 已提交
7 8 9
      :data="tableData"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      border
Mr.奇淼('s avatar
Mr.奇淼( 已提交
10
      row-key="authorityId"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
11 12 13 14 15
      stripe
      style="width: 100%"
    >
      <el-table-column label="角色id" min-width="180" prop="authorityId"></el-table-column>
      <el-table-column label="角色名称" min-width="180" prop="authorityName"></el-table-column>
J
jinlan.du 已提交
16
      <el-table-column fixed="right" label="操作" width="460">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
17
        <template slot-scope="scope">
LoeYueng's avatar
LoeYueng 已提交
18
          <el-button @click="opdendrawer(scope.row)" size="small" type="primary">设置权限</el-button>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
          <el-button
            @click="addAuthority(scope.row.authorityId)"
            icon="el-icon-plus"
            size="small"
            type="primary"
          >新增子角色</el-button>
          <el-button
            @click="copyAuthority(scope.row)"
            icon="el-icon-copy-document"
            size="small"
            type="primary"
          >拷贝</el-button>
          <el-button
            @click="editAuthority(scope.row)"
            icon="el-icon-edit"
            size="small"
            type="primary"
          >编辑</el-button>
          <el-button
            @click="deleteAuth(scope.row)"
            icon="el-icon-delete"
            size="small"
            type="danger"
          >删除</el-button>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
43 44 45
        </template>
      </el-table-column>
    </el-table>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
46
    <!-- 新增角色弹窗 -->
Mr.奇淼('s avatar
Mr.奇淼( 已提交
47
    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
48
      <el-form :model="form" :rules="rules" ref="authorityForm">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
49 50 51 52 53 54 55 56 57
        <el-form-item label="父级角色" prop="parentId">
          <el-cascader
            :disabled="dialogType=='add'"
            :options="AuthorityOption"
            :props="{ checkStrictly: true,label:'authorityName',value:'authorityId',disabled:'disabled',emitPath:false}"
            :show-all-levels="false"
            filterable
            v-model="form.parentId"
          ></el-cascader>
58
        </el-form-item>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
59
        <el-form-item label="角色ID" prop="authorityId">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
60
          <el-input :disabled="dialogType=='edit'" autocomplete="off" v-model="form.authorityId"></el-input>
61
        </el-form-item>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
62
        <el-form-item label="角色姓名" prop="authorityName">
63 64 65 66 67 68 69 70
          <el-input autocomplete="off" v-model="form.authorityName"></el-input>
        </el-form-item>
      </el-form>
      <div class="dialog-footer" slot="footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button @click="enterDialog" type="primary">确 定</el-button>
      </div>
    </el-dialog>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
71

Mr.奇淼('s avatar
Mr.奇淼( 已提交
72
    <el-drawer :visible.sync="drawer" :with-header="false" size="40%" title="角色配置" v-if="drawer">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
73
      <el-tabs :before-leave="autoEnter" class="role-box" type="border-card">
74
        <el-tab-pane label="角色菜单">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
75
          <Menus :row="activeRow" ref="menus" />
76 77
        </el-tab-pane>
        <el-tab-pane label="角色api">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
78
          <apis :row="activeRow" ref="apis" />
79
        </el-tab-pane>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
80
        <el-tab-pane label="资源权限">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
81
          <Datas :authority="tableData" :row="activeRow" ref="datas" />
Mr.奇淼('s avatar
Mr.奇淼( 已提交
82
        </el-tab-pane>
83 84
      </el-tabs>
    </el-drawer>
85 86 87 88
  </div>
</template>

<script>
Mr.奇淼('s avatar
注释  
Mr.奇淼( 已提交
89 90
// 获取列表内容封装在mixins内部  getTableData方法 初始化已封装完成

Mr.奇淼('s avatar
Mr.奇淼( 已提交
91 92 93
import {
  getAuthorityList,
  deleteAuthority,
94
  createAuthority,
95
  updateAuthority,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
96
  copyAuthority
97
} from "@/api/authority";
98

99 100 101
import Menus from "@/view/superAdmin/authority/components/menus";
import Apis from "@/view/superAdmin/authority/components/apis";
import Datas from "@/view/superAdmin/authority/components/datas";
102

103
import infoList from "@/mixins/infoList";
104
export default {
105
  name: "Authority",
106
  mixins: [infoList],
107
  data() {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
108
    var mustUint = (rule, value, callback) => {
109 110 111 112
      if (!/^[0-9]*[1-9][0-9]*$/.test(value)) {
        return callback(new Error("请输入正整数"));
      }
      return callback();
Mr.奇淼('s avatar
Mr.奇淼( 已提交
113 114
    };

115
    return {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
116 117
      AuthorityOption: [
        {
118 119
          authorityId: "0",
          authorityName: "根角色"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
120 121
        }
      ],
122
      listApi: getAuthorityList,
123
      drawer: false,
124
      dialogType: "add",
125
      activeRow: {},
Mr.奇淼('s avatar
Mr.奇淼( 已提交
126
      activeUserId: 0,
127
      dialogTitle: "新增角色",
128
      dialogFormVisible: false,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
129
      apiDialogFlag: false,
S
sun_song_1203 已提交
130
      copyForm: {},
Mr.奇淼('s avatar
Mr.奇淼( 已提交
131
      form: {
132 133 134
        authorityId: "",
        authorityName: "",
        parentId: "0"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
135 136 137
      },
      rules: {
        authorityId: [
Mr.奇淼('s avatar
Mr.奇淼( 已提交
138
          { required: true, message: "请输入角色ID", trigger: "blur" },
139
          { validator: mustUint, trigger: "blur" }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
140 141
        ],
        authorityName: [
142
          { required: true, message: "请输入角色名", trigger: "blur" }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
143 144
        ],
        parentId: [
145
          { required: true, message: "请选择请求方式", trigger: "blur" }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
146
        ]
147
      }
148
    };
149
  },
150 151
  components: {
    Menus,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
152 153
    Apis,
    Datas
154
  },
155
  methods: {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
156
    autoEnter(activeName, oldActiveName) {
157
      const paneArr = ["menus", "apis", "datas"];
Mr.奇淼('s avatar
Mr.奇淼( 已提交
158 159
      if (oldActiveName) {
        if (this.$refs[paneArr[oldActiveName]].needConfirm) {
160 161
          this.$refs[paneArr[oldActiveName]].enterAndNext();
          this.$refs[paneArr[oldActiveName]].needConfirm = false;
162 163 164
        }
      }
    },
165 166
    // 拷贝角色
    copyAuthority(row) {
167 168 169
      this.setOptions();
      this.dialogTitle = "拷贝角色";
      this.dialogType = "copy";
Mr.奇淼('s avatar
Mr.奇淼( 已提交
170
      for (let k in this.form) {
171
        this.form[k] = row[k];
172
      }
173 174
      this.copyForm = row;
      this.dialogFormVisible = true;
175
    },
176
    opdendrawer(row) {
177 178
      this.drawer = true;
      this.activeRow = row;
179
    },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
180
    // 删除角色
181
    deleteAuth(row) {
182 183 184 185
      this.$confirm("此操作将永久删除该角色, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
186 187
      })
        .then(async () => {
188
          const res = await deleteAuthority({ authorityId: row.authorityId });
189
          if (res.code == 0) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
190
            this.$message({
191 192 193
              type: "success",
              message: "删除成功!"
            });
194
            if (this.tableData.length == 1 && this.page > 1 ) {
195 196
              this.page--;
            }
197
            this.getTableData();
Mr.奇淼('s avatar
Mr.奇淼( 已提交
198
          }
199 200 201
        })
        .catch(() => {
          this.$message({
202 203 204 205
            type: "info",
            message: "已取消删除"
          });
        });
206
    },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
207 208
    // 初始化表单
    initForm() {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
209
      if (this.$refs.authorityForm) {
210
        this.$refs.authorityForm.resetFields();
Mr.奇淼('s avatar
Mr.奇淼( 已提交
211 212
      }
      this.form = {
213 214 215 216
        authorityId: "",
        authorityName: "",
        parentId: "0"
      };
217
    },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
218 219
    // 关闭窗口
    closeDialog() {
220 221 222
      this.initForm();
      this.dialogFormVisible = false;
      this.apiDialogFlag = false;
223
    },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
224
    // 确定弹窗
Mr.奇淼('s avatar
Mr.奇淼( 已提交
225

Mr.奇淼('s avatar
Mr.奇淼( 已提交
226
    async enterDialog() {
227
      if (this.form.authorityId == "0") {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
228
        this.$message({
229 230 231 232
          type: "error",
          message: "角色id不能为0"
        });
        return false;
Mr.奇淼('s avatar
Mr.奇淼( 已提交
233
      }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
234 235
      this.$refs.authorityForm.validate(async valid => {
        if (valid) {
236
          switch (this.dialogType) {
237
            case "add":
238
              {
239
                const res = await createAuthority(this.form);
240 241
                if (res.code == 0) {
                  this.$message({
242 243 244 245 246
                    type: "success",
                    message: "添加成功!"
                  });
                  this.getTableData();
                  this.closeDialog();
247 248
                }
              }
249 250
              break;
            case "edit":
251
              {
252
                const res = await updateAuthority(this.form);
253 254
                if (res.code == 0) {
                  this.$message({
255 256 257 258 259
                    type: "success",
                    message: "添加成功!"
                  });
                  this.getTableData();
                  this.closeDialog();
260 261
                }
              }
262 263
              break;
            case "copy": {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
264 265
              const data = {
                authority: {
266 267
                  authorityId: "string",
                  authorityName: "string",
Mr.奇淼('s avatar
Mr.奇淼( 已提交
268
                  datauthorityId: [],
269
                  parentId: "string"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
270 271
                },
                oldAuthorityId: 0
272 273 274 275 276 277 278
              };
              data.authority.authorityId = this.form.authorityId;
              data.authority.authorityName = this.form.authorityName;
              data.authority.parentId = this.form.parentId;
              data.authority.dataAuthorityId = this.copyForm.dataAuthorityId;
              data.oldAuthorityId = this.copyForm.authorityId;
              const res = await copyAuthority(data);
Mr.奇淼('s avatar
Mr.奇淼( 已提交
279 280
              if (res.code == 0) {
                this.$message({
281 282 283 284
                  type: "success",
                  message: "复制成功!"
                });
                this.getTableData();
285
              }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
286
            }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
287
          }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
288

289 290
          this.initForm();
          this.dialogFormVisible = false;
Mr.奇淼('s avatar
Mr.奇淼( 已提交
291
        }
292
      });
293
    },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
294 295 296
    setOptions() {
      this.AuthorityOption = [
        {
297 298
          authorityId: "0",
          authorityName: "根角色"
299
        }
300 301
      ];
      this.setAuthorityOptions(this.tableData, this.AuthorityOption, false);
Mr.奇淼('s avatar
Mr.奇淼( 已提交
302 303
    },
    setAuthorityOptions(AuthorityData, optionsData, disabled) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
304
      this.form.authorityId = String(this.form.authorityId);
Mr.奇淼('s avatar
Mr.奇淼( 已提交
305 306 307 308 309 310 311 312
      AuthorityData &&
        AuthorityData.map(item => {
          if (item.children && item.children.length) {
            const option = {
              authorityId: item.authorityId,
              authorityName: item.authorityName,
              disabled: disabled || item.authorityId == this.form.authorityId,
              children: []
313
            };
Mr.奇淼('s avatar
Mr.奇淼( 已提交
314 315 316 317
            this.setAuthorityOptions(
              item.children,
              option.children,
              disabled || item.authorityId == this.form.authorityId
318 319
            );
            optionsData.push(option);
Mr.奇淼('s avatar
Mr.奇淼( 已提交
320 321 322 323 324
          } else {
            const option = {
              authorityId: item.authorityId,
              authorityName: item.authorityName,
              disabled: disabled || item.authorityId == this.form.authorityId
325 326
            };
            optionsData.push(option);
327
          }
328
        });
329
    },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
330
    // 增加角色
331
    addAuthority(parentId) {
332 333 334 335 336 337
      this.initForm();
      this.dialogTitle = "新增角色";
      this.dialogType = "add";
      this.form.parentId = parentId;
      this.setOptions();
      this.dialogFormVisible = true;
338
    },
339
    // 编辑角色
340
    editAuthority(row) {
341 342 343
      this.setOptions();
      this.dialogTitle = "编辑角色";
      this.dialogType = "edit";
Mr.奇淼('s avatar
Mr.奇淼( 已提交
344
      for (let key in this.form) {
345
        this.form[key] = row[key];
346
      }
347 348
      this.setOptions();
      this.dialogFormVisible = true;
349
    }
350
  },
351
  async created() {
352 353
    this.pageSize = 999;
    await this.getTableData();
354
  }
355
};
356
</script>
357 358
<style lang="scss">
.authority {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
359
  .el-input-number {
360
    margin-left: 15px;
Mr.奇淼('s avatar
Mr.奇淼( 已提交
361
    span {
362 363 364
      display: none;
    }
  }
365 366 367 368 369
  .button-box {
    padding: 10px 20px;
    .el-button {
      float: right;
    }
370 371
  }
}
372
.role-box {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
373 374 375
  .el-tabs__content {
    height: calc(100vh - 150px);
    overflow: auto;
376
  }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
377
}
378
</style>