xiangqinxiaochengxu/miniprogram/pages/profile/profile.js

84 lines
1.9 KiB
JavaScript
Raw Normal View History

2026-04-17 10:49:14 +08:00
const request = require('../../utils/request')
2026-04-17 19:26:32 +08:00
function addCacheVersion(url, version) {
if (!url) {
return ''
}
const joiner = url.includes('?') ? '&' : '?'
return `${url}${joiner}v=${encodeURIComponent(version)}`
}
2026-04-17 10:49:14 +08:00
Page({
data: {
loading: false,
userInfo: null,
auditTextMap: {
0: '未提交',
1: '待审核',
2: '已通过',
3: '已驳回'
}
},
onShow() {
2026-04-17 19:26:32 +08:00
this.refreshFromCache()
this.loadProfile(true)
2026-04-17 10:49:14 +08:00
},
2026-04-17 19:26:32 +08:00
async loadProfile(silent = false) {
if (!silent) {
this.setData({ loading: true })
}
2026-04-17 10:49:14 +08:00
try {
const userInfo = await request({ url: '/users/me' })
2026-04-17 19:26:32 +08:00
this.applyUserInfo(userInfo)
wx.setStorageSync('profile_page_cache', userInfo)
2026-04-17 10:49:14 +08:00
} catch (err) {
console.error('Load profile failed:', err)
2026-04-17 19:26:32 +08:00
if (!this.data.userInfo) {
this.setData({ userInfo: null })
}
2026-04-17 10:49:14 +08:00
} finally {
2026-04-17 19:26:32 +08:00
if (!silent) {
this.setData({ loading: false })
}
}
},
applyUserInfo(userInfo) {
const avatarVersion = Date.now().toString()
this.setData({
userInfo: {
...userInfo,
avatar_url: addCacheVersion(userInfo.avatar_url || '', avatarVersion),
avatar_blur_url: addCacheVersion(userInfo.avatar_blur_url || '', avatarVersion),
nicknameText: userInfo.nickname || '未设置昵称',
auditStatusText: this.data.auditTextMap[userInfo.audit_status] || '未知'
}
})
},
refreshFromCache() {
const cached = wx.getStorageSync('profile_page_cache')
if (cached) {
this.applyUserInfo(cached)
2026-04-17 10:49:14 +08:00
}
},
goEdit() {
wx.navigateTo({ url: '/pages/profile-edit/profile-edit' })
},
goAudit() {
wx.navigateTo({ url: '/pages/audit-status/audit-status' })
},
goMyActivities() {
wx.navigateTo({ url: '/pages/my-activities/my-activities' })
},
goMyMatches() {
wx.navigateTo({ url: '/pages/my-matches/my-matches' })
}
})