后台管理权限自定义按钮指令v-hasPermi

news2024/9/22 3:46:37

第一步:在src下面建立一个自定义指令文件,放自定义指令方法

permission.js文件:

/**
 * v-hasPermi 操作权限处理
 */

import store from "@/store";

export default {
  inserted(el, binding) {
    const { value } = binding;
    //从仓库里面获取到后台给的数组
    const permission = store.getters && store.getters.permissions;
    console.log("permission===", permission);
    if (value && Array.isArray(value) && value.length > 0) {
      const hasPermissions = permission.some((item) => {
        return value.includes(item.perms);
      });

      if (!hasPermissions) {
        el.parentNode && el.parentNode.removeChild(el);
      }
    } else {
      throw new Error(`未设置权限`);
    }
  },
};

index.js文件:

import hasPermi from './permission'

const install = function(Vue) {
  Vue.directive('hasPermi', hasPermi)
}

export default install

后端返回的数据结构:

第二步:在main.js里面引入注册自定义指令

 

// 自定义指令
import directives from "./directives/index";
Vue.use(directives);

第三步:vuex仓库里面存放后端接口返回的用于控制权限按钮的数组 

 

 vuex里面需要用到的三个文件,可根据自身需求进行添加:

user.js

import { userInfo } from "@/api/system/login";
//封装的统一存本地的方法,可自行添加
import { mySessionStorage } from "../../../utils/encryption";

const user = {
  state: {
    permissions: mySessionStorage.getItem("permissionArray") || [],
  },
  actions: {
    // 获取用户信息
    getUserInfo({ commit }) {
      return new Promise(async (resolve, reject) => {
        try {
          const res = await userInfo();
          if (res.code === 200) {
            commit("SET_PERMISSION", res.data.authorities);
            resolve(res);
          } else {
            reject(res);
          }
        } catch (error) {
          reject(error);
        }
      });
    },
  },
  mutations: {
    SET_PERMISSION: (state, permission) => {
      state.permission = permission || [];
      //把获取到的路由存本地
      mySessionStorage.setItem("permissionArray", state.permission);
    },
  },
};
export default user;

index.js

import Vue from "vue";
import Vuex from "vuex";
import user from "./modules/user";
import getters from "./getters";


Vue.use(Vuex);
const store = new Vuex.Store({
  modules: {
    user,
  },
  getters,
});

export default store;

getters.js

const getters = {
  permissions: (state) => state.Layout_store.permissions,
};
export default getters;

user.js里面的getUserInfo函数放在合适的地方调用,这里是放在登录页面:

 关于页面使用:

 关于页面操作按钮过多,固定展示两个,其他放入更多里面:

首先utils里面定义一个函数计算权限:

import store from "@/store";

// 校验权限

export const hasPermi = (arr) => {

  return store.getters.permission.some((role) => {

    return arr.includes(role);

  });

};

页面使用:

import { hasPermi } from '@/util/util'

   // 获取table数据

    async getTableLists() {

      this.loading = true

      const params = {

        ...this.queryForm,

        ...this.form,

        startTime: this.startTime,

        endTime: this.endTime,

      }

      delete params.time

      const res = await certificateList(params)

      if (res.code === 200) {

        this.tableData = res.data.records

        this.tableData = res.data.records.map((item) => {

          item.permissionList = []

          if (hasPermi(['certificate_view'])) {

            item.permissionList.push({ viewBtn: true })

          }

          if (hasPermi(['certificate_edit']) && item.issueSize == 0) {

            item.permissionList.push({ editBtn: true })

          }

          if (hasPermi(['certificate_delete'])) {

            item.permissionList.push({ deleteBtn: true })

          }

          if (hasPermi(['certificate_record'])) {

            item.permissionList.push({ recordBtn: true })

          }

          return item

        })

        this.total = parseInt(res.data.total)

        this.loading = false

      } else {

        this.$message.error(res.msg)

        this.loading = false

      }

    },

