From ca2d313866680b3e218f7df34b22007b380a3306 Mon Sep 17 00:00:00 2001 From: ancong <12345678> Date: Wed, 22 Oct 2025 15:19:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=9D=83=E9=99=90=E5=AD=97?= =?UTF-8?q?=E7=AC=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.ts | 4 +++ src/store/user.ts | 5 ++-- src/utils/permission.js | 55 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 src/utils/permission.js diff --git a/src/main.ts b/src/main.ts index e9bce0d..91d875e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,6 +1,7 @@ import { createSSRApp } from 'vue' import App from './App.vue' import store from './store' +import { checkPermi, checkRole } from './utils/permission' // import store from './store' import dayjs from 'dayjs' import modal from './modal.ts' // plugins @@ -20,6 +21,9 @@ export function createApp() { app.config.globalProperties.$dayjs = dayjs app.config.globalProperties.$modal = modal app.config.globalProperties.$debouncemss = 600 //防抖时间 + app.config.globalProperties.$checkPermi = checkPermi + app.config.globalProperties.$checkRole = checkRole + app.use(uViewNext) // 注册组件库 app.use(store) diff --git a/src/store/user.ts b/src/store/user.ts index 3514586..9c9f186 100644 --- a/src/store/user.ts +++ b/src/store/user.ts @@ -203,7 +203,7 @@ export default defineStore( if (user.roles && user.roles.length > 0) { // 验证返回的roles是否是一个非空数组 userInfo.value.roles = user.roles - userInfo.value.permissions = user.permissions + //userInfo.value.permissions = user.permissions } else { userInfo.value.roles = ['ROLE_DEFAULT'] } @@ -214,7 +214,8 @@ export default defineStore( // } userInfo.value.name = user.nickName userInfo.value.avatar = avatar - + userInfo.value.permissions = user.permissions + storage.set(constant.permissions, user.permissions) storage.set(constant.name, user.nickName) storage.set(constant.avatar, userInfo.value.avatar) storage.set(constant.roles, userInfo.value.roles) diff --git a/src/utils/permission.js b/src/utils/permission.js new file mode 100644 index 0000000..796efda --- /dev/null +++ b/src/utils/permission.js @@ -0,0 +1,55 @@ +import useUserStore from '../store/user' + +/** + * 字符权限校验 + * @param {Array} value 校验值 + * @returns {Boolean} + */ +export function checkPermi(value) { + const userStore = useUserStore() + if (value && value instanceof Array && value.length > 0) { + // const permissions = store.getters && store.getters.permissions + const permissions = userStore.permissions && userStore.permissions + const permissionDatas = value + const all_permission = "*:*:*" + + const hasPermission = permissions.some(permission => { + return all_permission === permission || permissionDatas.includes(permission) + }) + + if (!hasPermission) { + return false + } + return true + } else { + console.error(`need roles! Like checkPermi="['system:user:add','system:user:edit']"`) + return false + } +} + +/** + * 角色权限校验 + * @param {Array} value 校验值 + * @returns {Boolean} + */ +export function checkRole(value) { + const userStore = useUserStore() + if (value && value instanceof Array && value.length > 0) { + //const roles = store.getters && store.getters.roles + const roles = userStore.roles && userStore.roles + const permissionRoles = value + const super_admin = "admin" + + const hasRole = roles.some(role => { + return super_admin === role || permissionRoles.includes(role) + }) + + if (!hasRole) { + return false + } + return true + } else { + console.error(`need roles! Like checkRole="['admin','editor']"`) + return false + } +} \ No newline at end of file