diff --git a/uniapp/pages/mine/profile/index.vue b/uniapp/pages/mine/profile/index.vue index 9661694..36c13ec 100644 --- a/uniapp/pages/mine/profile/index.vue +++ b/uniapp/pages/mine/profile/index.vue @@ -2,61 +2,44 @@ - - - 头像 - - + + + - + - - - 昵称 - - {{ userInfo.nickname || '未设置' }} - - + + + 昵称 + - - - UID - - {{ userInfo.uid || '--' }} - + + + UID + - - - - - - 修改昵称 - - - - - - - 取消 - - - 确定 - - + + + @@ -76,19 +59,23 @@ * 展示和修改用户头像、昵称 * UID 仅展示不可修改 */ -import { ref, computed, onMounted } from 'vue' +import { ref, reactive, computed, onMounted } from 'vue' import { onShow } from '@dcloudio/uni-app' import { useUserStore } from '@/store/user.js' -import { getProfile, updateProfile, updateAvatar } from '@/api/user.js' +import { updateProfile, updateAvatar } from '@/api/user.js' import config from '@/config/index.js' const userStore = useUserStore() // 状态 const loading = ref(false) +const saving = ref(false) const loadingText = ref('加载中...') -const nicknamePopupVisible = ref(false) -const newNickname = ref('') + +// 表单数据 +const formData = reactive({ + nickname: '' +}) // 用户信息 const userInfo = computed(() => ({ @@ -99,26 +86,10 @@ const userInfo = computed(() => ({ })) /** - * 获取用户资料 + * 初始化表单数据 */ -async function fetchProfile() { - try { - loading.value = true - loadingText.value = '加载中...' - - const res = await getProfile() - if (res.code === 0 && res.data) { - userStore.updateUserInfo(res.data) - } - } catch (error) { - console.error('获取用户资料失败:', error) - uni.showToast({ - title: '获取资料失败', - icon: 'none' - }) - } finally { - loading.value = false - } +function initFormData() { + formData.nickname = userStore.nickname || '' } /** @@ -135,10 +106,7 @@ function handleChangeAvatar() { }, fail: (err) => { if (err.errMsg && !err.errMsg.includes('cancel')) { - uni.showToast({ - title: '选择图片失败', - icon: 'none' - }) + uni.showToast({ title: '选择图片失败', icon: 'none' }) } } }) @@ -152,8 +120,7 @@ async function uploadAvatar(filePath) { try { loading.value = true loadingText.value = '上传中...' - - // 上传图片到服务器 + const uploadRes = await new Promise((resolve, reject) => { uni.uploadFile({ url: `${config.API_BASE_URL}/upload/image`, @@ -165,8 +132,7 @@ async function uploadAvatar(filePath) { success: (res) => { if (res.statusCode === 200) { try { - const data = JSON.parse(res.data) - resolve(data) + resolve(JSON.parse(res.data)) } catch (e) { reject(new Error('解析响应失败')) } @@ -177,18 +143,14 @@ async function uploadAvatar(filePath) { fail: (err) => reject(err) }) }) - + if (uploadRes.code === 0 && uploadRes.data) { - // 更新头像 const avatarUrl = uploadRes.data.url || uploadRes.data const updateRes = await updateAvatar(avatarUrl) - + if (updateRes.code === 0) { userStore.updateUserInfo({ avatar: avatarUrl }) - uni.showToast({ - title: '头像更新成功', - icon: 'success' - }) + uni.showToast({ title: '头像更新成功', icon: 'success' }) } else { throw new Error(updateRes.message || '更新头像失败') } @@ -197,253 +159,159 @@ async function uploadAvatar(filePath) { } } catch (error) { console.error('上传头像失败:', error) - uni.showToast({ - title: error.message || '上传失败', - icon: 'none' - }) + uni.showToast({ title: error.message || '上传失败', icon: 'none' }) } finally { loading.value = false } } /** - * 显示修改昵称弹窗 + * 保存资料(昵称) */ -function showNicknamePopup() { - newNickname.value = userInfo.value.nickname || '' - nicknamePopupVisible.value = true -} +async function handleSave() { + const nickname = formData.nickname.trim() -/** - * 隐藏修改昵称弹窗 - */ -function hideNicknamePopup() { - nicknamePopupVisible.value = false - newNickname.value = '' -} - -/** - * 更新昵称 - */ -async function handleUpdateNickname() { - const nickname = newNickname.value.trim() - if (!nickname) { - uni.showToast({ - title: '请输入昵称', - icon: 'none' - }) + uni.showToast({ title: '请输入昵称', icon: 'none' }) return } - - if (nickname === userInfo.value.nickname) { - hideNicknamePopup() + + if (nickname === userStore.nickname) { + uni.showToast({ title: '资料未修改', icon: 'none' }) return } - + try { + saving.value = true loading.value = true loadingText.value = '保存中...' - hideNicknamePopup() - + const res = await updateProfile({ nickname }) - + if (res.code === 0) { userStore.updateUserInfo({ nickname }) - uni.showToast({ - title: '昵称更新成功', - icon: 'success' - }) + uni.showToast({ title: '保存成功', icon: 'success' }) } else { - throw new Error(res.message || '更新失败') + throw new Error(res.message || '保存失败') } } catch (error) { - console.error('更新昵称失败:', error) - uni.showToast({ - title: error.message || '更新失败', - icon: 'none' - }) + console.error('保存资料失败:', error) + uni.showToast({ title: error.message || '保存失败', icon: 'none' }) } finally { + saving.value = false loading.value = false } } -/** - * 页面显示时刷新数据 - */ +// Lifecycle onShow(() => { - userStore.restoreFromStorage() + initFormData() }) -/** - * 页面加载 - */ onMounted(() => { userStore.restoreFromStorage() - fetchProfile() + initFormData() }) diff --git a/uniapp/static/mine/icon-user-icon-edit.png b/uniapp/static/mine/icon-user-icon-edit.png new file mode 100644 index 0000000..6670c70 Binary files /dev/null and b/uniapp/static/mine/icon-user-icon-edit.png differ