From 9ef35267cfc92018f45ae196a5ef68af93a50796 Mon Sep 17 00:00:00 2001 From: Ultrame <1019265060@qq.com> Date: Fri, 17 Oct 2025 17:10:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=BB=BAstore=E5=AD=98=E5=82=A8?= =?UTF-8?q?=E5=9C=B0=E5=8C=BA=E5=92=8C=E8=81=8C=E4=B8=9Apicker=E9=80=89?= =?UTF-8?q?=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/UserInfo/index.tsx | 51 ++++++++++++++++--------------- src/store/pickerOptionsStore.ts | 39 +++++++++++++++++++++++ src/user_pages/edit/index.tsx | 49 ++++++++++++++--------------- src/user_pages/myself/index.tsx | 12 ++++---- 4 files changed, 96 insertions(+), 55 deletions(-) create mode 100644 src/store/pickerOptionsStore.ts diff --git a/src/components/UserInfo/index.tsx b/src/components/UserInfo/index.tsx index 4088ff2..a73cf97 100644 --- a/src/components/UserInfo/index.tsx +++ b/src/components/UserInfo/index.tsx @@ -8,6 +8,7 @@ import { UserService, PickerOption } from "@/services/userService"; import { PopupPicker } from "@/components/Picker/index"; import { useUserActions } from "@/store/userStore"; import { UserInfoType } from "@/services/userService"; +import { useCities, useProfessions } from "@/store/pickerOptionsStore"; // 用户信息接口 // export interface UserInfo { @@ -82,33 +83,33 @@ export const UserInfoCard: React.FC = ({ }); // 职业数据 - const [professions, setProfessions] = useState([]); + const professions = useProfessions(); // 城市数据 - const [cities, setCities] = useState([]); + const cities = useCities(); // 页面加载时初始化数据 - useEffect(() => { - getProfessions(); - getCities(); - }, []); + // useEffect(() => { + // getProfessions(); + // getCities(); + // }, []); - const getProfessions = async () => { - try { - const res = await UserService.getProfessions(); - setProfessions(res); - } catch (e) { - console.log("获取职业失败:", e); - } - }; - const getCities = async () => { - try { - const res = await UserService.getCities(); - setCities(res); - } catch (e) { - console.log("获取职业失败:", e); - } - }; + // const getProfessions = async () => { + // try { + // const res = await UserService.getProfessions(); + // setProfessions(res); + // } catch (e) { + // console.log("获取职业失败:", e); + // } + // }; + // const getCities = async () => { + // try { + // const res = await UserService.getCities(); + // setCities(res); + // } catch (e) { + // console.log("获取职业失败:", e); + // } + // }; // 处理编辑弹窗 const handle_open_edit_modal = (field: string) => { if (field === "gender") { @@ -223,8 +224,8 @@ export const UserInfoCard: React.FC = ({ // 处理职业选择 const handle_occupation_change = (e: any) => { - const [country, province] = e; - handle_field_edit("occupation", `${country} ${province}`); + const [country, province, city] = e; + handle_field_edit("occupation", `${country} ${province} ${city}`); }; const handle_edit_modal_cancel = () => { setEditModalVisible(false); @@ -401,7 +402,7 @@ export const UserInfoCard: React.FC = ({ {user_info.occupation ? ( - {user_info.occupation.split(" ")[1]} + {user_info.occupation.split(" ")[2]} ) : is_current_user ? ( diff --git a/src/store/pickerOptionsStore.ts b/src/store/pickerOptionsStore.ts new file mode 100644 index 0000000..14695e3 --- /dev/null +++ b/src/store/pickerOptionsStore.ts @@ -0,0 +1,39 @@ +import { create } from "zustand"; + +import { UserService } from "@/services/userService"; + +export interface PickerOptionState { + cities: any[]; + professions: any[]; + getCities: () => Promise; + getProfessions: () => Promise; +} + +export const usePickerOption = create((set) => ({ + cities: [], + professions: [], + getCities: async () => { + try { + const res = await UserService.getCities(); + set({ cities: res }); + return res; + } catch (e) { + console.log("获取城市列表失败:", e); + throw e; + } + }, + getProfessions: async () => { + try { + const res = await UserService.getProfessions(); + set({ professions: res }); + return res; + } catch (e) { + console.log("获取职业失败:", e); + throw e; + } + }, +})); + +export const useCities = () => usePickerOption((state) => state.cities); +export const useProfessions = () => + usePickerOption((state) => state.professions); diff --git a/src/user_pages/edit/index.tsx b/src/user_pages/edit/index.tsx index dd10a6d..5056e07 100644 --- a/src/user_pages/edit/index.tsx +++ b/src/user_pages/edit/index.tsx @@ -11,6 +11,7 @@ import img from "@/config/images"; import CommonDialog from "@/components/CommonDialog"; import { useUserActions, useUserInfo } from "@/store/userStore"; import { UserInfoType } from "@/services/userService"; +import { useCities, useProfessions } from "@/store/pickerOptionsStore"; const EditProfilePage: React.FC = () => { const { updateUserInfo } = useUserActions(); @@ -46,10 +47,10 @@ const EditProfilePage: React.FC = () => { useState(false); // 职业数据 - const [professions, setProfessions] = useState([]); + const professions = useProfessions(); // 城市数据 - const [cities, setCities] = useState([]); + const cities = useCities(); // 监听store中的用户信息变化,同步到表单状态 useEffect(() => { @@ -70,27 +71,27 @@ const EditProfilePage: React.FC = () => { }, [user_info]); // 页面加载时初始化数据 - useEffect(() => { - getCities(); - getProfessions(); - }, []); + // useEffect(() => { + // getCities(); + // getProfessions(); + // }, []); - const getProfessions = async () => { - try { - const res = await UserService.getProfessions(); - setProfessions(res); - } catch (e) { - console.log("获取职业失败:", e); - } - }; - const getCities = async () => { - try { - const res = await UserService.getCities(); - setCities(res); - } catch (e) { - console.log("获取职业失败:", e); - } - }; + // const getProfessions = async () => { + // try { + // const res = await UserService.getProfessions(); + // setProfessions(res); + // } catch (e) { + // console.log("获取职业失败:", e); + // } + // }; + // const getCities = async () => { + // try { + // const res = await UserService.getCities(); + // setCities(res); + // } catch (e) { + // console.log("获取城市列表失败:", e); + // } + // }; // 加载用户信息 // const load_user_info = async () => { @@ -281,8 +282,8 @@ const EditProfilePage: React.FC = () => { // 处理职业选择 const handle_occupation_change = (e: any) => { - const [country, province] = e; - handle_field_edit("occupation", `${country} ${province}`); + const [country, province, city] = e; + handle_field_edit("occupation", `${country} ${province} ${city}`); }; // 处理退出登录 diff --git a/src/user_pages/myself/index.tsx b/src/user_pages/myself/index.tsx index e8c8ad9..b321f47 100644 --- a/src/user_pages/myself/index.tsx +++ b/src/user_pages/myself/index.tsx @@ -10,9 +10,10 @@ import { TennisMatch } from "@/../types/list/types"; import { withAuth, NTRPTestEntryCard } from "@/components"; import { EvaluateScene } from "@/store/evaluateStore"; import { useUserInfo } from "@/store/userStore"; -import { UserInfoType } from "@/services/userService"; +import { usePickerOption } from "@/store/pickerOptionsStore"; const MyselfPage: React.FC = () => { + const pickerOption = usePickerOption(); // 获取页面参数 const instance = Taro.getCurrentInstance(); const user_id = instance.router?.params?.userid || ""; @@ -64,11 +65,10 @@ const MyselfPage: React.FC = () => { // } // }; - // useEffect(() => { - // if (user_info.id) { - // load_game_data(); // 在 user_info 更新后调用 - // } - // }, [user_info]); + useEffect(() => { + pickerOption.getCities(); + pickerOption.getProfessions(); + }, []); // 页面加载时获取数据 // useEffect(() => {