Files
JayuCloud/WeChatProgram/pages/orders/index.js
T

584 lines
20 KiB
JavaScript
Raw Normal View History

2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-04-01 15:18:35 +08:00
const orderApi = require('../../api/order')
2026-05-23 18:45:41 +08:00
/**
* Purpose: auth supports index.js by holding or executing the auth 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.
*/
2026-04-01 15:18:35 +08:00
const auth = require('../../utils/auth')
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-04-01 15:18:35 +08:00
const navigation = require('../../utils/navigation')
2026-05-23 18:45:41 +08:00
/**
* Purpose: actionGuard supports index.js by holding or executing the action guard 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 actionGuard = require('../../utils/action-guard')
2026-05-23 18:45:41 +08:00
/**
* Purpose: theme supports index.js by holding or executing the theme 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 theme = require('../../utils/theme')
2026-04-01 15:18:35 +08:00
const { resolveLegacyImage } = require('../../utils/file')
const { formatDateTime, formatPrice, getOrderStatusClass, getOrderStatusText } = require('../../utils/format')
const { runWithLoading } = require('../../utils/loading')
2026-05-23 18:45:41 +08:00
/**
* Purpose: REVIEW_SCORE_OPTIONS supports index.js by holding or executing the review score 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.
*/
2026-05-23 15:16:40 +08:00
const REVIEW_SCORE_OPTIONS = [0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5]
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-05-23 15:16:40 +08:00
function toId(value) {
return value === undefined || value === null ? '' : String(value)
}
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-05-23 15:16:40 +08:00
function sameId(left, right) {
const leftId = toId(left)
const rightId = toId(right)
return leftId !== '' && leftId === rightId
}
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-05-23 15:16:40 +08:00
function safeList(value) {
return Array.isArray(value) ? value : []
}
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-05-23 15:16:40 +08:00
function trimText(value) {
return String(value || '').trim()
}
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-05-23 15:16:40 +08:00
function formatRating(value) {
const rating = Number(value)
if (!isFinite(rating) || rating <= 0) {
return ''
}
return rating % 1 === 0 ? String(rating) : rating.toFixed(1)
}
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-05-23 15:16:40 +08:00
function isSubmittedReview(review) {
if (!review) {
return false
}
if (Number(review.status) === 1) {
return true
}
return Boolean(review.rating)
}
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-05-23 15:16:40 +08:00
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
}
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-05-23 15:16:40 +08:00
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 || '已评价')
}
}
2026-05-23 18:45:41 +08:00
/**
* Purpose: buildPendingReview supports index.js by holding or executing the build pending 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.
*/
2026-05-23 15:16:40 +08:00
function buildPendingReview(review) {
if (!review || isSubmittedReview(review)) {
return null
}
return review
}
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-05-23 15:16:40 +08:00
function buildArtistChangeSummary(request) {
if (!request) {
return null
}
const originalName = request.originalArtistName || '原美甲师'
const requestedName = request.requestedArtistName || '新美甲师'
return {
...request,
displayText: `改派申请:${originalName} → ${requestedName}`,
reasonText: trimText(request.reason)
}
}
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-05-23 15:16:40 +08:00
function indexByOrderItemId(list) {
return safeList(list).reduce((result, item) => {
if (item && item.orderItemId) {
result[toId(item.orderItemId)] = item
}
return result
}, {})
}
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-05-23 15:16:40 +08:00
async function safeCall(factory, fallbackValue) {
try {
const result = await factory()
return result === undefined || result === null ? fallbackValue : result
} catch (error) {
return fallbackValue
}
}
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-05-23 15:16:40 +08:00
async function loadAssistData(orders) {
const pendingReviewsPromise = safeCall(() => orderApi.listMyPendingReviews(), [])
const pendingChangesPromise = safeCall(() => orderApi.listMyPendingArtistChangeRequests(), [])
const orderReviewListsPromise = Promise.all(orders.map(order => (
safeCall(() => orderApi.listOrderReviews(order.id), [])
)))
const [pendingReviews, pendingChanges, orderReviewLists] = await Promise.all([
pendingReviewsPromise,
pendingChangesPromise,
orderReviewListsPromise
])
const submittedReviews = []
safeList(orderReviewLists).forEach(list => {
safeList(list).forEach(review => {
if (isSubmittedReview(review)) {
submittedReviews.push(review)
}
})
})
return {
pendingReviewByItemId: indexByOrderItemId(pendingReviews),
submittedReviewByItemId: indexByOrderItemId(submittedReviews),
pendingChangeByItemId: indexByOrderItemId(pendingChanges)
}
}
2026-05-23 18:45:41 +08:00
/**
* Purpose: findReviewDraft supports index.js by holding or executing the find review draft 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.
*/
2026-05-23 15:16:40 +08:00
function findReviewDraft(orders, reviewId) {
const reviewIdText = toId(reviewId)
for (let i = 0; i < orders.length; i += 1) {
const items = safeList(orders[i].items)
for (let j = 0; j < items.length; j += 1) {
const pendingReview = items[j].pendingReview
if (pendingReview && toId(pendingReview.id) === reviewIdText) {
return items[j].reviewDraft || {}
}
}
}
return {}
}
2026-04-01 15:18:35 +08:00
Page({
data: Object.assign(theme.pageThemeData(getApp().globalData.shopTheme), {
2026-04-01 15:18:35 +08:00
loading: false,
isLoggedIn: false,
2026-05-23 15:16:40 +08:00
orders: [],
2026-05-23 18:45:41 +08:00
/**
* Purpose: focusOrderId stores an optional order id from message navigation so the target order can be surfaced.
* Usage: set it from onLoad route options and compare it while formatting order cards.
*/
focusOrderId: '',
2026-05-23 15:16:40 +08:00
reviewScoreOptions: REVIEW_SCORE_OPTIONS
}),
2026-04-01 15:18:35 +08:00
2026-05-23 18:45:41 +08:00
/**
* Purpose: onLoad captures message or deep-link order ids before the order list is loaded.
* Usage: mini-program runtime passes route options; store orderId for highlighting after loadOrders.
*/
onLoad(options) {
this.setData({
focusOrderId: (options && options.orderId) || ''
})
},
/**
* 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.
*/
2026-04-01 15:18:35 +08:00
onShow() {
this.loadOrders()
},
async loadOrders() {
const isLoggedIn = auth.isLoggedIn()
this.setData({
isLoggedIn,
loading: isLoggedIn
})
if (!isLoggedIn) {
this.setData({
orders: [],
loading: false
})
return
}
try {
const orders = await orderApi.getOrderList()
2026-05-23 15:16:40 +08:00
const list = safeList(orders)
const assistData = await loadAssistData(list)
2026-04-01 15:18:35 +08:00
const formattedOrders = await Promise.all(list.map(async order => ({
...order,
2026-05-23 18:45:41 +08:00
focusTarget: sameId(order.id, this.data.focusOrderId),
2026-04-01 15:18:35 +08:00
statusText: getOrderStatusText(order),
statusClass: getOrderStatusClass(order),
shopNameText: order.shopName ? `所属店铺:${order.shopName}` : '',
actualAmountText: `\u00a5${formatPrice(order.actualAmount)}`,
2026-05-23 15:16:40 +08:00
items: await Promise.all(safeList(order.items).map(async item => {
const itemId = toId(item.id)
const inlinePendingReview = resolveRelatedRecord(item.pendingReview || order.pendingReview, item.id)
const inlineReviewSummary = resolveRelatedRecord(item.reviewSummary || order.reviewSummary, item.id)
const inlinePendingChange = resolveRelatedRecord(item.pendingArtistChangeRequest || order.pendingArtistChangeRequest, item.id)
const pendingReview = buildPendingReview(inlinePendingReview || assistData.pendingReviewByItemId[itemId])
const reviewSummary = buildReviewSummary(inlineReviewSummary || assistData.submittedReviewByItemId[itemId])
const pendingArtistChangeRequest = buildArtistChangeSummary(inlinePendingChange || assistData.pendingChangeByItemId[itemId])
return {
...item,
productImageUrl: await resolveLegacyImage(item.productImage),
priceText: `\u00a5${formatPrice(item.price)}`,
appointmentText: formatDateTime(item.appointmentTime),
pendingReview,
reviewSummary,
reviewDraft: pendingReview ? { rating: 0, content: '' } : null,
pendingArtistChangeRequest
}
}))
2026-04-01 15:18:35 +08:00
})))
this.setData({
orders: formattedOrders
})
} catch (error) {
this.setData({
orders: []
})
wx.showToast({
title: error.message || '\u8ba2\u5355\u5217\u8868\u52a0\u8f7d\u5931\u8d25',
2026-04-01 15:18:35 +08:00
icon: 'none'
})
} finally {
this.setData({
loading: false
})
}
},
2026-05-23 18:45:41 +08:00
/**
* Purpose: handleOpenAuth supports index.js by holding or executing the handle open auth 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.
*/
2026-04-01 15:18:35 +08:00
handleOpenAuth() {
navigation.openAuth('/pages/orders/index')
},
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-04-01 15:18:35 +08:00
handleBack() {
navigation.goBack('/pages/profile/index')
},
2026-05-23 18:45:41 +08:00
/**
* Purpose: updateReviewDraft supports index.js by holding or executing the update review draft 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.
*/
2026-05-23 15:16:40 +08:00
updateReviewDraft(reviewId, patch) {
const reviewIdText = toId(reviewId)
const orders = this.data.orders.map(order => ({
...order,
items: safeList(order.items).map(item => {
const pendingReview = item.pendingReview
if (!pendingReview || toId(pendingReview.id) !== reviewIdText) {
return item
}
return {
...item,
reviewDraft: {
...(item.reviewDraft || {}),
...patch
}
}
})
}))
this.setData({
orders
})
},
2026-05-23 18:45:41 +08:00
/**
* Purpose: handleSelectReviewScore supports index.js by holding or executing the handle select review score 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.
*/
2026-05-23 15:16:40 +08:00
handleSelectReviewScore(event) {
const reviewId = event.currentTarget.dataset.reviewId
const score = Number(event.currentTarget.dataset.score)
this.updateReviewDraft(reviewId, {
rating: score
})
},
2026-05-23 18:45:41 +08:00
/**
* Purpose: handleReviewContentInput supports index.js by holding or executing the handle review content 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.
*/
2026-05-23 15:16:40 +08:00
handleReviewContentInput(event) {
const reviewId = event.currentTarget.dataset.reviewId
this.updateReviewDraft(reviewId, {
content: event.detail.value
})
},
async handleSubmitReview(event) {
const reviewId = event.currentTarget.dataset.reviewId
if (!reviewId) {
return
}
try {
await actionGuard.run(this, `order:review:${reviewId}`, async () => {
const draft = findReviewDraft(this.data.orders, reviewId)
const rating = Number(draft.rating)
if (!isFinite(rating) || rating < 0.5 || rating > 5 || rating * 2 % 1 !== 0) {
wx.showToast({
title: '请选择 0.5-5 分评分',
icon: 'none'
})
return
}
await runWithLoading(() => orderApi.submitArtistReview(reviewId, {
rating,
content: trimText(draft.content),
imageFileIds: []
}))
wx.showToast({
title: '评价已提交',
icon: 'none'
})
await this.loadOrders()
}, {
message: '评价正在提交中,请勿重复点击'
})
} catch (error) {
if (actionGuard.isGuardError(error)) {
return
}
wx.showToast({
title: error.message || '评价提交失败',
icon: 'none'
})
}
},
async handleApproveArtistChange(event) {
const requestId = event.currentTarget.dataset.requestId
if (!requestId) {
return
}
try {
await actionGuard.run(this, `order:artist-change:approve:${requestId}`, async () => {
const confirmed = await new Promise(resolve => {
wx.showModal({
title: '同意改派',
content: '确认同意这次美甲师改派申请吗?',
success: modalRes => resolve(Boolean(modalRes.confirm))
})
})
if (!confirmed) {
return
}
await runWithLoading(() => orderApi.approveArtistChange(requestId))
wx.showToast({
title: '已同意改派',
icon: 'none'
})
await this.loadOrders()
}, {
message: '改派处理中,请勿重复点击'
})
} catch (error) {
if (actionGuard.isGuardError(error)) {
return
}
wx.showToast({
title: error.message || '同意改派失败',
icon: 'none'
})
}
},
async handleRejectArtistChange(event) {
const requestId = event.currentTarget.dataset.requestId
if (!requestId) {
return
}
try {
await actionGuard.run(this, `order:artist-change:reject:${requestId}`, async () => {
const confirmed = await new Promise(resolve => {
wx.showModal({
title: '拒绝改派',
2026-05-23 18:45:41 +08:00
content: '确认拒绝这次美甲师改派申请吗?拒绝后可在下一步补充原因。',
2026-05-23 15:16:40 +08:00
confirmText: '拒绝',
success: modalRes => resolve(Boolean(modalRes.confirm))
})
})
if (!confirmed) {
return
}
2026-05-23 18:45:41 +08:00
const rejectReason = await this.promptRejectReason()
2026-05-23 15:16:40 +08:00
await runWithLoading(() => orderApi.rejectArtistChange(requestId, {
2026-05-23 18:45:41 +08:00
rejectReason
2026-05-23 15:16:40 +08:00
}))
wx.showToast({
title: '已拒绝改派',
icon: 'none'
})
await this.loadOrders()
}, {
message: '改派处理中,请勿重复点击'
})
} catch (error) {
if (actionGuard.isGuardError(error)) {
return
}
wx.showToast({
title: error.message || '拒绝改派失败',
icon: 'none'
})
}
},
2026-05-23 18:45:41 +08:00
/**
* Purpose: promptRejectReason collects an optional customer reason before rejecting an artist-change request.
* Usage: call it after the confirmation dialog and pass the returned text to rejectArtistChange.
*/
promptRejectReason() {
return new Promise(resolve => {
wx.showModal({
title: '拒绝原因',
editable: true,
placeholderText: '可选,告诉美甲师原因',
confirmText: '提交',
success: res => {
if (!res.confirm) {
resolve('')
return
}
resolve(trimText(res.content))
},
fail: () => resolve('')
})
})
},
2026-04-01 15:18:35 +08:00
async handleCancelOrder(event) {
const orderId = event.currentTarget.dataset.orderId
if (!orderId) {
return
}
try {
await actionGuard.run(this, `order:cancel:${orderId}`, async () => {
const confirmed = await new Promise(resolve => {
wx.showModal({
title: '\u53d6\u6d88\u8ba2\u5355',
content: '\u786e\u8ba4\u53d6\u6d88\u8fd9\u7b14\u8ba2\u5355\u5417\uff1f',
success: modalRes => resolve(Boolean(modalRes.confirm))
})
})
if (!confirmed) {
return
}
await runWithLoading(() => orderApi.cancelOrder(orderId))
wx.showToast({
title: '\u8ba2\u5355\u5df2\u53d6\u6d88',
icon: 'none'
})
await this.loadOrders()
}, {
message: '\u64cd\u4f5c\u8fdb\u884c\u4e2d\uff0c\u8bf7\u52ff\u91cd\u590d\u70b9\u51fb'
2026-04-01 15:18:35 +08:00
})
} catch (error) {
if (actionGuard.isGuardError(error)) {
return
}
2026-04-01 15:18:35 +08:00
wx.showToast({
title: error.message || '\u53d6\u6d88\u8ba2\u5355\u5931\u8d25',
2026-04-01 15:18:35 +08:00
icon: 'none'
})
}
}
})