446 lines
13 KiB
JavaScript
446 lines
13 KiB
JavaScript
// pages/order/orderEdit.js
|
||
const API = require('../../utils/api.js')
|
||
const { calcAge } = require('../../utils/format.js')
|
||
|
||
Page({
|
||
|
||
/**
|
||
* 页面的初始数据
|
||
*/
|
||
data: {
|
||
isEdit: false,
|
||
form: {
|
||
hospital: { name: '', department: '', doctor: '' },
|
||
schedule: { date: '', startTime: '08:30' },
|
||
escort: { serviceId: -1, serviceName: '' },
|
||
payment: { totalFee: '' },
|
||
notes: { patientNote: '' }
|
||
},
|
||
profileSummary: null,
|
||
// 新增模式下尚未选择档案时为 true,展示选择占位
|
||
needPickProfile: false,
|
||
// 档案选择弹层
|
||
showProfilePicker: false,
|
||
profileList: [],
|
||
filteredProfiles: [],
|
||
profileKeyword: '',
|
||
serviceOptions: [],
|
||
today: '',
|
||
submitting: false
|
||
},
|
||
|
||
/**
|
||
* 生命周期函数--监听页面加载
|
||
* 新增:?id=健康档案ID(患者信息取自档案),不带 id 则进入档案选择
|
||
* 编辑:?orderId=订单ID(回填订单数据)
|
||
*/
|
||
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 })
|
||
this.loadServices()
|
||
|
||
if (options && options.orderId) {
|
||
// 编辑模式
|
||
this.orderId = options.orderId
|
||
this.setData({ isEdit: true })
|
||
wx.setNavigationBarTitle({ title: '编辑订单' })
|
||
this.loadOrder(options.orderId)
|
||
return
|
||
}
|
||
|
||
wx.setNavigationBarTitle({ title: '新增预约' })
|
||
if (!options || !options.id) {
|
||
// 未携带档案ID:弹出档案选择
|
||
this.setData({ needPickProfile: true })
|
||
this.openProfilePicker()
|
||
return
|
||
}
|
||
this.profileId = options.id
|
||
// 组件根据档案ID自行加载(缓存优先)
|
||
this.setData({ profileId: options.id })
|
||
},
|
||
|
||
/**
|
||
* 空方法:阻止弹层内容区域点击冒泡关闭
|
||
*/
|
||
noop() {},
|
||
|
||
/**
|
||
* 档案选择弹层:打开并加载档案列表
|
||
*/
|
||
openProfilePicker() {
|
||
this.setData({ showProfilePicker: true, profileKeyword: '' })
|
||
if (this.data.profileList.length === 0) {
|
||
this.loadProfiles()
|
||
} else {
|
||
this.filterProfiles()
|
||
}
|
||
},
|
||
|
||
closeProfilePicker() {
|
||
this.setData({ showProfilePicker: false })
|
||
},
|
||
|
||
loadProfiles() {
|
||
API.healthProfile.getProfiles({ page: 1, pageSize: 100, sortBy: 'updatetime' })
|
||
.then(res => {
|
||
if (res.code !== 0) {
|
||
return wx.showToast({ title: res.msg || '获取档案失败', icon: 'none' })
|
||
}
|
||
const list = ((res.data && res.data.list) || []).map(item => {
|
||
const p = item.profile || {}
|
||
const age = calcAge(p.birth)
|
||
return {
|
||
id: item._id,
|
||
name: p.name || '',
|
||
mobile: p.mobile || '',
|
||
sexLabel: p.sex === 'male' ? '男' : (p.sex === 'female' ? '女' : ''),
|
||
ageText: age ? `${age}岁` : ''
|
||
}
|
||
})
|
||
this.setData({ profileList: list })
|
||
this.filterProfiles()
|
||
})
|
||
.catch(err => {
|
||
console.error('获取健康档案失败', err)
|
||
wx.showToast({ title: '网络错误,请重试', icon: 'none' })
|
||
})
|
||
},
|
||
|
||
onProfileSearch(e) {
|
||
this.setData({ profileKeyword: e.detail.value })
|
||
this.filterProfiles()
|
||
},
|
||
|
||
filterProfiles() {
|
||
const kw = (this.data.profileKeyword || '').trim()
|
||
const list = this.data.profileList
|
||
const filtered = kw
|
||
? list.filter(item => item.name.includes(kw) || (item.mobile || '').includes(kw))
|
||
: list
|
||
this.setData({ filteredProfiles: filtered })
|
||
},
|
||
|
||
/**
|
||
* 选中档案:加载档案信息并进入表单
|
||
*/
|
||
onProfileSelect(e) {
|
||
const id = e.currentTarget.dataset.id
|
||
if (!id) return
|
||
this.profileId = id
|
||
// 组件根据档案ID自行加载(缓存优先)
|
||
this.setData({ profileId: id, needPickProfile: false, showProfilePicker: false })
|
||
},
|
||
|
||
/**
|
||
* 编辑模式:加载订单并回填表单
|
||
*/
|
||
loadOrder(id) {
|
||
wx.showLoading({ title: '加载中...' })
|
||
API.escort.getRecordById(id)
|
||
.then(res => {
|
||
wx.hideLoading()
|
||
if (res.code !== 0 || !res.data || !res.data.record) {
|
||
wx.showToast({ title: res.msg || '订单加载失败', icon: 'none' })
|
||
setTimeout(() => wx.navigateBack(), 800)
|
||
return
|
||
}
|
||
const order = res.data.record
|
||
this.profileId = order.healthProfileId || null
|
||
// 患者信息取自订单快照(作为无关联档案时的回退),字段与 schema 一一对应
|
||
const p = order.patient || {}
|
||
this.patientInfo = {
|
||
name: p.name || '',
|
||
mobile: p.mobile || '',
|
||
birth: p.birth || '',
|
||
weight: p.weight || 0,
|
||
height: p.height || 0,
|
||
idnumber: p.idnumber || ''
|
||
}
|
||
if (p.sex) {
|
||
this.patientInfo.sex = p.sex
|
||
}
|
||
const sexLabel = p.sex === 'male' ? '男' : (p.sex === 'female' ? '女' : '')
|
||
const age = calcAge(p.birth)
|
||
this.setData({
|
||
// 有关联档案时交由 patient-info 按 ID 加载(不读缓存,以档案最新数据为准)
|
||
profileId: order.healthProfileId || '',
|
||
// 无关联档案时用订单 patient 快照兜底展示姓名/性别/年龄/电话;
|
||
// 血型、身高、体重、备注不属于 escort_record(schema 无 health 块),
|
||
// 有关联档案时由 patient-info 按 profileId 加载后经 onProfileLoaded 回填,
|
||
// 故这里不再读 order.health(该块恒为空,属死代码)
|
||
profileSummary: {
|
||
name: p.name || '',
|
||
mobile: p.mobile || '',
|
||
sexLabel,
|
||
ageText: age ? `${age}岁` : ''
|
||
},
|
||
form: {
|
||
hospital: {
|
||
name: (order.hospital && order.hospital.name) || '',
|
||
department: (order.hospital && order.hospital.department) || '',
|
||
doctor: (order.hospital && order.hospital.doctor) || ''
|
||
},
|
||
schedule: {
|
||
date: this.formatDate(order.schedule && order.schedule.date),
|
||
startTime: (order.schedule && order.schedule.startTime) || '08:30'
|
||
},
|
||
escort: {
|
||
serviceId: (order.escort && order.escort.serviceId) || -1,
|
||
serviceName: (order.escort && order.escort.serviceName) || ''
|
||
},
|
||
payment: {
|
||
totalFee: order.payment && order.payment.totalFee ? String(order.payment.totalFee) : '',
|
||
paid: !!(order.payment && order.payment.status === 'paid')
|
||
},
|
||
notes: { patientNote: (order.notes && order.notes.patientNote) || '' }
|
||
}
|
||
})
|
||
})
|
||
.catch(err => {
|
||
console.error('获取订单失败', err)
|
||
wx.hideLoading()
|
||
wx.showToast({ title: '网络错误,请重试', icon: 'none' })
|
||
})
|
||
},
|
||
|
||
/**
|
||
* 格式化日期为 YYYY-MM-DD(兼容时间戳/字符串)
|
||
*/
|
||
formatDate(date) {
|
||
if (!date) return ''
|
||
const d = new Date(date)
|
||
if (isNaN(d.getTime())) return String(date)
|
||
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`
|
||
},
|
||
|
||
/**
|
||
* patient-info 组件按档案ID加载完成:保存患者原始信息(供提交)及档案附加信息(供标签展示)
|
||
*/
|
||
onProfileLoaded(e) {
|
||
const profile = e.detail.profile || {}
|
||
const p = profile.profile || {}
|
||
const h = profile.health || {}
|
||
// 订单 patient 快照:与 escort_record schema 的 patient 字段一一对应。
|
||
// sex 是 male/female 枚举,空串会校验失败,为空时不写该字段
|
||
this.patientInfo = {
|
||
name: p.name || '',
|
||
mobile: p.mobile || '',
|
||
birth: p.birth || '',
|
||
weight: h.weight || 0,
|
||
height: h.height || 0,
|
||
idnumber: p.idnumber || ''
|
||
}
|
||
if (p.sex) {
|
||
this.patientInfo.sex = p.sex
|
||
}
|
||
this.setData({
|
||
profileSummary: {
|
||
bloodType: h.bloodType || '',
|
||
height: h.height || '',
|
||
weight: h.weight || '',
|
||
remark: h.remark || ''
|
||
}
|
||
})
|
||
},
|
||
|
||
/**
|
||
* 表单输入处理
|
||
*/
|
||
onInput(e) {
|
||
const key = e.currentTarget.dataset.key
|
||
const value = e.detail.value
|
||
this.setData({ [`form.${key}`]: value })
|
||
},
|
||
|
||
/**
|
||
* 就诊日期选择
|
||
*/
|
||
onDateChange(e) {
|
||
this.setData({ 'form.schedule.date': e.detail.value })
|
||
},
|
||
|
||
/**
|
||
* 开始时间选择
|
||
*/
|
||
onTimeChange(e) {
|
||
this.setData({ 'form.schedule.startTime': e.detail.value })
|
||
},
|
||
|
||
/**
|
||
* 加载陪诊服务列表(供服务名称下拉选择)
|
||
*/
|
||
loadServices() {
|
||
API.resource.getServices()
|
||
.then(res => {
|
||
if (res.code !== 0) return
|
||
this.setData({ serviceOptions: (res.data && res.data.services) || [] })
|
||
})
|
||
.catch(err => {
|
||
console.error('获取服务列表失败', err)
|
||
})
|
||
},
|
||
|
||
/**
|
||
* 服务名称选择:同时记录服务ID与服务名称
|
||
*/
|
||
onServiceChange(e) {
|
||
const svc = this.data.serviceOptions[Number(e.detail.value)]
|
||
if (!svc) return
|
||
this.setData({
|
||
'form.escort.serviceId': svc.id,
|
||
'form.escort.serviceName': svc.title
|
||
})
|
||
},
|
||
|
||
/**
|
||
* 是否已支付切换
|
||
*/
|
||
onPaidChange(e) {
|
||
this.setData({ 'form.payment.paid': e.detail.value })
|
||
},
|
||
|
||
/**
|
||
* 提交订单(新增或保存修改)
|
||
*/
|
||
onSubmit() {
|
||
if (this.data.submitting) return
|
||
|
||
const { form } = this.data
|
||
if (!this.profileId) return wx.showToast({ title: '请先选择健康档案', icon: 'none' })
|
||
// 患者信息取自档案/订单
|
||
const patient = this.patientInfo
|
||
if (!patient || !patient.name) return wx.showToast({ title: '健康档案信息未加载', icon: 'none' })
|
||
if (!form.hospital.name) return wx.showToast({ title: '请输入医院名称', icon: 'none' })
|
||
if (!form.schedule.date) return wx.showToast({ title: '请选择就诊日期', icon: 'none' })
|
||
|
||
const app = getApp()
|
||
const userId = app.globalData.user?._id
|
||
if (!userId) return wx.showToast({ title: '未获取到用户信息,请重新登录', icon: 'none' })
|
||
|
||
if (this.data.isEdit) {
|
||
this.updateOrder(form)
|
||
} else {
|
||
this.createOrder(form, userId)
|
||
}
|
||
},
|
||
|
||
/**
|
||
* 新增订单
|
||
*/
|
||
createOrder(form, userId) {
|
||
const payload = {
|
||
userId,
|
||
healthProfileId: this.profileId || null,
|
||
patient: this.patientInfo,
|
||
hospital: {
|
||
name: form.hospital.name,
|
||
department: form.hospital.department,
|
||
doctor: form.hospital.doctor
|
||
},
|
||
schedule: {
|
||
date: form.schedule.date,
|
||
startTime: form.schedule.startTime || '',
|
||
duration: 60
|
||
},
|
||
escort: {
|
||
serviceId: form.escort.serviceId || -1,
|
||
serviceName: form.escort.serviceName
|
||
},
|
||
payment: {
|
||
totalFee: form.payment.totalFee ? Number(form.payment.totalFee) : 0,
|
||
paidFee: 0,
|
||
status: 'unpaid'
|
||
},
|
||
notes: {
|
||
patientNote: form.notes.patientNote
|
||
}
|
||
}
|
||
|
||
this.setData({ submitting: true })
|
||
wx.showLoading({ title: '提交中...' })
|
||
|
||
API.escort.createRecord(payload)
|
||
.then(res => {
|
||
wx.hideLoading()
|
||
if (res.code !== 0) {
|
||
this.setData({ submitting: false })
|
||
return wx.showToast({ title: res.msg || '创建失败', icon: 'none' })
|
||
}
|
||
wx.showToast({ title: '创建成功', icon: 'success' })
|
||
setTimeout(() => {
|
||
wx.navigateBack()
|
||
}, 600)
|
||
})
|
||
.catch(err => {
|
||
console.error('创建订单失败', err)
|
||
this.setData({ submitting: false })
|
||
wx.hideLoading()
|
||
wx.showToast({ title: '网络错误,请重试', icon: 'none' })
|
||
})
|
||
},
|
||
|
||
/**
|
||
* 编辑模式:保存修改
|
||
*/
|
||
updateOrder(form) {
|
||
const payload = {
|
||
'hospital.name': form.hospital.name,
|
||
'hospital.department': form.hospital.department,
|
||
'hospital.doctor': form.hospital.doctor,
|
||
'schedule.date': form.schedule.date,
|
||
'schedule.startTime': form.schedule.startTime || '',
|
||
'escort.serviceId': form.escort.serviceId || -1,
|
||
'escort.serviceName': form.escort.serviceName,
|
||
'payment.totalFee': form.payment.totalFee ? Number(form.payment.totalFee) : 0,
|
||
'payment.paidFee': form.payment.paid ? (form.payment.totalFee ? Number(form.payment.totalFee) : 0) : 0,
|
||
'payment.status': form.payment.paid ? 'paid' : 'unpaid',
|
||
'notes.patientNote': form.notes.patientNote
|
||
}
|
||
|
||
this.setData({ submitting: true })
|
||
wx.showLoading({ title: '保存中...' })
|
||
|
||
API.escort.updateRecord(this.orderId, payload)
|
||
.then(res => {
|
||
wx.hideLoading()
|
||
if (res.code !== 0) {
|
||
this.setData({ submitting: false })
|
||
return wx.showToast({ title: res.msg || '保存失败', icon: 'none' })
|
||
}
|
||
wx.showToast({ title: '保存成功', icon: 'success' })
|
||
setTimeout(() => {
|
||
wx.navigateBack()
|
||
}, 600)
|
||
})
|
||
.catch(err => {
|
||
console.error('更新订单失败', err)
|
||
this.setData({ submitting: false })
|
||
wx.hideLoading()
|
||
wx.showToast({ title: '网络错误,请重试', icon: 'none' })
|
||
})
|
||
},
|
||
|
||
/**
|
||
* 页面相关事件处理函数--监听用户下拉动作
|
||
*/
|
||
onPullDownRefresh() {
|
||
|
||
},
|
||
|
||
/**
|
||
* 页面上拉触底事件的处理函数
|
||
*/
|
||
onReachBottom() {
|
||
|
||
},
|
||
|
||
/**
|
||
* 用户点击右上角分享
|
||
*/
|
||
onShareAppMessage() {
|
||
|
||
}
|
||
})
|