feat(me): 个人信息支持编辑姓名、性别、生日与地区
保存走 user/update 并回写全局用户(保留 token);取消编辑丢弃修改;修正生日默认值显示为 1970-01-01 的问题。
This commit is contained in:
+150
-2
@@ -16,7 +16,25 @@ Page({
|
||||
isAttendant: false,
|
||||
serviceAreaLabels: CITY_LABELS,
|
||||
serviceAreaIndex: 0,
|
||||
serviceAreaText: ''
|
||||
serviceAreaText: '',
|
||||
// 个人信息编辑态:只开放后端支持自助修改的字段(姓名/性别/生日/地区)
|
||||
editing: false,
|
||||
saving: false,
|
||||
today: '',
|
||||
form: {
|
||||
name: '',
|
||||
sex: '',
|
||||
birth: '',
|
||||
province: '',
|
||||
city: '',
|
||||
district: '',
|
||||
regionText: ''
|
||||
}
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
this.rawUser = null
|
||||
this.setData({ today: this.formatDate(new Date()) })
|
||||
},
|
||||
|
||||
onShow() {
|
||||
@@ -98,6 +116,129 @@ Page({
|
||||
wx.navigateTo({ url: '/pages/materials/index' })
|
||||
},
|
||||
|
||||
/**
|
||||
* 用当前用户数据构建编辑表单
|
||||
*/
|
||||
buildForm(user) {
|
||||
const profile = (user && user.profile) || {}
|
||||
const location = (user && user.location) || {}
|
||||
const province = location.province || ''
|
||||
const city = location.city || ''
|
||||
const district = location.district || ''
|
||||
return {
|
||||
name: profile.name || '',
|
||||
sex: profile.sex || 'male',
|
||||
birth: this.formatDate(profile.birth),
|
||||
province,
|
||||
city,
|
||||
district,
|
||||
regionText: [province, city, district].filter(Boolean).join(' ')
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 进入/退出编辑态:退出时不保存,直接丢弃未提交的修改
|
||||
*/
|
||||
onToggleEdit() {
|
||||
if (this.data.saving) return
|
||||
if (this.data.editing) {
|
||||
this.setData({ editing: false, form: this.buildForm(this.rawUser) })
|
||||
return
|
||||
}
|
||||
this.setData({ editing: true, form: this.buildForm(this.rawUser) })
|
||||
},
|
||||
|
||||
onFormInput(e) {
|
||||
this.setData({ [`form.${e.currentTarget.dataset.field}`]: e.detail.value })
|
||||
},
|
||||
|
||||
onSelectSex(e) {
|
||||
this.setData({ 'form.sex': e.currentTarget.dataset.sex })
|
||||
},
|
||||
|
||||
onBirthChange(e) {
|
||||
this.setData({ 'form.birth': e.detail.value })
|
||||
},
|
||||
|
||||
onRegionChange(e) {
|
||||
const region = e.detail.value || []
|
||||
const province = region[0] || ''
|
||||
const city = region[1] || ''
|
||||
const district = region[2] || ''
|
||||
this.setData({
|
||||
'form.province': province,
|
||||
'form.city': city,
|
||||
'form.district': district,
|
||||
'form.regionText': [province, city, district].filter(Boolean).join(' ')
|
||||
})
|
||||
},
|
||||
|
||||
/**
|
||||
* 保存个人信息:/user/update 只接受姓名、性别、生日、手机号与地区,这里提交可自助修改的字段
|
||||
*/
|
||||
onSaveProfile() {
|
||||
if (this.data.saving) return
|
||||
|
||||
const form = this.data.form
|
||||
const name = (form.name || '').trim()
|
||||
if (!name) {
|
||||
wx.showToast({ title: '请输入姓名', icon: 'none' })
|
||||
return
|
||||
}
|
||||
if (!form.sex) {
|
||||
wx.showToast({ title: '请选择性别', icon: 'none' })
|
||||
return
|
||||
}
|
||||
|
||||
const app = getApp()
|
||||
if (!app.globalData.user) {
|
||||
wx.showToast({ title: '请先登录', icon: 'none' })
|
||||
return
|
||||
}
|
||||
|
||||
this.setData({ saving: true })
|
||||
wx.showLoading({ title: '保存中...' })
|
||||
|
||||
API.user
|
||||
.update({
|
||||
profile: {
|
||||
name,
|
||||
sex: form.sex,
|
||||
birth: form.birth || ''
|
||||
},
|
||||
location: {
|
||||
province: form.province,
|
||||
city: form.city,
|
||||
district: form.district
|
||||
}
|
||||
})
|
||||
.then((res) => {
|
||||
wx.hideLoading()
|
||||
this.setData({ saving: false })
|
||||
if (!res || res.code !== 0) {
|
||||
wx.showToast({ title: (res && res.msg) || '保存失败', icon: 'none' })
|
||||
return
|
||||
}
|
||||
// 回写全局用户:响应是脱敏用户(不含本机 token),需保留本地 security
|
||||
const prev = app.globalData.user || {}
|
||||
const updated = {
|
||||
...prev,
|
||||
...((res.data && res.data.user) || {}),
|
||||
security: prev.security || {}
|
||||
}
|
||||
app.globalData.user = updated
|
||||
this.rawUser = updated
|
||||
this.setData({ editing: false })
|
||||
this.setUser(updated)
|
||||
wx.showToast({ title: '保存成功', icon: 'success' })
|
||||
})
|
||||
.catch((err) => {
|
||||
wx.hideLoading()
|
||||
this.setData({ saving: false })
|
||||
wx.showToast({ title: (err && err.message) || '网络错误,请重试', icon: 'none' })
|
||||
})
|
||||
},
|
||||
|
||||
/**
|
||||
* 加载当前登录用户信息
|
||||
*/
|
||||
@@ -117,6 +258,7 @@ Page({
|
||||
*/
|
||||
setUser(user) {
|
||||
if (!user) return
|
||||
this.rawUser = user
|
||||
const profile = user.profile || {}
|
||||
const location = user.location || {}
|
||||
const meta = user.meta || {}
|
||||
@@ -139,6 +281,11 @@ Page({
|
||||
createText: this.formatDate(meta.createtime)
|
||||
}
|
||||
})
|
||||
|
||||
// 编辑中不覆盖用户正在填写的表单
|
||||
if (!this.data.editing) {
|
||||
this.setData({ form: this.buildForm(user) })
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -147,7 +294,8 @@ Page({
|
||||
formatDate(dateStr) {
|
||||
if (!dateStr) return ''
|
||||
const date = new Date(dateStr)
|
||||
if (isNaN(date.getTime())) return ''
|
||||
// birth 默认值是 0(1970-01-01),视为未填写
|
||||
if (isNaN(date.getTime()) || date.getTime() <= 0) return ''
|
||||
const pad = (n) => String(n).padStart(2, '0')
|
||||
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user