584 lines
20 KiB
JavaScript
584 lines
20 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')
|
|
/**
|
|
* 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.
|
|
*/
|
|
const auth = require('../../utils/auth')
|
|
/**
|
|
* 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: 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')
|
|
/**
|
|
* 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')
|
|
const { resolveLegacyImage } = require('../../utils/file')
|
|
const { formatDateTime, formatPrice, getOrderStatusClass, getOrderStatusText } = require('../../utils/format')
|
|
const { runWithLoading } = require('../../utils/loading')
|
|
|
|
/**
|
|
* 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.
|
|
*/
|
|
const REVIEW_SCORE_OPTIONS = [0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5]
|
|
|
|
/**
|
|
* 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)
|
|
}
|
|
|
|
/**
|
|
* 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: 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.
|
|
*/
|
|
function buildPendingReview(review) {
|
|
if (!review || isSubmittedReview(review)) {
|
|
return null
|
|
}
|
|
return review
|
|
}
|
|
|
|
/**
|
|
* 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 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)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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.
|
|
*/
|
|
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 {}
|
|
}
|
|
|
|
Page({
|
|
data: Object.assign(theme.pageThemeData(getApp().globalData.shopTheme), {
|
|
loading: false,
|
|
isLoggedIn: false,
|
|
orders: [],
|
|
/**
|
|
* 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: '',
|
|
reviewScoreOptions: REVIEW_SCORE_OPTIONS
|
|
}),
|
|
|
|
/**
|
|
* 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.
|
|
*/
|
|
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()
|
|
const list = safeList(orders)
|
|
const assistData = await loadAssistData(list)
|
|
const formattedOrders = await Promise.all(list.map(async order => ({
|
|
...order,
|
|
focusTarget: sameId(order.id, this.data.focusOrderId),
|
|
statusText: getOrderStatusText(order),
|
|
statusClass: getOrderStatusClass(order),
|
|
shopNameText: order.shopName ? `所属店铺:${order.shopName}` : '',
|
|
actualAmountText: `\u00a5${formatPrice(order.actualAmount)}`,
|
|
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
|
|
}
|
|
}))
|
|
})))
|
|
|
|
this.setData({
|
|
orders: formattedOrders
|
|
})
|
|
} catch (error) {
|
|
this.setData({
|
|
orders: []
|
|
})
|
|
wx.showToast({
|
|
title: error.message || '\u8ba2\u5355\u5217\u8868\u52a0\u8f7d\u5931\u8d25',
|
|
icon: 'none'
|
|
})
|
|
} finally {
|
|
this.setData({
|
|
loading: false
|
|
})
|
|
}
|
|
},
|
|
|
|
/**
|
|
* 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.
|
|
*/
|
|
handleOpenAuth() {
|
|
navigation.openAuth('/pages/orders/index')
|
|
},
|
|
|
|
/**
|
|
* 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/profile/index')
|
|
},
|
|
|
|
/**
|
|
* 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.
|
|
*/
|
|
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
|
|
})
|
|
},
|
|
|
|
/**
|
|
* 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.
|
|
*/
|
|
handleSelectReviewScore(event) {
|
|
const reviewId = event.currentTarget.dataset.reviewId
|
|
const score = Number(event.currentTarget.dataset.score)
|
|
this.updateReviewDraft(reviewId, {
|
|
rating: score
|
|
})
|
|
},
|
|
|
|
/**
|
|
* 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.
|
|
*/
|
|
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: '拒绝改派',
|
|
content: '确认拒绝这次美甲师改派申请吗?拒绝后可在下一步补充原因。',
|
|
confirmText: '拒绝',
|
|
success: modalRes => resolve(Boolean(modalRes.confirm))
|
|
})
|
|
})
|
|
if (!confirmed) {
|
|
return
|
|
}
|
|
|
|
const rejectReason = await this.promptRejectReason()
|
|
|
|
await runWithLoading(() => orderApi.rejectArtistChange(requestId, {
|
|
rejectReason
|
|
}))
|
|
wx.showToast({
|
|
title: '已拒绝改派',
|
|
icon: 'none'
|
|
})
|
|
await this.loadOrders()
|
|
}, {
|
|
message: '改派处理中,请勿重复点击'
|
|
})
|
|
} catch (error) {
|
|
if (actionGuard.isGuardError(error)) {
|
|
return
|
|
}
|
|
wx.showToast({
|
|
title: error.message || '拒绝改派失败',
|
|
icon: 'none'
|
|
})
|
|
}
|
|
},
|
|
|
|
/**
|
|
* 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('')
|
|
})
|
|
})
|
|
},
|
|
|
|
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'
|
|
})
|
|
} catch (error) {
|
|
if (actionGuard.isGuardError(error)) {
|
|
return
|
|
}
|
|
wx.showToast({
|
|
title: error.message || '\u53d6\u6d88\u8ba2\u5355\u5931\u8d25',
|
|
icon: 'none'
|
|
})
|
|
}
|
|
}
|
|
})
|