Day08-Cos上传和权限数据

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


1.员工详情-员工头像组件

在这里插入图片描述
在这里插入图片描述

  • 创建imageupload组件(src/views/employee/components/imageUpload.vue)
<template>
    <div>
        <el-upload class="avatar-uploader" action="" :show-file-list="false" :before-upload="beforeAvatarUpload">
    
        </el-upload>
        <!-- (自动上传) action是上传地址 show-file-list不展示列表 -->
        <img v-if="value" :src="value" class="avatar" alt="">
        <i v-else class="el-icon-plus avatar-uploader-icon" />
    </div>
</template>
export default {
  props: {
    value: {
      type: String,
      default: ''
    }
  },
  data() {
    return {

    }
  },
  methods: {
    //检查方法 判断文件的类型和大小 return true;return false
    beforeAvatarUpload(file) {
      const isJPG = ['image/jpeg', 'image/png', 'image/gif', 'image/bmp'].includes(file.type)
      const isLt2M = file.size / 1024 / 1024 < 1
      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG PNG GIF BMP 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 1MB!')
      }
      return isJPG && isLt2M
    }
  },
  components: {

  }
}
<style scoped lang="scss">
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}

.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>

2.权限管理-搭建权限页面

  • 实现权限管理的页面结构-代码位置(src/views/permission/index.vue)
<template>
  <div class="container">
    <div class="app-container">
      <el-button class="btn-add" size="mini" type="primary">添加权限</el-button>
      <el-table>
        <el-table-column label="名称" />
        <el-table-column label="标识" />
        <el-table-column label="描述" />
        <el-table-column label="操作">
          <el-button size="mini" type="text">添加</el-button>
          <el-button size="mini" type="text">编辑</el-button>
          <el-button size="mini" type="text">删除</el-button>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
export default {
    name:'Permission',
  data() {
    return {

    }
  },
  components: {

  }
}
</script>

<style scoped lang="scss">
  .btn-add{
    margin: 10px;
  }
</style>

3.权限管理-获取数据转化树形

在这里插入图片描述

  • 获取权限方法(src/api/permission.js)
export function getPermissionList(){
  return request({
  	url:'/sys/permission' 
  })
} 
  • 获取数据-转化树形-控制二级权限不显式添加按钮(src/views/permission/index.vue)
<template>
  <div class="container">
    <div class="app-container">
      <el-button class="btn-add" size="mini" type="primary">添加权限</el-button>
      <el-table default-expand-all :data="list" row-key="id">
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="code" label="标识" />
        <el-table-column prop="description" label="描述" />
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button v-if="row.type === 1" size="mini" type="text">添加</el-button>
            <el-button size="mini" type="text">编辑</el-button>
            <el-button size="mini" type="text">删除</el-button>
          </template>

        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
import { getPermissionList } from '@/api/permission'
import { transListToTreeData } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      list: []
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      this.list = transListToTreeData(await getPermissionList(), 0) // 将列表数据转化成树形结构
    }
  }
}
</script>
<style>
.btn-add {
  margin: 10px;
}
</style>

4.权限管理-新增

  • 权限管理接口(src/api/permission.js)
// 新增权限
export function addPermission(data){
    return request({
        method:'post',
        url:'/sys/permission',
        data
    })
}
// 更新权限
export function updatePermission(data){
    return request({
        url:`/sys/permission/${data.id}`,
        method:'put',
        data
    })
}
//删除权限
export function delPermission(id){
    return request({
        url:`/sys/permission/${id}`,
        method:'delete'
    })
}
//获取权限详情
export function getPermissionDetail(id){
    return request({
        url:`/sys/permission/${id}`
    })
}
  • 新增编辑权限的弹层(src/views/permission/index.vue)
 <el-dialog :title="`${showText}权限点`" :visible="showDialog">
      <el-form ref="perForm" :model="formData" :rules="rules" label-width="120px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name">
  
          </el-input>
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code">
  
          </el-input>
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description">
  
          </el-input>
        </el-form-item>
        <el-form-item label="开启">
          <el-switch v-model="formData.enVisible" active-value="1" inactive-value="0">
  
          </el-switch>
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="7">
          <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          <el-button size="small" @click="btnCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
