Files
wxapp_companion/pages/order/orderEdit.js
T
2026-09-03 11:35:21 +08:00

391 lines
12 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: '' },
escort: { serviceName: '' },
payment: { totalFee: '' },
notes: { patientNote: '' }
},
profileSummary: null,
// 新增模式下尚未选择档案时为 true,展示选择占位
needPickProfile: false,
// 档案选择弹层
showProfilePicker: false,
profileList: [],
filteredProfiles: [],
profileKeyword: '',
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 })
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
// 患者信息取自订单快照(作为无关联档案时的回退)
const p = order.patient || {}
this.patientInfo = {
name: p.name || '',
mobile: p.mobile || '',
sex: p.sex || '',
birth: p.birth || '',
idnumber: p.idnumber || ''
}
const sexLabel = p.sex === 'male' ? '男' : (p.sex === 'female' ? '女' : '')
const age = calcAge(p.birth)
this.setData({
// 有关联档案时交由 patient-info 按 ID 加载(不读缓存,以档案最新数据为准)
profileId: order.healthProfileId || '',
profileSummary: {
name: p.name || '',
mobile: p.mobile || '',
sexLabel,
ageText: age ? `${age}岁` : '',
bloodType: (order.health && order.health.bloodType) || '',
height: (order.health && order.health.height) || '',
weight: (order.health && order.health.weight) || '',
remark: (order.health && order.health.remark) || ''
},
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) },
escort: { 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 || {}
this.patientInfo = {
name: p.name || '',
mobile: p.mobile || '',
sex: p.sex || '',
birth: p.birth || '',
idnumber: p.idnumber || ''
}
this.setData({
profileSummary: {
bloodType: (profile.health && profile.health.bloodType) || '',
height: (profile.health && profile.health.height) || '',
weight: (profile.health && profile.health.weight) || '',
remark: (profile.health && profile.health.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 })
},
/**
* 是否已支付切换
*/
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,
duration: 60
},
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,
'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() {
}
})