feat(me): 个人信息支持编辑姓名、性别、生日与地区

保存走 user/update 并回写全局用户(保留 token);取消编辑丢弃修改;修正生日默认值显示为 1970-01-01 的问题。
This commit is contained in:
lik
2026-11-10 21:13:37 +08:00
parent 1d8af0cb26
commit b200d0a1ab
3 changed files with 299 additions and 6 deletions
+150 -2
View File
@@ -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())}`
},
+79 -1
View File
@@ -51,6 +51,8 @@ page {
margin-left: 28rpx;
display: flex;
flex-direction: column;
flex: 1;
min-width: 0;
}
.user-name {
@@ -60,10 +62,26 @@ page {
.user-mobile {
margin-top: 10rpx;
font-size: 26rpx;
font-size: 28rpx;
opacity: 0.9;
}
/* === 编辑入口 === */
.edit-entry {
flex-shrink: 0;
margin-left: 16rpx;
padding: 10rpx 24rpx;
border-radius: 28rpx;
background-color: rgba(255, 255, 255, 0.16);
border: 1rpx solid rgba(255, 255, 255, 0.4);
font-size: 26rpx;
color: #ffffff;
&:active {
background-color: rgba(255, 255, 255, 0.28);
}
}
/* === 详细信息卡 === */
.info-card {
margin-top: 24rpx;
@@ -117,3 +135,63 @@ page {
font-size: 32rpx;
color: #C9BFB5;
}
/* === 编辑态表单 === */
.edit-input {
flex: 1;
height: 48rpx;
min-height: 48rpx;
text-align: right;
}
.edit-placeholder {
color: #C7BDB2;
}
.sex-edit {
display: flex;
align-items: center;
gap: 16rpx;
}
.sex-chip {
padding: 6rpx 32rpx;
border-radius: 24rpx;
background-color: #F4EFE9;
border: 1rpx solid transparent;
font-size: 26rpx;
color: #6F655C;
transition: all 0.2s ease;
&.active {
background-color: rgba(194, 106, 46, 0.1);
border-color: #C26A2E;
color: #C26A2E;
font-weight: 600;
}
}
/* === 保存按钮 === */
.save-bar {
margin-top: 32rpx;
}
.save-btn {
height: 96rpx;
line-height: 96rpx;
text-align: center;
border-radius: 48rpx;
background-color: #C26A2E;
color: #ffffff;
font-size: 30rpx;
font-weight: 600;
box-shadow: 0 8rpx 24rpx rgba(180, 85, 42, 0.25);
&::after {
border: none;
}
&:active {
opacity: 0.9;
}
}
+70 -3
View File
@@ -10,10 +10,32 @@
<text class="user-name">{{user.name}}</text>
<text class="user-mobile">{{user.mobile || '暂未绑定手机号'}}</text>
</view>
<view class="edit-entry {{editing ? 'active' : ''}}" bindtap="onToggleEdit">
<text>{{editing ? '取消' : '编辑'}}</text>
</view>
</view>
<!-- 详细信息 -->
<view class="info-card">
<!-- 姓名 -->
<view class="info-item">
<view class="info-left">
<t-icon name="user-circle" size="36rpx" color="#C26A2E" />
<text class="info-label">姓名</text>
</view>
<input
wx:if="{{editing}}"
class="info-value edit-input"
value="{{form.name}}"
maxlength="20"
placeholder="请输入姓名"
placeholder-class="edit-placeholder"
bindinput="onFormInput"
data-field="name"
/>
<text wx:else class="info-value">{{user.name}}</text>
</view>
<view class="info-item" wx:if="{{identityText}}">
<view class="info-left">
<t-icon name="award" size="36rpx" color="#C26A2E" />
@@ -21,20 +43,46 @@
</view>
<text class="info-value">{{identityText}}</text>
</view>
<!-- 性别 -->
<view class="info-item">
<view class="info-left">
<t-icon name="user" size="36rpx" color="#C26A2E" />
<text class="info-label">性别</text>
</view>
<text class="info-value">{{user.sexLabel}}</text>
<view class="sex-edit" wx:if="{{editing}}">
<view class="sex-chip {{form.sex === 'male' ? 'active' : ''}}" bindtap="onSelectSex" data-sex="male">
<text>男</text>
</view>
<view class="sex-chip {{form.sex === 'female' ? 'active' : ''}}" bindtap="onSelectSex" data-sex="female">
<text>女</text>
</view>
</view>
<text wx:else class="info-value">{{user.sexLabel}}</text>
</view>
<!-- 出生日期 -->
<view class="info-item">
<view class="info-left">
<t-icon name="calendar" size="36rpx" color="#C26A2E" />
<text class="info-label">出生日期</text>
</view>
<text class="info-value">{{user.birthText || '未填写'}}</text>
<picker
wx:if="{{editing}}"
mode="date"
value="{{form.birth || today}}"
start="1930-01-01"
end="{{today}}"
bindchange="onBirthChange"
>
<view class="info-value picker-value">
{{form.birth || '请选择'}}
<text class="arrow">›</text>
</view>
</picker>
<text wx:else class="info-value">{{user.birthText || '未填写'}}</text>
</view>
<view class="info-item">
<view class="info-left">
<t-icon name="mail" size="36rpx" color="#C26A2E" />
@@ -49,13 +97,27 @@
</view>
<text class="info-value">{{user.idnumber || '未填写'}}</text>
</view>
<!-- 所在地区 -->
<view class="info-item">
<view class="info-left">
<t-icon name="location" size="36rpx" color="#C26A2E" />
<text class="info-label">所在地区</text>
</view>
<text class="info-value">{{user.locationText || '未填写'}}</text>
<picker
wx:if="{{editing}}"
mode="region"
value="{{[form.province, form.city, form.district]}}"
bindchange="onRegionChange"
>
<view class="info-value picker-value">
{{form.regionText || '请选择'}}
<text class="arrow">›</text>
</view>
</picker>
<text wx:else class="info-value">{{user.locationText || '未填写'}}</text>
</view>
<view class="info-item" wx:if="{{isAttendant}}">
<view class="info-left">
<t-icon name="location" size="36rpx" color="#C26A2E" />
@@ -91,4 +153,9 @@
<text class="info-value">{{user.createText || '未知'}}</text>
</view>
</view>
<!-- 编辑态保存 -->
<view class="save-bar" wx:if="{{editing}}">
<button class="save-btn" loading="{{saving}}" bindtap="onSaveProfile">保存</button>
</view>
</view>