This commit is contained in:
2026-09-13 01:51:09 +08:00
parent b1a664a768
commit afd0973ce5
32 changed files with 935 additions and 171 deletions
+40 -9
View File
@@ -29,6 +29,7 @@ Page({
editVisible: false,
avatarUploading: false,
avatarChanged: false,
saving: false,
editForm: {
username: '',
sex: 0,
@@ -40,6 +41,11 @@ Page({
},
onLoad() {
this.setToday()
},
// 跨零点后回到本页时,生日选择器上限需重算,不能停留在昨天
setToday() {
const d = new Date()
const pad = (n: number) => (n < 10 ? `0${n}` : `${n}`)
this.setData({ today: `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}` })
@@ -47,6 +53,7 @@ Page({
onShow() {
this.syncTabBar()
this.setToday()
// 未登录直接回登录页(幂等跳转),不再发出必然 401 的请求
if (!auth.isLoggedIn()) {
redirectToLogin()
@@ -113,6 +120,7 @@ Page({
wx.showActionSheet({
itemList: ['男', '女'],
success: (res) => this.setData({ 'editForm.sex': res.tapIndex + 1 }),
fail: () => {}, // 取消选择属正常操作,不必抛错日志
})
},
@@ -123,29 +131,52 @@ Page({
},
onSaveProfile() {
if (this.data.avatarUploading) return
if (this.data.avatarUploading || this.data.saving) return
const f = this.data.editForm
const avatar = f.avatar || ''
const username = (f.username || '').trim()
if (!username) {
wx.showToast({ title: '昵称不能为空', icon: 'none' })
return
}
const finish = () => {
this.setData({
editVisible: false,
avatarUploading: false,
saving: false,
avatarChanged: false,
})
this.fetchUser()
}
const saveProfile = () => {
const u = this.data.userInfo
const body: Record<string, any> = {}
if (f.username) body.username = f.username
if (f.sex) body.sex = f.sex
if (f.birth_day) body.birth_day = f.birth_day
// 只提交真正变更的字段,避免无意义请求与误清空
if (username !== (u.username || '')) body.username = username
if (f.sex && f.sex !== u.sex) body.sex = f.sex
if (f.birth_day && f.birth_day !== u.birth_day) body.birth_day = f.birth_day
if (!Object.keys(body).length && !this.data.avatarChanged) {
this.setData({ editVisible: false })
return
}
this.setData({ saving: true })
request({ url: '/user/profile', method: 'PUT', data: body })
.then(() => {
wx.showToast({ title: '已保存', icon: 'success' })
this.setData({ editVisible: false, avatarUploading: false })
this.fetchUser()
finish()
})
.catch(() => this.setData({ avatarUploading: false }))
.catch(() => this.setData({ avatarUploading: false, saving: false }))
}
if (this.data.avatarChanged && avatar) {
this.setData({ avatarUploading: true })
uploadFile<{ avatar: string }>(avatar, 'file', '/user/avatar/upload')
.then(() => saveProfile())
.catch(() => this.setData({ avatarUploading: false }))
.catch(() => this.setData({ avatarUploading: false, saving: false }))
} else {
saveProfile()
}
@@ -161,7 +192,7 @@ Page({
wx.navigateTo({ url: '/pages/notification/notification' })
},
onTapCollect() {
wx.setStorageSync('zt_open_collect', 1)
wx.setStorageSync(STORAGE_KEYS.OPEN_COLLECT, 1)
wx.switchTab({ url: '/pages/outfit/outfit' })
},
onTapFlow() {