345 lines
11 KiB
JavaScript
345 lines
11 KiB
JavaScript
const API = require('../../utils/api.js')
|
|
const { formatDate } = require('../../utils/date.js')
|
|
|
|
Page({
|
|
data: {
|
|
service: {},
|
|
pageLoading: true,
|
|
pageError: '',
|
|
totalPrice: '0.00',
|
|
serviceIcon: 'user-business',
|
|
showOrderPopup: false,
|
|
submitting: false,
|
|
profiles: [],
|
|
selectedProfileId: '',
|
|
patientName: '',
|
|
patientPhone: '',
|
|
selectedPatient: null,
|
|
province: '',
|
|
provinceIndex: 0,
|
|
provinces: [],
|
|
hospital: '',
|
|
appointmentDate: '',
|
|
appointmentTime: '',
|
|
minAppointmentDate: '',
|
|
fixedTime: false,
|
|
timeOptions: [],
|
|
sessionIndex: 0,
|
|
timeStart: '',
|
|
timeEnd: '',
|
|
remark: '',
|
|
shippingAddress: '',
|
|
requiresAddress: false,
|
|
gender: '不限',
|
|
genderIndex: 0,
|
|
genderOptions: ['不限', '男', '女'],
|
|
department: '',
|
|
departments: ['内科', '外科', '妇产科', '儿科', '骨科', '神经科', '心血管内科', '消化内科', '呼吸内科', '内分泌科', '皮肤科', '眼科', '耳鼻喉科', '口腔科', '急诊科', '肿瘤科', '康复医学科', '老年病科', '中医科', '针灸推拿科']
|
|
},
|
|
|
|
onLoad(options) {
|
|
this.serviceId = Number(options.id)
|
|
this.openBookAfterLoad = options.book === '1'
|
|
if (!Number.isInteger(this.serviceId) || this.serviceId <= 0) {
|
|
this.setData({ pageLoading: false, pageError: '服务不存在或已下架' })
|
|
return
|
|
}
|
|
this.initializePage()
|
|
},
|
|
|
|
onShow() {
|
|
if (this.data.showOrderPopup) this.loadProfiles()
|
|
},
|
|
|
|
async initializePage() {
|
|
this.setData({ pageLoading: true, pageError: '' })
|
|
const services = await getApp().loadServices()
|
|
if (!services) {
|
|
this.setData({ pageLoading: false, pageError: '服务加载失败,请重试' })
|
|
return
|
|
}
|
|
const service = services.find((item) => item.id === this.serviceId)
|
|
if (!service) {
|
|
this.setData({ pageLoading: false, pageError: '服务不存在或已下架' })
|
|
return
|
|
}
|
|
|
|
this.applyBookingConfig(service)
|
|
const serviceView = {
|
|
...service,
|
|
notices: (service.notices || []).map(text => ({ id: text, text }))
|
|
}
|
|
this.setData({
|
|
service: serviceView,
|
|
serviceIcon: this.getServiceIcon(service.title),
|
|
totalPrice: service.price,
|
|
pageLoading: false
|
|
})
|
|
wx.setNavigationBarTitle({ title: service.title || '服务详情' })
|
|
if (this.openBookAfterLoad) {
|
|
this.openBookAfterLoad = false
|
|
this.openOrderPopup()
|
|
}
|
|
},
|
|
|
|
retryLoad() {
|
|
this.initializePage()
|
|
},
|
|
|
|
applyBookingConfig(service) {
|
|
const booking = service.booking || {}
|
|
const sessions = booking.sessions || []
|
|
const fixedTime = sessions.length > 0 && sessions.every((item) => !item.flexible)
|
|
const firstSession = sessions[0] || {}
|
|
const minDate = new Date()
|
|
minDate.setDate(minDate.getDate() + Number(booking.advanceDays || 0))
|
|
const minAppointmentDate = formatDate(minDate)
|
|
let timeEnd = firstSession.end || ''
|
|
if (!fixedTime && firstSession.end && booking.durationMinutes) {
|
|
const [hour, minute] = firstSession.end.split(':').map(Number)
|
|
const latest = hour * 60 + minute - booking.durationMinutes
|
|
timeEnd = `${String(Math.floor(latest / 60)).padStart(2, '0')}:${String(latest % 60).padStart(2, '0')}`
|
|
}
|
|
const provinces = service.areas || []
|
|
this.setData({
|
|
minAppointmentDate,
|
|
fixedTime,
|
|
timeOptions: sessions.map((item) => `${item.start} - ${item.end}`),
|
|
sessionIndex: 0,
|
|
timeStart: firstSession.start || '',
|
|
timeEnd,
|
|
provinces,
|
|
province: provinces[0] || '',
|
|
provinceIndex: 0,
|
|
requiresAddress: !!booking.requiresAddress
|
|
})
|
|
},
|
|
|
|
getServiceIcon(title) {
|
|
if (title.includes('夜间')) return 'moon'
|
|
if (title.includes('陪诊')) return 'time'
|
|
return 'user-business'
|
|
},
|
|
|
|
async ensureLogin() {
|
|
const app = getApp()
|
|
await app.waitForAuth()
|
|
if (app.globalData.user?._id) return true
|
|
const modal = await new Promise((resolve) => {
|
|
wx.showModal({
|
|
title: '登录后预约',
|
|
content: '登录成功后将返回当前服务并继续预约',
|
|
// showModal 的按钮文案上限 4 个字:写「登录并继续」会让弹窗直接 fail,
|
|
// 用户看到的就是「点了预约没反应」,所以这里必须压在 4 字内
|
|
confirmText: '去登录',
|
|
confirmColor: '#B8520A',
|
|
success: resolve,
|
|
fail: (err) => {
|
|
// 弹窗本身失败也不能静默:按「未确认」处理,避免整条链路无声中断
|
|
console.log('登录提示弹窗失败', err)
|
|
resolve({ confirm: false })
|
|
}
|
|
})
|
|
})
|
|
if (!modal.confirm) return null
|
|
// 用户明确点击登录时,解除主动退出对静默登录的限制。
|
|
wx.removeStorageSync('has_logout')
|
|
return app.silentSignin()
|
|
},
|
|
|
|
async openOrderPopup() {
|
|
if (!this.data.service.id || this.data.submitting) return
|
|
const loggedIn = await this.ensureLogin()
|
|
if (loggedIn === null) return
|
|
if (!loggedIn || !getApp().globalData.user?._id) {
|
|
wx.showToast({ title: '登录失败,请重试', icon: 'none' })
|
|
return
|
|
}
|
|
this.idempotencyKey = `order_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 12)}`
|
|
this.setData({
|
|
patientName: '',
|
|
patientPhone: '',
|
|
selectedProfileId: '',
|
|
selectedPatient: null,
|
|
showOrderPopup: true
|
|
})
|
|
this.loadProfiles()
|
|
},
|
|
|
|
loadProfiles() {
|
|
return API.healthProfile.getMyProfiles({ page: 1, pageSize: 50 })
|
|
.then((res) => {
|
|
if (res.code !== 0) throw new Error(res.msg || '就诊人加载失败')
|
|
const profiles = (res.data?.list || []).map((item) => ({
|
|
_id: item._id,
|
|
name: item.profile?.name || '',
|
|
mobile: item.profile?.mobile || '',
|
|
sex: item.profile?.sex || '',
|
|
birth: item.profile?.birth || ''
|
|
}))
|
|
this.setData({ profiles })
|
|
})
|
|
.catch((err) => {
|
|
console.log('获取就诊人列表失败', err)
|
|
wx.showToast({ title: '就诊人加载失败,请重试', icon: 'none' })
|
|
})
|
|
},
|
|
|
|
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,
|
|
selectedPatient: profile
|
|
})
|
|
},
|
|
|
|
goToAddProfile() {
|
|
wx.navigateTo({ url: '/pages/health/profileedit' })
|
|
},
|
|
|
|
closeOrderPopup() {
|
|
if (!this.data.submitting) this.setData({ showOrderPopup: false })
|
|
},
|
|
|
|
onProvinceChange(e) {
|
|
const index = Number(e.detail.value)
|
|
this.setData({ provinceIndex: index, province: this.data.provinces[index] })
|
|
},
|
|
|
|
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) {
|
|
if (this.data.fixedTime) {
|
|
const index = Number(e.detail.value)
|
|
const session = this.data.service.booking.sessions[index]
|
|
this.setData({ sessionIndex: index, appointmentTime: session.start })
|
|
return
|
|
}
|
|
this.setData({ appointmentTime: e.detail.value })
|
|
},
|
|
|
|
onGenderChange(e) {
|
|
const index = Number(e.detail.value)
|
|
this.setData({ genderIndex: index, gender: this.data.genderOptions[index] })
|
|
},
|
|
|
|
onRemarkChange(e) {
|
|
this.setData({ remark: e.detail.value })
|
|
},
|
|
|
|
onShippingAddressChange(e) {
|
|
this.setData({ shippingAddress: e.detail.value })
|
|
},
|
|
|
|
validateOrder() {
|
|
const data = this.data
|
|
if (!data.selectedProfileId || !data.selectedPatient) return '请选择就诊人'
|
|
if (!data.hospital.trim()) return '请输入就诊医院'
|
|
if (!data.department.trim()) return '请输入就诊科室'
|
|
if (!data.appointmentDate) return '请选择预约日期'
|
|
if (!data.appointmentTime) return '请选择预约时间'
|
|
if (data.requiresAddress && !data.shippingAddress.trim()) return '请填写完整收货地址'
|
|
if (!/^1[3-9]\d{9}$/.test(data.selectedPatient.mobile.trim())) {
|
|
return '就诊人手机号有误,请到就诊人档案修改'
|
|
}
|
|
return ''
|
|
},
|
|
|
|
submitOrder() {
|
|
if (this.data.submitting) return
|
|
const error = this.validateOrder()
|
|
if (error) {
|
|
wx.showToast({ title: error, icon: 'none' })
|
|
return
|
|
}
|
|
|
|
const data = this.data
|
|
const sexMap = { 男: 'male', 女: 'female' }
|
|
const orderData = {
|
|
idempotencyKey: this.idempotencyKey,
|
|
healthProfileId: data.selectedProfileId,
|
|
escort: {
|
|
serviceId: data.service.id,
|
|
sexRequirement: sexMap[data.gender] || 'none'
|
|
},
|
|
hospital: {
|
|
province: data.province,
|
|
name: data.hospital.trim(),
|
|
department: data.department.trim()
|
|
},
|
|
schedule: {
|
|
date: data.appointmentDate,
|
|
startTime: data.appointmentTime
|
|
},
|
|
shippingAddress: { address: data.shippingAddress.trim() },
|
|
notes: { patientNote: data.remark.trim() }
|
|
}
|
|
|
|
// 同步锁定按钮;网络重试沿用同一幂等键,服务端只创建一笔订单。
|
|
this.setData({ submitting: true })
|
|
API.escort.createRecord(orderData)
|
|
.then((res) => {
|
|
if (res.code !== 0) throw new Error(res.msg || '下单失败')
|
|
wx.showToast({ title: '下单成功', icon: 'success' })
|
|
this.setData({ showOrderPopup: false })
|
|
this.resetForm()
|
|
wx.navigateTo({ url: '/pages/escort/recordlist?tab=ongoing' })
|
|
})
|
|
.catch((err) => {
|
|
console.log('下单失败', err)
|
|
wx.showToast({ title: err.message || '网络请求失败,请重试', icon: 'none' })
|
|
})
|
|
.finally(() => this.setData({ submitting: false }))
|
|
},
|
|
|
|
resetForm() {
|
|
const provinces = this.data.provinces
|
|
this.setData({
|
|
selectedProfileId: '',
|
|
patientName: '',
|
|
patientPhone: '',
|
|
selectedPatient: null,
|
|
province: provinces[0] || '',
|
|
provinceIndex: 0,
|
|
hospital: '',
|
|
department: '',
|
|
appointmentDate: '',
|
|
appointmentTime: '',
|
|
sessionIndex: 0,
|
|
gender: '不限',
|
|
genderIndex: 0,
|
|
remark: '',
|
|
shippingAddress: ''
|
|
})
|
|
this.idempotencyKey = ''
|
|
},
|
|
|
|
onShareAppMessage() {
|
|
const service = this.data.service
|
|
return {
|
|
title: service.id ? `${service.title} - 专业陪诊服务` : '暖橙陪诊 · 专业陪诊服务',
|
|
path: service.id ? `/pages/escort/itemdetail?id=${service.id}` : '/pages/home/index'
|
|
}
|
|
},
|
|
|
|
onShareTimeline() {
|
|
const service = this.data.service
|
|
if (!service.id) return { title: '暖橙陪诊 · 专业陪诊服务' }
|
|
return { title: `${service.title} - 专业陪诊服务`, query: `id=${service.id}` }
|
|
}
|
|
})
|