feat(escort): 服务详情页增加支付方式弹窗,列表接入「其他项目」
提交预约后先选微信支付或线下支付,微信支付下单即拉起收银台并查单兜底;就医代办组补充 id 9 服务项。
This commit is contained in:
+108
-16
@@ -35,7 +35,10 @@ Page({
|
||||
requiresAddress: false,
|
||||
gender: '不限',
|
||||
genderIndex: 0,
|
||||
genderOptions: ['不限', '男', '女']
|
||||
genderOptions: ['不限', '男', '女'],
|
||||
payChannel: 'online_wechat',
|
||||
// 点击「提交预约」后弹出支付方式选择,默认微信支付
|
||||
showPayPopup: false
|
||||
},
|
||||
|
||||
onLoad(options) {
|
||||
@@ -318,6 +321,13 @@ Page({
|
||||
return ''
|
||||
},
|
||||
|
||||
onPayChannelChange(e) {
|
||||
const channel = e.currentTarget.dataset.channel
|
||||
if (!['online_wechat', 'offline'].includes(channel)) return
|
||||
if (this.data.submitting) return
|
||||
this.setData({ payChannel: channel })
|
||||
},
|
||||
|
||||
submitOrder() {
|
||||
if (this.data.submitting) return
|
||||
const error = this.validateOrder()
|
||||
@@ -325,10 +335,58 @@ Page({
|
||||
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 error = this.validateOrder()
|
||||
if (error) {
|
||||
wx.showToast({ title: error, icon: 'none' })
|
||||
return
|
||||
}
|
||||
|
||||
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 || '下单失败')
|
||||
const record = res.data?.record
|
||||
this.resetForm()
|
||||
|
||||
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' })
|
||||
} finally {
|
||||
this.setData({ submitting: false })
|
||||
}
|
||||
},
|
||||
|
||||
buildOrderData() {
|
||||
const data = this.data
|
||||
const sexMap = { 男: 'male', 女: 'female' }
|
||||
const orderData = {
|
||||
return {
|
||||
idempotencyKey: this.idempotencyKey,
|
||||
healthProfileId: data.selectedProfileId,
|
||||
escort: {
|
||||
@@ -344,24 +402,58 @@ Page({
|
||||
startTime: data.appointmentTime
|
||||
},
|
||||
shippingAddress: { address: data.shippingAddress.trim() },
|
||||
notes: { patientNote: data.remark.trim() }
|
||||
notes: { patientNote: data.remark.trim() },
|
||||
payment: { payChannel: data.payChannel }
|
||||
}
|
||||
},
|
||||
|
||||
// 同步锁定按钮;网络重试沿用同一幂等键,服务端只创建一笔订单。
|
||||
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' })
|
||||
/**
|
||||
* 发起微信支付: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 })
|
||||
})
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log('下单失败', err)
|
||||
wx.showToast({ title: err.message || '网络请求失败,请重试', icon: 'none' })
|
||||
|
||||
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'
|
||||
})
|
||||
.finally(() => this.setData({ submitting: false }))
|
||||
} 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() {
|
||||
|
||||
Reference in New Issue
Block a user