修复权限缓存问题
This commit is contained in:
39
wuye-manager-uniapp/stores/usePermStore.js
Normal file
39
wuye-manager-uniapp/stores/usePermStore.js
Normal file
@@ -0,0 +1,39 @@
|
||||
import {
|
||||
defineStore
|
||||
} from 'pinia'
|
||||
export const usePermStore = defineStore('perm', {
|
||||
state: () => ({
|
||||
permList: []
|
||||
}),
|
||||
getters: {
|
||||
// 封装权限判断,页面直接调用
|
||||
hasPermi: (state) => (perm, mode = 'or') => {
|
||||
const list = state.permList
|
||||
// 超级管理员
|
||||
if (list.includes('*')) return true
|
||||
const checkArr = Array.isArray(perm) ? perm : [perm]
|
||||
if (mode === 'and') {
|
||||
return checkArr.every(item => list.includes(item))
|
||||
} else {
|
||||
return checkArr.some(item => list.includes(item))
|
||||
}
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
// 初始化:页面onShow时从本地缓存加载权限(冷启动用)
|
||||
initPerm() {
|
||||
const perms = uni.getStorageSync('userPermission')
|
||||
this.permList = Array.isArray(perms) ? perms : []
|
||||
},
|
||||
// 登录成功:设置新权限,同步storage
|
||||
setPerm(list) {
|
||||
this.permList = list
|
||||
uni.setStorageSync('userPermission', list)
|
||||
},
|
||||
// 退出/切换账号:清空内存+本地缓存
|
||||
clearPerm() {
|
||||
this.permList = []
|
||||
uni.removeStorageSync('userPermission')
|
||||
}
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user