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

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,7 +1,12 @@
<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
@ -94,7 +99,6 @@ export default {
}
console.log('用户信息加载成功')
} catch (error) {
console.error('加载用户信息时发生错误:', error)
this.setDefaultValues()
@ -126,7 +130,7 @@ export default {
//
uni.showToast({
title: '头像修改功能开发中',
icon: 'none'
icon: 'none',
})
}
},
@ -137,12 +141,12 @@ export default {
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,7 +166,7 @@ export default {
async updateNickname(newNickname) {
try {
uni.showLoading({
title: '更新中...'
title: '更新中...',
})
const response = await updateNickName(newNickname)
@ -187,7 +191,7 @@ export default {
uni.showToast({
title: '昵称更新成功',
icon: 'success'
icon: 'success',
})
//
@ -195,13 +199,13 @@ export default {
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,7 +232,7 @@ export default {
confirmText: '确定',
cancelText: '取消',
success: resolve,
fail: reject
fail: reject,
})
})
@ -250,13 +254,13 @@ export default {
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>

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默认显示但减少延迟