我的账号页面数据与数据库同步,前后端API联调
后端实现: - UserController已有/user/info接口,返回用户完整信息 - 包含userId、username、nickname、phone、avatarUrl等字段 - 数据与user_info表对应 前端实现: - 修改account.vue页面 - loadUserInfo方法改为从后端API获取数据 - 添加更多用户字段:nickname、vipLevel、coins、streakDays - 绑定手机号后同步更新本地存储 - 优化用户名显示逻辑:优先username,其次nickname API优化: - api.js新增userApi.getInfo方法 - 与getUserInfo方法功能相同 功能特点: - 页面显示时从后端获取最新用户数据 - 数据与数据库保持同步 - 绑定手机号后更新本地缓存 - 错误处理更友好
This commit is contained in:
1 parent
52131ca4a5
commit
a4496effae
2 files changed
+35
-9
No files matched your search
@@ -3,7 +3,7 @@
|
||||
<view class="user-info-card">
|
||||
<image class="avatar-img" :src="userInfo.avatarUrl || defaultAvatar" mode="aspectFit" />
|
||||
<view class="user-detail">
|
||||
<text class="username">{{ userInfo.username || '玄机用户' }}</text>
|
||||
<text class="username">{{ userInfo.username || userInfo.nickname || '玄机用户' }}</text>
|
||||
<text class="user-id">ID: {{ userInfo.userId || '100001' }}</text>
|
||||
</view>
|
||||
</view>
|
||||
@@ -56,37 +56,56 @@ export default {
|
||||
userId: '',
|
||||
username: '玄机用户',
|
||||
avatarUrl: '',
|
||||
phone: ''
|
||||
}
|
||||
phone: '',
|
||||
nickname: '',
|
||||
vipLevel: 0,
|
||||
coins: 0,
|
||||
streakDays: 0
|
||||
},
|
||||
defaultAvatar: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
|
||||
}
|
||||
},
|
||||
onShow() {
|
||||
this.loadUserInfo()
|
||||
},
|
||||
methods: {
|
||||
loadUserInfo() {
|
||||
async loadUserInfo() {
|
||||
const userInfoStr = uni.getStorageSync('userInfo')
|
||||
if (userInfoStr) {
|
||||
this.userInfo = JSON.parse(userInfoStr)
|
||||
const localUserInfo = JSON.parse(userInfoStr)
|
||||
try {
|
||||
const res = await api.userApi.getInfo(localUserInfo.userId)
|
||||
if (res.success && res.data) {
|
||||
this.userInfo = res.data
|
||||
uni.setStorageSync('userInfo', JSON.stringify(res.data))
|
||||
} else {
|
||||
this.userInfo = localUserInfo
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('获取用户信息失败:', e)
|
||||
this.userInfo = localUserInfo
|
||||
}
|
||||
}
|
||||
},
|
||||
bindPhone() {
|
||||
async bindPhone() {
|
||||
uni.showModal({
|
||||
title: '绑定手机号',
|
||||
editable: true,
|
||||
placeholderText: '请输入手机号',
|
||||
success: async (res) => {
|
||||
if (res.confirm && res.content) {
|
||||
this.userInfo.phone = res.content
|
||||
try {
|
||||
await api.userApi.updatePhone(this.userInfo.userId, this.userInfo.phone)
|
||||
await api.userApi.updatePhone(this.userInfo.userId, res.content)
|
||||
this.userInfo.phone = res.content
|
||||
uni.setStorageSync('userInfo', JSON.stringify(this.userInfo))
|
||||
uni.showToast({
|
||||
title: '绑定成功',
|
||||
icon: 'success'
|
||||
})
|
||||
} catch (e) {
|
||||
console.error('绑定手机号失败:', e)
|
||||
uni.showToast({
|
||||
title: '绑定失败',
|
||||
title: '绑定失败:' + (e.message || '未知错误'),
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
|
||||
@@ -346,6 +346,13 @@ const userApi = {
|
||||
data: { userId: userId }
|
||||
})
|
||||
},
|
||||
getInfo: (userId) => {
|
||||
return request({
|
||||
url: '/user/info',
|
||||
method: 'GET',
|
||||
data: { userId: userId }
|
||||
})
|
||||
},
|
||||
updateVipLevel: (userId, vipLevel) => {
|
||||
return request({
|
||||
url: '/user/vip',
|
||||
|
||||
Reference in new issue
Block a user