first commit
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
import {create, type StateCreator} from 'zustand';
|
||||
import {createJSONStorage, devtools, persist} from 'zustand/middleware';
|
||||
import type {DictMap, DictStore, DictStoreActions, DictStoreState} from './types';
|
||||
import {getAllDict} from '@/api/system/sysDict';
|
||||
|
||||
const dictState: DictStoreState = {
|
||||
dictMap: {}
|
||||
};
|
||||
|
||||
const dictAction: StateCreator<DictStoreState, [], [], DictStoreActions> = (set, get) => ({
|
||||
initDict: async () => {
|
||||
try {
|
||||
const { data } = await getAllDict();
|
||||
const dictMap: DictMap = {};
|
||||
if (data.data) {
|
||||
data.data.forEach((dict) => {
|
||||
if (dict.code && dict.dict_items) {
|
||||
dictMap[dict.code] = dict.dict_items;
|
||||
}
|
||||
});
|
||||
}
|
||||
set({dictMap});
|
||||
} catch (error) {
|
||||
console.error('Failed to load dict data:', error);
|
||||
}
|
||||
},
|
||||
|
||||
getDictItem: (code, value) => {
|
||||
return (get().dictMap[code] || []).find(item => String(item.value) === String(value)) || null;
|
||||
},
|
||||
|
||||
getOptions: (code: string): { label: string; value: string }[] => {
|
||||
const items = get().dictMap[code] || [];
|
||||
return items.map((item) => ({
|
||||
label: item.label || '',
|
||||
value: item.value || '',
|
||||
}));
|
||||
},
|
||||
});
|
||||
|
||||
const useDictStore = create<DictStore>()(
|
||||
devtools(
|
||||
persist(
|
||||
(...args) => ({
|
||||
...dictState,
|
||||
...dictAction(...args),
|
||||
}),
|
||||
{
|
||||
name: 'dict-storage',
|
||||
storage: createJSONStorage(() => localStorage),
|
||||
}
|
||||
),
|
||||
{name: 'XinAdmin-Dict'}
|
||||
)
|
||||
);
|
||||
|
||||
export type {DictStore, DictStoreState, DictStoreActions, DictMap};
|
||||
export default useDictStore;
|
||||
@@ -0,0 +1,23 @@
|
||||
import type {IDictItem} from '@/domain/iDictItem';
|
||||
|
||||
|
||||
/** 字典 Map */
|
||||
export type DictMap = Record<string, IDictItem[]>;
|
||||
|
||||
/** 字典 Store 状态 */
|
||||
export interface DictStoreState {
|
||||
/** 字典数据映射 */
|
||||
dictMap: DictMap;
|
||||
}
|
||||
|
||||
/** 字典 Store 操作 */
|
||||
export interface DictStoreActions {
|
||||
/** 初始化字典数据 */
|
||||
initDict: () => Promise<void>;
|
||||
/** 根据编码获取字典 */
|
||||
getDictItem: (code: string, value?: string | number) => IDictItem | null;
|
||||
/** 获取 Select 选项格式数据 */
|
||||
getOptions: (code: string) => { label: string; value: string }[];
|
||||
}
|
||||
|
||||
export type DictStore = DictStoreState & DictStoreActions;
|
||||
@@ -0,0 +1,58 @@
|
||||
import {create, type StateCreator} from 'zustand';
|
||||
import {createJSONStorage, devtools, persist} from "zustand/middleware";
|
||||
import type {GlobalStoreState, GlobalStoreActions, GlobalStore} from "./types";
|
||||
import {configTheme, defaultColorTheme} from "@/layout/theme.ts";
|
||||
import type {LayoutType, ThemeProps} from "@/layout/typing.ts";
|
||||
import {getWebInfo} from "@/api";
|
||||
|
||||
const globalState: GlobalStoreState = {
|
||||
logo: "https://file.xinadmin.cn/file/favicons.ico",
|
||||
title: "Xin Admin",
|
||||
subtitle: "基于 Ant Design 的后台管理框架",
|
||||
describe: "Xin Admin 是一个基于 Ant Design 的后台管理框架",
|
||||
layout: "side",
|
||||
themeConfig: { ...defaultColorTheme, ...configTheme },
|
||||
themeDrawer: false,
|
||||
};
|
||||
|
||||
const globalAction: StateCreator<GlobalStoreState, [], [], GlobalStoreActions> = (set) => ({
|
||||
initWebInfo: async () => {
|
||||
try {
|
||||
const response = await getWebInfo();
|
||||
if (response.data.data) {
|
||||
set(response.data.data);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取网站信息失败:', error);
|
||||
}
|
||||
},
|
||||
setLayout: (layout: LayoutType) => {
|
||||
set({ layout });
|
||||
},
|
||||
setThemeConfig: (themeConfig: ThemeProps) => {
|
||||
set({ themeConfig });
|
||||
},
|
||||
|
||||
setThemeDrawer: (themeDrawer: boolean) => {
|
||||
set({ themeDrawer });
|
||||
},
|
||||
})
|
||||
|
||||
const useGlobalStore = create<GlobalStore>()(
|
||||
devtools(
|
||||
persist(
|
||||
(...args) => ({
|
||||
...globalState,
|
||||
...globalAction(...args),
|
||||
}),
|
||||
{
|
||||
name: 'global-storage',
|
||||
storage: createJSONStorage(() => localStorage),
|
||||
}
|
||||
),
|
||||
{ name: 'XinAdmin-Global' }
|
||||
)
|
||||
);
|
||||
|
||||
export type {GlobalStore, GlobalStoreState, GlobalStoreActions};
|
||||
export default useGlobalStore;
|
||||
@@ -0,0 +1,20 @@
|
||||
import type {LayoutType, ThemeProps} from "@/layout/typing.ts";
|
||||
|
||||
export interface GlobalStoreState {
|
||||
logo: string;
|
||||
title: string;
|
||||
subtitle: string;
|
||||
describe: string;
|
||||
layout: LayoutType;
|
||||
themeConfig: ThemeProps;
|
||||
themeDrawer: boolean;
|
||||
}
|
||||
|
||||
export interface GlobalStoreActions {
|
||||
setLayout: (layout: LayoutType) => void;
|
||||
initWebInfo: () => Promise<void>;
|
||||
setThemeConfig: (themeConfig: ThemeProps) => void;
|
||||
setThemeDrawer: (themeDrawer: boolean) => void;
|
||||
}
|
||||
|
||||
export type GlobalStore = GlobalStoreState & GlobalStoreActions;
|
||||
@@ -0,0 +1,51 @@
|
||||
import {create, type StateCreator} from 'zustand';
|
||||
import {createJSONStorage, devtools, persist} from "zustand/middleware";
|
||||
import {info, type InfoResponse, type LoginParams, logout} from "@/api/system/sys_user.ts";
|
||||
import {login} from "@/api/system/sys_user";
|
||||
import type {AuthStore, AuthStoreState, AuthStoreActions} from "./types";
|
||||
|
||||
const authState: AuthStoreState = {
|
||||
userinfo: {},
|
||||
access: [],
|
||||
};
|
||||
|
||||
const authAction: StateCreator<AuthStore, [], [], AuthStoreActions> = (set, get) => ({
|
||||
userId: () => get().userinfo.id,
|
||||
setAccess: (access) => set({access}),
|
||||
login: async (credentials: LoginParams) => {
|
||||
const { data } = await login(credentials);
|
||||
localStorage.setItem("token", data.data!.token);
|
||||
},
|
||||
logout: async () => {
|
||||
await logout();
|
||||
localStorage.removeItem("token");
|
||||
set(authState);
|
||||
},
|
||||
info: async () => {
|
||||
const result = await info();
|
||||
const data: InfoResponse = result.data.data!;
|
||||
set({
|
||||
userinfo: data.info,
|
||||
access: data.access,
|
||||
});
|
||||
},
|
||||
})
|
||||
|
||||
const useUserStore = create<AuthStore>()(
|
||||
devtools(
|
||||
persist(
|
||||
(...args) => ({
|
||||
...authState,
|
||||
...authAction(...args),
|
||||
}),
|
||||
{
|
||||
name: 'auth-storage',
|
||||
storage: createJSONStorage(() => localStorage),
|
||||
}
|
||||
),
|
||||
{ name: 'XinAdmin-Auth' }
|
||||
)
|
||||
);
|
||||
|
||||
export type {AuthStore, AuthStoreState, AuthStoreActions};
|
||||
export default useUserStore;
|
||||
@@ -0,0 +1,16 @@
|
||||
import type {LoginParams} from "@/api/system/sys_user";
|
||||
|
||||
export interface AuthStoreState {
|
||||
userinfo: any;
|
||||
access: any[];
|
||||
}
|
||||
|
||||
export interface AuthStoreActions {
|
||||
login: (credentials: LoginParams) => Promise<void>;
|
||||
logout: () => Promise<void>;
|
||||
userId: () => number;
|
||||
info: () => Promise<void>;
|
||||
setAccess: (assess: string[]) => void;
|
||||
}
|
||||
|
||||
export type AuthStore = AuthStoreState & AuthStoreActions;
|
||||
Reference in New Issue
Block a user