144 lines
3.6 KiB
JavaScript
144 lines
3.6 KiB
JavaScript
const {
|
|
request,
|
|
ensureLogin,
|
|
cents,
|
|
clientRequestId,
|
|
} = require('../../utils/api.js')
|
|
|
|
function defaultStartAt() {
|
|
const date = new Date(Date.now() + 30 * 60 * 1000)
|
|
date.setSeconds(0, 0)
|
|
return date
|
|
}
|
|
|
|
function isoMinutesAfter(date, minutes) {
|
|
return new Date(date.getTime() + minutes * 60 * 1000).toISOString()
|
|
}
|
|
|
|
Page({
|
|
data: {
|
|
storeId: '',
|
|
roomId: '',
|
|
loading: false,
|
|
errorMessage: '',
|
|
successMessage: '',
|
|
durationMinutes: 120,
|
|
pricingMode: 'HOURLY',
|
|
quote: null,
|
|
order: null,
|
|
payment: null,
|
|
},
|
|
onLoad(options) {
|
|
this.setData({
|
|
storeId: options.storeId || '',
|
|
roomId: options.roomId || '',
|
|
})
|
|
},
|
|
|
|
setDuration(event) {
|
|
this.setData({
|
|
durationMinutes: Number(event.currentTarget.dataset.minutes),
|
|
quote: null,
|
|
order: null,
|
|
payment: null,
|
|
errorMessage: '',
|
|
successMessage: '',
|
|
})
|
|
},
|
|
|
|
async quote() {
|
|
await this.withRequest(async () => {
|
|
const payload = this.pricingPayload()
|
|
const response = await request('/pricing/quote', { method: 'POST', data: payload })
|
|
this.setData({
|
|
quote: {
|
|
...response.data,
|
|
totalText: cents(response.data.totalCents),
|
|
depositText: cents(response.data.depositCents),
|
|
},
|
|
successMessage: '报价已生成',
|
|
})
|
|
})
|
|
},
|
|
|
|
async reserve() {
|
|
await this.withRequest(async () => {
|
|
const payload = this.pricingPayload()
|
|
const response = await request('/orders/reserve', { method: 'POST', data: payload })
|
|
this.setData({
|
|
order: response.data,
|
|
successMessage: '时段已预占,请继续支付',
|
|
})
|
|
})
|
|
},
|
|
|
|
async createTestPayment() {
|
|
if (!this.data.order || !this.data.order.orderId) {
|
|
this.setData({ errorMessage: '请先预占订单' })
|
|
return
|
|
}
|
|
await this.withRequest(async () => {
|
|
const response = await request('/payments', {
|
|
method: 'POST',
|
|
data: {
|
|
orderId: this.data.order.orderId,
|
|
provider: 'TEST',
|
|
clientRequestId: clientRequestId('miniapp-pay'),
|
|
},
|
|
})
|
|
this.setData({
|
|
payment: {
|
|
...response.data,
|
|
amountText: cents(response.data.amountCents),
|
|
},
|
|
successMessage: '测试支付单已创建',
|
|
})
|
|
})
|
|
},
|
|
|
|
async completeTestPayment() {
|
|
if (!this.data.payment || !this.data.payment.paymentId) {
|
|
this.setData({ errorMessage: '请先创建测试支付单' })
|
|
return
|
|
}
|
|
await this.withRequest(async () => {
|
|
await request(`/payments/${encodeURIComponent(this.data.payment.paymentId)}/test-complete`, {
|
|
method: 'POST',
|
|
data: {
|
|
callbackId: clientRequestId('miniapp-callback'),
|
|
amountCents: this.data.payment.amountCents,
|
|
},
|
|
})
|
|
this.setData({ successMessage: '测试支付已完成,生产环境将切换微信支付' })
|
|
})
|
|
},
|
|
|
|
openOrder(event) {
|
|
const orderId = event.currentTarget.dataset.orderId
|
|
if (!orderId) return
|
|
wx.navigateTo({ url: `/pages/orders/detail?orderId=${encodeURIComponent(orderId)}` })
|
|
},
|
|
|
|
pricingPayload() {
|
|
const start = defaultStartAt()
|
|
return {
|
|
roomId: this.data.roomId,
|
|
startAt: start.toISOString(),
|
|
endAt: isoMinutesAfter(start, this.data.durationMinutes),
|
|
pricingMode: this.data.pricingMode,
|
|
}
|
|
},
|
|
|
|
async withRequest(work) {
|
|
this.setData({ loading: true, errorMessage: '', successMessage: '' })
|
|
try {
|
|
await ensureLogin()
|
|
await work()
|
|
} catch (error) {
|
|
this.setData({ errorMessage: error.message || '操作失败' })
|
|
} finally {
|
|
this.setData({ loading: false })
|
|
}
|
|
},
|
|
})
|