2024-06-30 07:03:37 +00:00
|
|
|
|
import { computed } from 'vue';
|
|
|
|
|
|
2024-07-22 16:03:59 +00:00
|
|
|
|
import { preferences, updatePreferences } from '@vben/preferences';
|
2024-07-30 13:10:28 +00:00
|
|
|
|
import { useAccessStore, useUserStore } from '@vben/stores';
|
2024-06-30 07:03:37 +00:00
|
|
|
|
|
|
|
|
|
function useAccess() {
|
2024-07-30 13:10:28 +00:00
|
|
|
|
const accessStore = useAccessStore();
|
|
|
|
|
const userStore = useUserStore();
|
2024-07-05 15:15:46 +00:00
|
|
|
|
const accessMode = computed(() => {
|
2024-06-30 07:03:37 +00:00
|
|
|
|
return preferences.app.accessMode;
|
|
|
|
|
});
|
|
|
|
|
|
2024-07-05 15:51:50 +00:00
|
|
|
|
/**
|
|
|
|
|
* 基于角色判断是否有权限
|
|
|
|
|
* @description: Determine whether there is permission,The role is judged by the user's role
|
|
|
|
|
* @param roles
|
|
|
|
|
*/
|
2024-07-18 13:31:34 +00:00
|
|
|
|
function hasAccessByRoles(roles: string[]) {
|
2024-07-30 13:10:28 +00:00
|
|
|
|
const userRoleSet = new Set(userStore.userRoles);
|
2024-07-05 15:51:50 +00:00
|
|
|
|
const intersection = roles.filter((item) => userRoleSet.has(item));
|
|
|
|
|
return intersection.length > 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-06 05:28:08 +00:00
|
|
|
|
/**
|
|
|
|
|
* 基于权限码判断是否有权限
|
|
|
|
|
* @description: Determine whether there is permission,The 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-30 13:10:28 +00:00
|
|
|
|
const userCodesSet = new Set(accessStore.accessCodes);
|
2024-07-06 05:28:08 +00:00
|
|
|
|
|
|
|
|
|
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,
|
2024-07-06 05:28:08 +00:00
|
|
|
|
toggleAccessMode,
|
|
|
|
|
};
|
2024-06-30 07:03:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export { useAccess };
|