云e办前端学习(十)系统 *** 作角色管理页面

云e办前端学习(十)系统 *** 作角色管理页面,第1张

前言

本系列博客基于B站的云e办管理系统,前端和后端我都自己敲了一遍,这里做一个学习记录。云e办的原始视频链接如下:https://www.bilibili.com/video/BV1Ai4y1P7Tk?p=1


<template>
  <div>
    <div style="display: flex; justify-content: center; margin-top: 20px">
      <el-input
          placeholder="通过用户名搜索用户"
          prefix-icon="el-icon-search"
          style="width: 400px; margin-right: 20px"
          v-model="keywords"
          @keydown.enter.native="doSearch"
      ></el-input>
      <el-button type="primary" icon="el-icon-search" @click="doSearch"
      >搜索
      </el-button
      >
    </div>
    <div class="admin-container">
<!--      卡片-->
      <el-card class="admin-card" v-for="(admin, index) in admins" :key="index">
        <div slot="header" class="clearfix">
          <span>{{ admin.name }}</span>
<!--          删除按钮-->
          <el-button
              style="float: right; padding: 3px 0; color: red"
              type="text"
              icon="el-icon-delete"
              @click="deleteAdmin(admin)"
          ></el-button>
        </div>
        <div>
          <div class="img-content">
<!--            用户头像-->
            <img
                :src="admin.userFace"
                :alt="admin.name"
                :title="admin.name"
                class="userFace-img"
            />
          </div>
        </div>
        <div class="userInfo">
          <div>姓名:{{ admin.name }}</div>
          <div>手机号码:{{ admin.phone }}</div>
          <div>电话号码:{{ admin.telephone }}</div>
          <div>地址:{{ admin.address }}</div>
          <div class="userInfo">
            用户状态:
            <el-switch
                v-model="admin.enabled"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-text="启用"
                inactive-text="禁用"
                @change="enabledChange(admin)"
            >
            </el-switch>
          </div>
<!--          循环展示用户角色-->
          <div>
            用户角色:
            <el-tag
                style="margin-right: 10px"
                type="success"
                v-for="(role, indexj) in admin.roles"
                :key="indexj"
            >{{ role.nameZh }}
            </el-tag
            >
            <el-popover
                placement="right"
                title="角色列表"
                width="200"
                @show="showPop(admin)"
                @hide="hidePop(admin)"
                trigger="click"
            >
<!--              选择用户对应的角色-->
              <el-select v-model="selectRoles" multiple placeholder="请选择">
                <el-option
                    v-for="(r, index) in allRoles"
                    :key="index"
                    :label="r.nameZh"
                    :value="r.id"
                >
                </el-option>
              </el-select>
<!--              三个点点d出框-->
              <el-button
                  slot="reference"
                  type="text"
                  icon="el-icon-more"
              ></el-button>
            </el-popover>
          </div>
          <div>备注: {{ admin.remark }}</div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
export default {
  name: "SysAdmin",
  data() {
    return {
      admins: [],
      keywords: "",
      //所有的角色id
      allRoles: [],
      //被选择的角色id
      selectRoles: [],
    };
  },
  mounted() {
    this.initAdmins();
  },
  methods: {
    //隐藏d出框事件
    hidePop(admin) {
      let roles = [];
      Object.assign(roles, admin.roles);
      let flag = false;
      //当选择的长度和展示的角色长度不同,进行更新
      if (roles.length != this.selectRoles.length) {
        flag = true;
      } else {
        //如果长度相等,进行遍历,遍历删除相同的角色
        for (let i = 0; i < roles.length; i++) {
          let role = roles[i];
          for (let j = 0; j < this.selectRoles.length; j++) {
            //获取角色列表
            let sr = this.selectRoles[j];
            if (role.id == sr) {
              roles.splice(i, 1);
              i--;
              break;
            }
          }
        }
        //如果最后删光了,说明进行了变更,所以更新列表
        if (roles.length != 0) {
          flag = true;
        }
      }
      //更新选择角色
      if (flag) {
        let url = "/system/admin/role?adminId=" + admin.id;
        this.selectRoles.forEach((sr) => {
          url += "&rids=" + sr;
        });
        this.putRequest(url).then((resp) => {
          if (resp) {
            this.initAdmins();
          }
        });
      }
    },
    //d出显示数据
    showPop(admin) {
      this.initAllRoles();
      //拿到角色对象
      let roles = admin.roles;
      this.selectRoles = [];
      roles.forEach((r) => {
        this.selectRoles.push(r.id);
      });
    },
    initAllRoles() {
      this.getRequest("/system/admin/roles").then((resp) => {
        if (resp) {
          this.allRoles = resp;
        }
      });
    },
    enabledChange(admin) {
      this.putRequest("/system/admin/",admin).then((resp) => {
        if (resp) {
          this.initAdmins();
        }
      });
    },
    deleteAdmin(admin) {
      this.$confirm(
          "此 *** 作将永久删除" + admin.name + "用户, 是否继续?",
          "提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
      )
          .then(() => {
            this.deleteRequest("/system/admin/" + admin.id).then((resp) => {
              if (resp) {
                this.initAdmins();
              }
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
    },
    //关键词搜索
    doSearch() {
      this.initAdmins();
    },
    initAdmins() {
      this.getRequest("/system/admin/?keywords=" + this.keywords).then((resp) => {
        if (resp) {
          this.admins = resp;
        }
      });
    },
  },
};
</script>

<style>
.admin-card {
  width: 350px;
  margin-bottom: 20px;
}

.admin-container {
  display: flex;
  margin-top: 20px;
  justify-content: space-around;
  flex-wrap: wrap;
}

.userFace-img {
  width: 60px;
  height: 60px;
  border-radius: 30px;
}

.img-content {
  width: 100%;
  display: flex;
  justify-content: center;
}

.userInfo {
  font-size: 15px;
  color: skyblue;
}
</style>

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/927997.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-17
下一篇 2022-05-17

发表评论

登录后才能评论

评论列表(0条)

保存