This commit is contained in:
张成
2025-09-12 22:34:12 +08:00
parent 471244ee5d
commit 30d16946d2
30 changed files with 104 additions and 65 deletions

View File

@@ -0,0 +1,458 @@
import React, { useState, useEffect } from 'react';
import { View, Text, Image, ScrollView, Picker, Input } from '@tarojs/components';
import Taro from '@tarojs/taro';
import './index.scss';
import { UserInfo } from '@/components/UserInfo';
import { UserService } from '@/services/userService';
import { clear_login_state } from '@/services/loginService';
import { convert_db_gender_to_display } from '@/utils/genderUtils';
import { EditModal } from '@/components';
const EditProfilePage: React.FC = () => {
// 用户信息状态
const [user_info, setUserInfo] = useState<UserInfo>({
id: '1',
nickname: '加载中...',
avatar: require('@/static/userInfo/default_avatar.svg'),
join_date: '加载中...',
stats: {
following: 0,
friends: 0,
hosted: 0,
participated: 0
},
personal_profile: '加载中...',
location: '加载中...',
occupation: '加载中...',
ntrp_level: 'NTRP 3.0',
phone: '',
gender: ''
});
// 表单状态
const [form_data, setFormData] = useState({
nickname: '',
personal_profile: '',
location: '',
occupation: '',
ntrp_level: '4.0',
phone: '',
gender: '',
birthday: '2000-01-01'
});
// 加载状态
const [loading, setLoading] = useState(true);
// 编辑弹窗状态
const [edit_modal_visible, setEditModalVisible] = useState(false);
const [editing_field, setEditingField] = useState<string>('');
// 页面加载时初始化数据
useEffect(() => {
load_user_info();
}, []);
// 加载用户信息
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 || '',
location: user_data.location || '',
occupation: user_data.occupation || '',
ntrp_level: user_data.ntrp_level || 'NTRP 4.0',
phone: user_data.phone || '',
gender: user_data.gender || '',
birthday: '2000-01-01' // 默认生日,实际应该从用户数据获取
});
} 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 === '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, value: string) => {
try {
// 调用更新用户信息接口,只传递修改的字段
const update_data = { [field]: value };
await UserService.update_user_info(update_data);
// 更新本地状态
setFormData(prev => ({ ...prev, [field]: value }));
setUserInfo(prev => ({ ...prev, [field]: 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.detail.value;
// 使用工具函数转换:页面选择器值(0/1) -> 数据库值('0'/'1')
const gender_db_value = gender_value === 0 ? '0' : '1';
handle_field_edit('gender', gender_db_value);
};
// 处理生日选择
const handle_birthday_change = (e: any) => {
const birthday_value = e.detail.value;
handle_field_edit('birthday', birthday_value);
};
// 处理职业输入 - 实时更新本地状态
const handle_occupation_input = (e: any) => {
const occupation_value = e.detail.value;
setFormData(prev => ({ ...prev, occupation: occupation_value }));
};
// 处理职业输入 - 失去焦点时保存到服务器
const handle_occupation_blur = (e: any) => {
const occupation_value = e.detail.value;
handle_field_edit('occupation', occupation_value);
};
// 处理地区输入 - 实时更新本地状态
const handle_location_input = (e: any) => {
const location_value = e.detail.value;
setFormData(prev => ({ ...prev, location: location_value }));
};
// 处理地区输入 - 失去焦点时保存到服务器
const handle_location_blur = (e: any) => {
const location_value = e.detail.value;
handle_field_edit('location', location_value);
};
// 处理手机号输入 - 实时更新本地状态
const handle_phone_input = (e: any) => {
const phone_value = e.detail.value;
setFormData(prev => ({ ...prev, phone: phone_value }));
};
// 处理手机号输入 - 失去焦点时保存到服务器
const handle_phone_blur = (e: any) => {
const phone_value = e.detail.value;
handle_field_edit('phone', phone_value);
};
// 处理退出登录
const handle_logout = () => {
Taro.showModal({
title: '确认退出',
content: '确定要退出登录吗?',
success: (res) => {
if (res.confirm) {
// 清除用户数据
clear_login_state();
Taro.reLaunch({
url: '/login_pages/index/index'
});
}
}
});
};
return (
<View className="edit_profile_page">
{/* 导航栏 */}
{/* 主要内容 */}
<ScrollView className="main_content" scrollY>
{loading ? (
<View className="loading_container">
<Text className="loading_text">...</Text>
</View>
) : (
<>
{/* 头像编辑区域 */}
<View className="avatar_section">
<View className="avatar_container" onClick={handle_avatar_upload}>
<Image className="avatar" src={user_info.avatar} />
<View className="avatar_overlay">
<Image
className="upload_icon"
src={require('@/static/userInfo/edit2.svg')}
/>
</View>
</View>
</View>
{/* 基本信息编辑 */}
<View className="form_section">
{/* 名字 */}
<View className="form_group">
<View className="form_item" onClick={() => handle_open_edit_modal('nickname')}>
<View className="item_left">
<Image className="item_icon" src={require('@/static/userInfo/user1.svg')} />
<Text className="item_label"></Text>
</View>
<View className="item_right">
<Text className="item_value">{form_data.nickname || '188的王晨'}</Text>
<Image className="arrow_icon" src={require('@/static/list/icon-list-right-arrow.svg')} />
</View>
</View>
<View className="divider"></View>
</View>
{/* 性别 */}
<View className="form_group">
<Picker
mode="selector"
range={['男', '女']}
value={form_data.gender === '0' ? 0 : form_data.gender === '1' ? 1 : 0}
onChange={handle_gender_change}
>
<View className="form_item">
<View className="item_left">
<Image className="item_icon" src={require('@/static/userInfo/user2.svg')} />
<Text className="item_label"></Text>
</View>
<View className="item_right">
<Text className="item_value">
{convert_db_gender_to_display(form_data.gender)}
</Text>
<Image className="arrow_icon" src={require('@/static/list/icon-list-right-arrow.svg')} />
</View>
</View>
</Picker>
<View className="divider"></View>
</View>
{/* 生日 */}
<View className="form_group">
<Picker
mode="date"
value={form_data.birthday}
onChange={handle_birthday_change}
>
<View className="form_item">
<View className="item_left">
<Image className="item_icon" src={require('@/static/userInfo/tennis.svg')} />
<Text className="item_label"></Text>
</View>
<View className="item_right">
<Text className="item_value">{form_data.birthday}</Text>
<Image className="arrow_icon" src={require('@/static/list/icon-list-right-arrow.svg')} />
</View>
</View>
</Picker>
</View>
</View>
{/* 简介编辑 */}
<View className="form_section">
<View className="form_group">
<View className="form_item" onClick={() => handle_open_edit_modal('personal_profile')}>
<View className="item_left">
<Image className="item_icon" src={require('@/static/userInfo/message.svg')} />
<Text className="item_label"></Text>
</View>
<View className="item_right">
<Text className="item_value">
{form_data.personal_profile || '介绍一下自己'}
</Text>
<Image className="arrow_icon" src={require('@/static/list/icon-list-right-arrow.svg')} />
</View>
</View>
</View>
</View>
{/* 地区、NTRP水平、职业 */}
<View className="form_section">
<View className="form_group">
{/* 地区 */}
<View className="form_item">
<View className="item_left">
<Image className="item_icon" src={require('@/static/userInfo/location.svg')} />
<Text className="item_label"></Text>
</View>
<View className="item_right">
<Input
className="item_input"
value={form_data.location}
placeholder="请输入地区"
onInput={handle_location_input}
onBlur={handle_location_blur}
/>
</View>
</View>
<View className="divider"></View>
{/* NTRP水平 */}
<View className="form_item">
<View className="item_left">
<Image className="item_icon" src={require('@/static/userInfo/tennis.svg')} />
<Text className="item_label">NTRP </Text>
</View>
<View className="item_right">
<Text className="item_value">{form_data.ntrp_level}</Text>
<Image className="arrow_icon" src={require('@/static/list/icon-list-right-arrow.svg')} />
</View>
</View>
<View className="divider"></View>
{/* 职业 */}
<View className="form_item">
<View className="item_left">
<Image className="item_icon" src={require('@/static/userInfo/sc.svg')} />
<Text className="item_label"></Text>
</View>
<View className="item_right">
<Input
className="item_input"
value={form_data.occupation}
placeholder="请输入职业"
onInput={handle_occupation_input}
onBlur={handle_occupation_blur}
/>
</View>
</View>
</View>
</View>
{/* 手机号 */}
<View className="form_section">
<View className="form_group">
<View className="form_item">
<View className="item_left">
<Image className="item_icon" src={require('@/static/userInfo/message.svg')} />
<Text className="item_label"></Text>
</View>
<View className="item_right">
<Input
className="item_input"
value={form_data.phone}
placeholder="请输入手机号"
type="number"
onInput={handle_phone_input}
onBlur={handle_phone_blur}
/>
</View>
</View>
<View className="divider"></View>
</View>
</View>
{/* 退出登录 */}
<View className="logout_section">
<View className="logout_button" onClick={handle_logout}>
<Text className="logout_text">退</Text>
</View>
</View>
</>
)}
</ScrollView>
{/* 编辑弹窗 */}
<EditModal
visible={edit_modal_visible}
type={editing_field}
title={editing_field === 'nickname' ? '编辑名字' : '编辑简介'}
placeholder={editing_field === 'nickname' ? '请输入您的名字' : '介绍一下你的喜好,或者训练习惯'}
initialValue={form_data[editing_field as keyof typeof form_data] || ''}
maxLength={editing_field === 'nickname' ? 20 : 100}
onSave={handle_edit_modal_save}
onCancel={handle_edit_modal_cancel}
validationMessage={editing_field === 'nickname' ? '请填写 1-20 个字符' : '请填写 2-100 个字符'}
/>
</View>
);
};
export default EditProfilePage;