优化:用户名更改接口

This commit is contained in:
ivan 2026-03-22 19:17:09 +08:00
parent 4c0cf38bac
commit c6bb01f777
3 changed files with 19 additions and 7 deletions

View File

@ -146,12 +146,12 @@ export async function setSelfHeaderImg(
/**
* PUT /user/setSelfUsername
*
* Body: { username: string }
*
* Body: { nickName: string }
* ApiKeyAuth
*/
export interface ChangeSelfUsernameReq {
username: string
nickName: string
}
export async function setSelfUsername(

View File

@ -60,6 +60,16 @@ export const useUserStore = defineStore('user', () => {
const user = ref<UserInfo | null>(stored?.user ?? null)
const isLoggedIn = computed(() => !!token.value && !!user.value)
/** 显示用用户名:优先 nickName其次 userName */
const displayName = computed(() => {
const u = user.value
if (!u) return ''
const nick = (u.nickName ?? (u as Record<string, unknown>).nickname) as string | undefined
if (typeof nick === 'string' && nick.trim()) return nick.trim()
const name = (u.userName ?? (u as Record<string, unknown>).username) as string | undefined
if (typeof name === 'string' && name.trim()) return name.trim()
return ''
})
const avatarUrl = computed(() => {
const img = user.value?.headerImg
if (!img) return ''
@ -220,6 +230,7 @@ export const useUserStore = defineStore('user', () => {
token,
user,
isLoggedIn,
displayName,
avatarUrl,
balance,
setUser,

View File

@ -195,7 +195,8 @@ const userNameRaw = computed(() => {
const v = rawUser.value.userName
return typeof v === 'string' && v.trim() ? v.trim() : ''
})
const displayName = computed(() => userNameRaw.value || t('profile.defaultName'))
/** 显示用:统一取 nickNameuserStore.displayName */
const displayName = computed(() => userStore.displayName || t('profile.defaultName'))
const userIdText = computed(() => {
const uid = rawUser.value.id ?? rawUser.value.ID
if (uid == null || uid === '') return '--'
@ -262,7 +263,7 @@ async function copyWalletAddress() {
function onEditProfile() {
editNameDialogOpen.value = true
editingName.value = userNameRaw.value
editingName.value = userStore.displayName || userNameRaw.value
nameError.value = null
}
@ -311,7 +312,7 @@ async function saveName() {
return
}
const res = await setSelfUsername(authHeaders, { username: trimmed })
const res = await setSelfUsername(authHeaders, { nickName: trimmed })
if (res.code !== 0 && res.code !== 200) {
const errMsg = res.msg || t('profile.nameSaveFailed')
nameError.value = errMsg
@ -319,7 +320,7 @@ async function saveName() {
return
}
// userName/headerImg
// nickName/headerImg
await userStore.fetchUserInfo()
toastStore.show(t('profile.nameSaved'))
editNameDialogOpen.value = false