AvatarSettingPage.vue
2.7 KB
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
<!--
* @Date: 2025-03-24 13:04:21
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-03-26 00:46:59
* @FilePath: /mlaj/src/views/profile/settings/AvatarSettingPage.vue
* @Description: 修改头像页面
-->
<template>
<AppLayout title="">
<div
class="bg-gradient-to-br from-green-50 via-green-100/30 to-blue-50/30 min-h-screen"
>
<div class="px-4 py-6">
<FrostedGlass class="rounded-xl overflow-hidden">
<div class="p-4">
<div class="flex flex-col items-center">
<div class="mb-6">
<van-image
round
:src="
userAvatar || 'https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg'
"
alt="用户头像"
class="rounded-full border-4 border-white shadow-lg"
width="8rem"
height="8rem"
fit="cover"
/>
</div>
<p class="text-sm text-gray-500 mb-4">支持 jpg、png 格式,大小不超过 2MB</p>
<van-uploader
:after-read="handleAvatarChange"
:max-size="2 * 1024 * 1024"
accept="image/jpeg,image/png"
:preview-image="false"
>
<van-button
block
type="primary"
class="max-w-xs"
>
选择新头像
</van-button>
</van-uploader>
</div>
</div>
</FrostedGlass>
</div>
</div>
</AppLayout>
</template>
<script setup>
import { ref, onMounted } from "vue";
import AppLayout from "@/components/layout/AppLayout.vue";
import FrostedGlass from "@/components/ui/FrostedGlass.vue";
import { getUserInfoAPI, updateUserInfoAPI } from "@/api/users";
import { useTitle } from '@vueuse/core';
const $route = useRoute();
useTitle($route.meta.title);
// 用户头像
const userAvatar = ref("");
// 获取用户信息
onMounted(async () => {
try {
const response = await getUserInfoAPI();
if (response.data) {
userAvatar.value = response.data.user.avatar;
}
} catch (error) {
console.error("获取用户信息失败:", error);
}
});
// 处理头像上传
const handleAvatarChange = async (file) => {
try {
const formData = new FormData();
formData.append("avatar", file);
const response = await updateUserInfoAPI(formData);
if (response.data) {
userAvatar.value = response.data.url;
alert("头像上传成功");
}
} catch (error) {
console.error("头像上传失败:", error);
alert("头像上传失败,请重试");
}
};
</script>