// 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: { editData: { avatar: '', name: '', sex: '', birth: '', province: '', city: '', district: '', phone: '' }, today: '', saving: false }, onLoad() { 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() { const app = getApp() const user = app.globalData.user || {} const profile = user.profile || {} const location = user.location || {} this.setData({ editData: { // 头像修改暂未开放,这里只展示当前头像或默认头像。 avatar: profile.avatar || '', name: profile.name || '', phone: profile.mobile || '', sex: profile.sex || '', birth: formatDate(profile.birth), province: location.province || '', city: location.city || '', district: location.district || '' } }, () => 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) { const region = e.detail.value this.setData({ 'editData.province': region[0] || '', 'editData.city': region[1] || '', 'editData.district': region[2] || '' }) this.syncUnsavedGuard() }, onFieldInput(e) { const field = e.currentTarget.dataset.field this.setData({ [`editData.${field}`]: e.detail.value }) this.syncUnsavedGuard() }, onSave() { if (this.data.saving) return const editData = this.data.editData const app = getApp() const user = app.globalData.user if (!user || !user._id) { wx.showToast({ title: '请先登录', icon: 'none' }) return } this.setData({ saving: true }) wx.showLoading({ title: '保存中...' }) const updateData = { _id: user._id, profile: { name: editData.name, sex: editData.sex, birth: toISOString(editData.birth), mobile: editData.phone }, location: { province: editData.province, city: editData.city, district: editData.district } } API.user.update(updateData) .then((data) => { if (data.code === 0) { // 回写全局对象:「我的」页 onShow 依赖它刷新头像行与资料。 // 不能整替——/user/update 的响应是脱敏用户(security.token 已被删掉), // 整替会把本机 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() } else { wx.showToast({ title: data.msg || '保存失败', icon: 'none' }) } }) .catch(() => { wx.showToast({ title: '网络请求失败', icon: 'none' }) }) .finally(() => { wx.hideLoading() this.setData({ saving: false }) }) } })