Vue电商管理系统(3)

权限管理页面

  • 角色列表

  • 权限列表

    角色列表难点分析,个人认为就是分配权限,渲染页面的布局

页面渲染操作:
有element-ui的table组件+Layout 布局+tag标签,
在这里插入图片描述
分配权限:

在这里插入图片描述

代码操作:

<template>
  <div>
    <mian />
    <el-card>
      <el-button type="primary" class="btn" @click="addUser"
        >添加用户</el-button
      >
      <div class="content">
        <el-table :data="tableData" style="width: 100%" border>
          <el-table-column type="expand">
            <template slot-scope="scope">
              <el-row
                :class="[
                  'border-bottom',
                  i1 === 0 ? 'border-top' : '',
                  'active',
                  'border-left',
                ]"
                v-for="(item1, i1) in scope.row.children"
                :key="item1.id"
              >
                <!-- 渲染一级权限 -->
                <el-col :span="5">
                  <el-tag
                    closable
                    @close="removeAuthName(scope.row, item1.id)"
                    >{{ item1.authName }}</el-tag
                  >
                  <i class="el-icon-caret-right"></i>
                </el-col>
                <!-- 渲染二级三级权限 -->
                <el-col :span="19">
                  <!-- 循环渲染二级权限 -->
                  <el-row
                    :class="[in2 === 0 ? '' : 'border-top', 'border-left']"
                    v-for="(item2, in2) in item1.children"
                    :key="item2.id"
                  >
                    <el-col :span="6">
                      <el-tag
                        type="success"
                        closable
                        @close="removeAuthName(scope.row, item2.id)"
                        >{{ item2.authName }}</el-tag
                      >
                      <i class="el-icon-caret-right"></i>
                    </el-col>
                    <el-col :span="18">
                      <el-tag
                        type="warning"
                        closable
                        v-for="(item3, in3) in item2.children"
                        :key="item3.id"
                        @close="removeAuthName(scope.row, item3.id)"
                        >{{ item3.authName }}</el-tag
                      >
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
            </template>
          </el-table-column>
          <el-table-column label="#" type="index"> </el-table-column>
          <el-table-column label="角色名称" prop="roleName"> </el-table-column>
          <el-table-column label="角色描述" prop="roleDesc"> </el-table-column>
          <el-table-column label="操作" prop="desc">
            <template slot-scope="scope">
              <!-- //角色编辑 -->
              <el-button
                type="primary"
                size="mini"
                class="el-icon-edit"
                @click="editFormName(scope.row.id)"
                >&nbsp;编辑</el-button
              >
              <el-button
                type="danger"
                size="mini"
                class="el-icon-delete"
                @click="removeAddForm(scope.row.id)"
                >&nbsp;删除</el-button
              >
              <el-button
                type="warning"
                size="mini"
                class="el-icon-setting"
                @click="showSetDialog(scope.row)"
                >&nbsp;分配权限</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 添加角色的模态框 -->
      <el-dialog title="添加角色" :visible.sync="dialogFormVisible">
        <el-form :model="formName" :rules="rules" ref="formName">
          <el-form-item label="角色名称" :label-width="'80px'" prop="roleName">
            <el-input v-model="formName.roleName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="角色描述" :label-width="'80px'" prop="roleDesc">
            <el-input v-model="formName.roleDesc" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitForm('formName')"
            >确 定</el-button
          >
        </div>
      </el-dialog>
      <!-- 修改用户的弹框 -->
      <el-dialog title="修改角色" :visible.sync="dialogForm">
        <el-form :model="addForm" :rules="rules" ref="addForm">
          <el-form-item label="角色名称" :label-width="'80px'" prop="roleName">
            <el-input v-model="addForm.roleName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="角色描述" :label-width="'80px'" prop="roleDesc">
            <el-input v-model="addForm.roleDesc" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogForm = false">取 消</el-button>
          <el-button type="primary" @click="addSubmitForm('addForm')"
            >确 定</el-button
          >
        </div>
      </el-dialog>
      <!-- 分配权限的弹框 -->
      <el-dialog
        title="分配权限"
        :visible.sync="dialogVisible"
        width="50%"
        @close="setRightDialog"
      >
        <el-tree
          :data="rightList"
          show-checkbox
          node-key="id"
          default-expand-all
          :default-checked-keys="defKes"
          ref="treeRef"
          :props="defaultProps"
        >
        </el-tree>

        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="allotRight">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
