index.tsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. import { Image } from 'expo-image';
  2. import * as ImagePicker from 'expo-image-picker';
  3. import { useRouter } from 'expo-router';
  4. import React, { useEffect, useState } from 'react';
  5. import {
  6. Alert,
  7. ImageBackground,
  8. ScrollView,
  9. StatusBar,
  10. StyleSheet,
  11. Text,
  12. TextInput,
  13. TouchableOpacity,
  14. View,
  15. } from 'react-native';
  16. import { useSafeAreaInsets } from 'react-native-safe-area-context';
  17. import { Images } from '@/constants/images';
  18. import { useAuth } from '@/contexts/AuthContext';
  19. import { getUserInfo, updateNickname, updateUserInfo } from '@/services/user';
  20. interface FormData {
  21. nickname: string;
  22. avatar: string;
  23. sex: number; // 1-男 2-女 3-保密
  24. }
  25. export default function ProfileScreen() {
  26. const router = useRouter();
  27. const insets = useSafeAreaInsets();
  28. const { refreshUser } = useAuth();
  29. const [formData, setFormData] = useState<FormData>({
  30. nickname: '',
  31. avatar: '',
  32. sex: 3,
  33. });
  34. const [loading, setLoading] = useState(false);
  35. useEffect(() => {
  36. loadUserInfo();
  37. }, []);
  38. const loadUserInfo = async () => {
  39. try {
  40. const res = await getUserInfo();
  41. if (res) {
  42. setFormData({
  43. nickname: res.nickname || '',
  44. avatar: res.avatar || '',
  45. sex: (res as any).sex || 3,
  46. });
  47. }
  48. } catch (error) {
  49. console.error('获取用户信息失败:', error);
  50. }
  51. };
  52. const handleBack = () => {
  53. router.back();
  54. };
  55. const handleChooseAvatar = async () => {
  56. try {
  57. const permissionResult = await ImagePicker.requestMediaLibraryPermissionsAsync();
  58. if (!permissionResult.granted) {
  59. Alert.alert('提示', '需要相册权限才能选择头像');
  60. return;
  61. }
  62. const result = await ImagePicker.launchImageLibraryAsync({
  63. mediaTypes: ImagePicker.MediaTypeOptions.Images,
  64. allowsEditing: true,
  65. aspect: [1, 1],
  66. quality: 0.8,
  67. });
  68. if (!result.canceled && result.assets[0]) {
  69. const imageUri = result.assets[0].uri;
  70. setFormData(prev => ({ ...prev, avatar: imageUri }));
  71. Alert.alert('提示', '头像选择成功,保存时将更新');
  72. }
  73. } catch (error) {
  74. console.error('选择头像失败:', error);
  75. Alert.alert('提示', '选择头像失败');
  76. }
  77. };
  78. const handleSexChange = (sex: number) => {
  79. setFormData(prev => ({ ...prev, sex }));
  80. };
  81. const handleSave = async () => {
  82. if (!formData.nickname?.trim()) {
  83. Alert.alert('提示', '请输入昵称');
  84. return;
  85. }
  86. try {
  87. setLoading(true);
  88. // 更新昵称
  89. const nicknameRes = await updateNickname(formData.nickname);
  90. // 更新其他信息(性别等)
  91. const infoRes = await updateUserInfo({ sex: formData.sex } as any);
  92. if (nicknameRes || infoRes) {
  93. Alert.alert('提示', '保存成功', [
  94. {
  95. text: '确定',
  96. onPress: () => {
  97. refreshUser?.();
  98. router.back();
  99. }
  100. }
  101. ]);
  102. } else {
  103. Alert.alert('提示', '保存失败');
  104. }
  105. } catch (error) {
  106. console.error('保存失败:', error);
  107. Alert.alert('提示', '保存失败');
  108. } finally {
  109. setLoading(false);
  110. }
  111. };
  112. return (
  113. <ImageBackground
  114. source={{ uri: Images.common.commonBg }}
  115. style={styles.container}
  116. resizeMode="cover"
  117. >
  118. <StatusBar barStyle="light-content" />
  119. {/* 顶部导航 */}
  120. <View style={[styles.header, { paddingTop: insets.top }]}>
  121. <TouchableOpacity style={styles.backBtn} onPress={handleBack}>
  122. <Text style={styles.backIcon}>‹</Text>
  123. </TouchableOpacity>
  124. <Text style={styles.headerTitle}>个人资料</Text>
  125. <View style={styles.placeholder} />
  126. </View>
  127. <ScrollView style={styles.scrollView} showsVerticalScrollIndicator={false}>
  128. {/* 头像 */}
  129. <TouchableOpacity style={styles.avatarSection} onPress={handleChooseAvatar}>
  130. <View style={styles.avatarWrapper}>
  131. <Image
  132. source={{ uri: formData.avatar || Images.common.defaultAvatar }}
  133. style={styles.avatar}
  134. contentFit="cover"
  135. />
  136. </View>
  137. <Text style={styles.avatarTip}>点击更换头像</Text>
  138. </TouchableOpacity>
  139. {/* 表单 */}
  140. <View style={styles.formSection}>
  141. {/* 昵称 */}
  142. <View style={styles.formItem}>
  143. <Text style={styles.formLabel}>昵称</Text>
  144. <TextInput
  145. style={styles.formInput}
  146. value={formData.nickname}
  147. onChangeText={(text) => setFormData(prev => ({ ...prev, nickname: text }))}
  148. placeholder="请输入昵称"
  149. placeholderTextColor="#999"
  150. maxLength={20}
  151. />
  152. </View>
  153. {/* 性别 */}
  154. <View style={styles.formItem}>
  155. <Text style={styles.formLabel}>性别</Text>
  156. <View style={styles.sexOptions}>
  157. <TouchableOpacity
  158. style={[styles.sexOption, formData.sex === 1 && styles.sexOptionActive]}
  159. onPress={() => handleSexChange(1)}
  160. >
  161. <View style={[styles.radioOuter, formData.sex === 1 && styles.radioOuterActive]}>
  162. {formData.sex === 1 && <View style={styles.radioInner} />}
  163. </View>
  164. <Text style={[styles.sexText, formData.sex === 1 && styles.sexTextActive]}>男</Text>
  165. </TouchableOpacity>
  166. <TouchableOpacity
  167. style={[styles.sexOption, formData.sex === 2 && styles.sexOptionActive]}
  168. onPress={() => handleSexChange(2)}
  169. >
  170. <View style={[styles.radioOuter, formData.sex === 2 && styles.radioOuterActive]}>
  171. {formData.sex === 2 && <View style={styles.radioInner} />}
  172. </View>
  173. <Text style={[styles.sexText, formData.sex === 2 && styles.sexTextActive]}>女</Text>
  174. </TouchableOpacity>
  175. <TouchableOpacity
  176. style={[styles.sexOption, formData.sex === 3 && styles.sexOptionActive]}
  177. onPress={() => handleSexChange(3)}
  178. >
  179. <View style={[styles.radioOuter, formData.sex === 3 && styles.radioOuterActive]}>
  180. {formData.sex === 3 && <View style={styles.radioInner} />}
  181. </View>
  182. <Text style={[styles.sexText, formData.sex === 3 && styles.sexTextActive]}>保密</Text>
  183. </TouchableOpacity>
  184. </View>
  185. </View>
  186. </View>
  187. {/* 保存按钮 */}
  188. <TouchableOpacity
  189. style={[styles.saveBtn, loading && styles.saveBtnDisabled]}
  190. onPress={handleSave}
  191. disabled={loading}
  192. >
  193. <ImageBackground
  194. source={{ uri: Images.common.loginBtn }}
  195. style={styles.saveBtnBg}
  196. resizeMode="contain"
  197. >
  198. <Text style={styles.saveBtnText}>{loading ? '保存中...' : '确定'}</Text>
  199. </ImageBackground>
  200. </TouchableOpacity>
  201. </ScrollView>
  202. </ImageBackground>
  203. );
  204. }
  205. const styles = StyleSheet.create({
  206. container: {
  207. flex: 1,
  208. },
  209. header: {
  210. flexDirection: 'row',
  211. alignItems: 'center',
  212. justifyContent: 'space-between',
  213. paddingHorizontal: 10,
  214. height: 80,
  215. },
  216. backBtn: {
  217. width: 40,
  218. height: 40,
  219. justifyContent: 'center',
  220. alignItems: 'center',
  221. },
  222. backIcon: {
  223. fontSize: 32,
  224. color: '#fff',
  225. fontWeight: 'bold',
  226. },
  227. headerTitle: {
  228. fontSize: 16,
  229. fontWeight: 'bold',
  230. color: '#fff',
  231. },
  232. placeholder: {
  233. width: 40,
  234. },
  235. scrollView: {
  236. flex: 1,
  237. paddingHorizontal: 20,
  238. },
  239. avatarSection: {
  240. alignItems: 'center',
  241. paddingVertical: 30,
  242. },
  243. avatarWrapper: {
  244. width: 80,
  245. height: 80,
  246. borderRadius: 40,
  247. borderWidth: 3,
  248. borderColor: '#FFE996',
  249. overflow: 'hidden',
  250. },
  251. avatar: {
  252. width: '100%',
  253. height: '100%',
  254. },
  255. avatarTip: {
  256. marginTop: 10,
  257. fontSize: 12,
  258. color: 'rgba(255,255,255,0.7)',
  259. },
  260. formSection: {
  261. backgroundColor: 'rgba(255,255,255,0.1)',
  262. borderRadius: 10,
  263. padding: 15,
  264. },
  265. formItem: {
  266. flexDirection: 'row',
  267. alignItems: 'center',
  268. paddingVertical: 15,
  269. borderBottomWidth: 1,
  270. borderBottomColor: 'rgba(255,255,255,0.2)',
  271. },
  272. formLabel: {
  273. width: 60,
  274. fontSize: 14,
  275. color: '#fff',
  276. },
  277. formInput: {
  278. flex: 1,
  279. fontSize: 14,
  280. color: '#fff',
  281. padding: 0,
  282. },
  283. sexOptions: {
  284. flex: 1,
  285. flexDirection: 'row',
  286. alignItems: 'center',
  287. },
  288. sexOption: {
  289. flexDirection: 'row',
  290. alignItems: 'center',
  291. marginRight: 20,
  292. },
  293. sexOptionActive: {},
  294. radioOuter: {
  295. width: 18,
  296. height: 18,
  297. borderRadius: 9,
  298. borderWidth: 2,
  299. borderColor: 'rgba(255,255,255,0.5)',
  300. justifyContent: 'center',
  301. alignItems: 'center',
  302. marginRight: 6,
  303. },
  304. radioOuterActive: {
  305. borderColor: '#FC7D2E',
  306. },
  307. radioInner: {
  308. width: 10,
  309. height: 10,
  310. borderRadius: 5,
  311. backgroundColor: '#FC7D2E',
  312. },
  313. sexText: {
  314. fontSize: 14,
  315. color: 'rgba(255,255,255,0.7)',
  316. },
  317. sexTextActive: {
  318. color: '#fff',
  319. },
  320. saveBtn: {
  321. marginTop: 50,
  322. alignItems: 'center',
  323. },
  324. saveBtnDisabled: {
  325. opacity: 0.6,
  326. },
  327. saveBtnBg: {
  328. width: 280,
  329. height: 60,
  330. justifyContent: 'center',
  331. alignItems: 'center',
  332. },
  333. saveBtnText: {
  334. fontSize: 16,
  335. fontWeight: 'bold',
  336. color: '#fff',
  337. textShadowColor: '#000',
  338. textShadowOffset: { width: 1, height: 1 },
  339. textShadowRadius: 2,
  340. },
  341. });