const API = require('../../utils/api.js') Page({ data: { service: { id: null, title: '', subtitle: '', price: '0.00', image: '', tag: '', description: '', flow: [], notices: [] }, totalPrice: '0.00', showOrderPopup: false, profiles: [], // 已保存的就诊人 selectedProfileId: '', // 当前选中的就诊人档案 ID,为空表示手动填写 patientName: '', patientPhone: '', province: '', provinceIndex: 0, provinces: ['北京市', '天津市', '河北省', '山西省', '内蒙古自治区', '辽宁省', '吉林省', '黑龙江省', '上海市', '江苏省', '浙江省', '安徽省', '福建省', '江西省', '山东省', '河南省', '湖北省', '湖南省', '广东省', '广西壮族自治区', '海南省', '重庆市', '四川省', '贵州省', '云南省', '西藏自治区', '陕西省', '甘肃省', '青海省', '宁夏回族自治区', '新疆维吾尔自治区', '台湾省', '香港特别行政区', '澳门特别行政区'], hospital: '', appointmentDate: '', appointmentTime: '', remark: '', today: '', gender: '不限', genderIndex: 0, genderOptions: ['不限', '男', '女'], department: '', departments: ['内科', '外科', '妇产科', '儿科', '骨科', '神经科', '心血管内科', '消化内科', '呼吸内科', '内分泌科', '皮肤科', '眼科', '耳鼻喉科', '口腔科', '急诊科', '肿瘤科', '康复医学科', '老年病科', '中医科', '针灸推拿科'], hospitals: [] }, onLoad(options) { const today = new Date() const year = today.getFullYear() const month = String(today.getMonth() + 1).padStart(2, '0') const day = String(today.getDate()).padStart(2, '0') const todayStr = `${year}-${month}-${day}` this.setData({ today: todayStr }) const serviceId = parseInt(options.id) || 1 this.loadServiceDetail(serviceId) // 从列表页的「预约」进来(?book=1)时,直接把预约单弹出来; // 未登录会由 openOrderPopup 拦下并提示登录 if (options.book === '1') { this.openOrderPopup() } }, onShow() { // 从"新增就诊人"页面返回时,弹窗还开着就刷新一次可选的就诊人 if (this.data.showOrderPopup) { this.loadProfiles() } }, loadServiceDetail(id) { const app = getApp() let service = null for (const item of app.globalData.services) { if (item.id === id) { service = item break } } if (!service) { return } const totalPrice = this.calculateTotal(service.price, 1) this.setData({ service: service, totalPrice: totalPrice }) }, calculateTotal(price) { return price }, openOrderPopup() { const app = getApp() if (!app.globalData.user) { wx.showToast({ title: '请先登录', icon: 'none' }) return } const user = app.globalData.user this.setData({ patientName: user.profile?.name || '', patientPhone: user.profile?.mobile || '', selectedProfileId: '', showOrderPopup: true }) this.loadProfiles() }, // 读取已保存的就诊人,供弹窗内一键选择 loadProfiles() { API.healthProfile.getMyProfiles({ page: 1, pageSize: 20 }) .then((res) => { if (res.code !== 0) { return } const list = ((res.data && res.data.list) || []).map((item) => ({ _id: item._id, name: item.profile?.name || '', mobile: item.profile?.mobile || '' })) this.setData({ profiles: list }) }) .catch((err) => { console.log('获取就诊人列表失败', err) }) }, // 选择就诊人:自动带入姓名与联系电话 onSelectProfile(e) { const id = e.currentTarget.dataset.id const profile = this.data.profiles.find((item) => item._id === id) if (!profile) { return } this.setData({ selectedProfileId: id, patientName: profile.name, patientPhone: profile.mobile }) }, // 跳转到就诊人页新增 goToAddProfile() { wx.navigateTo({ url: '/pages/health/profileedit' }) }, closeOrderPopup() { this.setData({ showOrderPopup: false }) }, // 手动修改姓名/电话后,不再与所选档案对应,清空选择避免存档信息不一致 onPatientNameChange(e) { this.setData({ patientName: e.detail.value, selectedProfileId: '' }) }, onPatientPhoneChange(e) { this.setData({ patientPhone: e.detail.value, selectedProfileId: '' }) }, onProvinceChange(e) { const index = e.detail.value const province = this.data.provinces[index] this.setData({ provinceIndex: index, province: province }) }, onHospitalChange(e) { this.setData({ hospital: e.detail.value }) }, onDepartmentChange(e) { this.setData({ department: e.detail.value }) }, onDateChange(e) { this.setData({ appointmentDate: e.detail.value }) }, onTimeChange(e) { this.setData({ appointmentTime: e.detail.value }) }, onGenderChange(e) { const index = e.detail.value const gender = this.data.genderOptions[index] this.setData({ genderIndex: index, gender: gender }) }, onRemarkChange(e) { this.setData({ remark: e.detail.value }) }, submitOrder() { const { service, patientName, patientPhone, province, hospital, department, appointmentDate, appointmentTime, gender, remark } = this.data if (!patientName.trim()) { wx.showToast({ title: '请输入就诊人姓名', icon: 'none' }) return } if (!patientPhone.trim()) { wx.showToast({ title: '请输入联系电话', icon: 'none' }) return } if (!hospital.trim()) { wx.showToast({ title: '请输入就诊医院', icon: 'none' }) return } if (!department.trim()) { wx.showToast({ title: '请输入就诊科室', icon: 'none' }) return } if (!appointmentDate) { wx.showToast({ title: '请选择预约日期', icon: 'none' }) return } if (!appointmentTime) { wx.showToast({ title: '请选择预约时间', icon: 'none' }) return } const phoneRegex = /^1[3-9]\d{9}$/ if (!phoneRegex.test(patientPhone.trim())) { wx.showToast({ title: '请输入正确的手机号', icon: 'none' }) return } const app = getApp() const user = app.globalData.user let sex = "none" if (gender == '男') sex = 'male' else if (gender == '女') sex = 'female' const orderData = { userId: user._id, patient: { name: patientName.trim(), mobile: patientPhone.trim(), }, escort: { serviceId: service.id, serviceName: service.title, sexRequirement: sex }, hospital: { province: province, name: hospital.trim(), department: department.trim(), }, schedule: { date: appointmentDate, startTime: appointmentTime }, payment: { totalFee: service.price }, notes: { patientNote: remark.trim() } } // 选择了已保存的就诊人时,一并记录档案 ID if (this.data.selectedProfileId) { orderData.healthProfileId = this.data.selectedProfileId } wx.showLoading({ title: '提交中...' }) API.escort.createRecord(orderData) .then((data) => { wx.hideLoading() if (data.code === 0) { wx.showToast({ title: '下单成功', icon: 'success' }) this.setData({ showOrderPopup: false }) this.resetForm() wx.navigateTo({url: '/pages/escort/recordlist?tab=pending'}) } else { wx.showToast({ title: data.msg || '下单失败', icon: 'none' }) } }) .catch((err) => { wx.hideLoading() wx.showToast({ title: '网络请求失败', icon: 'none' }) console.log('下单失败', err) }) }, resetForm() { this.setData({ selectedProfileId: '', patientName: '', patientPhone: '', province: '', provinceIndex: 0, hospital: '', department: '', appointmentDate: '', appointmentTime: '', gender: '', genderIndex: 0, remark: '' }) const totalPrice = this.calculateTotal(this.data.service.price) this.setData({ totalPrice: totalPrice }) }, onShareAppMessage() { const { service } = this.data return { title: `${service.title} - 专业陪诊服务`, path: `/pages/escort/itemdetail?id=${service.id}` } } })