2026-04-17 10:49:14 +08:00
|
|
|
|
const request = require('../../utils/request')
|
2026-05-18 14:24:55 +08:00
|
|
|
|
const { resolveImageUrl, resolveImageList } = require('../../utils/media')
|
2026-04-17 10:49:14 +08:00
|
|
|
|
|
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 21:28:56 +08:00
|
|
|
|
function normalizeAvatarState(userInfo) {
|
2026-05-18 14:24:55 +08:00
|
|
|
|
const profileImages = resolveImageList(userInfo.profile_images)
|
2026-04-17 21:28:56 +08:00
|
|
|
|
return {
|
|
|
|
|
|
...userInfo,
|
|
|
|
|
|
avatar_url: resolveImageUrl(userInfo.avatar_url || ''),
|
2026-05-17 10:23:02 +08:00
|
|
|
|
avatar_blur_url: resolveImageUrl(userInfo.avatar_blur_url || ''),
|
|
|
|
|
|
profile_images: profileImages
|
2026-04-17 21:28:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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-06-18 23:06:40 +08:00
|
|
|
|
// 保护缓存中的头像:如果服务端返回空 avatar_url,保留缓存中的值
|
|
|
|
|
|
const cached = wx.getStorageSync('profile_page_cache') || {}
|
|
|
|
|
|
if (!userInfo.avatar_url && cached.avatar_url) {
|
|
|
|
|
|
userInfo.avatar_url = cached.avatar_url
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!userInfo.avatar_blur_url && cached.avatar_blur_url) {
|
|
|
|
|
|
userInfo.avatar_blur_url = cached.avatar_blur_url
|
|
|
|
|
|
}
|
|
|
|
|
|
if ((!userInfo.profile_images || !userInfo.profile_images.length) && cached.profile_images && cached.profile_images.length) {
|
|
|
|
|
|
userInfo.profile_images = cached.profile_images
|
|
|
|
|
|
}
|
2026-04-17 19:26:32 +08:00
|
|
|
|
this.applyUserInfo(userInfo)
|
2026-04-17 21:28:56 +08:00
|
|
|
|
wx.setStorageSync('profile_page_cache', normalizeAvatarState(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()
|
2026-04-17 21:28:56 +08:00
|
|
|
|
const normalized = normalizeAvatarState(userInfo)
|
2026-06-18 23:06:40 +08:00
|
|
|
|
// 优先使用 avatar_url,其次使用 profile_images[0]
|
|
|
|
|
|
const previewImage = normalized.avatar_url
|
|
|
|
|
|
|| (normalized.profile_images && normalized.profile_images.length ? normalized.profile_images[0] : '')
|
2026-04-17 19:26:32 +08:00
|
|
|
|
this.setData({
|
|
|
|
|
|
userInfo: {
|
2026-04-17 21:28:56 +08:00
|
|
|
|
...normalized,
|
|
|
|
|
|
avatar_url: addCacheVersion(normalized.avatar_url, avatarVersion),
|
|
|
|
|
|
avatar_blur_url: addCacheVersion(normalized.avatar_blur_url, avatarVersion),
|
2026-06-18 23:06:40 +08:00
|
|
|
|
previewImage: addCacheVersion(previewImage, avatarVersion),
|
2026-04-17 21:28:56 +08:00
|
|
|
|
nicknameText: normalized.nickname || '未设置昵称',
|
|
|
|
|
|
auditStatusText: this.data.auditTextMap[normalized.audit_status] || '未知'
|
2026-04-17 19:26:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
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' })
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|