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

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', method: 'PUT',
params: { nickName }, params: { nickName },
showLoading: true, showLoading: true,
// 强制将params作为查询参数发送
useQueryParams: true,
}).catch(error => { }).catch(error => {
console.warn('更新昵称API调用失败:', error) console.warn('更新昵称API调用失败:', error)
throw error throw error

View File

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

View File

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

View File

@ -246,7 +246,16 @@ export function request(options = {}) {
// 处理请求参数 // 处理请求参数
if (options.params && Object.keys(options.params).length > 0) { 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) { } else if (options.data && Object.keys(options.data).length > 0) {
requestOptions.data = { ...options.data } requestOptions.data = { ...options.data }
} else { } else {
@ -277,6 +286,8 @@ export function request(options = {}) {
data: requestOptions.data, data: requestOptions.data,
timeout: requestOptions.timeout, timeout: requestOptions.timeout,
baseUrl: BASE_URL, baseUrl: BASE_URL,
useQueryParams: options.useQueryParams,
hasParams: !!(options.params && Object.keys(options.params).length > 0),
}) })
// 显示loading默认显示但减少延迟 // 显示loading默认显示但减少延迟