From 58a3af8c84fc34128833389ad214ee67411badc1 Mon Sep 17 00:00:00 2001 From: LinkLi <317842744@qq.com> Date: Sat, 11 Mar 2023 01:59:27 +0000 Subject: [PATCH] =?UTF-8?q?!22=20issue#I6KY0M=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E5=BB=BA=E8=AE=AE=20*=20feat:=20=E5=A2=9E=E5=8A=A0=E5=85=A8?= =?UTF-8?q?=E5=B1=80=E6=9D=83=E9=99=90=E5=88=A4=E6=96=AD=E5=87=BD=E6=95=B0?= =?UTF-8?q?#I6KY0M?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/permission.ts | 45 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/utils/permission.ts diff --git a/src/utils/permission.ts b/src/utils/permission.ts new file mode 100644 index 00000000..a63ee628 --- /dev/null +++ b/src/utils/permission.ts @@ -0,0 +1,45 @@ +import { CACHE_KEY, useCache } from '@/hooks/web/useCache' + +const { t } = useI18n() // 国际化 + +/** + * 字符权限校验 + * @param {Array} value 校验值 + * @returns {Boolean} + */ +export function checkPermi(value: string[]) { + if (value && value instanceof Array && value.length > 0) { + const { wsCache } = useCache() + const permissionDatas = value + const all_permission = '*:*:*' + const permissions = wsCache.get(CACHE_KEY.USER).permissions + const hasPermission = permissions.some((permission) => { + return all_permission === permission || permissionDatas.includes(permission) + }) + return !!hasPermission + } else { + console.error(t('permission.hasPermission')) + return false + } +} + +/** + * 角色权限校验 + * @param {string[]} value 校验值 + * @returns {Boolean} + */ +export function checkRole(value: string[]) { + if (value && value instanceof Array && value.length > 0) { + const { wsCache } = useCache() + const permissionRoles = value + const super_admin = 'admin' + const roles = wsCache.get(CACHE_KEY.USER).roles + const hasRole = roles.some((role) => { + return super_admin === role || permissionRoles.includes(role) + }) + return !!hasRole + } else { + console.error(t('permission.hasRole')) + return false + } +}