<script>
import { getPermissionList, updatePermission, addPermission, getPermissionDetail, delPermission } from '@/api/permission';
import { transListToTreeData } from '@/utils';
export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      formData: {
        name: '',//名称
        code: '',//标识
        description: '',//描述
        type: '',//类型
        pid: '',
        enVisible: '0'
      },
      rules: {
        name: [
          { required: true, message: '权限名称不能为空', trigger: 'blur' }
        ],
        code: [
          {
            required: true,
            message: '权限标识不能为空',
            trigger: 'blur'
          }
        ]
      },
      showDialog: false
    }
  },
  computed: {
    showText() {
      return this.formData.id ? '编辑' : '新增'
    }
  },
  created() {
    this.getPermissionList()

  },
  methods: {
    async getPermissionList() {
      this.list = transListToTreeData(await getPermissionList(), 0);

    },
    handleClick(row) {
      console.log(row);
    },
    addPermission(pid, type) {
      console.log(1111)
      this.formData.pid = pid;
      this.formData.type = type
      this.showDialog = true
    },
    //确定事件
    btnOK() {
      this.$refs.perForm.validate().then(() => {
        if (this.formData.id) {
          return updatePermission(this.formData)
        }
        return addPermission(this.formData)
      }).then(() => {
        //提示
        this.$message.success('新增成功')
        this.getPermissionList()
        this.showDialog = false;
      })
    },
    //取消事件
    btnCancel(){
      this.formData = {
        name: '',//名称
        code: '',//标识
        description: '',//描述
        type: '',//类型
        pid: '',
        enVisible: '0'
      }
      this.$refs.perForm.resetFields()
      this.showDialog = false
    }
  },
  mounted() {
    console.log(this.list)
  },
  components: {

  }
}
</script>
  • 添加权限事件
 <el-button class="btn-add" size="mini" type="primary" @click="addPermission(0,1)">添加权限</el-button>
addPermission(pid,type){
	this.formData.pid = pid;
	this.formData.type = type;
	this.showDialog = true;
}
  • 编辑弹层
   <el-button size="mini" type="text" @click="editPermission(row.id)">编辑</el-button>
<script>
  //编辑
    async editPermission(id){
      //根据获取id获取详情
      this.formData = await getPermissionDetail(id)
      this.showDialog = true
    },
</script>
  • 删除
 <el-button size="mini" type="text" @click="delPermission(row.id)">删除</el-button>
 <script>
async delPermission(id){
  try{
   await this.$confirm('确定要删除该数据吗')
   await delPermission(id)
   this.getPermissionList()
   this.$message.success('删除成功')
   }catch(error){
    console.log(error)
    }
}
</script>

5.权限应用-权限概念

在这里插入图片描述
在这里插入图片描述

  • 权限是通过角色这个中间人实现,首先员工拥有角色,角色拥有权限,那么员工自动拥有角色所对应的权限
  • 需要给 员工分配角色,给角色分配权限
    在这里插入图片描述
    在这里插入图片描述

6.员工分配角色-弹出层

在这里插入图片描述

  • 获取可用角色的接口(src/api/employee.js)
//获取可用角色
export function getEnableRoleList(){
    return request({
        url:'/sys/role/list/enabled'
    })
}
  • 声明变量和角色列表(src/views/employee/index.vue)
data(){
  return{
    showRoleDialog:false,//控制角色弹窗
      roleList:[],//接收角色列表
      roleIds:[],//用来双向绑定数据
   }
}
  • 点击角色按钮-获取角色列表(src/views/employee/index.vue)
 <el-button size="mini" type="text" @click="btnRole">角色</el-button>
 <script>
 	 //角色
    async btnRole(){
      this.showRoleDialog = true
      this.roleList = await getEnableRoleList(); 
    }
 </script>
  • 放置弹层-绑定变量和渲染checkbox-代码位置(src/views/employee/index.vue)
<el-dialog :visible.sync="showRoleDialog" title="分配角色">
      <!-- 弹层内容 -->
      <!-- checkbox -->
      <el-checkbox-group v-model="roleIds">
        <!-- 放置n个的checkbox  要执行checkbox的存储值 item.id-->
        <el-checkbox
          v-for="item in roleList"
          :key="item.id"
          :label="item.id"
        >{{ item.name }}</el-checkbox>
      </el-checkbox-group>
       <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" size="mini" @click="btnRoleOK">确定</el-button>
          <el-button @click="showRoleDialog = false">取消</el-button>
        </el-col>
      </el-row>
</el-dialog>

7.权限应用-员工分配角色-回显数据并提交

在这里插入图片描述

  • 员工分配角色接口(src/api/employee.js)
export function assignRole(data){
  return request({
     url:'/sys/user/assignRoles',
     method:'put',
     data
   })
}
  • 点击角色按钮时,获取员工已经拥有的角色,并记录当前点击的用户id(src/views/employee/index.vue)
