设置页面修改昵称和头像,修改后端数据成功

This commit is contained in:
WindowBird 2025-08-20 10:12:49 +08:00
parent 1e9088bf2b
commit 72e7bd790f
4 changed files with 68 additions and 51 deletions

View File

@ -123,6 +123,8 @@ export function updateNickName(nickName) {
method: 'PUT',
params: { nickName },
showLoading: true,
// 强制将params作为查询参数发送
useQueryParams: true,
}).catch(error => {
console.warn('更新昵称API调用失败:', error)
throw error

View File

@ -3,7 +3,7 @@ export const DEV_CONFIG = {
// 临时token用于开发测试
TEMP_TOKEN:
'\n' +
'eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjIyMzRmNTVlLTQ4NTEtNDQwOS05MjIwLWQ0M2M0NzMwZDgzMiJ9.yWNuS7YH_MTYW5Tf3VjzEHki0z4erEwHkb6CVO7Ix7p_zvbfMyc9XHsP80DRczWEuXlglfMQV8EbYtkTBLj8Rg',
'eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjY2M2UzOWVkLTg4N2MtNGUxOS1iZDBiLWFmZTY1ZmI3Mjk2YiJ9.nMIcrWJK3l5itjQH-okwKL2X4Tresr_sKmgMQ66nHsjYGHK9Xyz5YHO2oDeF-sPt1BxHbz4fyBXcSWhr1HwWTQ',
// 是否使用临时token
USE_TEMP_TOKEN: true,

View File

@ -1,13 +1,18 @@
<template>
<view class="page">
<view class="info">
<view v-for="(item, index) in userInfoSettings" :key="index" class="info-row" @click="handleItemClick(item)">
<view
v-for="(item, index) in userInfoSettings"
:key="index"
class="info-row"
@click="handleItemClick(item)"
>
<view class="label">{{ item.label }}</view>
<view class="value">
<image
v-if="item.type === 'avatar' && item.value"
:src="item.value"
class="avatar-preview"
<image
v-if="item.type === 'avatar' && item.value"
:src="item.value"
class="avatar-preview"
mode="aspectFill"
/>
<text v-else-if="item.type === 'avatar'">点击设置头像</text>
@ -45,10 +50,10 @@ export default {
onLoad() {
console.log('设置页面开始加载...')
console.log('初始userInfoSettings:', this.userInfoSettings)
//
this.loadUserInfo()
console.log('设置页面加载完成')
},
onShow() {
@ -59,24 +64,24 @@ export default {
//
loadUserInfo() {
console.log('开始加载用户信息...')
try {
const userInfo = uni.getStorageSync('userInfo')
console.log('从本地存储获取的用户信息:', userInfo)
//
if (!userInfo) {
console.log('本地存储中没有用户信息')
this.setDefaultValues()
return
}
if (typeof userInfo !== 'object') {
console.log('用户信息格式不正确,不是对象类型')
this.setDefaultValues()
return
}
//
const nicknameItem = this.userInfoSettings.find(item => item.type === 'nickname')
if (nicknameItem) {
@ -84,7 +89,7 @@ export default {
nicknameItem.value = nickName
console.log('设置昵称显示:', nickName)
}
//
const avatarItem = this.userInfoSettings.find(item => item.type === 'avatar')
if (avatarItem) {
@ -92,29 +97,28 @@ export default {
avatarItem.value = avatar
console.log('设置头像显示:', avatar)
}
console.log('用户信息加载成功')
} catch (error) {
console.error('加载用户信息时发生错误:', error)
this.setDefaultValues()
}
},
//
setDefaultValues() {
console.log('设置默认值...')
const nicknameItem = this.userInfoSettings.find(item => item.type === 'nickname')
if (nicknameItem) {
nicknameItem.value = '昵称'
}
const avatarItem = this.userInfoSettings.find(item => item.type === 'avatar')
if (avatarItem) {
avatarItem.value = ''
}
console.log('默认值设置完成')
},
@ -126,7 +130,7 @@ export default {
//
uni.showToast({
title: '头像修改功能开发中',
icon: 'none'
icon: 'none',
})
}
},
@ -134,15 +138,15 @@ export default {
//
showNicknameInput() {
const currentNickname = this.userInfoSettings.find(item => item.type === 'nickname').value
uni.showModal({
title: '修改昵称',
content: '请输入新的昵称',
content: '',
editable: true,
placeholderText: '请输入昵称',
confirmText: '确定',
cancelText: '取消',
success: async (res) => {
success: async res => {
if (res.confirm && res.content) {
const newNickname = res.content.trim()
if (newNickname) {
@ -150,11 +154,11 @@ export default {
} else {
uni.showToast({
title: '昵称不能为空',
icon: 'none'
icon: 'none',
})
}
}
}
},
})
},
@ -162,11 +166,11 @@ export default {
async updateNickname(newNickname) {
try {
uni.showLoading({
title: '更新中...'
title: '更新中...',
})
const response = await updateNickName(newNickname)
uni.hideLoading()
if (response.code === 200) {
@ -187,21 +191,21 @@ export default {
uni.showToast({
title: '昵称更新成功',
icon: 'success'
icon: 'success',
})
//
try {
const userInfo = uni.getStorageSync('userInfo') || {}
uni.$emit('userInfoUpdated', {
nickName: newNickname,
avatar: userInfo.avatar || ''
avatar: userInfo.avatar || '',
})
} catch (error) {
console.error('获取用户信息失败:', error)
uni.$emit('userInfoUpdated', {
nickName: newNickname,
avatar: ''
avatar: '',
})
}
} else {
@ -212,7 +216,7 @@ export default {
console.error('更新昵称失败:', error)
uni.showToast({
title: error.message || '更新失败',
icon: 'none'
icon: 'none',
})
}
},
@ -228,35 +232,35 @@ export default {
confirmText: '确定',
cancelText: '取消',
success: resolve,
fail: reject
fail: reject,
})
})
if (!res.confirm) {
return
}
// 退API
const response = await userLogout()
if (response.code === 200) {
// token
clearToken()
//
uni.removeStorageSync('userInfo')
//
uni.showToast({
title: '退出成功',
icon: 'success',
duration: 1500
duration: 1500,
})
//
setTimeout(() => {
uni.reLaunch({
url: '/pages/login/login'
url: '/pages/login/login',
})
}, 1500)
} else {
@ -267,10 +271,10 @@ export default {
uni.showToast({
title: error.message || '退出失败',
icon: 'none',
duration: 2000
duration: 2000,
})
}
}
},
},
}
</script>
@ -304,7 +308,7 @@ export default {
margin: 0 34rpx;
cursor: pointer;
transition: background-color 0.2s ease;
&:active {
background-color: #f5f5f5;
}
@ -323,14 +327,14 @@ export default {
display: flex;
align-items: center;
justify-content: flex-end;
.avatar-preview {
width: 60rpx;
height: 60rpx;
border-radius: 50%;
margin-right: 10rpx;
}
.arrow {
margin-left: 10rpx;
color: #bbbbbb;
@ -352,7 +356,7 @@ export default {
border-radius: 24.5px;
cursor: pointer;
transition: all 0.3s ease;
&:active {
background: #f5f5f5;
transform: scale(0.98);
@ -372,7 +376,7 @@ export default {
border-radius: 24.5px;
cursor: pointer;
transition: all 0.3s ease;
&:active {
background: #d0d0d0;
transform: scale(0.98);
@ -392,7 +396,7 @@ export default {
border-radius: 24.5px;
cursor: pointer;
transition: all 0.3s ease;
&:active {
background: #d0d0d0;
transform: scale(0.98);
@ -412,7 +416,7 @@ export default {
border-radius: 24.5px;
cursor: pointer;
transition: all 0.3s ease;
&:active {
background: #d0d0d0;
transform: scale(0.98);

View File

@ -246,7 +246,16 @@ export function request(options = {}) {
// 处理请求参数
if (options.params && Object.keys(options.params).length > 0) {
requestOptions.data = { ...options.params }
// 对于GET请求或明确指定使用查询参数的请求params作为查询参数
if (requestOptions.method === 'GET' || options.useQueryParams) {
const queryString = Object.keys(options.params)
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(options.params[key])}`)
.join('&')
requestOptions.url += (requestOptions.url.includes('?') ? '&' : '?') + queryString
} else {
// 对于其他请求方法params作为请求体数据
requestOptions.data = { ...options.params }
}
} else if (options.data && Object.keys(options.data).length > 0) {
requestOptions.data = { ...options.data }
} else {
@ -277,6 +286,8 @@ export function request(options = {}) {
data: requestOptions.data,
timeout: requestOptions.timeout,
baseUrl: BASE_URL,
useQueryParams: options.useQueryParams,
hasParams: !!(options.params && Object.keys(options.params).length > 0),
})
// 显示loading默认显示但减少延迟