html代码:

   <el-table-column align="center" label="操作" fixed="right" width="200">
          <template slot-scope="scope">
            <div
              v-for="(item, index) in scope.row.permissionList.length > 3
                ? scope.row.permissionList.slice(0, 2)
                : scope.row.permissionList"
              :key="index"
              style="display: inline-block; margin-right: 10px;"
            >
              <el-button
                v-if="item.viewBtn"
                type="text"
                @click="onView(scope.row)"
                class="check-btn"
              >
                查看
              </el-button>
              <el-button
                v-if="item.editBtn"
                type="text"
                @click="onEdit(scope.row)"
                class="edit-btn"
              >
                编辑
              </el-button>
              <el-button
                v-if="item.deleteBtn"
                type="text"
                @click="onDelete(scope.row.id)"
                class="del-btn"
              >
                删除
              </el-button>
              <el-button
                v-if="item.recordBtn"
                type="text"
                @click="onRecord(scope.row)"
                class="check-btn"
              >
                发证记录
              </el-button>
            </div>
            <el-dropdown v-if="scope.row.permissionList.length > 3">
              <el-button type="text" size="medium" class="more-btn mr10">
                更多
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <div
                  v-for="(item1, index1) in scope.row.permissionList.slice(2)"
                  :key="index1"
                >
                  <el-dropdown-item v-if="item1.viewBtn">
                    <el-button
                      type="text"
                      @click="onView(scope.row)"
                      class="check-btn"
                    >
                      查看
                    </el-button>
                  </el-dropdown-item>
                  <el-dropdown-item v-if="item1.editBtn">
                    <el-button
                      type="text"
                      @click="onEdit(scope.row)"
                      class="edit-btn"
                    >
                      编辑
                    </el-button>
                  </el-dropdown-item>
                  <el-dropdown-item v-if="item1.deleteBtn">
                    <el-button
                      type="text"
                      @click="onDelete(scope.row.id)"
                      class="del-btn"
                    >
                      删除
                    </el-button>
                  </el-dropdown-item>
                  <el-dropdown-item v-if="item1.recordBtn">
                    <el-button
                      type="text"
                      @click="onRecord(scope.row)"
                      class="check-btn"
                    >
                      发证记录
                    </el-button>
                  </el-dropdown-item>
                </div>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>

 完结!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2039263.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

软件设计之MySQL(2)

软件设计之MySQL(2) 此篇应在JavaSE之后进行学习: 路线图推荐&#xff1a; 【Java学习路线-极速版】【Java架构师技术图谱】 Navicat可以在软件管家下载 使用navicat连接mysql数据库创建数据库、表、转储sql文件&#xff0c;导入sql数据 学习内容&#xff1a; 基础的SELECT语…

数据分析:宏基因组数据的荟萃分析

禁止商业或二改转载&#xff0c;仅供自学使用&#xff0c;侵权必究&#xff0c;如需截取部分内容请后台联系作者! 介绍 宏基因组数据的荟萃分析是一种综合多个独立宏基因组研究结果的方法&#xff0c;目的是揭示不同人群或样本中微生物群落的共同特征和差异。这种方法特别适用…

ubantu安装python3.10

1.从官网下载安装 1.1安装依赖 sudo apt update sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev1.2从官网下载源文件 wget https://www.python.org/ftp/pyth…

设计资讯 | 巴黎 2024 年奥运会“另一个自我”以 DAB 汽车定制电动摩托车的形式亮相

巴黎 2024 年奥运会运动作为定制电动摩托车 DAB Motors 融入了2024 年巴黎奥运会的精神&#xff0c;通过其定制电动摩托车诠释了奥运会的五环。这些车辆由其服务部门 DAB Custom Studio (DCS) 提供&#xff0c;颜色编码与奥运五环一样。每种颜色代表一项运动&#xff1a;蓝色代…

RobotFramework框架+Selenium实现UI自动化测试(十六)

学习目录 1 UI自动化测试 2安装RF框架所需的 robotframework-seleniumlibrary包 1&#xff09; robotframework-seleniumLibrary版本说明 2&#xff09; robotframework-selenium2Library版本说明 3 selenium介绍 3.1 配置操作系统环境支持使用selenium打开浏览器 3.2 s…

「OC」简单网络请求的实现

「OC」简单网络请求的实现 文章目录 「OC」简单网络请求的实现写在前面URL和API网络请求的流程网络申请数据解析参考文章 写在前面 在暑假最后一个项目天气预报之中&#xff0c;使用了网络请求&#xff0c;虽然说还是不太理解网络请求之中的相关内容&#xff0c;以及在写天气预…

傻瓜式一步到位Mysql 8.0 密码修改

5.7之前修改密码语句 update user set authentication_string password(“root”) where user “root”; mysql 5.7.9以后废弃了password字段和password()函数&#xff1b;并在user表加了authentication_string:字段表示用户密码 #进入到mysql 安装目录下 #停止 mysql 服务 …

低碳环保测试知识问答活动

全国生态日&#xff0c;倒计时1天。为了组织这场关于“低碳环保测试知识问答”主题的线上知识竞赛&#xff0c;我们历经从活动方案策划到落地答题小程序上线。 一、活动背景 通过举办此次知识竞赛&#xff0c;旨在提高公众对低碳环保的认识和参与度&#xff0c;推广低碳生活方…

网络协议七 应用层 DNS协议 和 DHCP协议 这两个都了解就好

