|
|
@ -1,6 +1,7 @@
|
|
|
|
import { produce } from 'immer';
|
|
|
|
import { produce } from 'immer';
|
|
|
|
import { setCookie } from 'nookies';
|
|
|
|
import { setCookie } from 'nookies';
|
|
|
|
import { create } from 'zustand';
|
|
|
|
import { create } from 'zustand';
|
|
|
|
|
|
|
|
import { devtools, persist } from 'zustand/middleware';
|
|
|
|
|
|
|
|
|
|
|
|
import { apiGetMyUserInfo, apiUserLogin } from '@/services';
|
|
|
|
import { apiGetMyUserInfo, apiUserLogin } from '@/services';
|
|
|
|
|
|
|
|
|
|
|
@ -19,54 +20,63 @@ interface UserState {
|
|
|
|
getUserInfo: () => Promise<boolean>;
|
|
|
|
getUserInfo: () => Promise<boolean>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 创建状态存储
|
|
|
|
const useUserStore = create<UserState>()(
|
|
|
|
const useUserStore = create<UserState>((set) => {
|
|
|
|
devtools(
|
|
|
|
const getUserInfo = async () => {
|
|
|
|
persist(
|
|
|
|
const result = await apiGetMyUserInfo();
|
|
|
|
(set) => {
|
|
|
|
if (result.code === 200) await set(produce((state) => void (state.userInfo = result.data)));
|
|
|
|
const getUserInfo = async () => {
|
|
|
|
return result.code === 200;
|
|
|
|
const result = await apiGetMyUserInfo();
|
|
|
|
};
|
|
|
|
if (result.code === 200) await set(produce((state) => void (state.userInfo = result.data)));
|
|
|
|
|
|
|
|
return result.code === 200;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
userInfo: {
|
|
|
|
userInfo: {
|
|
|
|
avatar: '',
|
|
|
|
avatar: '',
|
|
|
|
badgeList: [0],
|
|
|
|
badgeList: [0],
|
|
|
|
birthDay: '',
|
|
|
|
birthDay: '',
|
|
|
|
commentReplyCount: 0,
|
|
|
|
commentReplyCount: 0,
|
|
|
|
contributorRole: '',
|
|
|
|
contributorRole: '',
|
|
|
|
fansCount: '',
|
|
|
|
fansCount: '',
|
|
|
|
followCount: '',
|
|
|
|
followCount: '',
|
|
|
|
haveNewMessage: false,
|
|
|
|
haveNewMessage: false,
|
|
|
|
id: '',
|
|
|
|
id: '',
|
|
|
|
ipLocation: '',
|
|
|
|
ipLocation: '',
|
|
|
|
journalCount: 0,
|
|
|
|
journalCount: 0,
|
|
|
|
mobile: '',
|
|
|
|
mobile: '',
|
|
|
|
nickName: '',
|
|
|
|
nickName: '',
|
|
|
|
relation: 0,
|
|
|
|
relation: 0,
|
|
|
|
sex: 0,
|
|
|
|
sex: 0,
|
|
|
|
signature: '',
|
|
|
|
signature: '',
|
|
|
|
songCount: 0,
|
|
|
|
songCount: 0,
|
|
|
|
thumbUpCount: 0,
|
|
|
|
thumbUpCount: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// 展示登录框
|
|
|
|
// 展示登录框
|
|
|
|
showLogin: false,
|
|
|
|
showLogin: false,
|
|
|
|
// 调用出登录框
|
|
|
|
// 调用出登录框
|
|
|
|
setShowLogin: (value) => set({ showLogin: value }),
|
|
|
|
setShowLogin: (value) => set({ showLogin: value }),
|
|
|
|
// 更新用户信息
|
|
|
|
// 更新用户信息
|
|
|
|
setUserInfo: (userInfo) => set({ userInfo }),
|
|
|
|
setUserInfo: (userInfo) => set({ userInfo }),
|
|
|
|
// 获取用户信息
|
|
|
|
// 获取用户信息
|
|
|
|
getUserInfo,
|
|
|
|
getUserInfo,
|
|
|
|
// 用户登录
|
|
|
|
// 用户登录
|
|
|
|
userLogin: async (params) => {
|
|
|
|
userLogin: async (params) => {
|
|
|
|
const result = await apiUserLogin(params);
|
|
|
|
const result = await apiUserLogin(params);
|
|
|
|
if (result.code !== 200) return false;
|
|
|
|
if (result.code !== 200) return false;
|
|
|
|
await setCookie(null, 'token', result.data, {
|
|
|
|
await setCookie(null, 'token', result.data, {
|
|
|
|
maxAge: 7 * 24 * 60 * 60,
|
|
|
|
maxAge: 7 * 24 * 60 * 60,
|
|
|
|
path: '/',
|
|
|
|
path: '/',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return await getUserInfo();
|
|
|
|
return await getUserInfo();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
name: 'user',
|
|
|
|
|
|
|
|
getStorage: () => localStorage,
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
export default useUserStore;
|
|
|
|
export default useUserStore;
|
|
|
|