Files
JayuCloud/FD-WeChat-Fronted/admin/pages/orders/index.js
T
2026-06-15 15:53:55 +08:00

754 lines
30 KiB
JavaScript

/**
* Purpose: orderApi supports index.js by holding or executing the order api 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 orderApi = require('../../api/order')
const paymentApi = require('../../api/payment')
/**
* 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: permission supports index.js by holding or executing the permission 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 permission = require('../../utils/permission')
/**
* 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 { setTempRecord } = require('../../utils/storage')
const { consumeDirty, markDirty } = require('../../utils/page-state')
/**
* Purpose: STATUS_OPTIONS supports index.js by holding or executing the status options 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 STATUS_OPTIONS = [
{ label: '全部', value: '' },
{ label: '待服务', value: 1 },
{ label: '已完成', value: 2 },
{ label: '已取消', value: 0 }
]
/**
* Purpose: toId supports index.js by holding or executing the to id page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function toId(value) {
return value === undefined || value === null ? '' : String(value)
}
/**
* Purpose: sameId supports index.js by holding or executing the same id page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function sameId(left, right) {
const leftId = toId(left)
const rightId = toId(right)
return leftId !== '' && leftId === rightId
}
/**
* Purpose: safeList supports index.js by holding or executing the safe list page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function safeList(value) {
return Array.isArray(value) ? value : []
}
/**
* Purpose: trimText supports index.js by holding or executing the trim text page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function trimText(value) {
return String(value || '').trim()
}
/**
* Purpose: formatRating supports index.js by holding or executing the format rating page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function formatRating(value) {
const rating = Number(value)
if (!isFinite(rating) || rating <= 0) {
return ''
}
return rating % 1 === 0 ? String(rating) : rating.toFixed(1)
}
function getPaymentStatusText(status) {
if (status === 'PAID') {
return '已支付'
}
if (status === 'PAYING') {
return '支付中'
}
if (status === 'REFUNDED') {
return '已退款'
}
return '未支付'
}
/**
* Purpose: isSubmittedReview supports index.js by holding or executing the is submitted review page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function isSubmittedReview(review) {
if (!review) {
return false
}
if (Number(review.status) === 1) {
return true
}
return Boolean(review.rating)
}
/**
* Purpose: resolveRelatedRecord supports index.js by holding or executing the resolve related record page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function resolveRelatedRecord(source, orderItemId) {
if (Array.isArray(source)) {
return source.find(item => sameId(item && item.orderItemId, orderItemId)) || null
}
if (source && typeof source === 'object') {
if (!source.orderItemId || sameId(source.orderItemId, orderItemId)) {
return source
}
}
return null
}
/**
* Purpose: buildReviewSummary supports index.js by holding or executing the build review summary page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function buildReviewSummary(review) {
if (!review || !isSubmittedReview(review)) {
return null
}
const ratingText = formatRating(review.rating)
return {
...review,
ratingText,
contentText: trimText(review.content),
displayText: ratingText ? `已评价:${ratingText} 分` : (review.statusText || '已评价')
}
}
/**
* Purpose: buildArtistChangeSummary supports index.js by holding or executing the build artist change summary page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function buildArtistChangeSummary(request) {
if (!request) {
return null
}
const originalName = request.originalArtistName || '原美甲师'
const requestedName = request.requestedArtistName || '新美甲师'
return {
...request,
displayText: `${originalName} → ${requestedName}`,
reasonText: trimText(request.reason)
}
}
/**
* Purpose: indexByOrderItemId supports index.js by holding or executing the index by order item id page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function indexByOrderItemId(list) {
return safeList(list).reduce((result, item) => {
if (item && item.orderItemId) {
result[toId(item.orderItemId)] = item
}
return result
}, {})
}
/**
* Purpose: safeCall supports index.js by holding or executing the safe call page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
async function safeCall(factory, fallbackValue) {
try {
const result = await factory()
return result === undefined || result === null ? fallbackValue : result
} catch (error) {
return fallbackValue
}
}
/**
* Purpose: loadAssistData supports index.js by holding or executing the load assist data page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
async function loadAssistData(orders) {
const pendingChangesPromise = safeCall(() => orderApi.listMyPendingArtistChangeRequests(), [])
const orderReviewListsPromise = Promise.all(orders.map(order => (
safeCall(() => orderApi.listOrderReviews(order.id), [])
)))
const [pendingChanges, orderReviewLists] = await Promise.all([
pendingChangesPromise,
orderReviewListsPromise
])
const submittedReviews = []
safeList(orderReviewLists).forEach(list => {
safeList(list).forEach(review => {
if (isSubmittedReview(review)) {
submittedReviews.push(review)
}
})
})
return {
pendingChangeByItemId: indexByOrderItemId(pendingChanges),
submittedReviewByItemId: indexByOrderItemId(submittedReviews)
}
}
/**
* Purpose: hasProductionFlag supports index.js by holding or executing the has production flag page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function hasProductionFlag(userInfo) {
const sources = [
userInfo && userInfo.participateProduction,
userInfo && userInfo.participateProductionFlag,
userInfo && userInfo.artistParticipateProduction,
userInfo && userInfo.nailArtistInfo && userInfo.nailArtistInfo.participateProduction,
userInfo && userInfo.artistInfo && userInfo.artistInfo.participateProduction
]
return sources.some(value => value === true || Number(value) === 1)
}
/**
* Purpose: canRequestArtistChangeForItem supports index.js by holding or executing the can request artist change for item page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function canRequestArtistChangeForItem(userInfo, order, item, pendingChange) {
if (!userInfo || !order || !item || pendingChange || Number(order.status) !== 1) {
return false
}
if (permission.isNailArtist(userInfo)) {
return !item.artistId || sameId(item.artistId, userInfo.id)
}
if (sameId(item.artistId, userInfo.id)) {
return true
}
return permission.isShopManager(userInfo) && hasProductionFlag(userInfo)
}
/**
* Purpose: formatOrderItem supports index.js by holding or executing the format order item page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function formatOrderItem(order, item, userInfo, assistData) {
const itemId = toId(item.id)
const inlineReviewSummary = resolveRelatedRecord(item.reviewSummary || order.reviewSummary, item.id)
const inlinePendingChange = resolveRelatedRecord(item.pendingArtistChangeRequest || order.pendingArtistChangeRequest, item.id)
const reviewSummary = buildReviewSummary(inlineReviewSummary || assistData.submittedReviewByItemId[itemId])
const pendingArtistChangeRequest = buildArtistChangeSummary(inlinePendingChange || assistData.pendingChangeByItemId[itemId])
return {
...item,
displayPrice: format.formatPrice(item.price),
displayAppointmentTime: format.formatDateTime(item.appointmentTime),
reviewSummary,
reviewStatusText: reviewSummary ? reviewSummary.displayText : '待评价',
pendingArtistChangeRequest,
canRequestArtistChange: canRequestArtistChangeForItem(userInfo, order, item, pendingArtistChangeRequest)
}
}
/**
* Purpose: formatOrder supports index.js by holding or executing the format order page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function formatOrder(order, userInfo, assistData) {
const items = safeList(order.items).map(item => formatOrderItem(order, item, userInfo, assistData))
const orderReviewSummary = buildReviewSummary(order.reviewSummary)
const paymentStatus = order.paymentStatus || 'UNPAID'
return {
...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),
canManualConfirmPay: paymentStatus !== 'PAID' && paymentStatus !== 'REFUNDED',
items,
reviewSummary: orderReviewSummary,
reviewStatusText: orderReviewSummary ? orderReviewSummary.displayText : ''
}
}
/**
* Purpose: selectArtist supports index.js by holding or executing the select artist page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function selectArtist(artists) {
return new Promise(resolve => {
wx.showActionSheet({
itemList: artists.map(item => item.artistName || `美甲师 ${item.artistId}`),
success: result => resolve(artists[result.tapIndex] || null),
fail: () => resolve(null)
})
})
}
/**
* Purpose: promptChangeReason supports index.js by holding or executing the prompt change reason page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function promptChangeReason() {
return new Promise(resolve => {
wx.showModal({
title: '改派原因',
content: '可填写改派原因,留空也可以提交。',
editable: true,
placeholderText: '例如档期冲突、客户沟通调整',
confirmText: '提交',
success: result => {
if (!result.confirm) {
resolve(null)
return
}
resolve(trimText(result.content))
},
fail: () => resolve(null)
})
})
}
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: keyword supports index.js by holding or executing the keyword 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.
*/
keyword: '',
/**
* Purpose: status supports index.js by holding or executing the status 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.
*/
status: '',
/**
* Purpose: statusOptions supports index.js by holding or executing the status options 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.
*/
statusOptions: STATUS_OPTIONS,
/**
* Purpose: list supports index.js by holding or executing the list 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.
*/
list: [],
/**
* Purpose: total supports index.js by holding or executing the total 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.
*/
total: 0,
/**
* Purpose: loading supports index.js by holding or executing the loading 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.
*/
loading: false,
/**
* Purpose: userInfo supports index.js by holding or executing the user info 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.
*/
userInfo: null,
canManualConfirmPay: false,
/**
* Purpose: initialized supports index.js by holding or executing the initialized 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.
*/
initialized: false
},
/**
* Purpose: onShow supports index.js by holding or executing the on show 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.
*/
onShow() {
if (!navigation.ensureLogin('进入订单管理')) {
return
}
const userInfo = getApp().globalData.userInfo
const permissionCodes = getApp().globalData.permissionCodes || []
this.setData({
userInfo,
canManualConfirmPay: permission.hasPermission(permissionCodes, 'payment:manage')
})
if (!this.data.initialized || consumeDirty('admin-orders')) {
this.loadOrders()
}
},
async handleManualConfirmPay(event) {
if (!this.data.canManualConfirmPay) {
wx.showToast({
title: '当前账号没有确认支付权限',
icon: 'none'
})
return
}
const order = event.currentTarget.dataset.order
if (!order) {
return
}
const confirmed = 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 (confirmed === null) {
return
}
try {
await paymentApi.manualConfirmOrder(order.id, confirmed)
markDirty('admin-workbench')
wx.showToast({
title: '支付已确认',
icon: 'none'
})
this.loadOrders()
} catch (error) {
wx.showToast({
title: error.message || '确认支付失败',
icon: 'none'
})
}
},
/**
* Purpose: onPullDownRefresh supports index.js by holding or executing the on pull down refresh 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.
*/
onPullDownRefresh() {
this.loadOrders().finally(() => {
wx.stopPullDownRefresh()
})
},
/**
* Purpose: handleKeywordInput supports index.js by holding or executing the handle keyword input 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.
*/
handleKeywordInput(event) {
this.setData({
/**
* Purpose: keyword supports index.js by holding or executing the keyword 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.
*/
keyword: event.detail.value
})
},
/**
* Purpose: handleStatusChange supports index.js by holding or executing the handle status change 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.
*/
handleStatusChange(event) {
const status = event.currentTarget.dataset.status
this.setData({
status
})
this.loadOrders()
},
async loadOrders() {
const requestId = (this._loadRequestId || 0) + 1
this._loadRequestId = requestId
this.setData({
/**
* Purpose: loading supports index.js by holding or executing the loading 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.
*/
loading: true
})
try {
const result = await orderApi.listOrders({
/**
* Purpose: page supports index.js by holding or executing the page 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.
*/
page: 1,
/**
* Purpose: size supports index.js by holding or executing the size 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.
*/
size: 20,
/**
* Purpose: keyword supports index.js by holding or executing the keyword 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.
*/
keyword: this.data.keyword || undefined,
/**
* Purpose: status supports index.js by holding or executing the status 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.
*/
status: this.data.status === '' ? undefined : this.data.status
})
const rawList = safeList(result && result.list)
const assistData = await loadAssistData(rawList)
if (requestId !== this._loadRequestId) {
return
}
this.setData({
/**
* Purpose: list supports index.js by holding or executing the list 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.
*/
list: rawList.map(item => formatOrder(item, this.data.userInfo, assistData)),
/**
* Purpose: total supports index.js by holding or executing the total 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.
*/
total: Number(result && result.total) || 0,
/**
* Purpose: initialized supports index.js by holding or executing the initialized 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.
*/
initialized: true
})
} catch (error) {
if (requestId !== this._loadRequestId) {
return
}
wx.showToast({
/**
* Purpose: title supports index.js by holding or executing the title 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.
*/
title: error.message || '订单加载失败',
/**
* Purpose: icon supports index.js by holding or executing the icon 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.
*/
icon: 'none'
})
} finally {
if (requestId === this._loadRequestId) {
this.setData({
/**
* Purpose: loading supports index.js by holding or executing the loading 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.
*/
loading: false
})
}
}
},
/**
* Purpose: handleSearch supports index.js by holding or executing the handle search 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.
*/
handleSearch() {
this.loadOrders()
},
handleOpenWorkbench() {
navigation.switchTab('/pages/workbench/index').catch(() => undefined)
},
handleOpenManage() {
navigation.switchTab('/pages/manage/index').catch(() => undefined)
},
handleOpenProfile() {
navigation.switchTab('/pages/profile/index').catch(() => undefined)
},
/**
* Purpose: handleOpenOrder supports index.js by holding or executing the handle open order 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.
*/
handleOpenOrder(event) {
const order = event.currentTarget.dataset.order
if (!order) {
return
}
setTempRecord('currentOrder', order)
navigation.navigate('/pages/order-detail/index', {
/**
* Purpose: id supports index.js by holding or executing the id 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.
*/
id: order.id
}).catch(() => undefined)
},
async handleRequestArtistChange(event) {
const orderId = event.currentTarget.dataset.orderId
const orderItemId = event.currentTarget.dataset.orderItemId
if (!orderId || !orderItemId) {
return
}
try {
const options = await orderApi.getArtistChangeOptions(orderId, orderItemId)
const artists = safeList(options && options.artists)
if (!artists.length) {
wx.showToast({
/**
* Purpose: title supports index.js by holding or executing the title 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.
*/
title: '暂无可改派美甲师',
/**
* Purpose: icon supports index.js by holding or executing the icon 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.
*/
icon: 'none'
})
return
}
const targetArtist = await selectArtist(artists)
if (!targetArtist) {
return
}
const reason = await promptChangeReason()
if (reason === null) {
return
}
await orderApi.requestArtistChange({
orderId,
orderItemId,
/**
* Purpose: requestedArtistId supports index.js by holding or executing the requested artist id 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.
*/
requestedArtistId: targetArtist.artistId,
reason
})
markDirty('admin-workbench')
wx.showToast({
/**
* Purpose: title supports index.js by holding or executing the title 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.
*/
title: '改派申请已提交',
/**
* Purpose: icon supports index.js by holding or executing the icon 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.
*/
icon: 'none'
})
this.loadOrders()
} catch (error) {
wx.showToast({
/**
* Purpose: title supports index.js by holding or executing the title 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.
*/
title: error.message || '改派申请失败',
/**
* Purpose: icon supports index.js by holding or executing the icon 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.
*/
icon: 'none'
})
}
},
async handleProcessOrder(event) {
const order = event.currentTarget.dataset.order
if (!order) {
return
}
const isArtist = permission.isNailArtist(this.data.userInfo)
const options = isArtist
? [{ label: '标记为已完成', status: 2 }]
: [
{ label: '调整为待服务', status: 1 },
{ label: '标记为已完成', status: 2 },
{ label: '标记为已取消', status: 0 }
]
wx.showActionSheet({
/**
* Purpose: itemList supports index.js by holding or executing the item list 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.
*/
itemList: options.map(item => item.label),
/**
* Purpose: success supports index.js by holding or executing the success 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.
*/
success: async result => {
const target = options[result.tapIndex]
if (!target) {
return
}
try {
await orderApi.updateOrderStatus(order.id, target.status)
markDirty('admin-workbench')
wx.showToast({
/**
* Purpose: title supports index.js by holding or executing the title 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.
*/
title: '订单状态已更新',
/**
* Purpose: icon supports index.js by holding or executing the icon 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.
*/
icon: 'none'
})
this.loadOrders()
} catch (error) {
wx.showToast({
/**
* Purpose: title supports index.js by holding or executing the title 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.
*/
title: error.message || '订单处理失败',
/**
* Purpose: icon supports index.js by holding or executing the icon 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.
*/
icon: 'none'
})
}
}
})
}
})