feat: 头像上传/修改 + 个人资料连贯设置面板
- 头像:users.avatar 列 + POST /api/auth/avatar(OSS 上传/清除)+ toPublicUser 返回 avatar; user store updateAvatar + avatar 计算(自定义优先,ui-avatars 兜底) - 共享 saveImage 工具(admin 上传与头像复用,消除重复) - 个人资料布局:单一连贯设置面板(divide-y 分区:基本信息含头像编辑 / 安全设置 / 危险区), 去掉小卡片堆叠;i18n 头像键
This commit is contained in:
+25
-2
@@ -6,6 +6,7 @@ export interface User {
|
||||
username: string
|
||||
email: string
|
||||
phone: string | null
|
||||
avatar: string | null
|
||||
role: 'user' | 'admin'
|
||||
createdAt: string
|
||||
}
|
||||
@@ -90,8 +91,29 @@ export const useUserStore = defineStore('user', () => {
|
||||
}
|
||||
}
|
||||
|
||||
// 头像(按用户名生成,无后端存储)
|
||||
const avatar = computed(() => (user.value ? AVATAR_URL(user.value.username) : ''))
|
||||
// 更新头像(上传后由接口返回新 user,直接替换)
|
||||
const updateAvatar = async (file: File | null): Promise<ApiResult> => {
|
||||
try {
|
||||
if (file) {
|
||||
const fd = new FormData()
|
||||
fd.append('file', file)
|
||||
const data = await $fetch<{ user: User }>('/api/auth/avatar', { method: 'POST', body: fd })
|
||||
user.value = data.user
|
||||
} else {
|
||||
const data = await $fetch<{ user: User }>('/api/auth/avatar', { method: 'POST', body: { clear: true } })
|
||||
user.value = data.user
|
||||
}
|
||||
return { success: true }
|
||||
} catch (err) {
|
||||
return { success: false, code: (err as any)?.data?.code || 'SERVER_ERROR' }
|
||||
}
|
||||
}
|
||||
|
||||
// 头像:用户自定义优先,否则按用户名生成
|
||||
const avatar = computed(() => {
|
||||
if (!user.value) return ''
|
||||
return user.value.avatar || AVATAR_URL(user.value.username)
|
||||
})
|
||||
|
||||
return {
|
||||
user,
|
||||
@@ -102,5 +124,6 @@ export const useUserStore = defineStore('user', () => {
|
||||
logout,
|
||||
restoreUser,
|
||||
updateProfile,
|
||||
updateAvatar,
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user