import React, { useState, useEffect } from "react"; import { View, Text, Image, ScrollView, Button } from "@tarojs/components"; import { PopupPicker } from "@/components/Picker/index"; import Taro from "@tarojs/taro"; import "./index.scss"; import { UserInfo } from "@/components/UserInfo"; import { UserService, PickerOption } from "@/services/userService"; import { clear_login_state } from "@/services/loginService"; import { convert_db_gender_to_display } from "@/utils/genderUtils"; import { EditModal } from "@/components"; import img from "@/config/images"; const EditProfilePage: React.FC = () => { // 用户信息状态 const [user_info, setUserInfo] = useState({ id: "1", nickname: "加载中...", avatar: require("@/static/userInfo/default_avatar.svg"), join_date: "加载中...", stats: { following: 0, friends: 0, hosted: 0, participated: 0, }, personal_profile: "加载中...", occupation: "加载中...", ntrp_level: "NTRP 3.0", phone: "", gender: "", country: "", province: "", city: "", }); // 表单状态 const [form_data, setFormData] = useState({ nickname: "", personal_profile: "", occupation: "", ntrp_level: "4.0", phone: "", gender: "", birthday: "2000-01-01", country: "", province: "", city: "", }); // 加载状态 const [loading, setLoading] = useState(true); // 编辑弹窗状态 const [edit_modal_visible, setEditModalVisible] = useState(false); const [editing_field, setEditingField] = useState(""); const [gender_picker_visible, setGenderPickerVisible] = useState(false); const [birthday_picker_visible, setBirthdayPickerVisible] = useState(false); const [location_picker_visible, setLocationPickerVisible] = useState(false); const [ntrp_picker_visible, setNtrpPickerVisible] = useState(false); const [occupation_picker_visible, setOccupationPickerVisible] = useState(false); // 职业数据 const [professions, setProfessions] = useState([]); // 城市数据 const [cities, setCities] = useState([]); // 页面加载时初始化数据 useEffect(() => { load_user_info(); 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 load_user_info = async () => { try { setLoading(true); const user_data = await UserService.get_user_info(); setUserInfo(user_data); setFormData({ nickname: user_data.nickname || "", personal_profile: user_data.personal_profile || "", occupation: user_data.occupation || "", ntrp_level: user_data.ntrp_level || "NTRP 4.0", phone: user_data.phone || "", gender: user_data.gender || "", birthday: user_data.birthday || "", country: user_data.country || "", province: user_data.province || "", city: user_data.city || "", }); } catch (error) { console.error("加载用户信息失败:", error); Taro.showToast({ title: "加载用户信息失败", icon: "error", duration: 2000, }); } finally { setLoading(false); } }; // 处理头像上传 const handle_avatar_upload = () => { Taro.chooseImage({ count: 1, sizeType: ["compressed"], sourceType: ["album", "camera"], success: async (res) => { const tempFilePath = res.tempFilePaths[0]; try { const avatar_url = await UserService.upload_avatar(tempFilePath); setUserInfo((prev) => ({ ...prev, avatar: avatar_url })); Taro.showToast({ title: "头像上传成功", icon: "success", }); } catch (error) { console.error("头像上传失败:", error); Taro.showToast({ title: "头像上传失败", icon: "none", }); } }, }); }; // 处理编辑弹窗 const handle_open_edit_modal = (field: string) => { if (field === "gender") { setGenderPickerVisible(true); return; } if (field === "birthday") { setBirthdayPickerVisible(true); return; } if (field === "location") { setLocationPickerVisible(true); return; } if (field === "ntrp_level") { setNtrpPickerVisible(true); return; } if (field === "occupation") { setOccupationPickerVisible(true); return; } if (field === "nickname") { // 手动输入 setEditingField(field); setEditModalVisible(true); } else { setEditingField(field); setEditModalVisible(true); } }; const handle_edit_modal_save = async (value: string) => { try { // 调用更新用户信息接口,只传递修改的字段 const update_data = { [editing_field]: value }; await UserService.update_user_info(update_data); // 更新本地状态 setFormData((prev) => ({ ...prev, [editing_field]: value })); setUserInfo((prev) => ({ ...prev, [editing_field]: value })); // 关闭弹窗 setEditModalVisible(false); setEditingField(""); // 显示成功提示 Taro.showToast({ title: "保存成功", icon: "success", }); } catch (error) { console.error("保存失败:", error); Taro.showToast({ title: "保存失败", icon: "error", }); } }; const handle_edit_modal_cancel = () => { setEditModalVisible(false); setEditingField(""); }; // 处理字段编辑 const handle_field_edit = async ( field: string | { [key: string]: string }, value?: string ) => { try { if ( typeof field === "object" && field !== null && !Array.isArray(field) ) { await UserService.update_user_info({ ...field }); // 更新本地状态 setFormData((prev) => ({ ...prev, ...field })); setUserInfo((prev) => ({ ...prev, ...field })); } else { // 调用更新用户信息接口,只传递修改的字段 const update_data = { [field as string]: value }; await UserService.update_user_info(update_data); // 更新本地状态 setFormData((prev) => ({ ...prev, [field as string]: value })); setUserInfo((prev) => ({ ...prev, [field as string]: value })); } // 显示成功提示 Taro.showToast({ title: "保存成功", icon: "success", }); } catch (error) { console.error("保存失败:", error); Taro.showToast({ title: "保存失败", icon: "error", }); } }; // 处理性别选择 const handle_gender_change = (e: any) => { const gender_value = e[0]; handle_field_edit("gender", gender_value); }; // 处理生日选择 const handle_birthday_change = (e: any) => { const [year, month, day] = e; handle_field_edit( "birthday", `${year}-${String(month).padStart(2, "0")}-${String(day).padStart( 2, "0" )}` ); }; // 处理地区选择 const handle_location_change = (e: any) => { const [country, province, city] = e; handle_field_edit({ country, province, city }); }; // 处理NTRP水平选择 const handle_ntrp_level_change = (e: any) => { const ntrp_level_value = e[0]; handle_field_edit("ntrp_level", ntrp_level_value); }; // 处理职业选择 const handle_occupation_change = (e: any) => { const [country, province] = e; handle_field_edit("occupation", `${country} ${province}`); }; // 处理退出登录 const handle_logout = () => { Taro.showModal({ title: "确认退出", content: "确定要退出登录吗?", success: (res) => { if (res.confirm) { // 清除用户数据 clear_login_state(); Taro.reLaunch({ url: "/login_pages/index/index", }); } }, }); }; const onGetPhoneNumber = async (e) => { if (!e.detail || !e.detail.code) { Taro.showToast({ title: "获取手机号失败,请重试", icon: "none", duration: 2000, }); return; } try { const phone = await UserService.parse_phone(e.detail.code); handle_field_edit("phone", phone); } catch (e) { console.error("解析手机号失败:", e); Taro.showToast({ title: "解析手机号失败,请重试", icon: "none", duration: 2000, }); } }; return ( {/* 导航栏 */} { Taro.navigateBack(); }} > {/* 主要内容 */} {loading ? ( 加载中... ) : ( <> {/* 头像编辑区域 */} {/* 基本信息编辑 */} {/* 名字 */} handle_open_edit_modal("nickname")} > 名字 {form_data.nickname || "188的王晨"} {/* 性别 */} handle_open_edit_modal("gender")} > 性别 {convert_db_gender_to_display(form_data.gender)} {/* 生日 */} handle_open_edit_modal("birthday")} > 生日 {form_data.birthday} {/* 简介编辑 */} handle_open_edit_modal("personal_profile")} > 简介 {form_data.personal_profile.replace(/\n/g, " ") || "介绍一下自己"} {/* 地区、NTRP水平、职业 */} {/* 地区 */} handle_open_edit_modal("location")} > 地区 {`${form_data.country} ${form_data.province} ${form_data.city}`} {/* NTRP水平 */} handle_open_edit_modal("ntrp_level")} > NTRP 水平 {form_data.ntrp_level} {/* 职业 */} handle_open_edit_modal("occupation")} > 职业 {form_data.occupation} {/* 手机号 */} 手机 {/* */} {/* 注销账号 */} 注销账号 {/* 退出登录 */} 退出登录 )} {/* 编辑弹窗 */} {/* 性别选择弹窗 */} {gender_picker_visible && ( )} {/* 生日选择弹窗 */} {birthday_picker_visible && ( )} {/* 地区选择弹窗 */} {location_picker_visible && ( )} {/* NTRP水平选择弹窗 */} {ntrp_picker_visible && ( )} {/* 职业选择弹窗 */} {occupation_picker_visible && ( )} ); }; export default EditProfilePage;