80 lines
1.7 KiB
TypeScript
80 lines
1.7 KiB
TypeScript
import { defineStore } from 'pinia'
|
|
|
|
import { computed, ref } from 'vue'
|
|
import { getModuleQxApi } from '@/request/api'
|
|
import { useUserStore } from './user'
|
|
|
|
const PERMISSION_PATH_MAP = [
|
|
{
|
|
moduleId: 'cw_zdorder_f',
|
|
path: '/pages-sub/fukuan',
|
|
},
|
|
{
|
|
moduleId: 'car_daily',
|
|
path: '/pages-sub/baoxiao',
|
|
}
|
|
]
|
|
|
|
export const usePermissionStore = defineStore(
|
|
'permission',
|
|
() => {
|
|
const userStore = useUserStore()
|
|
const modulePermissions = ref({})
|
|
|
|
async function init() {
|
|
if (!userStore.isLogined) {
|
|
return
|
|
}
|
|
|
|
if (Object.keys(modulePermissions.value).length > 0) {
|
|
return
|
|
}
|
|
|
|
const moduleQxRes = await getModuleQxApi()
|
|
|
|
if (moduleQxRes && moduleQxRes.data) {
|
|
console.log(moduleQxRes)
|
|
modulePermissions.value = moduleQxRes.data
|
|
}
|
|
}
|
|
|
|
function reset() {
|
|
modulePermissions.value = {}
|
|
}
|
|
|
|
function hasPermission(moduleid: string): boolean {
|
|
const val = modulePermissions.value?.[moduleid]
|
|
return val === '完全'
|
|
}
|
|
|
|
function checkPermissionByUrl(url?: string): boolean {
|
|
// 1、获取当前页面路径
|
|
if (!url) {
|
|
const pages = getCurrentPages()
|
|
const current = pages[pages.length - 1]
|
|
url = `/${current.route}`
|
|
}
|
|
|
|
// 2、匹配路径规则
|
|
const match = PERMISSION_PATH_MAP.find(item =>
|
|
url === item.path || url.startsWith(`${item.path}/`),
|
|
)
|
|
|
|
// 3、不在权限表 → 放行
|
|
if (!match)
|
|
return true
|
|
|
|
// 4、在权限表 → 必须满足“moduleId存在 + 值为完全”
|
|
return hasPermission(match.moduleId)
|
|
}
|
|
|
|
return {
|
|
modulePermissions,
|
|
init,
|
|
reset,
|
|
hasPermission,
|
|
checkPermissionByUrl,
|
|
}
|
|
},
|
|
)
|