Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x | /**
* 个人资料头部 - 头像、用户名和简介
*/
import { useState } from 'react';
import { Alert, TouchableOpacity, View } from 'react-native';
import * as ImagePicker from 'expo-image-picker';
import { LinearGradient } from 'expo-linear-gradient';
import { Avatar, Spinner, Text, YStack } from 'tamagui';
import { IconSymbol } from '@/src/components/ui/IconSymbol';
import { useUserStore } from '@/src/store/userStore';
import { useThemeColors, useIsDarkMode } from '@/src/hooks/useThemeColors';
interface ProfileHeaderProps {
username?: string;
bio?: string;
onAvatarUpdate?: () => void;
equippedBadge?: { icon: string; color: string; gradient?: string[] } | null;
}
export function ProfileHeader({
username = '未登录',
bio = '这个人很懒,什么都没留下~',
onAvatarUpdate,
equippedBadge,
}: ProfileHeaderProps) {
const { user, uploadAvatar, deleteAvatar } = useUserStore();
const colors = useThemeColors();
const isDark = useIsDarkMode();
const [uploading, setUploading] = useState(false);
const [cacheBuster, setCacheBuster] = useState(0);
const avatarUrl = user?.avatarUrl ?? null;
const avatarSrc = avatarUrl ? `${avatarUrl}?v=${cacheBuster}` : null;
// 拍照
const pickFromCamera = async () => {
try {
const cameraPerm = await ImagePicker.requestCameraPermissionsAsync();
if (cameraPerm.status !== 'granted') {
Alert.alert('需要权限', '请允许相机权限或从相册选择图片');
return pickFromLibrary();
}
const result = await ImagePicker.launchCameraAsync({
quality: 0.8,
allowsEditing: true,
aspect: [1, 1],
});
if ('canceled' in result && !result.canceled && result.assets?.[0]) {
await doUploadAvatar(result.assets[0].uri);
}
} catch (e) {
console.warn('相机错误:', e);
}
};
// 从相册选择
const pickFromLibrary = async () => {
try {
const libPerm = await ImagePicker.requestMediaLibraryPermissionsAsync();
if (libPerm.status !== 'granted') {
Alert.alert('需要权限', '请允许访问相册以选择图片');
return;
}
const result = await ImagePicker.launchImageLibraryAsync({
quality: 0.8,
allowsEditing: true,
aspect: [1, 1],
mediaTypes: ['images'],
});
if ('canceled' in result && !result.canceled && result.assets?.[0]) {
await doUploadAvatar(result.assets[0].uri);
}
} catch (e) {
console.warn('相册错误:', e);
}
};
// 执行上传头像
const doUploadAvatar = async (uri: string) => {
try {
setUploading(true);
await uploadAvatar(uri);
setCacheBuster((v) => v + 1);
Alert.alert('成功', '头像已更新');
onAvatarUpdate?.();
} catch (e: unknown) {
const message = e instanceof Error ? e.message : '请稍后再试';
Alert.alert('上传失败', message);
} finally {
setUploading(false);
}
};
// 删除头像
const handleDeleteAvatar = async () => {
try {
setUploading(true);
await deleteAvatar();
setCacheBuster((v) => v + 1);
onAvatarUpdate?.();
Alert.alert('成功', '头像已删除');
} catch (e: unknown) {
const message = e instanceof Error ? e.message : '请稍后再试';
Alert.alert('删除失败', message);
} finally {
setUploading(false);
}
};
// 点击头像
const onPressAvatar = () => {
const actions: { text: string; style?: 'cancel' | 'destructive'; onPress?: () => void }[] = [
{ text: '取消', style: 'cancel' },
{ text: '从相册选择', onPress: pickFromLibrary },
{ text: '拍照', onPress: pickFromCamera },
];
if (avatarUrl) {
actions.push({
text: '删除头像',
style: 'destructive',
onPress: handleDeleteAvatar,
});
}
Alert.alert('更换头像', '请选择图片来源', actions);
};
return (
<YStack width="100%" alignItems="center" position="relative" paddingBottom="$4">
{/* 顶部渐变背景 - 美化版本 */}
<YStack width="100%" height={200} position="relative" overflow="hidden">
{/* 主渐变层 */}
<LinearGradient
colors={
isDark
? (['#2D1F1A', '#3D2A1F', '#1F1714', colors.background] as const)
: (['#FEBE98', '#FFD5C4', '#FFE4D9', '#FFF8F5'] as const)
}
start={{ x: 0, y: 0 }}
end={{ x: 0, y: 1 }}
style={{ position: 'absolute', top: 0, left: 0, right: 0, bottom: 0 }}
/>
{/* 次级渐变层 - 增加深度 */}
<LinearGradient
colors={
isDark
? (['rgba(254, 190, 152, 0.05)', 'transparent', 'transparent'] as const)
: (['rgba(255, 255, 255, 0.3)', 'transparent', 'transparent'] as const)
}
start={{ x: 0, y: 0 }}
end={{ x: 1, y: 0 }}
style={{ position: 'absolute', top: 0, left: 0, right: 0, bottom: 0 }}
/>
{/* 装饰性图案 - 圆形 1 (大) */}
<YStack
position="absolute"
top={-50}
right={-50}
width={180}
height={180}
borderRadius={90}
backgroundColor={isDark ? 'rgba(254, 190, 152, 0.04)' : 'rgba(255, 255, 255, 0.15)'}
style={{
shadowColor: isDark ? '#FEBE98' : '#FFFFFF',
shadowOffset: { width: 0, height: 0 },
shadowOpacity: isDark ? 0.1 : 0.3,
shadowRadius: 30,
}}
/>
{/* 装饰性图案 - 圆形 2 (中) */}
<YStack
position="absolute"
bottom={-30}
left={-30}
width={120}
height={120}
borderRadius={60}
backgroundColor={isDark ? 'rgba(254, 190, 152, 0.06)' : 'rgba(255, 255, 255, 0.2)'}
/>
{/* 装饰性图案 - 小方形 (左上) */}
<YStack
position="absolute"
top={20}
left={20}
width={50}
height={50}
borderRadius="$6"
backgroundColor={isDark ? 'rgba(255, 255, 255, 0.02)' : 'rgba(255, 255, 255, 0.12)'}
transform={[{ rotate: '15deg' }]}
/>
{/* 装饰性图案 - 小圆形 (右下) */}
<YStack
position="absolute"
bottom={40}
right={40}
width={30}
height={30}
borderRadius={15}
backgroundColor={isDark ? 'rgba(255, 255, 255, 0.03)' : 'rgba(255, 255, 255, 0.18)'}
/>
{/* 波浪纹理效果 */}
<YStack
position="absolute"
top={60}
left={-100}
width={300}
height={100}
borderRadius={150}
backgroundColor={isDark ? 'rgba(254, 190, 152, 0.02)' : 'rgba(255, 255, 255, 0.08)'}
transform={[{ scaleX: 2 }]}
/>
</YStack>
{/* 头像区域 - 美化版本 */}
<YStack position="absolute" top={90} alignItems="center" zIndex={10}>
<TouchableOpacity testID="user-avatar" onPress={onPressAvatar} activeOpacity={0.85}>
<YStack position="relative" alignItems="center">
{/* 外层光晕 */}
<YStack
position="absolute"
width={148}
height={148}
borderRadius={74}
backgroundColor={isDark ? 'rgba(254, 190, 152, 0.15)' : 'rgba(254, 190, 152, 0.25)'}
style={{
shadowColor: colors.primary,
shadowOffset: { width: 0, height: 0 },
shadowOpacity: isDark ? 0.3 : 0.5,
shadowRadius: 20,
}}
/>
{/* 中层装饰环 */}
<YStack
position="absolute"
width={138}
height={138}
borderRadius={69}
borderWidth={3}
borderColor={isDark ? 'rgba(254, 190, 152, 0.2)' : 'rgba(255, 255, 255, 0.6)'}
style={{
shadowColor: isDark ? colors.primary : '#FFFFFF',
shadowOffset: { width: 0, height: 4 },
shadowOpacity: 0.3,
shadowRadius: 8,
}}
/>
{/* 头像主体 */}
<YStack
borderRadius={64}
overflow="hidden"
borderWidth={4}
borderColor={colors.cardBackground as any}
style={{
shadowColor: isDark ? '#000000' : colors.primary,
shadowOffset: { width: 0, height: 8 },
shadowOpacity: isDark ? 0.4 : 0.25,
shadowRadius: 16,
elevation: 12,
}}
>
<Avatar circular size={128} borderWidth={0} elevation={0}>
{uploading ? (
<Avatar.Fallback
backgroundColor={colors.backgroundMuted as any}
justifyContent="center"
alignItems="center"
>
<Spinner size="large" color={colors.error as any} />
</Avatar.Fallback>
) : avatarSrc ? (
<Avatar.Image src={avatarSrc} />
) : (
<Avatar.Fallback
backgroundColor={colors.primary as any}
justifyContent="center"
alignItems="center"
>
{/* 渐变背景 */}
<LinearGradient
colors={
isDark
? ([colors.primary, colors.primaryDark] as const)
: ([colors.primaryLight, colors.primary] as const)
}
start={{ x: 0, y: 0 }}
end={{ x: 1, y: 1 }}
style={{
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
width: 128,
height: 128,
alignItems: 'center',
justifyContent: 'center',
}}
/>
<IconSymbol name="person.fill" size={50} color="white" />
</Avatar.Fallback>
)}
</Avatar>
</YStack>
{/* 相机按钮 - 增强版 */}
{!uploading && (
<YStack
position="absolute"
bottom={4}
right={4}
width={40}
height={40}
borderRadius={20}
overflow="hidden"
style={{
shadowColor: colors.primary,
shadowOffset: { width: 0, height: 4 },
shadowOpacity: 0.3,
shadowRadius: 8,
elevation: 8,
}}
>
<LinearGradient
colors={
isDark
? ([colors.primary, colors.primaryDark] as const)
: ([colors.primary, colors.primaryDark] as const)
}
start={{ x: 0, y: 0 }}
end={{ x: 1, y: 1 }}
style={{
width: 40,
height: 40,
alignItems: 'center',
justifyContent: 'center',
borderWidth: 3,
borderColor: colors.cardBackground as any,
borderRadius: 20,
}}
>
<IconSymbol name="camera.fill" size={18} color="white" />
</LinearGradient>
</YStack>
)}
</YStack>
</TouchableOpacity>
</YStack>
{/* 用户信息 */}
<YStack width="100%" alignItems="center" gap="$2.5" paddingTop={40} paddingBottom="$1">
{/* 用户名和勋章 */}
<YStack alignItems="center" gap="$1.5">
<View testID="user-username">
<Text
fontSize={24}
fontWeight="700"
color={colors.text as any}
textAlign="center"
numberOfLines={1}
>
{username}
</Text>
</View>
{/* 已装备的勋章 */}
{equippedBadge && (
<YStack
flexDirection="row"
alignItems="center"
gap="$1.5"
paddingHorizontal="$2.5"
paddingVertical="$1"
backgroundColor={(equippedBadge.color + '15') as any}
borderRadius={12}
borderWidth={1}
borderColor={(equippedBadge.color + '30') as any}
>
{equippedBadge.gradient ? (
<LinearGradient
colors={equippedBadge.gradient as [string, string]}
style={{
width: 18,
height: 18,
borderRadius: 9,
alignItems: 'center',
justifyContent: 'center',
}}
>
<IconSymbol name={equippedBadge.icon as any} size={12} color="white" />
</LinearGradient>
) : (
<IconSymbol
name={equippedBadge.icon as any}
size={16}
color={equippedBadge.color}
/>
)}
</YStack>
)}
</YStack>
<Text
fontSize={12}
color={colors.textSecondary as any}
textAlign="center"
numberOfLines={2}
paddingHorizontal="$6"
lineHeight={18}
>
{bio}
</Text>
</YStack>
</YStack>
);
}
|