220 lines
6.0 KiB
JavaScript
220 lines
6.0 KiB
JavaScript
// pages/healthprofile/profileInfo.js
|
|
const API = require('../../utils/api.js')
|
|
const store = require('../../utils/store.js')
|
|
|
|
const SEX_MAP = [
|
|
{ label: '男', value: 'male' },
|
|
{ label: '女', value: 'female' }
|
|
]
|
|
const BLOOD_TYPES = ['A', 'B', 'O', 'AB']
|
|
|
|
Page({
|
|
data: {
|
|
isEdit: false,
|
|
profileId: '',
|
|
isSubmitting: false,
|
|
form: {
|
|
name: '',
|
|
mobile: '',
|
|
sexLabel: '',
|
|
birth: '',
|
|
idnumber: '',
|
|
address: '',
|
|
height: '',
|
|
weight: '',
|
|
bloodType: '',
|
|
remark: ''
|
|
},
|
|
region: [],
|
|
regionLabel: '',
|
|
sexLabels: SEX_MAP.map(item => item.label),
|
|
bloodTypes: BLOOD_TYPES
|
|
},
|
|
|
|
onLoad(options) {
|
|
const now = new Date()
|
|
const today = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')}`
|
|
this.setData({ today })
|
|
if (options && options.id) {
|
|
this.setData({ isEdit: true, profileId: options.id })
|
|
wx.setNavigationBarTitle({ title: '档案详情' })
|
|
this.loadProfile(options.id)
|
|
} else {
|
|
wx.setNavigationBarTitle({ title: '新建档案' })
|
|
}
|
|
},
|
|
|
|
/**
|
|
* 加载档案详情
|
|
*/
|
|
loadProfile(id) {
|
|
API.healthProfile.getProfileById(id)
|
|
.then(res => {
|
|
if (res.code !== 0 || !res.data || !res.data.profile) {
|
|
wx.showToast({ title: res.msg || '获取档案失败', icon: 'none' })
|
|
return
|
|
}
|
|
const p = res.data.profile
|
|
const sexItem = SEX_MAP.find(item => item.value === p.profile.sex)
|
|
const loc = p.location || {}
|
|
this.setData({
|
|
form: {
|
|
name: p.profile.name || '',
|
|
mobile: p.profile.mobile || '',
|
|
sex: p.profile.sex || '',
|
|
sexLabel: sexItem ? sexItem.label : '',
|
|
birth: p.profile.birth || '',
|
|
idnumber: p.profile.idnumber || '',
|
|
address: loc.address || '',
|
|
height: p.health.height || '',
|
|
weight: p.health.weight || '',
|
|
bloodType: p.health.bloodType || '',
|
|
remark: p.health.remark || ''
|
|
},
|
|
region: [loc.province, loc.city, loc.district].filter(Boolean),
|
|
regionLabel: [loc.province, loc.city, loc.district].filter(Boolean).join(' ')
|
|
})
|
|
})
|
|
.catch(() => {
|
|
wx.showToast({ title: '网络错误,请重试', icon: 'none' })
|
|
})
|
|
},
|
|
|
|
/**
|
|
* 文本输入统一处理
|
|
*/
|
|
onFieldChange(e) {
|
|
const { field } = e.currentTarget.dataset
|
|
this.setData({ [`form.${field}`]: e.detail.value })
|
|
},
|
|
|
|
onSexChange(e) {
|
|
const item = SEX_MAP[e.detail.value]
|
|
if (item) {
|
|
this.setData({ 'form.sex': item.value, 'form.sexLabel': item.label })
|
|
}
|
|
},
|
|
|
|
onBloodChange(e) {
|
|
this.setData({ 'form.bloodType': this.data.bloodTypes[e.detail.value] })
|
|
},
|
|
|
|
onBirthChange(e) {
|
|
this.setData({ 'form.birth': e.detail.value })
|
|
},
|
|
|
|
onRegionChange(e) {
|
|
const value = e.detail.value || []
|
|
this.setData({ region: value, regionLabel: value.join(' ') })
|
|
},
|
|
|
|
/**
|
|
* 校验并提交
|
|
*/
|
|
onSave() {
|
|
if (this.data.isSubmitting) return
|
|
|
|
const { form, isEdit, profileId } = this.data
|
|
if (!form.name.trim()) {
|
|
wx.showToast({ title: '请输入患者姓名', icon: 'none' })
|
|
return
|
|
}
|
|
if (!form.mobile.trim()) {
|
|
wx.showToast({ title: '请输入患者电话', icon: 'none' })
|
|
return
|
|
}
|
|
|
|
const region = this.data.region
|
|
const payload = {
|
|
profile: {
|
|
name: form.name.trim(),
|
|
mobile: form.mobile.trim(),
|
|
sex: form.sex || '',
|
|
birth: form.birth || '',
|
|
idnumber: form.idnumber.trim()
|
|
},
|
|
location: {
|
|
province: region[0] || '',
|
|
city: region[1] || '',
|
|
district: region[2] || '',
|
|
address: form.address.trim()
|
|
},
|
|
health: {
|
|
height: parseFloat(form.height) || 0,
|
|
weight: parseFloat(form.weight) || 0,
|
|
bloodType: form.bloodType,
|
|
remark: form.remark.trim()
|
|
}
|
|
}
|
|
|
|
this.setData({ isSubmitting: true })
|
|
|
|
const request = isEdit
|
|
? API.healthProfile.updateProfile(profileId, payload)
|
|
: API.healthProfile.createProfile(payload)
|
|
|
|
request.then(res => {
|
|
if (res.code !== 0) {
|
|
wx.showToast({ title: res.msg || '保存失败', icon: 'none' })
|
|
return
|
|
}
|
|
wx.showToast({
|
|
title: isEdit ? '保存成功' : '创建成功',
|
|
icon: 'success'
|
|
})
|
|
setTimeout(() => wx.navigateBack(), 800)
|
|
})
|
|
.catch(() => {
|
|
wx.showToast({ title: '网络错误,请重试', icon: 'none' })
|
|
})
|
|
.finally(() => {
|
|
this.setData({ isSubmitting: false })
|
|
})
|
|
},
|
|
|
|
/**
|
|
* 跳转陪诊预约(携带档案患者信息)
|
|
*/
|
|
onBooking() {
|
|
const { profileId, form } = this.data
|
|
const params = [
|
|
`id=${profileId}`,
|
|
`name=${encodeURIComponent(form.name)}`,
|
|
`mobile=${form.mobile}`,
|
|
`sex=${form.sex || ''}`,
|
|
`birth=${form.birth || ''}`
|
|
].join('&')
|
|
wx.navigateTo({ url: `/pages/order/orderEdit?${params}` })
|
|
},
|
|
|
|
/**
|
|
* 删除档案(仅编辑模式)
|
|
*/
|
|
onDelete() {
|
|
const { profileId } = this.data
|
|
wx.showModal({
|
|
title: '删除确认',
|
|
content: '确定要删除该健康档案吗?删除后不可恢复。',
|
|
confirmColor: '#ff4d4f',
|
|
success: res => {
|
|
if (!res.confirm) return
|
|
API.healthProfile.deleteProfile(profileId)
|
|
.then(res2 => {
|
|
if (res2.code !== 0) {
|
|
wx.showToast({ title: res2.msg || '删除失败', icon: 'none' })
|
|
return
|
|
}
|
|
// 清除缓存并通知列表页刷新
|
|
store.removeProfile(profileId)
|
|
store.notifyProfileChange('remove', profileId)
|
|
wx.showToast({ title: '删除成功', icon: 'success' })
|
|
setTimeout(() => wx.navigateBack(), 800)
|
|
})
|
|
.catch(() => {
|
|
wx.showToast({ title: '网络错误,请重试', icon: 'none' })
|
|
})
|
|
}
|
|
})
|
|
}
|
|
})
|