完善表单校验与公共数据逻辑

Co-Authored-By: Kimi-K3.1-Pro <noreply@moonshot.ai>
This commit is contained in:
lik
2026-10-24 18:49:02 +08:00
committed by trae-solo-agent
co-authored by Kimi-K3.1-Pro
parent d731fc1da3
commit a45a3d7a86
18 changed files with 496 additions and 336 deletions
+30 -29
View File
@@ -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()
+3 -7
View File
@@ -7,19 +7,15 @@
行内沿用列表页/弹窗表单的语言:标签左走 --muted,值右走 --ink 加粗,
行间隔用 2rpx --line 发丝线 -->
<view class="form-card">
<!-- 头像:右侧 chevron 是"可换图"的点击暗示。
无真头像时渲染与「我的」页同一个默认底座(浅橙底 + 暖陶土 user 线标),
不再用 home-ink 位图——位图着色读不到令牌,换主色要整图重上色。
注:新选的图是微信本地临时路径,utils/api.js 无文件上传接口,
目前不会落库,只在本次编辑中生效 -->
<view class="form-row" bindtap="onChooseAvatar" hover-class="form-row-hover">
<!-- 头像修改暂未开放,此行只展示当前头像,不保留点击暗示。
无真头像时渲染与「我的」页同一个默认底座(浅橙底 + 暖陶土 user 线标)。 -->
<view class="form-row">
<text class="form-label">头像</text>
<view class="avatar-wrap">
<view class="avatar avatar-default" wx:if="{{!editData.avatar}}">
<t-icon name="user" size="48rpx" color="#9B4508" />
</view>
<image class="avatar" wx:else src="{{editData.avatar}}" mode="aspectFill"></image>
<t-icon name="chevron-right" class="form-arrow" size="32rpx" />
</view>
</view>