// pages/order/orderDetail.js const API = require('../../utils/api.js') const STATUS_MAP = { pending: '待确认', confirmed: '已确认', cancel_requested: '待取消', in_progress: '进行中', completed: '已完成', cancelled: '已取消' } const STATUS_DESC_MAP = { pending: '订单已创建,等待确认', confirmed: '订单已确认,等待服务', cancel_requested: '用户已申请取消,待确认', in_progress: '陪诊服务正在进行', completed: '陪诊服务已完成', cancelled: '订单已取消' } // 取消与 status 解耦:展示状态优先取 cancelStatus function resolveDisplayStatus(order) { const cancelStatus = order?.cancelStatus || 'none' if (cancelStatus === 'cancelled') return 'cancelled' if (cancelStatus === 'requested') return 'cancel_requested' return order?.status || 'pending' } const PAYMENT_STATUS_MAP = { unpaid: '未支付', partial: '部分支付', paid: '已支付', refunding: '退款中', refunded: '已退款' } // 支付方式:与 schema 的 payment.method 枚举一一对应 const PAYMENT_METHOD_MAP = { online_wechat: '线上微信', offline_cash: '线下现金', offline_transfer: '线下转账' } // 支付渠道:用户下单时的选择,历史订单缺失按线下解释 const PAY_CHANNEL_MAP = { online_wechat: '微信支付', offline: '线下支付' } function formatDate(timestamp) { if (!timestamp) return '' const date = new Date(timestamp) const y = date.getFullYear() const m = String(date.getMonth() + 1).padStart(2, '0') const d = String(date.getDate()).padStart(2, '0') return y + '-' + m + '-' + d } function formatTime(timestamp) { if (!timestamp) return '' const date = new Date(timestamp) const h = String(date.getHours()).padStart(2, '0') const min = String(date.getMinutes()).padStart(2, '0') return h + ':' + min } function formatDateTime(timestamp) { if (!timestamp) return '' return formatDate(timestamp) + ' ' + formatTime(timestamp) } Page({ data: { orderId: '', loaded: false, // 订单数据 order: null, // 格式化后的显示字段 statusText: '', statusDesc: '', attendantFirstChar: '', scheduleDateText: '', scheduleStartTimeText: '', scheduleEndTimeText: '', createtimeText: '', updatetimeText: '', paymentStatusText: '', paymentMethodText: '', payChannelText: '', showQueryRefundBtn: false, showManualRefundBtn: false, queryingRefund: false, completingRefund: false, attendantSexText: '', sexRequirementText: '', // 操作按钮配置 showConfirmBtn: false, showCancelBtn: false, cancelBtnText: '取消订单', showStartBtn: false, showCompleteBtn: false }, onLoad(options) { const id = options.id || '' this.setData({ orderId: id }) if (id) { this.loadOrderDetail(id) } }, onPullDownRefresh() { if (this.data.orderId) { this.loadOrderDetail(this.data.orderId).then(() => { wx.stopPullDownRefresh() }) } else { wx.stopPullDownRefresh() } }, async loadOrderDetail(id) { wx.showLoading({ title: '加载中...' }) try { const res = await API.escort.getRecordById(id) wx.hideLoading() if (res.code !== 0) { wx.showToast({ title: res.msg || '加载失败', icon: 'none' }) return } const order = res.data?.record || {} this.applyOrderData(order) } catch (err) { wx.hideLoading() console.error('加载订单详情失败', err) wx.showToast({ title: '网络错误', icon: 'none' }) } }, applyOrderData(order) { const cancelStatus = order.cancelStatus || 'none' const status = resolveDisplayStatus(order) const attendant = order.attendant || {} const schedule = order.schedule || {} const payment = order.payment || {} const escort = order.escort || {} const meta = order.meta || {} this.setData({ loaded: true, order: order, // 有关联档案时交由 patient-info 按 ID 加载(不读缓存,以档案最新数据为准) healthProfileId: order.healthProfileId || '', statusText: STATUS_MAP[status] || status, statusDesc: STATUS_DESC_MAP[status] || '', statusClass: status, attendantFirstChar: (attendant.name || '?')[0], scheduleDateText: formatDate(schedule.date), scheduleStartTimeText: schedule.startTime || '', scheduleEndTimeText: schedule.endTime || '', createtimeText: formatDateTime(meta.createtime), updatetimeText: formatDateTime(meta.updatetime), paymentStatusText: PAYMENT_STATUS_MAP[payment.status] || payment.status || '未支付', paymentMethodText: PAYMENT_METHOD_MAP[payment.method] || '', payChannelText: PAY_CHANNEL_MAP[payment.payChannel || 'offline'] || '', // 线上退款:回调丢失时由管理端主动查退款补写终态; // 人工核销:线下退款、或历史手工标记「线上微信」但无退款单的订单; // 后端在存在处理中的线上退款时会拒绝,避免误把微信退款标成已完成 showQueryRefundBtn: payment.status === 'refunding' && payment.method === 'online_wechat', showManualRefundBtn: payment.status === 'refunding', queryingRefund: false, completingRefund: false, attendantSexText: attendant.sex === 'male' ? '男' : attendant.sex === 'female' ? '女' : '', sexRequirementText: escort.sexRequirement === 'male' ? '要求男陪诊' : escort.sexRequirement === 'female' ? '要求女陪诊' : '不限', // 操作按钮(任何状态下均可编辑订单信息) showEditBtn: true, showConfirmBtn: cancelStatus === 'none' && status === 'pending', showCancelBtn: cancelStatus === 'requested' || (cancelStatus === 'none' && (status === 'pending' || status === 'confirmed')), cancelBtnText: cancelStatus === 'requested' ? '同意取消' : '取消订单', showStartBtn: cancelStatus === 'none' && status === 'confirmed', showCompleteBtn: cancelStatus === 'none' && status === 'in_progress' }) }, // 拨打电话 callPhone(e) { // 兼容 patient-info 组件的 call 事件与 data-phone 用法 const phone = (e.detail && e.detail.mobile) || e.currentTarget.dataset.phone if (!phone) { wx.showToast({ title: '暂无电话', icon: 'none' }) return } wx.makePhoneCall({ phoneNumber: phone }) }, // 点击患者信息,进入健康档案详情 onProfileTap() { if (!this.data.healthProfileId) { wx.showToast({ title: '该订单未关联健康档案', icon: 'none' }) return } wx.navigateTo({ url: `/pages/healthprofile/profileInfo?id=${this.data.healthProfileId}` }) }, // 编辑订单 onEdit() { wx.navigateTo({ url: `/pages/order/orderEdit?orderId=${this.data.orderId}` }) }, // 确认订单 onConfirm() { wx.showModal({ title: '确认订单', content: '确认接受此订单?', confirmColor: '#1abc9c', success: (res) => { if (res.confirm) { this.updateStatus('confirmed') } } }) }, // 取消订单 onCancel() { wx.showModal({ title: '取消订单', content: '确定要取消此订单?', confirmColor: '#ff6b6b', success: (res) => { if (res.confirm) { this.updateStatus('cancelled') } } }) }, // 开始服务 onStart() { wx.showModal({ title: '开始服务', content: '确认开始陪诊服务?', confirmColor: '#1abc9c', success: (res) => { if (res.confirm) { this.updateStatus('in_progress') } } }) }, // 完成服务 onComplete() { wx.showModal({ title: '完成服务', content: '确认陪诊服务已完成?', confirmColor: '#1abc9c', success: (res) => { if (res.confirm) { this.updateStatus('completed') } } }) }, // 主动查询退款结果:退款回调丢失时补写终态 async onQueryRefund() { if (this.data.queryingRefund) return this.setData({ queryingRefund: true }) wx.showLoading({ title: '查询中...' }) try { const res = await API.escort.queryRefund(this.data.orderId) wx.hideLoading() if (res.code !== 0) { wx.showToast({ title: res.msg || '查询失败', icon: 'none' }) return } const status = res.data?.refund?.status wx.showToast({ title: status === 'success' ? '退款已成功' : '查询完成', icon: status === 'success' ? 'success' : 'none' }) this.loadOrderDetail(this.data.orderId) } catch (err) { wx.hideLoading() console.error('查询退款结果失败', err) wx.showToast({ title: '网络错误', icon: 'none' }) } finally { this.setData({ queryingRefund: false }) } }, // 线下/人工退款:平台确认已退款后核销,避免订单永远停在「退款中」 onCompleteManualRefund() { if (this.data.completingRefund) return wx.showModal({ title: '确认退款完成', content: '请确认已通过现金/转账等方式将款项退还给用户,操作后订单将显示「已退款」。', success: (modal) => { if (modal.confirm) this.doCompleteManualRefund() } }) }, async doCompleteManualRefund() { this.setData({ completingRefund: true }) wx.showLoading({ title: '处理中...' }) try { const res = await API.escort.completeManualRefund(this.data.orderId) wx.hideLoading() if (res.code !== 0) { wx.showToast({ title: res.msg || '操作失败', icon: 'none' }) return } wx.showToast({ title: '已标记退款完成', icon: 'success' }) this.loadOrderDetail(this.data.orderId) } catch (err) { wx.hideLoading() console.error('人工核销退款失败', err) wx.showToast({ title: '网络错误', icon: 'none' }) } finally { this.setData({ completingRefund: false }) } }, async updateStatus(newStatus) { wx.showLoading({ title: '处理中...' }) try { const res = await API.escort.updateStatus(this.data.orderId, { status: newStatus }) wx.hideLoading() if (res.code !== 0) { wx.showToast({ title: res.msg || '操作失败', icon: 'none' }) return } wx.showToast({ title: '操作成功', icon: 'success' }) this.loadOrderDetail(this.data.orderId) } catch (err) { wx.hideLoading() console.error('更新状态失败', err) wx.showToast({ title: '网络错误', icon: 'none' }) } } })