feat(escort): 微信支付入口与取消状态展示

- 下单改为提交预约后弹窗选择支付方式,默认微信支付
- 预约详情支持发起微信支付、查单确认与待支付期间切换支付方式
- 预约列表展示支付渠道、待支付/超时状态并提供去支付入口
- 取消状态与 status 解耦,展示状态优先取 cancelStatus
- 详情页医院选择回传字段同步移除 manual
This commit is contained in:
lik
2026-11-05 18:55:18 +08:00
parent f70a8f8b72
commit 8eb7bc9e7a
11 changed files with 627 additions and 90 deletions
+128 -35
View File
@@ -22,7 +22,7 @@ Page({
provinces: [],
hospital: '',
hospitalOptions: [],
hospitalManual: false,
hospitalCustom: false,
appointmentDate: '',
appointmentTime: '',
minAppointmentDate: '',
@@ -34,6 +34,9 @@ Page({
remark: '',
shippingAddress: '',
requiresAddress: false,
payChannel: 'online_wechat',
// 点击「提交预约」后弹出支付方式选择,默认微信支付
showPayPopup: false,
gender: '不限',
genderIndex: 0,
genderOptions: ['不限', '男', '女']
@@ -72,12 +75,9 @@ Page({
const app = getApp()
// 启动时 app 已在预取,这里只等它落定后用最新列表刷新下拉;
// 没有可用医院时退回手动输入,避免下拉是空的还阻断下单
// 列表为空时组件内的「其他医院」入口仍可手填,不会阻断下单
app.loadHospitals().then(() => {
this.refreshHospitalOptions()
if (!this.data.hospitalOptions.length && !this.data.hospitalManual) {
this.setData({ hospitalManual: true })
}
})
await app.waitForAuth()
if (app.globalData.user?._id) this.loadProfiles()
@@ -122,7 +122,7 @@ Page({
province: provinces[0] || '',
provinceIndex: 0,
hospital: '',
hospitalManual: false,
hospitalCustom: false,
appointmentDate: '',
appointmentTime: '',
shippingAddress: '',
@@ -209,7 +209,7 @@ Page({
provinceIndex: index,
province,
hospital: '',
hospitalManual: false
hospitalCustom: false
}, () => this.refreshHospitalOptions(province))
},
@@ -218,7 +218,8 @@ Page({
const hospitalOptions = getApp().getHospitalsByProvince(province)
let hospital = ''
if (this.data.hospitalManual) {
if (this.data.hospitalCustom) {
// 「其他医院」手填的名称不在库内,刷新列表时原样保留
hospital = this.data.hospital
} else {
const selected = hospitalOptions.find((item) => item._id === this.selectedHospitalId)
@@ -233,11 +234,11 @@ Page({
this.setData({ hospitalOptions, hospital })
},
// 医院选择组件回传:name 为最终值,manual 表示是否手动输入,hospital 为库内记录
// 医院选择组件回传:name 为最终值,manual 表示是否为「其他医院」手填,hospital 为库内记录
onHospitalChange(e) {
const { name, manual, hospital } = e.detail
this.selectedHospitalId = hospital?._id || ''
this.setData({ hospital: name, hospitalManual: manual })
this.setData({ hospital: name, hospitalCustom: manual })
},
onDateChange(e) {
@@ -263,6 +264,13 @@ Page({
this.setData({ remark: e.detail.value })
},
onPayChannelChange(e) {
const channel = e.currentTarget.dataset.channel
if (!['online_wechat', 'offline'].includes(channel)) return
if (this.data.submitting) return
this.setData({ payChannel: channel })
},
onShippingAddressChange(e) {
this.setData({ shippingAddress: e.detail.value })
},
@@ -270,7 +278,9 @@ Page({
validateOrder() {
const data = this.data
if (!data.selectedProfileId || !data.selectedPatient) return '请选择就诊人'
if (!data.hospital.trim()) return data.hospitalManual ? '请输入就诊医院' : '请选择就诊医院'
if (!data.hospital.trim()) {
return data.hospitalOptions.length ? '请选择就诊医院' : '请填写就诊医院名称'
}
if (!data.appointmentDate) return '请选择预约日期'
if (!data.appointmentTime) return '请选择预约时间'
if (data.requiresAddress && !data.shippingAddress.trim()) return '请填写完整收货地址'
@@ -282,7 +292,26 @@ Page({
async submitOrder() {
if (this.data.submitting) return
this.setData({ submitting: true })
// 先做一次表单校验,避免用户选完支付方式才发现信息没填全
const error = this.validateOrder()
if (error) {
wx.showToast({ title: error, icon: 'none' })
return
}
this.setData({ showPayPopup: true, payChannel: 'online_wechat' })
},
onClosePayPopup() {
if (this.data.submitting) return
this.setData({ showPayPopup: false })
},
/**
* 确认支付方式后真正提交订单
*/
async onConfirmPay() {
if (this.data.submitting) return
this.setData({ submitting: true, showPayPopup: false })
try {
const loggedIn = await this.ensureLogin()
if (loggedIn === null) return
@@ -298,33 +327,24 @@ Page({
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()
},
schedule: {
date: data.appointmentDate,
startTime: data.appointmentTime
},
shippingAddress: { address: data.shippingAddress.trim() },
notes: { patientNote: data.remark.trim() }
}
const payChannel = this.data.payChannel
const orderData = this.buildOrderData()
// 网络重试沿用同一幂等键,服务端只创建一笔订单。
const res = await API.escort.createRecord(orderData)
if (res.code !== 0) throw new Error(res.msg || '下单失败')
wx.showToast({ title: '下单成功', icon: 'success' })
const record = res.data?.record
this.resetForm()
wx.navigateTo({ url: '/pages/escort/recordlist?tab=ongoing' })
if (payChannel === 'offline') {
wx.showToast({ title: '线下支付,请联系平台收款', icon: 'none' })
wx.navigateTo({ url: '/pages/escort/recordlist?tab=ongoing' })
return
}
// 选微信支付:下单成功后立即拉起收银台,支付结果以后端查单为准
if (record?._id) await this.startWechatPay(record._id)
else wx.navigateTo({ url: '/pages/escort/recordlist?tab=ongoing' })
} catch (err) {
console.log('下单失败', err)
wx.showToast({ title: err.message || '网络请求失败,请重试', icon: 'none' })
@@ -333,6 +353,79 @@ Page({
}
},
buildOrderData() {
const data = this.data
const sexMap = { 男: 'male', 女: 'female' }
return {
idempotencyKey: this.idempotencyKey,
healthProfileId: data.selectedProfileId,
escort: {
serviceId: data.service.id,
sexRequirement: sexMap[data.gender] || 'none'
},
hospital: {
province: data.province,
name: data.hospital.trim()
},
schedule: {
date: data.appointmentDate,
startTime: data.appointmentTime
},
shippingAddress: { address: data.shippingAddress.trim() },
notes: { patientNote: data.remark.trim() },
payment: { payChannel: data.payChannel }
}
},
/**
* 发起微信支付:payOrder → requestPayment → 查单确认
* 支付按钮防抖(this.paying),前端回调只作提示,最终状态以 getPayStatus 为准
*/
async startWechatPay(recordId) {
if (this.paying) return
this.paying = true
let paid = false
try {
const res = await API.escort.payOrder(recordId)
if (res.code !== 0 || !res.data?.payParams) {
wx.showToast({ title: res.msg || '支付发起失败,请稍后重试', icon: 'none' })
return
}
const payResult = await new Promise((resolve) => {
wx.requestPayment({
...res.data.payParams,
success: () => resolve({ ok: true }),
fail: (err) => resolve({ ok: false, err })
})
})
if (!payResult.ok) {
const errMsg = payResult.err?.errMsg || ''
if (/cancel/i.test(errMsg)) {
wx.showToast({ title: '已取消支付', icon: 'none' })
return
}
// 非取消失败可能是网络中断但支付已成功:先查单兜底
}
const statusRes = await API.escort.getPayStatus(recordId).catch(() => null)
paid = statusRes?.code === 0 && statusRes.data?.paymentStatus === 'paid'
wx.showToast({
title: paid ? '支付成功' : '支付结果确认中',
icon: paid ? 'success' : 'none'
})
} catch (err) {
console.log('发起支付失败', err)
wx.showToast({ title: '支付发起失败,请稍后重试', icon: 'none' })
} finally {
this.paying = false
setTimeout(() => {
wx.navigateTo({ url: `/pages/escort/recorddetail?id=${recordId}` })
}, paid ? 600 : 0)
}
},
resetForm() {
const provinces = this.data.provinces
this.selectedHospitalId = ''
@@ -344,7 +437,7 @@ Page({
province: provinces[0] || '',
provinceIndex: 0,
hospital: '',
hospitalManual: false,
hospitalCustom: false,
appointmentDate: '',
appointmentTime: '',
sessionIndex: 0,