This commit is contained in:
张成
2025-09-10 21:43:36 +08:00
parent d38baa9c5b
commit 5fdee20d45
12 changed files with 281 additions and 152 deletions

View File

@@ -16,7 +16,7 @@ interface GlobalState {
interface GlobalActions {
updateState: (payload: Record<string, any>) => void;
getNavbarHeightInfo: () => void;
getCurrentLocationInfo: () => void;
getCurrentLocationInfo: () => any;
}
// 完整的 Store 类型
type GlobalStore = GlobalState & GlobalActions;
@@ -50,14 +50,13 @@ export const useGlobalStore = create<GlobalStore>()((set, get) => ({
},
// 获取位置信息
getCurrentLocationInfo: () => {
return getCurrentLocation().then((res) => {
set({
getLocationLoading: false,
location: res || {},
});
return res;
getCurrentLocationInfo: async () => {
const res = await getCurrentLocation()
set({
getLocationLoading: false,
location: res || {},
});
return res;
},
// 更新store数据

View File

@@ -175,7 +175,7 @@ export const useListStore = create<TennisStore>()((set, get) => ({
// 第一次进入页面时传入 isRefresh 参数
if (isFirstLoad) {
reqParams.isRefresh = true;
reqParams.seachOption.isRefresh = true;
}
}

View File

@@ -1,13 +1,25 @@
import { create } from 'zustand'
import { fetchUserProfile, updateUserProfile, UserInfoType } from '@/services/loginService'
import { fetchUserProfile, updateUserProfile, UserInfoType } from '@/services/userService'
export interface UserState {
user: UserInfoType
loading: boolean
error: string | null
// 获取用户信息
fetchUserInfo: () => Promise<void>
updateUserInfo: (userInfo: Partial<UserInfoType>) => void
// 更新用户信息(先调用接口更新,再重新获取最新数据)
updateUserInfo: (userInfo: Partial<UserInfoType>) => Promise<void>
// 更新用户位置(特殊处理,需要重新获取详情数据)
updateUserLocation: (latitude: number, longitude: number) => Promise<void>
// 清除用户数据
clearUserData: () => void
}
export const useUser = create<UserState>()((set) => ({
export const useUser = create<UserState>()((set, get) => ({
user: {
id: 0,
"openid": "",
@@ -19,6 +31,7 @@ export const useUser = create<UserState>()((set) => ({
"country": "",
"province": "",
"city": "",
"district":"",
"language": "",
"phone": "13800138000",
"is_subscribed": "0",
@@ -27,20 +40,102 @@ export const useUser = create<UserState>()((set) => ({
"subscribe_time": "2024-06-15 14:00:00",
"last_login_time": "2024-06-15 14:00:00"
},
loading: false,
error: null,
// 获取用户信息
fetchUserInfo: async () => {
const res = await fetchUserProfile()
console.log(res)
set({ user: res.data })
try {
set({ loading: true, error: null })
const res = await fetchUserProfile()
if (res.code === 0) {
set({ user: res.data, loading: false })
} else {
set({ error: res.message || '获取用户信息失败', loading: false })
}
} catch (error) {
console.error('获取用户信息失败:', error)
set({ error: '获取用户信息失败', loading: false })
}
},
updateUserInfo: async(userInfo: Partial<UserInfoType>) => {
const res = await updateUserProfile(userInfo)
set({ user: res.data })
// 更新用户信息
updateUserInfo: async (userInfo: Partial<UserInfoType>) => {
try {
set({ loading: true, error: null })
// 先调用更新接口
const updateRes = await updateUserProfile(userInfo)
if (updateRes.code !== 0) {
throw new Error(updateRes.message || '更新用户信息失败')
}
// 更新成功后,重新获取最新的用户信息
await get().fetchUserInfo()
} catch (error) {
console.error('更新用户信息失败:', error)
set({ error: '更新用户信息失败', loading: false })
throw error
}
},
// 更新用户位置
updateUserLocation: async (latitude: number, longitude: number) => {
try {
set({ loading: true, error: null })
// 调用位置更新接口
const locationRes = await updateUserProfile({ latitude, longitude })
if (locationRes.code !== 0) {
throw new Error(locationRes.message || '更新位置失败')
}
// 位置更新成功后,重新获取最新的用户信息
await get().fetchUserInfo()
} catch (error) {
console.error('更新用户位置失败:', error)
set({ error: '更新用户位置失败', loading: false })
throw error
}
},
// 清除用户数据
clearUserData: () => {
set({
user: {
id: 0,
"openid": "",
"unionid": "",
"session_key": "",
"nickname": "",
"avatar_url": "",
"gender": "",
"country": "",
"province": "",
"city": "",
"language": "",
"phone": "",
"is_subscribed": "0",
"latitude": 0,
"longitude": 0,
"subscribe_time": "",
"last_login_time": ""
},
loading: false,
error: null
})
}
}))
export const useUserInfo = () => useUser((state) => state.user)
export const useUserLoading = () => useUser((state) => state.loading)
export const useUserError = () => useUser((state) => state.error)
export const useUserActions = () => useUser((state) => ({
fetchUserInfo: state.fetchUserInfo,
updateUserInfo: state.updateUserInfo
updateUserInfo: state.updateUserInfo,
updateUserLocation: state.updateUserLocation,
clearUserData: state.clearUserData
}))