+
[
// { id: '', name: '全部工程项目' }, // 添加空选项
...userStore.projects
]);
+const proxy = getCurrentInstance()?.proxy as any;
const selectedProjectId = ref(userStore.selectedProject?.id || '');
@@ -37,13 +43,60 @@ watch(
{ deep: true }
);
-const handleSelect = (projectId: string) => {
+/** 切换项目逻辑 */
+const handleSelect = async (projectId: string) => {
+ proxy.$cache.local.setJSON('isCheckRole', 'true');
+
+ const userStore = useUserStore();
+ const permissionStore = usePermissionStore();
const selectedProject = projects.value.find((p) => p.id === projectId);
- if (selectedProject) {
- userStore.setSelectedProject(selectedProject);
- console.log(userStore.selectedProject); // 打印选中的项目
- }
+ if (!selectedProject) return;
+ const loadingInstance = ElLoading.service({
+ lock: true,
+ text: '项目切换中...',
+ background: 'rgba(0, 0, 0, 0.7)'
+ });
+ await changeProject(projectId);
+
+ // 更新项目 & 权限
+ userStore.setSelectedProject(selectedProject);
+ await userStore.setInfo();
+ await userStore.setRoles(); // 这里会刷新 permissions/roles
+ // 重新生成路由
+ permissionStore.generateRoutes().then((routeList) => {
+ const currentPath = router.currentRoute.value.fullPath;
+ const exist = currentPath == '/' || currentPath == '/index' ? true : routeExists(currentPath, routeList);
+ if (exist) return loadingInstance.close();
+
+ proxy?.$tab.closeAllPage();
+ router.push('/index');
+ loadingInstance.close();
+
+ // 刷新当前路由
+ });
};
+function routeExists(fullPath: string, routes: any[], parentPath = ''): boolean {
+ for (const route of routes) {
+ // 拼接完整 path
+ let currentPath = route.path.startsWith('/') ? route.path : `${parentPath}/${route.path}`;
+
+ // 处理多余的 "//"
+ currentPath = currentPath.replace(/\/+/g, '/');
+
+ // 判断
+ if (currentPath === fullPath) {
+ return true;
+ }
+
+ // 递归子路由
+ if (route.children && route.children.length > 0) {
+ if (routeExists(fullPath, route.children, currentPath)) {
+ return true;
+ }
+ }
+ }
+ return false;
+}