Files
JayuCloud/WeChatProgram/admin/pages/order-detail/index.js
T

200 lines
7.2 KiB
JavaScript

/**
* Purpose: orderApi loads full backend order detail so direct entry and message navigation can render complete data.
* Usage: call getOrderDetail with the current route id before displaying the order detail page.
*/
const orderApi = require('../../api/order')
const paymentApi = require('../../api/payment')
const permission = require('../../utils/permission')
/**
* Purpose: navigation supports index.js by holding or executing the navigation page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const navigation = require('../../utils/navigation')
/**
* Purpose: format supports index.js by holding or executing the format page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const format = require('../../utils/format')
const { getTempRecord, clearTempRecord } = require('../../utils/storage')
const { markDirty } = require('../../utils/page-state')
function getPaymentStatusText(status) {
if (status === 'PAID') {
return '已支付'
}
if (status === 'PAYING') {
return '支付中'
}
if (status === 'REFUNDED') {
return '已退款'
}
return '未支付'
}
Page({
/**
* Purpose: data supports index.js by holding or executing the data page or API role.
* Usage: import, call, read, or update this page-data from the owning page/module while preserving the surrounding loading and permission flow.
*/
data: {
/**
* Purpose: loading marks whether the detail endpoint is currently fetching order data.
* Usage: bind it to loading and retry states while preserving any cached order shown on screen.
*/
loading: false,
/**
* Purpose: orderId keeps the backend order id used by reload and direct-entry flows.
* Usage: set it from route options or cached list data before calling loadOrderDetail.
*/
orderId: '',
/**
* Purpose: order supports index.js by holding or executing the order page or API role.
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
*/
order: null,
canManualConfirmPay: false
},
/**
* Purpose: onLoad supports index.js by holding or executing the on load page or API role.
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
*/
onLoad(options) {
const permissionCodes = getApp().globalData.permissionCodes || []
const cachedOrder = getTempRecord('currentOrder', null)
const orderId = (options && options.id) || (cachedOrder && cachedOrder.id) || ''
this.setData({
orderId,
order: cachedOrder ? this.formatOrder(cachedOrder) : null,
canManualConfirmPay: permission.hasPermission(permissionCodes, 'payment:manage')
})
if (orderId) {
this.loadOrderDetail(orderId)
}
},
/**
* Purpose: loadOrderDetail replaces list-row summary data with the authoritative admin order-detail response.
* Usage: call it from onLoad and retry buttons with the backend order id; failures leave cached data visible.
*/
async loadOrderDetail(orderId) {
this.setData({ loading: true })
try {
const detail = await orderApi.getOrderDetail(orderId)
this.setData({
order: this.formatOrder(detail)
})
} catch (error) {
wx.showToast({
title: error.message || '订单详情加载失败',
icon: 'none'
})
} finally {
this.setData({ loading: false })
}
},
/**
* Purpose: formatOrder prepares order and item fields for WXML, including review and change-request summaries.
* Usage: run every cached or fetched order object through this method before assigning it to data.order.
*/
formatOrder(order) {
const items = Array.isArray(order && order.items) ? order.items : []
const paymentStatus = order && order.paymentStatus ? order.paymentStatus : 'UNPAID'
return order ? {
...order,
displayStatusText: format.getOrderStatusText(order),
displayPaymentStatusText: getPaymentStatusText(paymentStatus),
displayCreateTime: format.formatDateTime(order.createTime),
displayPayAmount: format.formatPrice(order.payAmount || order.actualAmount),
displayPaidAmount: format.formatPrice(order.paidAmount),
displayPayTime: format.formatDateTime(order.payTime),
canManualConfirmPay: paymentStatus !== 'PAID' && paymentStatus !== 'REFUNDED',
items: items.map(item => ({
...item,
displayAppointmentTime: item.appointmentTime ? format.formatDateTime(item.appointmentTime) : '',
reviewStatusText: item.reviewStatusText || (item.reviewSummary ? '已评价' : '待评价'),
changeStatusText: item.pendingArtistChangeRequest ? item.pendingArtistChangeRequest.displayText : ''
}))
} : null
},
/**
* Purpose: handleRetry reloads backend detail when direct entry failed or no cached data exists.
* Usage: bind it to the empty state action so operators can recover without returning to the list.
*/
handleRetry() {
if (!this.data.orderId) {
navigation.goBack('/pages/orders/index')
return
}
this.loadOrderDetail(this.data.orderId)
},
async handleManualConfirmPay() {
if (!this.data.canManualConfirmPay) {
wx.showToast({
title: '当前账号没有确认支付权限',
icon: 'none'
})
return
}
const order = this.data.order
if (!order) {
return
}
const remark = await new Promise(resolve => {
wx.showModal({
title: '手动确认支付',
content: `确认将订单 ${order.orderNo || order.id} 标记为已支付?`,
editable: true,
placeholderText: '备注,可留空',
confirmText: '确认',
success: result => {
if (!result.confirm) {
resolve(null)
return
}
resolve(String(result.content || '').trim())
},
fail: () => resolve(null)
})
})
if (remark === null) {
return
}
try {
await paymentApi.manualConfirmOrder(order.id, remark)
markDirty('admin-orders')
markDirty('admin-workbench')
wx.showToast({
title: '支付已确认',
icon: 'none'
})
this.loadOrderDetail(order.id)
} catch (error) {
wx.showToast({
title: error.message || '确认支付失败',
icon: 'none'
})
}
},
/**
* Purpose: onUnload supports index.js by holding or executing the on unload page or API role.
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
*/
onUnload() {
clearTempRecord('currentOrder')
},
/**
* Purpose: handleBack supports index.js by holding or executing the handle back page or API role.
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
*/
handleBack() {
navigation.goBack('/pages/orders/index')
}
})