优化:用户名更改接口
This commit is contained in:
parent
4c0cf38bac
commit
c6bb01f777
@ -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(
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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'))
|
||||
/** 显示用:统一取 nickName(userStore.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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user