Files

247 lines
5.7 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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
})
})
},
// 本页是就诊人档案表单(姓名 / 手机号 / 身份证号),转发卡片不指向本页,
// 统一落到首页,避免好友打开一个需要登录的空表单;
// 也不提供朋友圈分享——朋友圈只能分享当前页 URL,会把档案页地址带出去
onShareAppMessage() {
return {
title: '暖橙陪诊 · 专业陪诊服务',
path: '/pages/home/index'
}
}
})