权限重构
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
<template>
|
||||
<div class="select-container">
|
||||
<div class="select-container" v-loading.fullscreen.lock="fullscreenLoading">
|
||||
<label for="projectSelect" class="select-label">项目列表:</label>
|
||||
<el-select
|
||||
id="projectSelect"
|
||||
@ -19,12 +19,18 @@
|
||||
import { ref, computed, watch } from 'vue';
|
||||
import { useUserStore } from '@/store/modules/user';
|
||||
import { getProjectTeam } from '@/utils/projectTeam';
|
||||
|
||||
import router, { resetRouter } from '@/router';
|
||||
import usePermissionStore from '@/store/modules/permission';
|
||||
import { isHttp } from '@/utils/validate';
|
||||
import { changeProject } from '@/api/project/project';
|
||||
const fullscreenLoading = ref(false);
|
||||
const route = useRoute();
|
||||
const userStore = useUserStore();
|
||||
const projects = computed(() => [
|
||||
// { 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;
|
||||
}
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
|
Reference in New Issue
Block a user