93 lines
2.6 KiB
JavaScript
93 lines
2.6 KiB
JavaScript
const orderApi = require('../../api/order')
|
|
const auth = require('../../utils/auth')
|
|
const navigation = require('../../utils/navigation')
|
|
const theme = require('../../utils/theme')
|
|
const { resolveLegacyImage } = require('../../utils/file')
|
|
const { formatDateTime, formatPrice, getOrderStatusClass, getOrderStatusText } = require('../../utils/format')
|
|
const { runWithLoading } = require('../../utils/loading')
|
|
|
|
function safeList(value) {
|
|
return Array.isArray(value) ? value : []
|
|
}
|
|
|
|
async function formatOrder(order) {
|
|
return {
|
|
...order,
|
|
statusText: getOrderStatusText(order),
|
|
statusClass: getOrderStatusClass(order),
|
|
amountText: `¥${formatPrice(order && order.actualAmount)}`,
|
|
items: await Promise.all(safeList(order && order.items).map(async item => ({
|
|
...item,
|
|
productImageUrl: await resolveLegacyImage(item.productImage),
|
|
priceText: `¥${formatPrice(item.price)}`,
|
|
appointmentText: formatDateTime(item.appointmentTime)
|
|
})))
|
|
}
|
|
}
|
|
|
|
Page({
|
|
data: Object.assign(theme.pageThemeData(getApp().globalData.shopTheme), {
|
|
loading: false,
|
|
isLoggedIn: false,
|
|
orders: [],
|
|
focusOrderId: ''
|
|
}),
|
|
|
|
onLoad(options) {
|
|
this.setData({
|
|
focusOrderId: (options && options.orderId) || ''
|
|
})
|
|
},
|
|
|
|
onShow() {
|
|
this.loadOrders()
|
|
},
|
|
|
|
handleBack() {
|
|
navigation.goBack('/pages/profile/index')
|
|
},
|
|
|
|
async loadOrders() {
|
|
const isLoggedIn = auth.isLoggedIn()
|
|
this.setData({ isLoggedIn })
|
|
if (!isLoggedIn) {
|
|
navigation.openAuth(navigation.getCurrentPageUrl())
|
|
return
|
|
}
|
|
this.setData({ loading: true })
|
|
try {
|
|
const list = safeList(await orderApi.getArtistOrders())
|
|
const orders = await Promise.all(list.map(formatOrder))
|
|
this.setData({ orders })
|
|
} catch (error) {
|
|
wx.showToast({ title: error.message || '美甲师订单加载失败', icon: 'none' })
|
|
} finally {
|
|
this.setData({ loading: false })
|
|
}
|
|
},
|
|
|
|
openCalendar() {
|
|
navigation.navigate('/pages/artist-calendar/index')
|
|
},
|
|
|
|
async completeOrder(event) {
|
|
const orderId = event.currentTarget.dataset.orderId
|
|
if (!orderId) return
|
|
const confirmed = await new Promise(resolve => {
|
|
wx.showModal({
|
|
title: '完成服务',
|
|
content: '确认这笔订单已经完成服务吗?',
|
|
success: res => resolve(Boolean(res.confirm))
|
|
})
|
|
})
|
|
if (!confirmed) return
|
|
try {
|
|
await runWithLoading(() => orderApi.completeArtistOrder(orderId))
|
|
wx.showToast({ title: '服务已完成', icon: 'none' })
|
|
await this.loadOrders()
|
|
} catch (error) {
|
|
wx.showToast({ title: error.message || '完成订单失败', icon: 'none' })
|
|
}
|
|
}
|
|
})
|