165 lines
5.3 KiB
JavaScript
165 lines
5.3 KiB
JavaScript
/**
|
||
* Soul创业派对 - 代付详情页
|
||
* 好友打开后看到订单信息,点击「帮他付款」完成代付
|
||
*/
|
||
const app = getApp()
|
||
|
||
Page({
|
||
data: {
|
||
statusBarHeight: 44,
|
||
requestSn: '',
|
||
detail: null,
|
||
loading: true,
|
||
paying: false,
|
||
isInitiator: false,
|
||
requesterMsg: '',
|
||
amountDisplay: '0.00' // 预格式化金额,WXML 中 toFixed 可能不生效
|
||
},
|
||
|
||
onLoad(options) {
|
||
this.setData({ statusBarHeight: app.globalData.statusBarHeight || 44 })
|
||
const requestSn = (options.requestSn || '').trim()
|
||
if (!requestSn) {
|
||
wx.showToast({ title: '代付链接无效', icon: 'none' })
|
||
setTimeout(() => wx.switchTab({ url: '/pages/index/index' }), 1500)
|
||
return
|
||
}
|
||
this.setData({ requestSn })
|
||
this.loadDetail()
|
||
},
|
||
|
||
async loadDetail() {
|
||
const { requestSn } = this.data
|
||
if (!requestSn) return
|
||
this.setData({ loading: true })
|
||
try {
|
||
const res = await app.request(`/api/miniprogram/gift-pay/detail?requestSn=${encodeURIComponent(requestSn)}`)
|
||
if (res && res.success) {
|
||
const myId = app.globalData.userInfo?.id || ''
|
||
const isInitiator = !!myId && res.initiatorUserId === myId
|
||
const requesterMsg = isInitiator
|
||
? '分享给好友,好友打开后即可为你完成支付。'
|
||
: (res.initiatorMsg || `" 请帮我代付「${res.sectionTitle || res.description || '该商品'}」,非常感谢! "`)
|
||
const amountDisplay = (res.amount != null && res.amount !== '')
|
||
? Number(res.amount).toFixed(2)
|
||
: '0.00'
|
||
this.setData({ detail: res, loading: false, isInitiator, requesterMsg, amountDisplay })
|
||
} else {
|
||
this.setData({ loading: false })
|
||
wx.showToast({ title: res?.error || '加载失败', icon: 'none' })
|
||
}
|
||
} catch (e) {
|
||
this.setData({ loading: false })
|
||
wx.showToast({ title: '加载失败', icon: 'none' })
|
||
}
|
||
},
|
||
|
||
async doPay() {
|
||
// 优先尝试静默获取 openId(有会话时可直接发起支付)
|
||
let openId = app.globalData.openId || wx.getStorageSync('openId')
|
||
if (!openId) {
|
||
wx.showLoading({ title: '获取支付凭证...', mask: true })
|
||
openId = await app.getOpenId()
|
||
wx.hideLoading()
|
||
}
|
||
if (!openId) {
|
||
const { confirm } = await new Promise(r => {
|
||
wx.showModal({
|
||
title: '请先登录',
|
||
content: '登录后可帮他完成代付',
|
||
confirmText: '去登录',
|
||
cancelText: '取消',
|
||
success: res => r(res)
|
||
})
|
||
})
|
||
if (confirm) wx.switchTab({ url: '/pages/my/my' })
|
||
return
|
||
}
|
||
const { requestSn, detail } = this.data
|
||
if (!requestSn || !detail) return
|
||
|
||
this.setData({ paying: true })
|
||
wx.showLoading({ title: '创建订单中...', mask: true })
|
||
try {
|
||
const res = await app.request({
|
||
url: '/api/miniprogram/gift-pay/pay',
|
||
method: 'POST',
|
||
data: {
|
||
requestSn,
|
||
openId,
|
||
userId: app.globalData.userInfo?.id || ''
|
||
}
|
||
})
|
||
wx.hideLoading()
|
||
if (!res || !res.success || !res.data?.payParams) {
|
||
throw new Error(res?.error || '创建订单失败')
|
||
}
|
||
const payParams = res.data.payParams
|
||
payParams._orderSn = res.data.orderSn
|
||
|
||
await new Promise((resolve, reject) => {
|
||
wx.requestPayment({
|
||
...payParams,
|
||
signType: payParams.signType || 'MD5',
|
||
success: resolve,
|
||
fail: reject
|
||
})
|
||
})
|
||
|
||
wx.showToast({ title: '代付成功', icon: 'success' })
|
||
this.setData({ paying: false })
|
||
setTimeout(() => {
|
||
wx.navigateBack({ fail: () => wx.switchTab({ url: '/pages/index/index' }) })
|
||
}, 1500)
|
||
} catch (e) {
|
||
this.setData({ paying: false })
|
||
if (e.errMsg && e.errMsg.includes('cancel')) {
|
||
wx.showToast({ title: '已取消支付', icon: 'none' })
|
||
} else {
|
||
const errMsg = e.message || e.error || '支付失败'
|
||
const isPrepayError = /prepay_id|创建订单|支付请求/i.test(errMsg)
|
||
if (isPrepayError) {
|
||
const { confirm } = await new Promise(r => {
|
||
wx.showModal({
|
||
title: '订单创建失败',
|
||
content: errMsg + '\n\n是否重新创建订单并重试?',
|
||
confirmText: '重新创建订单',
|
||
cancelText: '取消',
|
||
success: res => r(res)
|
||
})
|
||
})
|
||
if (confirm) this.doPay()
|
||
} else {
|
||
wx.showToast({ title: errMsg, icon: 'none' })
|
||
}
|
||
}
|
||
}
|
||
},
|
||
|
||
goBack() {
|
||
app.goBackOrToHome()
|
||
},
|
||
|
||
goToInitiatorProfile() {
|
||
const { detail } = this.data
|
||
if (!detail?.initiatorUserId) return
|
||
wx.navigateTo({ url: `/pages/member-detail/member-detail?id=${detail.initiatorUserId}` })
|
||
},
|
||
|
||
goToArticle() {
|
||
const { detail } = this.data
|
||
if (!detail || detail.productType !== 'section' || !detail.productId) return
|
||
const mid = detail.productMid || app.getSectionMid?.(detail.productId)
|
||
const q = mid ? `mid=${mid}` : `id=${detail.productId}`
|
||
wx.navigateTo({ url: `/pages/read/read?${q}` })
|
||
},
|
||
|
||
onShareAppMessage() {
|
||
const { requestSn } = this.data
|
||
return {
|
||
title: '好友请你帮忙代付 - Soul创业派对',
|
||
path: requestSn ? `/pages/gift-pay/detail?requestSn=${requestSn}` : '/pages/gift-pay/detail'
|
||
}
|
||
}
|
||
})
|