417 lines
11 KiB
JavaScript
417 lines
11 KiB
JavaScript
const orderApi = require('../../api/order')
|
|
const navigation = require('../../utils/navigation')
|
|
const permission = require('../../utils/permission')
|
|
const format = require('../../utils/format')
|
|
const { setTempRecord } = require('../../utils/storage')
|
|
const { consumeDirty, markDirty } = require('../../utils/page-state')
|
|
|
|
const STATUS_OPTIONS = [
|
|
{ label: '全部', value: '' },
|
|
{ label: '待服务', value: 1 },
|
|
{ label: '已完成', value: 2 },
|
|
{ label: '已取消', value: 0 }
|
|
]
|
|
|
|
function toId(value) {
|
|
return value === undefined || value === null ? '' : String(value)
|
|
}
|
|
|
|
function sameId(left, right) {
|
|
const leftId = toId(left)
|
|
const rightId = toId(right)
|
|
return leftId !== '' && leftId === rightId
|
|
}
|
|
|
|
function safeList(value) {
|
|
return Array.isArray(value) ? value : []
|
|
}
|
|
|
|
function trimText(value) {
|
|
return String(value || '').trim()
|
|
}
|
|
|
|
function formatRating(value) {
|
|
const rating = Number(value)
|
|
if (!isFinite(rating) || rating <= 0) {
|
|
return ''
|
|
}
|
|
return rating % 1 === 0 ? String(rating) : rating.toFixed(1)
|
|
}
|
|
|
|
function isSubmittedReview(review) {
|
|
if (!review) {
|
|
return false
|
|
}
|
|
if (Number(review.status) === 1) {
|
|
return true
|
|
}
|
|
return Boolean(review.rating)
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
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 || '已评价')
|
|
}
|
|
}
|
|
|
|
function buildArtistChangeSummary(request) {
|
|
if (!request) {
|
|
return null
|
|
}
|
|
const originalName = request.originalArtistName || '原美甲师'
|
|
const requestedName = request.requestedArtistName || '新美甲师'
|
|
return {
|
|
...request,
|
|
displayText: `${originalName} → ${requestedName}`,
|
|
reasonText: trimText(request.reason)
|
|
}
|
|
}
|
|
|
|
function indexByOrderItemId(list) {
|
|
return safeList(list).reduce((result, item) => {
|
|
if (item && item.orderItemId) {
|
|
result[toId(item.orderItemId)] = item
|
|
}
|
|
return result
|
|
}, {})
|
|
}
|
|
|
|
async function safeCall(factory, fallbackValue) {
|
|
try {
|
|
const result = await factory()
|
|
return result === undefined || result === null ? fallbackValue : result
|
|
} catch (error) {
|
|
return fallbackValue
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
function formatOrder(order, userInfo, assistData) {
|
|
const items = safeList(order.items).map(item => formatOrderItem(order, item, userInfo, assistData))
|
|
const orderReviewSummary = buildReviewSummary(order.reviewSummary)
|
|
return {
|
|
...order,
|
|
displayStatusText: format.getOrderStatusText(order),
|
|
displayCreateTime: format.formatDateTime(order.createTime),
|
|
displayPayAmount: format.formatPrice(order.payAmount || order.actualAmount),
|
|
items,
|
|
reviewSummary: orderReviewSummary,
|
|
reviewStatusText: orderReviewSummary ? orderReviewSummary.displayText : ''
|
|
}
|
|
}
|
|
|
|
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)
|
|
})
|
|
})
|
|
}
|
|
|
|
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({
|
|
data: {
|
|
keyword: '',
|
|
status: '',
|
|
statusOptions: STATUS_OPTIONS,
|
|
list: [],
|
|
total: 0,
|
|
loading: false,
|
|
userInfo: null,
|
|
initialized: false
|
|
},
|
|
|
|
onShow() {
|
|
if (!navigation.ensureLogin('进入订单管理')) {
|
|
return
|
|
}
|
|
|
|
const userInfo = getApp().globalData.userInfo
|
|
this.setData({
|
|
userInfo
|
|
})
|
|
|
|
if (!this.data.initialized || consumeDirty('admin-orders')) {
|
|
this.loadOrders()
|
|
}
|
|
},
|
|
|
|
onPullDownRefresh() {
|
|
this.loadOrders().finally(() => {
|
|
wx.stopPullDownRefresh()
|
|
})
|
|
},
|
|
|
|
handleKeywordInput(event) {
|
|
this.setData({
|
|
keyword: event.detail.value
|
|
})
|
|
},
|
|
|
|
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({
|
|
loading: true
|
|
})
|
|
|
|
try {
|
|
const result = await orderApi.listOrders({
|
|
page: 1,
|
|
size: 20,
|
|
keyword: this.data.keyword || undefined,
|
|
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({
|
|
list: rawList.map(item => formatOrder(item, this.data.userInfo, assistData)),
|
|
total: Number(result && result.total) || 0,
|
|
initialized: true
|
|
})
|
|
} catch (error) {
|
|
if (requestId !== this._loadRequestId) {
|
|
return
|
|
}
|
|
wx.showToast({
|
|
title: error.message || '订单加载失败',
|
|
icon: 'none'
|
|
})
|
|
} finally {
|
|
if (requestId === this._loadRequestId) {
|
|
this.setData({
|
|
loading: false
|
|
})
|
|
}
|
|
}
|
|
},
|
|
|
|
handleSearch() {
|
|
this.loadOrders()
|
|
},
|
|
|
|
handleOpenOrder(event) {
|
|
const order = event.currentTarget.dataset.order
|
|
if (!order) {
|
|
return
|
|
}
|
|
|
|
setTempRecord('currentOrder', order)
|
|
navigation.navigate('/pages/order-detail/index', {
|
|
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({
|
|
title: '暂无可改派美甲师',
|
|
icon: 'none'
|
|
})
|
|
return
|
|
}
|
|
|
|
const targetArtist = await selectArtist(artists)
|
|
if (!targetArtist) {
|
|
return
|
|
}
|
|
|
|
const reason = await promptChangeReason()
|
|
if (reason === null) {
|
|
return
|
|
}
|
|
|
|
await orderApi.requestArtistChange({
|
|
orderId,
|
|
orderItemId,
|
|
requestedArtistId: targetArtist.artistId,
|
|
reason
|
|
})
|
|
markDirty('admin-workbench')
|
|
wx.showToast({
|
|
title: '改派申请已提交',
|
|
icon: 'none'
|
|
})
|
|
this.loadOrders()
|
|
} catch (error) {
|
|
wx.showToast({
|
|
title: error.message || '改派申请失败',
|
|
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({
|
|
itemList: options.map(item => item.label),
|
|
success: async result => {
|
|
const target = options[result.tapIndex]
|
|
if (!target) {
|
|
return
|
|
}
|
|
|
|
try {
|
|
await orderApi.updateOrderStatus(order.id, target.status)
|
|
markDirty('admin-workbench')
|
|
wx.showToast({
|
|
title: '订单状态已更新',
|
|
icon: 'none'
|
|
})
|
|
this.loadOrders()
|
|
} catch (error) {
|
|
wx.showToast({
|
|
title: error.message || '订单处理失败',
|
|
icon: 'none'
|
|
})
|
|
}
|
|
}
|
|
})
|
|
}
|
|
})
|