currentUserId:null,//记录当前点击的用户id
//角色
    async btnRole(id){
      this.showRoleDialog = true
      this.roleList = await getEnableRoleList();
      //记录当前点击的id 确定取消要存取给对应的用户
      this.currentUserId = id;
      const { roleIds } = await getEmployeeDetail(id)
      this.roleIds = roleIds
      this.showRoleDialog = true //放到最后
    }
  • 点击确定实现给用户分配角色(src/views/employee/index.vue)
//点击角色的确定
async btnRoleOK(){
  await assignRole(){
     id:this.currentUserId,
     roleIds:this.roleIds
  }
}

8.给角色分配权限-弹出层

在这里插入图片描述

  • 声明变量控制弹层显式和接收权限数据(src/views/role/index.vue)
data(){
  return{
     showPermissionDialog: false,
     permissionData: []
  }
}
  • 点击分配权限-弹出层-获取数据并转化树形-代码位置(src/views/role/index.vue)
<el-button size="mini" type="text" @click="btnPermission">分配权限</el-button>
<script>
  async btnPermission(){
    this.showPermissionDialog = true
    this.permissionData = transListToTreeData(await getPermissionList(),0) 
  }
</script>
  • 放置弹层和树组件-代码位置(src/views/role/index.vue)
<!-- 放置权限弹层 -->
    <el-dialog :visible.sync="showPermissionDialog" title="分配权限">
      <!-- 放置权限数据 -->
      <el-tree
        :data="permissionData"
        :props="{ label: 'name' }"
        show-checkbox
        default-expand-all
      />
    </el-dialog>

9.权限应用-角色分配权限-显式已有权限数据

在这里插入图片描述

  • 获取角色详情的接口(src/api/role.js)
export function getRoleDetail(id){
  return request({
    url:`/sys/role/${id}` 
  })
}
  • 声明变量记录 当前点击的角色id和角色所拥有的权限数据(src/views/role/index.vue)
data(){
  return{
     currentRoleId:null,//当前点击的角色id
      permIds:[],//角色所拥有的权限数据 
  }
}
  • 点击分配权限按钮时 传递角色id,根据id获取该角色所拥有的权限(src/views/role/index.vue)
<el-button size="mini" type="text" @click="btnPermission(row.id)">分配权限</el-button>
<script>
  async btnPermission(id){
    this.currentRoleId = id
    const { permIds } = await getRoleDetail(id)
    this.permIds = permIds
    this.permissionData = transListToTreeData(await getPermissionList(),0);
    this.showPermissionDialog = true
  }
</script>
  • 设置el-tree组件的属性-node-key和当前选中数据-代码位置(src/views/role/index.vue)
 <el-tree node-key="id" :data="permissionData" :props="{label:'name'}" show-checkbox default-expand-all :default-checked-keys="permIds">
</el-tree>

10.确定提交

在这里插入图片描述

  • 分配权限的接口(src/api/role.js)
export function assignPerm(data) { 
    return request({
        url:'/sys/role/assignPrem',
        method:'put',
        data
    })
 }
  • 确定和取消(src/views/role/index.vue)
 //确定
    async btnPermissionOK(){
     await assignPerm({
        id:this.currentRoleId,
        permIds:this.$refs.permTree.getCheckedKeys()
      })
      this.$message.success('角色分配成功')
      this.showPermissionDialog = false
    }

相关推荐

  1. cos + vue + Element UI 文件的实现

    2024-07-20 11:34:10       43 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-20 11:34:10       172 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-20 11:34:10       190 阅读
  3. 在Django里面运行非项目文件

    2024-07-20 11:34:10       158 阅读
  4. Python语言-面向对象

    2024-07-20 11:34:10       171 阅读

热门阅读

  1. abc362(abcde)

    2024-07-20 11:34:10       30 阅读
  2. [jieba_fast][python]jieba_fast所有whl文件下载地址汇总

    2024-07-20 11:34:10       30 阅读
  3. 【Android】本地化的实现

    2024-07-20 11:34:10       32 阅读
  4. 刷题Day57|107. 寻找存在的路径

    2024-07-20 11:34:10       33 阅读
  5. PEFT的几种方式

    2024-07-20 11:34:10       34 阅读
  6. springSecurity学习之springSecurity过滤web请求

    2024-07-20 11:34:10       37 阅读
  7. GEE错误:Error: Encoded object too large. (Error code: 3)

    2024-07-20 11:34:10       28 阅读