From b200d0a1ab624061c81dac7b017dc930e6ec030c Mon Sep 17 00:00:00 2001 From: lik Date: Tue, 10 Nov 2026 21:13:37 +0800 Subject: [PATCH] =?UTF-8?q?feat(me):=20=E4=B8=AA=E4=BA=BA=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E6=94=AF=E6=8C=81=E7=BC=96=E8=BE=91=E5=A7=93=E5=90=8D?= =?UTF-8?q?=E3=80=81=E6=80=A7=E5=88=AB=E3=80=81=E7=94=9F=E6=97=A5=E4=B8=8E?= =?UTF-8?q?=E5=9C=B0=E5=8C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 保存走 user/update 并回写全局用户(保留 token);取消编辑丢弃修改;修正生日默认值显示为 1970-01-01 的问题。 --- pages/me/index.js | 152 +++++++++++++++++++++++++++++++++++++++++++- pages/me/index.less | 80 ++++++++++++++++++++++- pages/me/index.wxml | 73 ++++++++++++++++++++- 3 files changed, 299 insertions(+), 6 deletions(-) diff --git a/pages/me/index.js b/pages/me/index.js index c3aa882..5c718df 100644 --- a/pages/me/index.js +++ b/pages/me/index.js @@ -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())}` }, diff --git a/pages/me/index.less b/pages/me/index.less index 6a94f0e..a4ccf65 100644 --- a/pages/me/index.less +++ b/pages/me/index.less @@ -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; + } +} diff --git a/pages/me/index.wxml b/pages/me/index.wxml index cd5c943..e5f0536 100644 --- a/pages/me/index.wxml +++ b/pages/me/index.wxml @@ -10,10 +10,32 @@ {{user.name}} {{user.mobile || '暂未绑定手机号'}} + + {{editing ? '取消' : '编辑'}} + + + + + + 姓名 + + + {{user.name}} + + @@ -21,20 +43,46 @@ {{identityText}} + + 性别 - {{user.sexLabel}} + + + 男 + + + 女 + + + {{user.sexLabel}} + + 出生日期 - {{user.birthText || '未填写'}} + + + {{form.birth || '请选择'}} + › + + + {{user.birthText || '未填写'}} + @@ -49,13 +97,27 @@ {{user.idnumber || '未填写'}} + + 所在地区 - {{user.locationText || '未填写'}} + + + {{form.regionText || '请选择'}} + › + + + {{user.locationText || '未填写'}} + @@ -91,4 +153,9 @@ {{user.createText || '未知'}} + + + + +