应用层常见的协议 1. DNS 协议 了解 将baidu.com 解析成具体IP的协议 实际上可以理解为&#xff1a;客户端 通过 DNS 协议 和 DNS 服务器进行交互&#xff0c;将域名转换成IP 2. DHCP 协议 了解 从DHCP 服务器自动获取IP地址

P1305 新二叉树

题目&#xff1a; 洛谷传送门&#xff1a;P1305 新二叉树 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) AC代码&#xff1a; #include<bits/stdc.h> using namespace std; int n,ans; char root; struct node{char nod,r,l; }a[100000]; void dfs(char idx){cout<…

Erupt 项目搭建

创建Spring Boot项目 Maven依赖 Spring Boot版本为 2.7.10&#xff0c;erupt版本为 1.12.14 erupt版本要与Spring Boot版本适配&#xff0c;3.x.x版本Spring Boot暂不适用说是 <properties><erupt.version>1.12.14</erupt.version></properties> <…

全场景——(三)USB开发基础(1)

文章目录 一、USB 系统硬件框架和软件框架1.1 实验现象1.2 硬件框架1.3 软件框架 二、USB 电气信号2.1 USB 设备状态切换图2.2 硬件线路2.3 电子信号2.4 低速/全速信号电平2.5 高速信号电平2.6 设备连接与断开2.6.1 连接2.6.2 断开 2.7 复位2.8 设备速率识别2.8.1 低速/全速2.8…

【STM32】入门教程(完整版汇总非常详细)

本教程参考b站江协科技STM32入门教程&#xff0c;视频链接&#xff1a; 哔哩哔哩江协科技STM32入门教程https://www.bilibili.com/video/BV1th411z7sn/?spm_id_from333.337.search-card.all.click “唯一能持久的竞争优势是胜过竞争对手的学习能力。”——盖亚斯 希望对您有帮…

栈的定义及基本操作

栈的定义和特点 栈是一个特殊的线性表&#xff0c;是限定在一端进行插入和删除操作的线性表&#xff1b; 插入到栈顶称作入栈&#xff08;PUSH) 从栈顶删除最后一个元素称作出栈 &#xff08;POP&#xff09; 图1 栈的表示 假设三个元素a,b,c入栈顺序为a&#xf…

【洛谷/水滴题解】[NOIP2005 普及组] 采药

1.难度&#xff1a;普及- 2.类型&#xff1a;dp&#xff0c;01背包 3.描述&#xff0c;输入输出格式&#xff0c;样例&#xff1a; 4.AC代码&#xff1a; #include<bits/stdc.h> using namespace std; int t,m; int dp[1005]; int main() {ios::sync_with_stdio(false…

MySQL第8讲--DCL(数据控制语言)的基本操作

文章目录 前言DCL(数据控制语言)用户管理查询用户创建用户修改用户密码删除用户 权限控制查询权限授予权限撤销权限 前言 在第六讲MySQL第6讲–DQL(数据查询语言)的基本操作之基本和条件查询和第七讲MySQL第7讲–DQL(数据查询语言)的基本操作中我们讲述了DQL中的&#xff0c;基…

高性能云桌面在工业设计中的应用,百度智能云云桌面给出答案

在之前的文章中我们讲到&#xff0c;边缘云桌面可以通过在距离用户较近的边缘节点上直接处理数据和应用&#xff0c;减少了数据传输时间&#xff0c;降低延迟&#xff0c;提高数据处理的速度和安全性。用户可以通过任何设备&#xff0c;随时随地访问自己的桌面环境&#xff0c;…

每日OJ_牛客HJ59 找出字符串中第一个只出现一次的字符

目录 牛客HJ59 找出字符串中第一个只出现一次的字符 解析代码 牛客HJ59 找出字符串中第一个只出现一次的字符 找出字符串中第一个只出现一次的字符_牛客题霸_牛客网 解析代码 可以用一个数组的每一个位置表示对应的位置。对应的字符位置存放字符出现的次数。统计完之后&…

【云备份】学习bundle库

文章目录 1.认识bundle⽂件压缩库bundle库实现⽂件压缩bundle实现文件解压缩 1.认识bundle⽂件压缩库 BundleBundle 是一个嵌入式压缩库&#xff0c;支持23种压缩算法和2种存档格式。使用的时候只需要加入两个文件 bundle.h 和 bundle.cpp 即可。 showcase #include <cass…

安全入门day.02

一、环境部署 1、windows版本 2、Linux版本 二、WEB应用组成角色名词 1、开发语言 2、程序源码 3、中间件容器 4、数据库 5、服务器操作系统 6、第三方软件 三、WEB应用安全漏洞分类 1、SQL注入 2、文件安全 3、RCE执行 4、XSS跨站 5、CSRF/SSRF/CRLF 6、反序列…