admin-vben/packages/effects/access/src/use-access.ts

53 lines
1.4 KiB
TypeScript
Raw Normal View History

import { computed } from 'vue';
import { preferences, updatePreferences } from '@vben/preferences';
import { useCoreAccessStore } from '@vben/stores';
function useAccess() {
const coreAccessStore = useCoreAccessStore();
const accessMode = computed(() => {
return preferences.app.accessMode;
});
/**
*
* @description: Determine whether there is permissionThe role is judged by the user's role
* @param roles
*/
2024-07-18 13:31:34 +00:00
function hasAccessByRoles(roles: string[]) {
2024-07-09 16:50:41 +00:00
const userRoleSet = new Set(coreAccessStore.userRoles);
const intersection = roles.filter((item) => userRoleSet.has(item));
return intersection.length > 0;
}
/**
*
* @description: Determine whether there is permissionThe permission code is judged by the user's permission code
* @param codes
*/
2024-07-18 13:31:34 +00:00
function hasAccessByCodes(codes: string[]) {
2024-07-09 16:50:41 +00:00
const userCodesSet = new Set(coreAccessStore.accessCodes);
const intersection = codes.filter((item) => userCodesSet.has(item));
return intersection.length > 0;
}
async function toggleAccessMode() {
updatePreferences({
app: {
accessMode:
preferences.app.accessMode === 'frontend' ? 'backend' : 'frontend',
},
});
}
return {
accessMode,
2024-07-18 13:31:34 +00:00
hasAccessByCodes,
hasAccessByRoles,
toggleAccessMode,
};
}
export { useAccess };