我的页面数据与数据库同步,保持一致性
问题: - “我的”页面用户数据从本地存储获取 - 与“我的账号”页面数据来源不一致 - 可能导致数据不同步 解决方案: - 修改mine.vue页面 - loadUserData方法改为从后端API获取数据 - 新增loadLocalData方法作为备用 - 数据来源与account.vue保持一致 功能特点: - 页面显示时从后端获取最新用户数据 - 数据与user_info表保持同步 - 更新本地缓存确保一致性 - 错误时使用本地数据兜底 - 优化判断条件,避免0值被误判 数据字段: - coins: 金币数 - vipLevel: VIP等级 - username: 用户名 - avatar: 头像URL - userId: 用户ID
This commit is contained in:
1 parent
a4496effae
commit
aa0e7403d6
1 file changed
+37
-4
@@ -54,6 +54,7 @@
|
||||
|
||||
<script>
|
||||
import CustomTabbar from '@/components/custom-tabbar/custom-tabbar.vue'
|
||||
import api from '@/utils/api.js'
|
||||
|
||||
export default {
|
||||
components: {
|
||||
@@ -65,6 +66,7 @@ export default {
|
||||
vipLevel: 0,
|
||||
username: '玄机用户',
|
||||
avatar: '',
|
||||
userId: '',
|
||||
menus: [
|
||||
{ name: '开通会员', icon: '👑', path: '/pages/mine/vip' },
|
||||
{ name: '金币任务', icon: '🪙', path: '/pages/mine/coins' },
|
||||
@@ -98,19 +100,50 @@ export default {
|
||||
},
|
||||
onShareTimeline() {
|
||||
return {
|
||||
title: '玄机排盘 - 专业命理测算工具',
|
||||
title: '玄机排盘 - 专业命理测算工具,八字、紫微、奇门遁甲等应有尽有!',
|
||||
query: '',
|
||||
imageUrl: '/static/share.png'
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
loadUserData() {
|
||||
async loadUserData() {
|
||||
const userInfoStr = uni.getStorageSync('userInfo')
|
||||
if (userInfoStr) {
|
||||
const localUserInfo = JSON.parse(userInfoStr)
|
||||
this.userId = localUserInfo.userId
|
||||
|
||||
try {
|
||||
const res = await api.userApi.getInfo(this.userId)
|
||||
if (res.success && res.data) {
|
||||
const userData = res.data
|
||||
this.coins = userData.coins || 0
|
||||
this.vipLevel = userData.vipLevel || 0
|
||||
this.username = userData.username || userData.nickname || '玄机用户'
|
||||
this.avatar = userData.avatarUrl || ''
|
||||
|
||||
uni.setStorageSync('userCoins', this.coins)
|
||||
uni.setStorageSync('vipLevel', this.vipLevel)
|
||||
uni.setStorageSync('username', this.username)
|
||||
uni.setStorageSync('userAvatar', this.avatar)
|
||||
uni.setStorageSync('userInfo', JSON.stringify(userData))
|
||||
} else {
|
||||
this.loadLocalData()
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('获取用户信息失败:', e)
|
||||
this.loadLocalData()
|
||||
}
|
||||
} else {
|
||||
this.loadLocalData()
|
||||
}
|
||||
},
|
||||
loadLocalData() {
|
||||
const coins = uni.getStorageSync('userCoins')
|
||||
if (coins) {
|
||||
if (coins !== null && coins !== undefined && coins !== '') {
|
||||
this.coins = parseInt(coins)
|
||||
}
|
||||
const vipLevel = uni.getStorageSync('vipLevel')
|
||||
if (vipLevel) {
|
||||
if (vipLevel !== null && vipLevel !== undefined && vipLevel !== '') {
|
||||
this.vipLevel = parseInt(vipLevel)
|
||||
}
|
||||
const username = uni.getStorageSync('username')
|
||||
|
||||
Reference in new issue
Block a user