1
This commit is contained in:
@@ -731,16 +731,16 @@ export const useListStore = create<TennisStore>()((set, get) => ({
|
||||
const state = get();
|
||||
// 从 area 中获取省份,area 格式: ["中国", 省份, 城市]
|
||||
const country = "中国";
|
||||
const province = state.area?.at(1) || "上海"; // area[1] 是省份
|
||||
|
||||
const cn_city = state.area?.at(1) || "上海市"; // area[1] 是省份
|
||||
|
||||
const res = await getDistricts({
|
||||
country,
|
||||
state: province
|
||||
state: cn_city
|
||||
});
|
||||
|
||||
if (res.code === 0 && res.data) {
|
||||
const districts = res.data.map((item) => ({
|
||||
label: item.cn_city,
|
||||
label: item.cn_county,
|
||||
value: item.id.toString(),
|
||||
id: item.id,
|
||||
}));
|
||||
|
||||
@@ -36,6 +36,7 @@ const getTimeNextDate = (time: string) => {
|
||||
// 请求锁,避免重复调用
|
||||
let isFetchingLastTestResult = false;
|
||||
let isCheckingNicknameStatus = false;
|
||||
const CITY_CACHE_KEY = "USER_SELECTED_CITY";
|
||||
|
||||
export const useUser = create<UserState>()((set) => ({
|
||||
user: {},
|
||||
@@ -44,41 +45,50 @@ export const useUser = create<UserState>()((set) => ({
|
||||
const res = await fetchUserProfile();
|
||||
const userData = res.data;
|
||||
set({ user: userData });
|
||||
|
||||
|
||||
// 优先使用缓存中的城市,不使用用户信息中的位置
|
||||
// 检查是否有缓存的城市
|
||||
const CITY_CACHE_KEY = "USER_SELECTED_CITY";
|
||||
|
||||
const cachedCity = (Taro as any).getStorageSync?.(CITY_CACHE_KEY);
|
||||
|
||||
|
||||
|
||||
|
||||
if (cachedCity && Array.isArray(cachedCity) && cachedCity.length === 2) {
|
||||
// 如果有缓存的城市,使用缓存,不更新 area
|
||||
console.log("[userStore] 检测到缓存的城市,使用缓存,不更新 area");
|
||||
return userData;
|
||||
}
|
||||
|
||||
|
||||
// 只有当没有缓存时,才使用用户信息中的位置
|
||||
if (userData?.last_location_province) {
|
||||
const listStore = useListStore.getState();
|
||||
const currentArea = listStore.area;
|
||||
|
||||
// 只有当 area 不存在时才使用用户信息中的位置
|
||||
if (!currentArea) {
|
||||
const newArea: [string, string] = ["中国", userData.last_location_province];
|
||||
listStore.updateArea(newArea);
|
||||
// 保存到缓存
|
||||
try {
|
||||
(Taro as any).setStorageSync?.(CITY_CACHE_KEY, newArea);
|
||||
} catch (error) {
|
||||
console.error("保存城市缓存失败:", error);
|
||||
}
|
||||
useUser.getState().updateCache(newArea);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return userData;
|
||||
} catch (error) {
|
||||
console.error("获取用户信息失败:", error);
|
||||
return undefined;
|
||||
}
|
||||
},
|
||||
|
||||
// 更新缓存
|
||||
updateCache: async (newArea: [string, string]) => {
|
||||
try {
|
||||
(Taro as any).setStorageSync?.(CITY_CACHE_KEY, newArea);
|
||||
} catch (error) {
|
||||
console.error("保存城市缓存失败:", error);
|
||||
}
|
||||
},
|
||||
|
||||
updateUserInfo: async (userInfo: Partial<UserInfoType>) => {
|
||||
try {
|
||||
// 先更新后端
|
||||
@@ -86,7 +96,7 @@ export const useUser = create<UserState>()((set) => ({
|
||||
// 然后立即更新本地状态(乐观更新)
|
||||
set((state) => {
|
||||
const newUser = { ...state.user, ...userInfo };
|
||||
|
||||
|
||||
// 当 userLastLocationProvince 更新时,同步更新 area
|
||||
if (userInfo.last_location_province) {
|
||||
const listStore = useListStore.getState();
|
||||
@@ -97,7 +107,7 @@ export const useUser = create<UserState>()((set) => ({
|
||||
listStore.updateArea(newArea);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return { user: newUser };
|
||||
});
|
||||
// 不再每次都重新获取完整用户信息,减少请求次数
|
||||
@@ -195,6 +205,7 @@ export const useNicknameChangeStatus = () =>
|
||||
export const useUserActions = () =>
|
||||
useUser((state) => ({
|
||||
fetchUserInfo: state.fetchUserInfo,
|
||||
updateCache: state.updateCache,
|
||||
updateUserInfo: state.updateUserInfo,
|
||||
checkNicknameChangeStatus: state.checkNicknameChangeStatus,
|
||||
updateNickname: state.updateNickname,
|
||||
|
||||
Reference in New Issue
Block a user