committed by
trae-solo-agent
co-authored by
Kimi-K3.1-Pro
parent
d731fc1da3
commit
a45a3d7a86
+30
-29
@@ -1,5 +1,7 @@
|
||||
// pages/mine/profileedit.js
|
||||
const API = require('../../utils/api.js')
|
||||
const { formatDate, toISOString } = require('../../utils/date.js')
|
||||
const { setUnsavedGuard } = require('../../utils/form.js')
|
||||
|
||||
Page({
|
||||
data: {
|
||||
@@ -18,13 +20,28 @@ Page({
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
const today = new Date()
|
||||
this.setData({
|
||||
today: `${today.getFullYear()}-${String(today.getMonth() + 1).padStart(2, '0')}-${String(today.getDate()).padStart(2, '0')}`
|
||||
})
|
||||
this.unsavedGuardActive = false
|
||||
this.initialEditSnapshot = ''
|
||||
this.setData({ today: formatDate(new Date()) })
|
||||
this.initEditData()
|
||||
},
|
||||
|
||||
onUnload() {
|
||||
if (this.unsavedGuardActive) setUnsavedGuard(false)
|
||||
},
|
||||
|
||||
captureInitialForm() {
|
||||
this.initialEditSnapshot = JSON.stringify(this.data.editData)
|
||||
this.syncUnsavedGuard()
|
||||
},
|
||||
|
||||
syncUnsavedGuard() {
|
||||
const dirty = JSON.stringify(this.data.editData) !== this.initialEditSnapshot
|
||||
if (dirty === this.unsavedGuardActive) return
|
||||
this.unsavedGuardActive = dirty
|
||||
setUnsavedGuard(dirty)
|
||||
},
|
||||
|
||||
// 表单初值取自全局用户对象。页面栈上「我的」页的 onShow 读的也是它,
|
||||
// 所以保存成功后必须回写 app.globalData.user,否则返回时看到的还是旧值
|
||||
initEditData() {
|
||||
@@ -33,52 +50,33 @@ Page({
|
||||
const profile = user.profile || {}
|
||||
const location = user.location || {}
|
||||
|
||||
let birth = ''
|
||||
if (profile.birth) {
|
||||
const date = new Date(profile.birth)
|
||||
birth = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`
|
||||
}
|
||||
|
||||
this.setData({
|
||||
editData: {
|
||||
// 空字符串表示"无真头像",模板据此渲染浅橙底 + 暖陶土 user 线标的默认头像
|
||||
// 头像修改暂未开放,这里只展示当前头像或默认头像。
|
||||
avatar: profile.avatar || '',
|
||||
name: profile.name || '',
|
||||
phone: profile.mobile || '',
|
||||
sex: profile.sex || '',
|
||||
birth,
|
||||
birth: formatDate(profile.birth),
|
||||
province: location.province || '',
|
||||
city: location.city || '',
|
||||
district: location.district || ''
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
onChooseAvatar() {
|
||||
wx.chooseMedia({
|
||||
count: 1,
|
||||
mediaType: ['image'],
|
||||
sourceType: ['album', 'camera'],
|
||||
success: (res) => {
|
||||
// 微信返回的是本地临时路径,没有文件上传接口可落库,
|
||||
// 故只在本次编辑中生效
|
||||
this.setData({
|
||||
'editData.avatar': res.tempFiles[0].tempFilePath
|
||||
})
|
||||
}
|
||||
})
|
||||
}, () => this.captureInitialForm())
|
||||
},
|
||||
|
||||
onSelectSex(e) {
|
||||
this.setData({
|
||||
'editData.sex': e.currentTarget.dataset.sex
|
||||
})
|
||||
this.syncUnsavedGuard()
|
||||
},
|
||||
|
||||
onBirthChange(e) {
|
||||
this.setData({
|
||||
'editData.birth': e.detail.value
|
||||
})
|
||||
this.syncUnsavedGuard()
|
||||
},
|
||||
|
||||
onRegionChange(e) {
|
||||
@@ -88,6 +86,7 @@ Page({
|
||||
'editData.city': region[1] || '',
|
||||
'editData.district': region[2] || ''
|
||||
})
|
||||
this.syncUnsavedGuard()
|
||||
},
|
||||
|
||||
onFieldInput(e) {
|
||||
@@ -95,6 +94,7 @@ Page({
|
||||
this.setData({
|
||||
[`editData.${field}`]: e.detail.value
|
||||
})
|
||||
this.syncUnsavedGuard()
|
||||
},
|
||||
|
||||
onSave() {
|
||||
@@ -117,7 +117,7 @@ Page({
|
||||
profile: {
|
||||
name: editData.name,
|
||||
sex: editData.sex,
|
||||
birth: editData.birth ? new Date(editData.birth).toISOString() : null,
|
||||
birth: toISOString(editData.birth),
|
||||
mobile: editData.phone
|
||||
},
|
||||
location: {
|
||||
@@ -135,6 +135,7 @@ Page({
|
||||
// 整替会把本机 token 抹掉,之后所有接口都变成未登录。security 保留本地那份
|
||||
const prev = app.globalData.user || {}
|
||||
app.globalData.user = { ...prev, ...data.data.user, security: prev.security || {} }
|
||||
this.captureInitialForm()
|
||||
wx.showToast({ title: '保存成功', icon: 'success' })
|
||||
// 返回上一页(「我的」),由它的 onShow 重新读取全局对象
|
||||
wx.navigateBack()
|
||||
|
||||
Reference in New Issue
Block a user