|
|
|
@ -1,10 +1,10 @@ |
|
|
|
<script setup lang="ts"> |
|
|
|
import { useAuthSession } from '../../../composables/useAuthSession' |
|
|
|
import { request, unwrapApiBody, type ApiResponse } from '../../../utils/http/factory' |
|
|
|
|
|
|
|
definePageMeta({ title: '资料' }) |
|
|
|
|
|
|
|
const { refresh: refreshAuthSession } = useAuthSession() |
|
|
|
const { fetchData, getApiErrorMessage } = useClientApi() |
|
|
|
|
|
|
|
type ProfileGet = { |
|
|
|
profile: { |
|
|
|
@ -62,11 +62,11 @@ async function onAvatarFileChange(ev: Event) { |
|
|
|
try { |
|
|
|
const form = new FormData() |
|
|
|
form.append('file', file) |
|
|
|
const res = await request<ApiResponse<{ files: { url: string }[] }>>('/api/file/upload', { |
|
|
|
const { files } = await fetchData<{ files: { url: string }[] }>('/api/file/upload', { |
|
|
|
method: 'POST', |
|
|
|
body: form, |
|
|
|
notify: false, |
|
|
|
}) |
|
|
|
const { files } = unwrapApiBody(res) |
|
|
|
const url = files[0]?.url |
|
|
|
if (!url) { |
|
|
|
message.value = '上传未返回地址' |
|
|
|
@ -75,10 +75,7 @@ async function onAvatarFileChange(ev: Event) { |
|
|
|
state.avatar = url |
|
|
|
message.value = '头像已上传,请点击下方「保存」写入资料' |
|
|
|
} catch (e: unknown) { |
|
|
|
message.value = |
|
|
|
typeof e === 'object' && e !== null && 'statusMessage' in e |
|
|
|
? String((e as { statusMessage: string }).statusMessage) |
|
|
|
: '头像上传失败' |
|
|
|
message.value = getApiErrorMessage(e) |
|
|
|
} finally { |
|
|
|
uploadingAvatar.value = false |
|
|
|
} |
|
|
|
@ -96,11 +93,11 @@ async function onHeaderIconFileChange(ev: Event) { |
|
|
|
try { |
|
|
|
const form = new FormData() |
|
|
|
form.append('file', file) |
|
|
|
const res = await request<ApiResponse<{ files: { url: string }[] }>>('/api/file/upload', { |
|
|
|
const { files } = await fetchData<{ files: { url: string }[] }>('/api/file/upload', { |
|
|
|
method: 'POST', |
|
|
|
body: form, |
|
|
|
notify: false, |
|
|
|
}) |
|
|
|
const { files } = unwrapApiBody(res) |
|
|
|
const url = files[0]?.url |
|
|
|
if (!url) { |
|
|
|
message.value = '上传未返回地址' |
|
|
|
@ -109,10 +106,7 @@ async function onHeaderIconFileChange(ev: Event) { |
|
|
|
state.publicHomeHeaderIconUrl = url |
|
|
|
message.value = '顶栏图标已上传,请点击下方「保存」写入' |
|
|
|
} catch (e: unknown) { |
|
|
|
message.value = |
|
|
|
typeof e === 'object' && e !== null && 'statusMessage' in e |
|
|
|
? String((e as { statusMessage: string }).statusMessage) |
|
|
|
: '顶栏图标上传失败' |
|
|
|
message.value = getApiErrorMessage(e) |
|
|
|
} finally { |
|
|
|
uploadingHeaderIcon.value = false |
|
|
|
} |
|
|
|
@ -121,12 +115,12 @@ async function onHeaderIconFileChange(ev: Event) { |
|
|
|
async function load() { |
|
|
|
loading.value = true |
|
|
|
try { |
|
|
|
const [profileRes, meCfgRes] = await Promise.all([ |
|
|
|
request<ApiResponse<ProfileGet>>('/api/me/profile'), |
|
|
|
request<ApiResponse<MeConfigGet>>('/api/config/me'), |
|
|
|
const [profilePayload, meCfgPayload] = await Promise.all([ |
|
|
|
fetchData<ProfileGet>('/api/me/profile'), |
|
|
|
fetchData<MeConfigGet>('/api/config/me'), |
|
|
|
]) |
|
|
|
const p = unwrapApiBody(profileRes).profile |
|
|
|
const cfg = unwrapApiBody(meCfgRes).config |
|
|
|
const p = profilePayload.profile |
|
|
|
const cfg = meCfgPayload.config |
|
|
|
state.nickname = p.nickname ?? '' |
|
|
|
state.avatar = p.avatar ?? '' |
|
|
|
state.avatarVisibility = p.avatarVisibility |
|
|
|
@ -155,8 +149,9 @@ async function save() { |
|
|
|
message.value = '社交链接 JSON 无效' |
|
|
|
return |
|
|
|
} |
|
|
|
await request('/api/me/profile', { |
|
|
|
await fetchData('/api/me/profile', { |
|
|
|
method: 'PUT', |
|
|
|
notify: false, |
|
|
|
body: { |
|
|
|
nickname: state.nickname || null, |
|
|
|
avatar: state.avatar || null, |
|
|
|
@ -168,12 +163,14 @@ async function save() { |
|
|
|
}, |
|
|
|
}) |
|
|
|
await Promise.all([ |
|
|
|
request('/api/config/me', { |
|
|
|
fetchData('/api/config/me', { |
|
|
|
method: 'PUT', |
|
|
|
notify: false, |
|
|
|
body: { key: 'publicHomeHeaderTitle', value: state.publicHomeHeaderTitle }, |
|
|
|
}), |
|
|
|
request('/api/config/me', { |
|
|
|
fetchData('/api/config/me', { |
|
|
|
method: 'PUT', |
|
|
|
notify: false, |
|
|
|
body: { key: 'publicHomeHeaderIconUrl', value: state.publicHomeHeaderIconUrl }, |
|
|
|
}), |
|
|
|
]) |
|
|
|
@ -185,10 +182,7 @@ async function save() { |
|
|
|
} |
|
|
|
await load() |
|
|
|
} catch (e: unknown) { |
|
|
|
message.value = |
|
|
|
typeof e === 'object' && e !== null && 'statusMessage' in e |
|
|
|
? String((e as { statusMessage: string }).statusMessage) |
|
|
|
: '保存失败' |
|
|
|
message.value = getApiErrorMessage(e) |
|
|
|
} finally { |
|
|
|
saving.value = false |
|
|
|
} |
|
|
|
|