修改界面中,临时提交
This commit is contained in:
@@ -0,0 +1,221 @@
|
||||
const API = require('../../utils/api.js')
|
||||
|
||||
// 性别选项,与后端 schema 的 male / female 对应
|
||||
const SEX_OPTIONS = [{
|
||||
value: 'male',
|
||||
label: '男'
|
||||
},
|
||||
{
|
||||
value: 'female',
|
||||
label: '女'
|
||||
}
|
||||
]
|
||||
|
||||
const MOBILE_REGEX = /^1[3-9]\d{9}$/
|
||||
|
||||
Page({
|
||||
data: {
|
||||
id: '',
|
||||
isEdit: false,
|
||||
saving: false,
|
||||
today: '',
|
||||
sexOptions: SEX_OPTIONS,
|
||||
form: {
|
||||
name: '',
|
||||
sex: '',
|
||||
birth: '',
|
||||
mobile: '',
|
||||
idnumber: '',
|
||||
remark: ''
|
||||
}
|
||||
},
|
||||
|
||||
onLoad(options) {
|
||||
const id = options.id || ''
|
||||
|
||||
this.setData({
|
||||
id,
|
||||
isEdit: !!id,
|
||||
today: this.formatDate(new Date())
|
||||
})
|
||||
|
||||
wx.setNavigationBarTitle({
|
||||
title: id ? '编辑就诊人' : '新增就诊人'
|
||||
})
|
||||
|
||||
if (id) {
|
||||
this.loadProfile(id)
|
||||
}
|
||||
},
|
||||
|
||||
formatDate(date) {
|
||||
const year = date.getFullYear()
|
||||
const month = String(date.getMonth() + 1).padStart(2, '0')
|
||||
const day = String(date.getDate()).padStart(2, '0')
|
||||
return `${year}-${month}-${day}`
|
||||
},
|
||||
|
||||
// 编辑模式:读取档案详情回填表单
|
||||
loadProfile(id) {
|
||||
API.healthProfile.getMyProfileById(id)
|
||||
.then((res) => {
|
||||
if (res.code !== 0) {
|
||||
wx.showToast({
|
||||
title: res.msg || '加载失败',
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
}
|
||||
const detail = (res.data && res.data.profile) || {}
|
||||
const profile = detail.profile || {}
|
||||
const health = detail.health || {}
|
||||
|
||||
this.setData({
|
||||
form: {
|
||||
name: profile.name || '',
|
||||
sex: profile.sex || '',
|
||||
birth: profile.birth || '',
|
||||
mobile: profile.mobile || '',
|
||||
idnumber: profile.idnumber || '',
|
||||
remark: health.remark || ''
|
||||
}
|
||||
})
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log('获取就诊人信息失败', err)
|
||||
wx.showToast({
|
||||
title: '网络请求失败',
|
||||
icon: 'none'
|
||||
})
|
||||
})
|
||||
},
|
||||
|
||||
onFieldInput(e) {
|
||||
const field = e.currentTarget.dataset.field
|
||||
this.setData({
|
||||
[`form.${field}`]: e.detail.value
|
||||
})
|
||||
},
|
||||
|
||||
onSexSelect(e) {
|
||||
const sex = e.currentTarget.dataset.sex
|
||||
// 再次点击已选中的性别则取消选择
|
||||
this.setData({
|
||||
'form.sex': this.data.form.sex === sex ? '' : sex
|
||||
})
|
||||
},
|
||||
|
||||
onBirthChange(e) {
|
||||
this.setData({
|
||||
'form.birth': e.detail.value
|
||||
})
|
||||
},
|
||||
|
||||
// 表单校验,返回错误提示文案;通过校验返回空字符串
|
||||
validate() {
|
||||
const {
|
||||
name,
|
||||
mobile
|
||||
} = this.data.form
|
||||
|
||||
if (!name.trim()) {
|
||||
return '请输入就诊人姓名'
|
||||
}
|
||||
if (!mobile.trim()) {
|
||||
return '请输入联系电话'
|
||||
}
|
||||
if (!MOBILE_REGEX.test(mobile.trim())) {
|
||||
return '请输入正确的手机号'
|
||||
}
|
||||
return ''
|
||||
},
|
||||
|
||||
onSave() {
|
||||
if (this.data.saving) {
|
||||
return
|
||||
}
|
||||
|
||||
const errorMessage = this.validate()
|
||||
if (errorMessage) {
|
||||
wx.showToast({
|
||||
title: errorMessage,
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
const {
|
||||
name,
|
||||
sex,
|
||||
birth,
|
||||
mobile,
|
||||
idnumber,
|
||||
remark
|
||||
} = this.data.form
|
||||
|
||||
// 新增用嵌套对象(schema 的 new Model 需要嵌套结构);
|
||||
// 更新用点号路径(schema 更新姓名时会额外写 profile.pinyin,整体替换 profile 会冲突)
|
||||
const payload = this.data.isEdit ? {
|
||||
'profile.name': name.trim(),
|
||||
'profile.sex': sex,
|
||||
'profile.birth': birth,
|
||||
'profile.mobile': mobile.trim(),
|
||||
'profile.idnumber': idnumber.trim(),
|
||||
'health.remark': remark.trim()
|
||||
} : {
|
||||
profile: {
|
||||
name: name.trim(),
|
||||
sex: sex,
|
||||
birth: birth,
|
||||
mobile: mobile.trim(),
|
||||
idnumber: idnumber.trim()
|
||||
},
|
||||
health: {
|
||||
remark: remark.trim()
|
||||
}
|
||||
}
|
||||
|
||||
this.setData({
|
||||
saving: true
|
||||
})
|
||||
wx.showLoading({
|
||||
title: '保存中...'
|
||||
})
|
||||
|
||||
const request = this.data.isEdit ?
|
||||
API.healthProfile.updateMyProfile(this.data.id, payload) :
|
||||
API.healthProfile.createMyProfile(payload)
|
||||
|
||||
request
|
||||
.then((res) => {
|
||||
wx.hideLoading()
|
||||
if (res.code !== 0) {
|
||||
wx.showToast({
|
||||
title: res.msg || '保存失败',
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
}
|
||||
wx.showToast({
|
||||
title: '保存成功',
|
||||
icon: 'success'
|
||||
})
|
||||
setTimeout(() => {
|
||||
wx.navigateBack()
|
||||
}, 800)
|
||||
})
|
||||
.catch((err) => {
|
||||
wx.hideLoading()
|
||||
console.log('保存就诊人失败', err)
|
||||
wx.showToast({
|
||||
title: '网络请求失败',
|
||||
icon: 'none'
|
||||
})
|
||||
})
|
||||
.finally(() => {
|
||||
this.setData({
|
||||
saving: false
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user