authority.vue 10.8 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">
60 61 62 63 64 65 66
          <el-input-number
            :disabled="dialogType=='edit'"
            step-strictly
            :step="1"
            autocomplete="off"
            v-model="form.authorityId"
          ></el-input-number>
67
        </el-form-item>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
68
        <el-form-item label="角色姓名" prop="authorityName">
69 70 71 72 73 74 75 76
          <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.奇淼( 已提交
77

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

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

Mr.奇淼('s avatar
Mr.奇淼( 已提交
97 98 99
import {
  getAuthorityList,
  deleteAuthority,
100
  createAuthority,
101
  updateAuthority,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
102
  copyAuthority
103
} from "@/api/authority";
104

105 106 107
import Menus from "@/view/superAdmin/authority/components/menus";
import Apis from "@/view/superAdmin/authority/components/apis";
import Datas from "@/view/superAdmin/authority/components/datas";
108

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

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

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