2021SC@SDUSC 软件工程应用与实践(10)——addNewRole

本文详细介绍了如何在项目中实现用户角色的添加功能,包括在用户管理界面添加分配角色的按钮,设计分配角色的弹框,并提供添加、删除角色以及更新角色权限的API操作。通过示例代码展示了使用Vue.js和Element UI组件库构建的用户角色管理界面,以及对应的后端请求方法。
摘要由CSDN通过智能技术生成

2021SC@SDUSC

一、引言

本篇文章将对项目中添加用户角色的功能模块进行分析。

二、代码分析

添加用户角色

首先在用户管理操作中加入分配角色按钮。
定义好分配角色的弹框。

<template>
  <div>
    <div style="text-align: left" v-loading="loading">
      <el-input placeholder="请输入角色英文名称..."
                size="mini"
                style="width: 250px"
                v-model="newRole">
        <template slot="prepend">ROLE_</template>
      </el-input>
      <el-input
        placeholder="请输入角色中文名称..."
        size="mini"
        style="width: 250px"
        v-model="newRoleZh">
      </el-input>
      <el-button type="primary" size="mini" style="margin-left: 5px" @click="addNewRole">添加角色</el-button>
    </div>
    <div style="margin-top: 10px;text-align: left">
      <el-collapse v-model="activeColItem" accordion style="width: 500px;" @change="collapseChange">
        <el-collapse-item v-for="(item,index) in roles" :title="item.nameZh" :name="item.id" :key="item.name">
          <el-card class="box-card">
            <div slot="header">
              <span>可访问的资源</span>
              <el-button type="text"
                         style="color: #f6061b;margin: 0px;float: right; padding: 3px 0;width: 15px;height:15px"
                         icon="el-icon-delete" @click="deleteRole(item.id,item.nameZh)"></el-button>
            </div>
            <div>
              <el-tree :props="props"
                       :key="item.id"
                       :data="treeData"
                       :default-checked-keys="checkedKeys"
                       node-key="id"
                       ref="tree"
                       show-checkbox
                       highlight-current
                       @check-change="handleCheckChange">
              </el-tree>
            </div>
            <div style="display: flex;justify-content: flex-end;margin-right: 10px">
              <el-button size="mini" @click="cancelUpdateRoleMenu">取消修改</el-button>
              <el-button type="primary" size="mini" @click="updateRoleMenu(index)">确认修改</el-button>
            </div>
          </el-card>
        </el-collapse-item>
      </el-collapse>
    </div>
  </div>
</template>

发送api请求,在methods中定义方法实现。

<script>
  import {isNotNullORBlank} from '../../../utils/utils'
  export default{
    mounted: function () {
      this.loading = true;
      this.initRoles();
    },
    methods: {
      deleteRole(rid, rname){
        var _this = this;
        this.$confirm('删除角色[' + rname + '], 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          _this.loading = true;
          _this.deleteRequest("/system/basic/role/" + rid).then(resp=> {
            if (resp && resp.status == 200) {
              var data = resp.data;
              _this.$message({type: data.status, message: data.msg})
              _this.initRoles();
            } else {
              _this.loading = false;
            }
          })
        }).catch(() => {
          _this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      addNewRole(){
        if (isNotNullORBlank(this.newRole, this.newRoleZh)) {
          this.loading = true;
          var _this = this;
          this.postRequest("/system/basic/addRole", {
            role: this.newRole,
            roleZh: this.newRoleZh
          }).then(resp=> {
            if (resp && resp.status == 200) {
              var data = resp.data;
              _this.$message({type: data.status, message: data.msg});
              _this.initRoles();
              _this.newRole = '';
              _this.newRoleZh = '';
            } else {
              _this.loading = false;
            }
          })
        }
      },
      //有五个树,但是五个树用的同一个数据源
      updateRoleMenu(index){
        var checkedKeys = this.$refs.tree[index].getCheckedKeys(true);
        var _this = this;
        this.putRequest("/system/basic/updateMenuRole", {
          rid: this.activeColItem,
          mids: checkedKeys
        }).then(resp=> {
          if (resp && resp.status == 200) {
            var data = resp.data;
            _this.$message({type: data.status, message: data.msg})
            _this.activeColItem = -1;
          }
        })
      },
      collapseChange(activeName){
        if (activeName == '') {
          return;
        }
        var _this = this;
        this.getRequest("/system/basic/menuTree/" + activeName).then(resp=> {
          if (resp && resp.status == 200) {
            var data = resp.data;
            _this.treeData = data.menus;
            _this.checkedKeys = data.mids;
          }
        })
      },
      handleCheckChange(data, checked, indeterminate) {
//        console.log(data,checked,indeterminate)
      },
      initRoles(){
        var _this = this;
        this.getRequest("/system/basic/roles").then(resp=> {
          _this.loading = false;
          if (resp && resp.status == 200) {
            _this.roles = resp.data;
            _this.activeColItem=-1
          }
        })
      },
      cancelUpdateRoleMenu(){
        this.activeColItem = -1;
      }
    },
    data(){
      return {
        props: {
          label: 'name',
          children: 'children'
        },
        newRole: '',
        newRoleZh: '',
        roles: [],
        treeData: [],
        checkedKeys: [],
        loading: false,
        activeColItem: -1
      };
    }
  }
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LiuFangdi145

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值