增加权限字符
This commit is contained in:
parent
b04e2e4253
commit
ca2d313866
|
|
@ -1,6 +1,7 @@
|
||||||
import { createSSRApp } from 'vue'
|
import { createSSRApp } from 'vue'
|
||||||
import App from './App.vue'
|
import App from './App.vue'
|
||||||
import store from './store'
|
import store from './store'
|
||||||
|
import { checkPermi, checkRole } from './utils/permission'
|
||||||
// import store from './store'
|
// import store from './store'
|
||||||
import dayjs from 'dayjs'
|
import dayjs from 'dayjs'
|
||||||
import modal from './modal.ts' // plugins
|
import modal from './modal.ts' // plugins
|
||||||
|
|
@ -20,6 +21,9 @@ export function createApp() {
|
||||||
app.config.globalProperties.$dayjs = dayjs
|
app.config.globalProperties.$dayjs = dayjs
|
||||||
app.config.globalProperties.$modal = modal
|
app.config.globalProperties.$modal = modal
|
||||||
app.config.globalProperties.$debouncemss = 600 //防抖时间
|
app.config.globalProperties.$debouncemss = 600 //防抖时间
|
||||||
|
app.config.globalProperties.$checkPermi = checkPermi
|
||||||
|
app.config.globalProperties.$checkRole = checkRole
|
||||||
|
|
||||||
app.use(uViewNext) // 注册组件库
|
app.use(uViewNext) // 注册组件库
|
||||||
|
|
||||||
app.use(store)
|
app.use(store)
|
||||||
|
|
|
||||||
|
|
@ -203,7 +203,7 @@ export default defineStore(
|
||||||
if (user.roles && user.roles.length > 0) {
|
if (user.roles && user.roles.length > 0) {
|
||||||
// 验证返回的roles是否是一个非空数组
|
// 验证返回的roles是否是一个非空数组
|
||||||
userInfo.value.roles = user.roles
|
userInfo.value.roles = user.roles
|
||||||
userInfo.value.permissions = user.permissions
|
//userInfo.value.permissions = user.permissions
|
||||||
} else {
|
} else {
|
||||||
userInfo.value.roles = ['ROLE_DEFAULT']
|
userInfo.value.roles = ['ROLE_DEFAULT']
|
||||||
}
|
}
|
||||||
|
|
@ -214,7 +214,8 @@ export default defineStore(
|
||||||
// }
|
// }
|
||||||
userInfo.value.name = user.nickName
|
userInfo.value.name = user.nickName
|
||||||
userInfo.value.avatar = avatar
|
userInfo.value.avatar = avatar
|
||||||
|
userInfo.value.permissions = user.permissions
|
||||||
|
storage.set(constant.permissions, user.permissions)
|
||||||
storage.set(constant.name, user.nickName)
|
storage.set(constant.name, user.nickName)
|
||||||
storage.set(constant.avatar, userInfo.value.avatar)
|
storage.set(constant.avatar, userInfo.value.avatar)
|
||||||
storage.set(constant.roles, userInfo.value.roles)
|
storage.set(constant.roles, userInfo.value.roles)
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue