perf: Improve the use of store in the app

This commit is contained in:
vince
2024-07-05 23:15:46 +08:00
parent ad6ae1d21d
commit 8042e2b044
39 changed files with 387 additions and 224 deletions

View File

@@ -3,6 +3,8 @@ import type { RouteRecordRaw } from 'vue-router';
import type { GeneratorMenuAndRoutesOptions } from '../types';
import { cloneDepp } from '@vben-core/toolkit';
import { generateMenus } from './generate-menus';
import { generateRoutesByBackend } from './generate-routes-backend';
import { generateRoutesByFrontend } from './generate-routes-frontend';
@@ -12,6 +14,8 @@ async function generateMenusAndRoutes(
options: GeneratorMenuAndRoutesOptions,
) {
const { router } = options;
options.routes = cloneDepp(options.routes);
// 生成路由
const accessibleRoutes = await generateRoutes(mode, options);

View File

@@ -1,4 +1,4 @@
export { default as Authority } from './authority.vue';
export * from './generate-menu-and-routes';
export { default as RoleAuthority } from './role-authority.vue';
export type * from './types';
export * from './use-access';

View File

@@ -9,15 +9,15 @@ interface Props {
* - When the permission mode is 'backend', the value can be a code permission value.
* @default ''
*/
value?: string[];
roles?: string[];
}
defineOptions({
name: 'Authority',
name: 'FrontendAuthority',
});
withDefaults(defineProps<Props>(), {
value: undefined,
roles: undefined,
});
</script>

View File

@@ -1,28 +1,13 @@
import { computed } from 'vue';
import { preferences } from '@vben-core/preferences';
import { useAccessStore } from '@vben-core/stores';
function useAccess() {
const accessStore = useAccessStore();
const currentAccessMode = computed(() => {
const accessMode = computed(() => {
return preferences.app.accessMode;
});
/**
* 更改账号角色
* @param roles
*/
async function changeRoles(roles: string[]): Promise<void> {
if (preferences.app.accessMode !== 'frontend') {
throw new Error(
'The current access mode is not frontend, so the role cannot be changed',
);
}
accessStore.setUserRoles(roles);
}
return { changeRoles, currentAccessMode };
return { accessMode };
}
export { useAccess };