优化:用户名更改接口
This commit is contained in:
parent
4c0cf38bac
commit
c6bb01f777
@ -146,12 +146,12 @@ export async function setSelfHeaderImg(
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* PUT /user/setSelfUsername
|
* PUT /user/setSelfUsername
|
||||||
* 修改自身用户名
|
* 修改自身昵称
|
||||||
* Body: { username: string }
|
* Body: { nickName: string }
|
||||||
* 需鉴权(ApiKeyAuth)
|
* 需鉴权(ApiKeyAuth)
|
||||||
*/
|
*/
|
||||||
export interface ChangeSelfUsernameReq {
|
export interface ChangeSelfUsernameReq {
|
||||||
username: string
|
nickName: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function setSelfUsername(
|
export async function setSelfUsername(
|
||||||
|
|||||||
@ -60,6 +60,16 @@ export const useUserStore = defineStore('user', () => {
|
|||||||
const user = ref<UserInfo | null>(stored?.user ?? null)
|
const user = ref<UserInfo | null>(stored?.user ?? null)
|
||||||
|
|
||||||
const isLoggedIn = computed(() => !!token.value && !!user.value)
|
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 avatarUrl = computed(() => {
|
||||||
const img = user.value?.headerImg
|
const img = user.value?.headerImg
|
||||||
if (!img) return ''
|
if (!img) return ''
|
||||||
@ -220,6 +230,7 @@ export const useUserStore = defineStore('user', () => {
|
|||||||
token,
|
token,
|
||||||
user,
|
user,
|
||||||
isLoggedIn,
|
isLoggedIn,
|
||||||
|
displayName,
|
||||||
avatarUrl,
|
avatarUrl,
|
||||||
balance,
|
balance,
|
||||||
setUser,
|
setUser,
|
||||||
|
|||||||
@ -195,7 +195,8 @@ const userNameRaw = computed(() => {
|
|||||||
const v = rawUser.value.userName
|
const v = rawUser.value.userName
|
||||||
return typeof v === 'string' && v.trim() ? v.trim() : ''
|
return typeof v === 'string' && v.trim() ? v.trim() : ''
|
||||||
})
|
})
|
||||||
const displayName = computed(() => userNameRaw.value || t('profile.defaultName'))
|
/** 显示用:统一取 nickName(userStore.displayName) */
|
||||||
|
const displayName = computed(() => userStore.displayName || t('profile.defaultName'))
|
||||||
const userIdText = computed(() => {
|
const userIdText = computed(() => {
|
||||||
const uid = rawUser.value.id ?? rawUser.value.ID
|
const uid = rawUser.value.id ?? rawUser.value.ID
|
||||||
if (uid == null || uid === '') return '--'
|
if (uid == null || uid === '') return '--'
|
||||||
@ -262,7 +263,7 @@ async function copyWalletAddress() {
|
|||||||
|
|
||||||
function onEditProfile() {
|
function onEditProfile() {
|
||||||
editNameDialogOpen.value = true
|
editNameDialogOpen.value = true
|
||||||
editingName.value = userNameRaw.value
|
editingName.value = userStore.displayName || userNameRaw.value
|
||||||
nameError.value = null
|
nameError.value = null
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -311,7 +312,7 @@ async function saveName() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const res = await setSelfUsername(authHeaders, { username: trimmed })
|
const res = await setSelfUsername(authHeaders, { nickName: trimmed })
|
||||||
if (res.code !== 0 && res.code !== 200) {
|
if (res.code !== 0 && res.code !== 200) {
|
||||||
const errMsg = res.msg || t('profile.nameSaveFailed')
|
const errMsg = res.msg || t('profile.nameSaveFailed')
|
||||||
nameError.value = errMsg
|
nameError.value = errMsg
|
||||||
@ -319,7 +320,7 @@ async function saveName() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 接口成功后刷新用户信息,确保 userName/headerImg 等字段一致
|
// 接口成功后刷新用户信息,确保 nickName/headerImg 等字段一致
|
||||||
await userStore.fetchUserInfo()
|
await userStore.fetchUserInfo()
|
||||||
toastStore.show(t('profile.nameSaved'))
|
toastStore.show(t('profile.nameSaved'))
|
||||||
editNameDialogOpen.value = false
|
editNameDialogOpen.value = false
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user