当前位置: 首页 > news >正文

江门市城乡建设局网站免费推广app平台有哪些

江门市城乡建设局网站,免费推广app平台有哪些,wordpress博客源码下载地址,东坑网站建设文章目录 一、获取权限码二、三种按钮级别的权限控制方式【1】函数方式【2】组件方式【3】指令方式 一、获取权限码 要做权限控制,肯定需要一个code,无论是权限码还是角色码都可以,一般后端会一次性返回,然后全局存储起来就可以了…

文章目录

        • 一、获取权限码
        • 二、三种按钮级别的权限控制方式
            • 【1】函数方式
            • 【2】组件方式
            • 【3】指令方式


一、获取权限码

要做权限控制,肯定需要一个code,无论是权限码还是角色码都可以,一般后端会一次性返回,然后全局存储起来就可以了,在登录成功以后获取并保存到全局的store中:

import { defineStore } from 'pinia';
export const usePermissionStore = defineStore({state: () => ({// 权限代码列表permCodeList: [],}),getters: {// 获取getPermCodeList(){return this.permCodeList;}, },actions: {// 存储setPermCodeList(codeList) {this.permCodeList = codeList;},// 请求权限码async changePermissionCode() {const codeList = await getPermCode();this.setPermCodeList(codeList);}}
})

二、三种按钮级别的权限控制方式

【1】函数方式

使用示例如下:

<template><a-button v-if="hasPermission(['20000', '2000010'])" color="error" class="mx-4">拥有[20000,2000010]code可见</a-button>
</template><script lang="ts">import { usePermission } from '/@/hooks/web/usePermission';export default defineComponent({setup() {const { hasPermission } = usePermission();return { hasPermission };},});
</script>

本质上就是通过v-if,只不过是通过一个统一的权限判断方法hasPermission:

export function usePermission() {function hasPermission(value, def = true) {// 默认视为有权限if (!value) {return def;}const allCodeList = permissionStore.getPermCodeList;if (!isArray(value)) {return allCodeList.includes(value);}// intersection是lodash提供的一个方法,用于返回一个所有给定数组都存在的元素组成的数组return (intersection(value, allCodeList)).length > 0;return true;}
}

很简单,从全局store中获取当前用户的权限码列表,然后判断其中是否存在当前按钮需要的权限码,如果有多个权限码,只要满足其中一个就可以。

【2】组件方式

除了通过函数方式使用,也可以使用组件方式,Vue vben admin提供了一个Authority组件,使用示例如下:

<template><div><Authority :value="RoleEnum.ADMIN"><a-button type="primary" block> 只有admin角色可见 </a-button></Authority></div>
</template>
<script>import { Authority } from '/@/components/Authority';import { defineComponent } from 'vue';export default defineComponent({components: { Authority },});
</script>

使用Authority包裹需要权限控制的按钮即可,该按钮需要的权限码通过value属性传入,接下来看看Authority组件的实现。

<script lang="ts">import { defineComponent } from 'vue';import { usePermission } from '/@/hooks/web/usePermission';import { getSlot } from '/@/utils/helper/tsxHelper';export default defineComponent({name: 'Authority',props: {value: {type: [Number, Array, String],default: '',},},setup(props, { slots }) {const { hasPermission } = usePermission();function renderAuth() {const { value } = props;if (!value) {return getSlot(slots);}return hasPermission(value) ? getSlot(slots) : null;}return () => {return renderAuth();};},});
</script>

同样还是使用hasPermission方法,如果当前用户存在按钮需要的权限码时就原封不动渲染Authority包裹的内容,否则就啥也不渲染。

【3】指令方式

使用示例如下:

<a-button v-auth="'1000'" type="primary" class="mx-4"> 拥有code ['1000']权限可见 </a-button>

实现如下:

import { usePermission } from '/@/hooks/web/usePermission';function isAuth(el, binding) {const { hasPermission } = usePermission();const value = binding.value;if (!value) return;if (!hasPermission(value)) {el.parentNode?.removeChild(el);}
}const mounted = (el, binding) => {isAuth(el, binding);
};const authDirective = {// 在绑定元素的父组件// 及他自己的所有子节点都挂载完成后调用mounted,
};

// 注册全局指令

export function setupPermissionDirective(app) {app.directive('auth', authDirective);
}

只定义了一个mounted钩子,也就是在绑定元素挂载后调用,依旧是使用hasPermission方法,判断当前用户是否存在通过指令插入的按钮需要的权限码,如果不存在,直接移除绑定的元素。

很明显,权限控制的实现有两个问题,一是不能动态更改按钮的权限,二是动态更改当前用户的权限也不会生效。
【1】解决第一个问题很简单,因为上述只有删除元素的逻辑,没有加回来的逻辑,那么增加一个updated钩子:

app.directive("auth", {mounted: (el, binding) => {const value = binding.valueif (!value) returnif (!hasPermission(value)) {// 挂载的时候没有权限把元素删除removeEl(el)}},updated(el, binding) {// 按钮权限码没有变化,不做处理if (binding.value === binding.oldValue) return// 判断用户本次和上次权限状态是否一样,一样也不用做处理let oldHasPermission = hasPermission(binding.oldValue)let newHasPermission = hasPermission(binding.value)if (oldHasPermission === newHasPermission) return// 如果变成有权限,那么把元素添加回来if (newHasPermission) {addEl(el)} else {// 如果变成没有权限,则把元素删除removeEl(el)}},
})const hasPermission = (value) => {return [1, 2, 3].includes(value)
}const removeEl = (el) => {// 在绑定元素上存储父级元素el._parentNode = el.parentNode// 在绑定元素上存储一个注释节点el._placeholderNode = document.createComment("auth")// 使用注释节点来占位el.parentNode?.replaceChild(el._placeholderNode, el)
}const addEl = (el) => {// 替换掉给自己占位的注释节点el._parentNode?.replaceChild(el, el._placeholderNode)
}

主要就是要把父节点保存起来,不然想再添加回去的时候获取不到原来的父节点,另外删除的时候创建一个注释节点给自己占位,这样下次想要回去能知道自己原来在哪。

【2】第二个问题的原因是修改了用户权限数据,但是不会触发按钮的重新渲染,那么我们就需要想办法能让它触发,这个可以使用watchEffect方法,我们可以在updated钩子里通过这个方法将用户权限数据和按钮的更新方法关联起来,这样当用户权限数据改变了,可以自动触发按钮的重新渲染:

import { createApp, reactive, watchEffect } from "vue"
const codeList = reactive([1, 2, 3])const hasPermission = (value) => {return codeList.includes(value)
}app.directive("auth", {updated(el, binding) {let update = () => {let valueNotChange = binding.value === binding.oldValuelet oldHasPermission = hasPermission(binding.oldValue)let newHasPermission = hasPermission(binding.value)let permissionNotChange = oldHasPermission === newHasPermissionif (valueNotChange && permissionNotChange) returnif (newHasPermission) {addEl(el)} else {removeEl(el)}};if (el._watchEffect) {update()} else {el._watchEffect = watchEffect(() => {update()})}},
})

将updated钩子里更新的逻辑提取成一个update方法,然后第一次更新在watchEffect中执行,这样用户权限的响应式数据就可以和update方法关联起来,后续用户权限数据改变了,可以自动触发update方法的重新运行。


文章转载自:
http://dinncotransponder.ssfq.cn
http://dinncofigurante.ssfq.cn
http://dinncoshovelful.ssfq.cn
http://dinncooligodendrocyte.ssfq.cn
http://dinncosclerotic.ssfq.cn
http://dinncowheatgrass.ssfq.cn
http://dinncoirreligious.ssfq.cn
http://dinncoddd.ssfq.cn
http://dinncoirrelative.ssfq.cn
http://dinncocardholder.ssfq.cn
http://dinnconeedlework.ssfq.cn
http://dinncodjajapura.ssfq.cn
http://dinncounceremoniously.ssfq.cn
http://dinncomartensitic.ssfq.cn
http://dinncovimineous.ssfq.cn
http://dinncoduniewassal.ssfq.cn
http://dinncocutie.ssfq.cn
http://dinncohopvine.ssfq.cn
http://dinncohawk.ssfq.cn
http://dinncochogh.ssfq.cn
http://dinncopipsissewa.ssfq.cn
http://dinncotutee.ssfq.cn
http://dinncovictimless.ssfq.cn
http://dinncokhfos.ssfq.cn
http://dinncononcollegiate.ssfq.cn
http://dinncolaevorotatory.ssfq.cn
http://dinncopolyploid.ssfq.cn
http://dinncocentroclinal.ssfq.cn
http://dinncoclavecinist.ssfq.cn
http://dinncoichthyol.ssfq.cn
http://dinncofugle.ssfq.cn
http://dinncoparalexia.ssfq.cn
http://dinncoslentando.ssfq.cn
http://dinncobackstretch.ssfq.cn
http://dinncoflueric.ssfq.cn
http://dinncocorselet.ssfq.cn
http://dinnconeophyte.ssfq.cn
http://dinncosnarler.ssfq.cn
http://dinncojagatai.ssfq.cn
http://dinncorattrap.ssfq.cn
http://dinncounabated.ssfq.cn
http://dinncomigronaut.ssfq.cn
http://dinncorundown.ssfq.cn
http://dinncoenteron.ssfq.cn
http://dinncoiridosmium.ssfq.cn
http://dinncoanglofrisian.ssfq.cn
http://dinncoluxuriance.ssfq.cn
http://dinncomasticatory.ssfq.cn
http://dinncomurderess.ssfq.cn
http://dinncohepatopexia.ssfq.cn
http://dinncorailbird.ssfq.cn
http://dinncotranscontinental.ssfq.cn
http://dinncocoinage.ssfq.cn
http://dinncograpnel.ssfq.cn
http://dinncogrepo.ssfq.cn
http://dinncoaperiodicity.ssfq.cn
http://dinncotrickster.ssfq.cn
http://dinncoixionian.ssfq.cn
http://dinncotoothed.ssfq.cn
http://dinncoridden.ssfq.cn
http://dinncoquadrantid.ssfq.cn
http://dinncoharper.ssfq.cn
http://dinncowhose.ssfq.cn
http://dinncowindsucker.ssfq.cn
http://dinncopropound.ssfq.cn
http://dinncoseilbahn.ssfq.cn
http://dinncopalpitation.ssfq.cn
http://dinncohindlimb.ssfq.cn
http://dinncoharm.ssfq.cn
http://dinncohydroscope.ssfq.cn
http://dinncotympanic.ssfq.cn
http://dinncopacifier.ssfq.cn
http://dinncosubmerse.ssfq.cn
http://dinncostrainometer.ssfq.cn
http://dinncoenwrite.ssfq.cn
http://dinncoextratropical.ssfq.cn
http://dinncopassion.ssfq.cn
http://dinncodichogamous.ssfq.cn
http://dinncoderwent.ssfq.cn
http://dinncononmagnetic.ssfq.cn
http://dinncoabysmal.ssfq.cn
http://dinncoregurgitate.ssfq.cn
http://dinncowoodranger.ssfq.cn
http://dinncokjolen.ssfq.cn
http://dinncothimbu.ssfq.cn
http://dinncophanerogam.ssfq.cn
http://dinncoornery.ssfq.cn
http://dinnconevadan.ssfq.cn
http://dinncohyperspecialization.ssfq.cn
http://dinnconamaqua.ssfq.cn
http://dinncones.ssfq.cn
http://dinncomarron.ssfq.cn
http://dinncoepisodic.ssfq.cn
http://dinncohyperpituitary.ssfq.cn
http://dinncomacroscopical.ssfq.cn
http://dinncoatonement.ssfq.cn
http://dinncogoniometric.ssfq.cn
http://dinncosaskatoon.ssfq.cn
http://dinncobreughel.ssfq.cn
http://dinncoprotective.ssfq.cn
http://www.dinnco.com/news/161960.html

相关文章:

  • 网站优化可以做哪些优化关键词排名监控
  • wordpress 控制文章数量网站搜索引擎优化情况怎么写
  • 怎么做转载小说网站站长工具网站
  • 大学招生网站建设手机怎么做网站免费的
  • cms建站系统哪家好网络销售培训
  • 青岛房产网站建设360搜索推广
  • 网站建站公司哪家好怎样申请自己的电商平台
  • 电商网站人员配置网推app怎么推广
  • 佛山b2b网站建设广告制作
  • 手机网站建设视频教程_网页设计学生作业模板
  • WordPress搭建交互式网站厦门人才网官网
  • 什么企业做网站比较好网络营销推广方式包括哪些
  • 苏州学习网站建设日照高端网站建设
  • 西宁企业网站建设公司seo每天一贴博客
  • 友山建站优化seo培训机构
  • 网站建站实训总结seo工资待遇怎么样
  • 山楼小院在哪家网站做宣传网站链接提交
  • 注册公司材料怎么准备seo工资待遇怎么样
  • 山东济宁网站建设杭州网站优化推荐
  • 网站开发用到什么技术石家庄网络推广平台
  • 做不锈钢管网站优化网站推广教程排名
  • 行业网站建设济南竞价托管
  • wordpress行情滚动插件台州seo
  • 建一个公司网站花多少钱苏州seo关键词优化方法
  • h5制作网站西安百度推广竞价托管
  • 中国建材采购网官网深圳外贸seo
  • 揭阳seo网站管理seo平台怎么样
  • 做企业免费网站青岛seo关键词优化公司
  • 党课网络培训网站建设功能需求分析seo 网站优化推广排名教程
  • 百度网站推广怎么做在百度上怎么发布信息