mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 05:09:19 +08:00
fix(profile): 头像延迟上传 + 修复表单字段绑定
1. 头像不再选择后立即上传,改为缓存到 pendingAvatarFile,保存时再上传 2. 修复 formData 使用方式:reactive 对象直接用 formData.xxx,不需要 .value 3. 增加 avatarImageId 字段到表单数据 4. 头像预览使用本地 URL.createObjectURL 临时展示
This commit is contained in:
@@ -17,11 +17,16 @@ const submitting = ref(false);
|
|||||||
const userInfo = ref<UserVO.UserInfoVO | null>(null);
|
const userInfo = ref<UserVO.UserInfoVO | null>(null);
|
||||||
const avatarLoading = ref(false);
|
const avatarLoading = ref(false);
|
||||||
|
|
||||||
|
// 待上传的头像文件(用户选择后先缓存,保存时再上传)
|
||||||
|
const pendingAvatarFile = ref<File | null>(null);
|
||||||
|
const previewAvatarUrl = ref<string | null>(null);
|
||||||
|
|
||||||
// 表单数据
|
// 表单数据
|
||||||
const formData = reactive({
|
const formData = reactive({
|
||||||
realName: '',
|
realName: '',
|
||||||
phone: '',
|
phone: '',
|
||||||
remark: '',
|
remark: '',
|
||||||
|
avatarImageId: null as number | null,
|
||||||
});
|
});
|
||||||
|
|
||||||
const formRef = ref<any>(null);
|
const formRef = ref<any>(null);
|
||||||
@@ -31,11 +36,12 @@ async function fetchUserInfo() {
|
|||||||
loading.value = true;
|
loading.value = true;
|
||||||
const data = await getUserInfoApi();
|
const data = await getUserInfoApi();
|
||||||
userInfo.value = data;
|
userInfo.value = data;
|
||||||
formData.value = {
|
formData.realName = data.realName || '';
|
||||||
realName: data.realName || '',
|
formData.phone = data.phone || '';
|
||||||
phone: data.phone || '',
|
formData.remark = data.remark || '';
|
||||||
remark: data.remark || '',
|
// 重置头像预览
|
||||||
};
|
previewAvatarUrl.value = null;
|
||||||
|
pendingAvatarFile.value = null;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('获取用户信息失败:', error);
|
console.error('获取用户信息失败:', error);
|
||||||
message.error('获取用户信息失败');
|
message.error('获取用户信息失败');
|
||||||
@@ -44,7 +50,10 @@ async function fetchUserInfo() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleAvatarUpload(file: File) {
|
async function handleAvatarChange(info: any) {
|
||||||
|
const file = info.file.originFileObj || info.file;
|
||||||
|
if (!file) return false;
|
||||||
|
|
||||||
if (!file.type.startsWith('image/')) {
|
if (!file.type.startsWith('image/')) {
|
||||||
message.error('只能上传图片文件');
|
message.error('只能上传图片文件');
|
||||||
return false;
|
return false;
|
||||||
@@ -54,21 +63,9 @@ async function handleAvatarUpload(file: File) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
// 缓存头像文件,预览用本地 URL
|
||||||
avatarLoading.value = true;
|
pendingAvatarFile.value = file;
|
||||||
const fileInfo = await uploadFileToOss(file, {
|
previewAvatarUrl.value = URL.createObjectURL(file);
|
||||||
category: 'user-avatar',
|
|
||||||
errorMessage: '头像上传失败',
|
|
||||||
});
|
|
||||||
// 更新用户头像
|
|
||||||
await updateUserInfoApi({ avatarImageId: fileInfo.id });
|
|
||||||
userInfo.value!.avatar = fileInfo.storagePath;
|
|
||||||
message.success('头像上传成功');
|
|
||||||
} catch (error) {
|
|
||||||
console.error('头像上传失败:', error);
|
|
||||||
} finally {
|
|
||||||
avatarLoading.value = false;
|
|
||||||
}
|
|
||||||
return false; // 阻止默认上传
|
return false; // 阻止默认上传
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,12 +73,26 @@ async function handleSubmit() {
|
|||||||
try {
|
try {
|
||||||
await formRef.value?.validate();
|
await formRef.value?.validate();
|
||||||
submitting.value = true;
|
submitting.value = true;
|
||||||
await updateUserInfoApi(formData.value);
|
|
||||||
|
// 如果有待上传的头像,先上传
|
||||||
|
if (pendingAvatarFile.value) {
|
||||||
|
avatarLoading.value = true;
|
||||||
|
try {
|
||||||
|
const fileInfo = await uploadFileToOss(pendingAvatarFile.value, {
|
||||||
|
category: 'user-avatar',
|
||||||
|
errorMessage: '头像上传失败',
|
||||||
|
});
|
||||||
|
formData.avatarImageId = fileInfo.id;
|
||||||
|
} finally {
|
||||||
|
avatarLoading.value = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await updateUserInfoApi(formData);
|
||||||
message.success('保存成功');
|
message.success('保存成功');
|
||||||
await fetchUserInfo();
|
await fetchUserInfo();
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
if (error?.errorFields) {
|
if (error?.errorFields) {
|
||||||
// 表单验证错误
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.error('保存失败:', error);
|
console.error('保存失败:', error);
|
||||||
@@ -93,13 +104,13 @@ async function handleSubmit() {
|
|||||||
|
|
||||||
function handleReset() {
|
function handleReset() {
|
||||||
if (userInfo.value) {
|
if (userInfo.value) {
|
||||||
formData.value = {
|
formData.realName = userInfo.value.realName || '';
|
||||||
realName: userInfo.value.realName || '',
|
formData.phone = userInfo.value.phone || '';
|
||||||
phone: userInfo.value.phone || '',
|
formData.remark = userInfo.value.remark || '';
|
||||||
remark: userInfo.value.remark || '',
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
formRef.value?.resetFields();
|
formRef.value?.resetFields();
|
||||||
|
previewAvatarUrl.value = null;
|
||||||
|
pendingAvatarFile.value = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
fetchUserInfo();
|
fetchUserInfo();
|
||||||
@@ -132,18 +143,18 @@ fetchUserInfo();
|
|||||||
<div class="flex items-center">
|
<div class="flex items-center">
|
||||||
<div class="mr-6">
|
<div class="mr-6">
|
||||||
<Upload
|
<Upload
|
||||||
:before-upload="handleAvatarUpload"
|
:before-upload="handleAvatarChange"
|
||||||
:show-upload-list="false"
|
:show-upload-list="false"
|
||||||
accept="image/*"
|
accept="image/*"
|
||||||
>
|
>
|
||||||
<div class="relative cursor-pointer">
|
<div class="relative cursor-pointer">
|
||||||
<Spin
|
<Spin
|
||||||
v-if="avatarLoading"
|
v-if="avatarLoading"
|
||||||
class="absolute inset-0 flex items-center justify-center bg-white/80"
|
class="absolute inset-0 flex items-center justify-center bg-white/80 z-10"
|
||||||
/>
|
/>
|
||||||
<img
|
<img
|
||||||
v-if="userInfo?.avatar"
|
v-if="previewAvatarUrl || userInfo?.avatar"
|
||||||
:src="userInfo.avatar"
|
:src="previewAvatarUrl || userInfo?.avatar"
|
||||||
alt="头像"
|
alt="头像"
|
||||||
class="h-24 w-24 rounded-full object-cover"
|
class="h-24 w-24 rounded-full object-cover"
|
||||||
/>
|
/>
|
||||||
@@ -164,7 +175,7 @@ fetchUserInfo();
|
|||||||
<div>
|
<div>
|
||||||
<p class="font-medium text-slate-800">头像</p>
|
<p class="font-medium text-slate-800">头像</p>
|
||||||
<p class="mt-1 text-xs text-slate-500">
|
<p class="mt-1 text-xs text-slate-500">
|
||||||
点击图片上传,建议大小不超过 2MB
|
点击选择新头像,保存后生效,建议大小不超过 2MB
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user