12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- //@ts-nocheck
- import { defineStore } from "pinia";
- import { getUserInfoByCode } from "@/api/login";
- import { useSystemStore } from "@/pinia/system-store";
- export declare type UserInfo = {
- avatar: string;
- token: string;
- sid: string;
- ip: string;
- menus: string[]; // 菜单列表,字符串数组
- code: string;
- codeRs: string;
- name: string;
- deptCode: string;
- deptName: string;
- ybCode: string;
- roles: number[]; // 角色列表,字符串数组
- partTimeDept: string[]; // 兼职部门列表,字符串数组
- partTimeDeptCode: string;
- partTimeDeptName: string;
- partTimeDeptMap: Record<string, string>; // 兼职部门映射,键为部门编码,值为部门名称
- };
- let initUserInfo = false;
- export const useUserStore = defineStore("user", {
- state: (): {
- userInfo: UserInfo;
- randomSid: string;
- } => ({
- userInfo: {
- avatar: "",
- token: "",
- sid: "",
- ip: "",
- menus: [],
- code: "",
- codeRs: "",
- name: "",
- deptCode: "",
- deptName: "",
- ybCode: "",
- roles: [],
- partTimeDept: [],
- partTimeDeptCode: "",
- partTimeDeptName: "",
- partTimeDeptMap: {},
- },
- randomSid: "",
- }),
- getters: {
- getUserInfo: async (state): Promise<boolean> => {
- if (initUserInfo) return true;
- if (localStorage.token !== null) {
- const userInfo = await getUserInfoByCode().catch(() => {
- return null;
- });
- if (userInfo !== null) {
- state.userInfo = userInfo;
- useSystemStore().setConfig(userInfo.userConfigJson);
- initUserInfo = true;
- return true;
- } else {
- return false;
- }
- }
- },
- getToken: () => {
- return localStorage.token;
- },
- getSid(state): string {
- return state.userInfo.sid + state.randomSid;
- },
- },
- actions: {
- setToken(val: string) {
- localStorage.token = val;
- },
- setUserInfo(val: UserInfo) {
- localStorage.token = val.token;
- this.userInfo = val;
- },
- setRandomSid(val: string) {
- this.randomSid = val;
- },
- },
- });
|