import mian from "../bread/Breadcrumb";
export default {
  name: "",
  data() {
    return {
      tableData: [], //角色列表的数据
      dialogFormVisible: false, //添加用户弹框的状态值
      //添加用户的对象
      formName: {
        roleName: "",
        roleDesc: "",
      },
      //添加用户的表单校验、
      rules: {
        roleName: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        roleDesc: [
          { required: true, message: "请输入角色描述", trigger: "blur" },
          {
            min: 3,
            max: 20,
            message: "长度在 3 到 20 个字符",
            trigger: "blur",
          },
        ],
      },
      //修改的数据
      addForm: {
        roleName: "",
        roleDesc: "",
        roleId: "",
      },
      // //修改的弹框状态
      dialogForm: false,
      // //修改的ID
      id: "",
      //分配权限的弹框的状态值
      dialogVisible: false,
      //所有权限的数据
      rightList: [],
      defaultProps: {
        label: "authName",
        children: "children",
      },
      defKes: [], //默认选中节点的ID值
      roleId: "", //即将分配权限的ID
    };
  },
  components: {
    mian,
  },
  mounted() {
    this.addUserList();
  },
  methods: {
    // 获取所有的数据
    addUserList() {
      this.$axios
        .get(`https://www.liulongbin.top:8888/api/private/v1/roles`)
        .then((res) => {
          if (res.meta.status == 200) {
            this.tableData = res.data;
          }
        });
    },
    //添加用户的按钮的状态
    addUser() {
      this.dialogFormVisible = true;
    },
    //点击添加用户的弹框的确认按钮提交用户
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios
            .post(
              `https://www.liulongbin.top:8888/api/private/v1/roles`,
              this.formName
            )
            .then((res) => {
              if (res.meta.status !== 201) {
                this.$message.error(res.meta.msg);
              } else {
                this.$message.success(res.meta.msg);
                this.addUserList();
                this.dialogFormVisible = false;
                this.dialogForm = false;
              }
            });
        }
      });
    },
    editFormName(id) {
      this.$axios
        .get(`https://www.liulongbin.top:8888/api/private/v1/roles/${id}`)
        .then((res) => {
          if (res.meta.status !== 200) {
            return this.$message.error(res.meta.msg);
          }
          this.$message.success(res.meta.msg);
          this.addForm = res.data;
          this.addForm.roleId = res.data.roleId;
        });
      this.dialogForm = true;
    },
    addSubmitForm(addForm) {
      this.$refs[addForm].validate((valid) => {
        if (valid) {
          this.$axios
            .put(
              `https://www.liulongbin.top:8888/api/private/v1/roles/${this.addForm.roleId}`,
              {
                roleName: this.addForm.roleName,
                roleDesc: this.addForm.roleDesc,
              }
            )
            .then((res) => {
              console.log(res);
              if (res.meta.status !== 200) {
                this.$message.error("编辑提交用户失败");
              }
              this.$message.success("编辑用户提交角色成功");
              this.dialogForm = false;
              this.addUserList();
            });
        }
      });
    },
    //点击删除权限
    removeAuthName(role, rightId) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios
            .delete(
              `https://www.liulongbin.top:8888/api/private/v1/roles/${role.id}/rights/${rightId}`
            )
            .then((res) => {
              if (res.meta.status !== 200)
                return this.$message.error(res.meta.msg);
              this.$message.success(res.meta.msg);
              role.children = res.data;
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //展示权限的对话框
    showSetDialog(role) {
      this.roleId = role.id;
      this.$axios
        .get(`https://www.liulongbin.top:8888/api/private/v1/rights/tree`)
        .then((res) => {
          if (res.meta.status !== 200) {
            return this.$message.error(res.meat.msg);
          }
          this.rightList = res.data;
        });
      this.getDefKes(role, this.defKes);
      this.dialogVisible = true;
    },
    //通多递归的形式,获取角色下所有三级权限的Id并保存到defKes数组中
    getDefKes(node, arr) {
      //如果当前node节点不包含childern属性,则是三级节点

      if (!node.children) {
        return arr.push(node.id);
      }
      node.children.forEach((item) => this.getDefKes(item, arr));
    },
    //监听分配权限对话框的关闭事件
    setRightDialog() {
      this.defKes = [];
    },
    //点击为角色分配权限
    allotRight() {
      const keys = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys(),
      ];
      const idStr = keys.join(",");
      this.$axios
        .post(
          `https://www.liulongbin.top:8888/api/private/v1/roles/${this.roleId}/rights`,
          { rids: idStr }
        )
        .then((res) => {
          if (res.meta.status !== 200) {
            return this.$nessage.error(res.meta.msg);
          }
          this.$message.success(res.meta.msg);
          this.addUserList();
          this.dialogVisible = false;
        });
    },
    //删除角色
    removeAddForm(re) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
        this.$axios.delete(`https://www.liulongbin.top:8888/api/private/v1/roles/${re}`).then(res=>{
         if(res.meta.status!==200){
           return this.$message.erroe("删除角色失败")
         }
         this.$message.success("删除角色成功")
          this.addUserList();
        })
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  computed: {},
  watch: {},
};
</script>

商品管理的页面

  • 商品列表
  • 分类参数
  • 商品分类

Steps 步骤条,
在这里插入图片描述

Cascader 级联选择器
在这里插入图片描述

Upload 上传图片
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值