123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- import { userInfoApi } from "@/api/login";
- import { create } from "zustand";
- import { createJSONStorage, devtools, persist } from "zustand/middleware";
- interface State {
- userInfo: any;
- }
- interface Action {
- setUserInfo: (lang: State["userInfo"]) => void;
- reset: () => void;
- getUserInfo: () => void;
- }
- const initialState: State = {
- userInfo: "",
- };
- export const useUserInfoStore = create<State & Action>()(
- devtools(
- persist(
- (set) => {
- return {
- ...initialState,
- setUserInfo: (userInfo: State["userInfo"]) =>
- set({
- userInfo,
- }),
- reset: () => {
- set(initialState);
- },
- async getUserInfo() {
- const res = await userInfoApi();
- if (res?.data) {
- set({ userInfo: res?.data });
- }
- },
- };
- },
- {
- name: "globalStore",
- storage: createJSONStorage(() => localStorage),
- }
- ),
- { name: "globalStore" }
- )
- );
|