feat: 逻辑调整

This commit is contained in:
rd
2025-09-11 16:08:28 +08:00
parent 7e6e086098
commit b5abdb0887
9 changed files with 279 additions and 153 deletions

View File

@ -1,113 +1,123 @@
import router from '@/router';
import { defineStore } from 'pinia';
import { fetchProfileInfo } from '@/api/all/login';
// import { useSidebarStore } from '@/stores/modules/side-bar';
import { glsWithCatch, slsWithCatch, rlsWithCatch } from '@/utils/stroage';
interface UserInfo {
id: number;
name: string;
head_image: String;
current_enterprise_id: number;
mobile: string;
primary_enterprise: any; // 主企业信息
[key: string]: any;
// 添加其他用户属性...
}
interface UserState {
token: string;
userInfo: UserInfo | null;
// allowAccessRoutes: string[];
// isLogin: boolean;
}
interface UserInfo {
id: number;
mobile: string;
name: string;
}
export const useUserStore = defineStore('user', {
state: (): UserState => ({
token: glsWithCatch('accessToken') || '',
userInfo: (glsWithCatch('userInfo') && JSON.parse(glsWithCatch('userInfo') as string)) || null,
// allowAccessRoutes:
// (glsWithCatch('allowAccessRoutes') && JSON.parse(glsWithCatch('allowAccessRoutes') as string)) || [], // 允许访问的路由列表
}),
getters: {
isLogin(): boolean {
return !!this.token;
},
},
actions: {
// 设置 Token
setToken(token: string) {
this.token = `Bearer ${token}`;
slsWithCatch('accessToken', this.token);
},
clearToken() {
this.token = '';
rlsWithCatch('accessToken');
},
// 获取 Token
getToken() {
return this.token;
},
// 设置用户信息
setUserInfo(userInfo: UserInfo | null) {
this.userInfo = userInfo;
slsWithCatch('userInfo', JSON.stringify(userInfo));
},
clearUserInfo() {
this.userInfo = null;
rlsWithCatch('userInfo');
},
// 获取用户信息
async getUserInfo() {
const { code, data } = await fetchProfileInfo();
if (code === 200) {
this.setUserInfo(data);
}
},
// clearUserAllowAccessRoutes() {
// this.allowAccessRoutes = [];
// rlsWithCatch('allowAccessRoutes');
// },
// getUserAllowAccessRoutes() {
// const sidebarStore = useSidebarStore();
// const menuList = sidebarStore.menuList;
// const appRoutes = router.getRoutes();
// appRoutes.forEach((route: any) => {
// if (!route.meta?.requiresAuth) {
// this.allowAccessRoutes.push(route.name);
// }
// });
// const pushAllowAccessRoutes = (includeRouteNames: string[]) => {
// const matchedRoute = appRoutes
// .filter((route: any) => includeRouteNames.includes(route.name))
// .map((route: any) => route.name);
// this.allowAccessRoutes.push(...matchedRoute);
// };
// menuList.forEach((item) => {
// if (item.children && item.children.length > 0) {
// item.children.forEach((child: any) => {
// pushAllowAccessRoutes(child.includeRouteNames);
// });
// } else {
// pushAllowAccessRoutes(item.includeRouteNames);
// }
// });
// this.allowAccessRoutes = uniq(this.allowAccessRoutes);
// slsWithCatch('allowAccessRoutes', JSON.stringify(this.allowAccessRoutes));
// },
},
});
import { defineStore } from 'pinia';
import { fetchProfileInfo } from '@/api/all/login';
// import { useSidebarStore } from '@/stores/modules/side-bar';
import { glsWithCatch, rlsWithCatch, slsWithCatch } from '@/utils/stroage';
import { useEnterpriseStore } from '@/stores/modules/enterprise';
interface UserInfo {
id: number;
name: string;
head_image: String;
current_enterprise_id: number;
mobile: string;
primary_enterprise: any; // 主企业信息
[key: string]: any;
// 添加其他用户属性...
}
interface UserState {
token: string;
userInfo: UserInfo | null;
// allowAccessRoutes: string[];
// isLogin: boolean;
}
interface UserInfo {
id: number;
mobile: string;
name: string;
}
export const useUserStore = defineStore('user', {
state: (): UserState => ({
token: glsWithCatch('accessToken') || '',
userInfo: (glsWithCatch('userInfo') && JSON.parse(glsWithCatch('userInfo') as string)) || null,
// allowAccessRoutes:
// (glsWithCatch('allowAccessRoutes') && JSON.parse(glsWithCatch('allowAccessRoutes') as string)) || [], // 允许访问的路由列表
}),
getters: {
isLogin(): boolean {
return !!this.token;
},
},
actions: {
// 设置 Token
setToken(token: string) {
this.token = `Bearer ${token}`;
slsWithCatch('accessToken', this.token);
},
clearToken() {
this.token = '';
rlsWithCatch('accessToken');
},
// 获取 Token
getToken() {
return this.token;
},
// 设置用户信息
setUserInfo(userInfo: UserInfo | null) {
this.userInfo = userInfo;
slsWithCatch('userInfo', JSON.stringify(userInfo));
},
clearUserInfo() {
this.userInfo = null;
rlsWithCatch('userInfo');
},
// 获取用户信息
async getUserInfo() {
const enterpriseStore = useEnterpriseStore();
const { code, data } = await fetchProfileInfo();
if (code === 200) {
this.setUserInfo(data);
const { primary_enterprise } = data;
if (!enterpriseStore.enterpriseInfo && !isEmpty(primary_enterprise)) {
console.log('setEnterpriseInfo');
enterpriseStore.setEnterpriseInfo(primary_enterprise);
}
}
},
// clearUserAllowAccessRoutes() {
// this.allowAccessRoutes = [];
// rlsWithCatch('allowAccessRoutes');
// },
// getUserAllowAccessRoutes() {
// const sidebarStore = useSidebarStore();
// const menuList = sidebarStore.menuList;
// const appRoutes = router.getRoutes();
// appRoutes.forEach((route: any) => {
// if (!route.meta?.requiresAuth) {
// this.allowAccessRoutes.push(route.name);
// }
// });
// const pushAllowAccessRoutes = (includeRouteNames: string[]) => {
// const matchedRoute = appRoutes
// .filter((route: any) => includeRouteNames.includes(route.name))
// .map((route: any) => route.name);
// this.allowAccessRoutes.push(...matchedRoute);
// };
// menuList.forEach((item) => {
// if (item.children && item.children.length > 0) {
// item.children.forEach((child: any) => {
// pushAllowAccessRoutes(child.includeRouteNames);
// });
// } else {
// pushAllowAccessRoutes(item.includeRouteNames);
// }
// });
// this.allowAccessRoutes = uniq(this.allowAccessRoutes);
// slsWithCatch('allowAccessRoutes', JSON.stringify(this.allowAccessRoutes));
// },
},
});