fix: 修改头像
This commit is contained in:
parent
9b34b51caf
commit
e89b3c0bd8
|
|
@ -1,3 +1,4 @@
|
|||
import * as ImagePicker from 'expo-image-picker'
|
||||
import { Ionicons } from '@expo/vector-icons'
|
||||
import { Block, ConfirmModal, Img, Input, Text, Toast } from '@share/components'
|
||||
import { router, Stack } from 'expo-router'
|
||||
|
|
@ -5,8 +6,10 @@ import { observer } from 'mobx-react-lite'
|
|||
import React, { useEffect, useState } from 'react'
|
||||
import { ScrollView } from 'react-native'
|
||||
|
||||
import { imgPicker } from '@/@share/apis'
|
||||
import { authClient } from '@/lib/auth'
|
||||
import { userStore } from '@/stores'
|
||||
import { uploadFile } from '@/utils'
|
||||
|
||||
type InfoItem = {
|
||||
id: string
|
||||
|
|
@ -55,9 +58,41 @@ function EditNicknameModal({ initialName, onConfirm, onCancel }: EditNicknameMod
|
|||
export default observer(function ProfilePage() {
|
||||
const { user } = userStore
|
||||
|
||||
const handleEditAvatar = () => {
|
||||
// TODO: 打开相册/拍照更换头像
|
||||
Toast.show({ title: '更换头像功能开发中' })
|
||||
const handleEditAvatar = async () => {
|
||||
try {
|
||||
const assets = await imgPicker({
|
||||
maxImages: 1,
|
||||
type: ImagePicker.MediaTypeOptions.Images,
|
||||
resultType: 'asset',
|
||||
})
|
||||
const asset = assets[0] as ImagePicker.ImagePickerAsset
|
||||
if (!asset) return
|
||||
|
||||
Toast.showLoading({ title: '上传中...', duration: 0 })
|
||||
const url = await uploadFile({
|
||||
uri: asset.uri,
|
||||
mimeType: asset.mimeType ?? 'image/jpeg',
|
||||
fileName: asset.fileName ?? `avatar_${Date.now()}.jpg`,
|
||||
})
|
||||
|
||||
const res = await authClient.updateUser({ image: url })
|
||||
const err = (res as { error?: { message?: string } }).error
|
||||
if (err) {
|
||||
Toast.hideLoading()
|
||||
Toast.show({ title: err.message || '头像更新失败,请重试' })
|
||||
return
|
||||
}
|
||||
if (userStore.user) {
|
||||
userStore.setUser({ ...userStore.user, image: url })
|
||||
}
|
||||
Toast.hideLoading()
|
||||
Toast.show({ title: '头像已更新' })
|
||||
} catch (e) {
|
||||
Toast.hideLoading()
|
||||
if (e instanceof Error && e.message !== '未选择任何图片') {
|
||||
Toast.show({ title: '上传失败,请重试' })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const handleEditNickname = () => {
|
||||
|
|
|
|||
|
|
@ -106,12 +106,12 @@ export default observer(function SettingsPage() {
|
|||
)}
|
||||
</Block>
|
||||
<Block className="flex-1">
|
||||
<Text className="text-[16px] font-[700] text-black">{user?.name || user?.email || '未登录'}</Text>
|
||||
{user?.email && user?.name && (
|
||||
<Text className="mt-[4px] text-[12px] text-gray-500">{user.email}</Text>
|
||||
<Text className="text-[16px] font-[700] text-black">{user?.name || user?.phoneNumber || '未登录'}</Text>
|
||||
{user?.phoneNumber && user?.name && (
|
||||
<Text className="mt-[4px] text-[12px] text-gray-500">{user.phoneNumber}</Text>
|
||||
)}
|
||||
</Block>
|
||||
<Ionicons color="#9CA3AF" name="chevron-forward" size={20} />
|
||||
<Ionicons color="#323232" name="chevron-forward" size={20} />
|
||||
</Block>
|
||||
)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue