237 lines
5.3 KiB
JavaScript
237 lines
5.3 KiB
JavaScript
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: '',
|
||
height: '',
|
||
weight: '',
|
||
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 || '',
|
||
// 身高的 schema 默认值是 0,0 视为未填写,回填成空串而不是 "0"
|
||
height: health.height ? String(health.height) : '',
|
||
weight: health.weight ? String(health.weight) : '',
|
||
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,
|
||
height,
|
||
weight,
|
||
remark
|
||
} = this.data.form
|
||
|
||
// schema 里身高/体重是 Number,未填按 0(Number('') 即 0,非法输入 NaN 也回落 0)
|
||
const heightNum = Number(height) || 0
|
||
const weightNum = Number(weight) || 0
|
||
|
||
// 新增用嵌套对象(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.height': heightNum,
|
||
'health.weight': weightNum,
|
||
'health.remark': remark.trim()
|
||
} : {
|
||
profile: {
|
||
name: name.trim(),
|
||
sex: sex,
|
||
birth: birth,
|
||
mobile: mobile.trim(),
|
||
idnumber: idnumber.trim()
|
||
},
|
||
health: {
|
||
height: heightNum,
|
||
weight: weightNum,
|
||
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
|
||
})
|
||
})
|
||
}
|
||
})
|