更新了AI页和我的页,一级页面都重构完成。
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
// pages/mine/profileedit.js
|
||||
const API = require('../../utils/api.js')
|
||||
|
||||
Page({
|
||||
data: {
|
||||
editData: {
|
||||
avatar: '',
|
||||
name: '',
|
||||
sex: '',
|
||||
birth: '',
|
||||
province: '',
|
||||
city: '',
|
||||
district: '',
|
||||
phone: ''
|
||||
},
|
||||
today: '',
|
||||
saving: false
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
const today = new Date()
|
||||
this.setData({
|
||||
today: `${today.getFullYear()}-${String(today.getMonth() + 1).padStart(2, '0')}-${String(today.getDate()).padStart(2, '0')}`
|
||||
})
|
||||
this.initEditData()
|
||||
},
|
||||
|
||||
// 表单初值取自全局用户对象。页面栈上「我的」页的 onShow 读的也是它,
|
||||
// 所以保存成功后必须回写 app.globalData.user,否则返回时看到的还是旧值
|
||||
initEditData() {
|
||||
const app = getApp()
|
||||
const user = app.globalData.user || {}
|
||||
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,
|
||||
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
|
||||
})
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
onSelectSex(e) {
|
||||
this.setData({
|
||||
'editData.sex': e.currentTarget.dataset.sex
|
||||
})
|
||||
},
|
||||
|
||||
onBirthChange(e) {
|
||||
this.setData({
|
||||
'editData.birth': e.detail.value
|
||||
})
|
||||
},
|
||||
|
||||
onRegionChange(e) {
|
||||
const region = e.detail.value
|
||||
this.setData({
|
||||
'editData.province': region[0] || '',
|
||||
'editData.city': region[1] || '',
|
||||
'editData.district': region[2] || ''
|
||||
})
|
||||
},
|
||||
|
||||
onFieldInput(e) {
|
||||
const field = e.currentTarget.dataset.field
|
||||
this.setData({
|
||||
[`editData.${field}`]: e.detail.value
|
||||
})
|
||||
},
|
||||
|
||||
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: editData.birth ? new Date(editData.birth).toISOString() : null,
|
||||
mobile: editData.phone
|
||||
},
|
||||
location: {
|
||||
province: editData.province,
|
||||
city: editData.city,
|
||||
district: editData.district
|
||||
}
|
||||
}
|
||||
|
||||
API.user.update(updateData)
|
||||
.then((data) => {
|
||||
if (data.code === 0) {
|
||||
// 回写全局对象:「我的」页 onShow 依赖它刷新头像行与资料
|
||||
app.globalData.user = data.data.user
|
||||
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 })
|
||||
})
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user