Files
JayuCloud/YumiNail-Frontend/src/views/ArtistOrderView.vue
T

454 lines
20 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<div class="page">
<header class="header">
<a-button type="text" @click="router.back()">
<LeftOutlined :style="{ fontSize: '20px' }" />
</a-button>
<h1>美甲师订单</h1>
<a-button v-if="showCalendarEntry" type="link" @click="router.push('/artist-calendar')">日历</a-button>
<div v-else class="placeholder" />
</header>
<main class="content">
<section v-if="orders.length" class="filter-panel">
<a-input v-model:value="filters.keyword" allow-clear placeholder="搜索订单号、客户、作品" />
<div class="filter-row">
<a-select v-model:value="filters.source" class="filter-select" placeholder="订单来源">
<a-select-option value="all">全部来源</a-select-option>
<a-select-option value="SERVICE">客户预约我的订单</a-select-option>
<a-select-option value="SELF">我下的订单</a-select-option>
<a-select-option value="BOTH">两者都有</a-select-option>
</a-select>
<a-select v-model:value="filters.status" class="filter-select" placeholder="订单状态">
<a-select-option value="all">全部状态</a-select-option>
<a-select-option :value="1">待使用</a-select-option>
<a-select-option :value="2">已完成</a-select-option>
<a-select-option :value="3">已取消</a-select-option>
</a-select>
</div>
<div class="filter-row">
<a-date-picker v-model:value="filters.date" class="filter-date" popup-class-name="yumi-date-picker-panel" placeholder="预约日期" />
<a-input-number v-model:value="filters.minAmount" class="filter-number" :min="0" placeholder="最低金额" />
<a-input-number v-model:value="filters.maxAmount" class="filter-number" :min="0" placeholder="最高金额" />
</div>
<div class="filter-actions">
<span>共 {{ filteredOrders.length }} / {{ orders.length }} 个订单</span>
<a-button @click="resetFilters">重置</a-button>
</div>
</section>
<a-empty v-if="!orders.length" description="暂无订单">
<a-button type="primary" @click="handleRefreshClick">刷新</a-button>
</a-empty>
<a-empty v-else-if="!filteredOrders.length" description="没有符合条件的订单">
<a-button @click="resetFilters">清空筛选</a-button>
</a-empty>
<a-card
v-for="order in filteredOrders"
:key="order.id"
:ref="(el: any) => setOrderRef(order.id, el)"
class="order-card"
:class="{ 'is-target': isTargetOrder(order.id) }"
>
<div class="order-head">
<div class="order-title">
<span>订单号:{{ order.orderNo }}</span>
<span v-if="order.artistOrderSourceText" class="source-tag" :class="sourceClass(order.artistOrderSource)">
{{ order.artistOrderSourceText }}
</span>
</div>
<span class="status" :class="statusClass(order.status)">{{ order.statusText }}</span>
</div>
<div class="customer-meta">
<span>预约用户:{{ order.customerNickname || '用户' }}</span>
<span v-if="order.customerPhoneMasked">联系电话:{{ order.customerPhoneMasked }}</span>
</div>
<div v-for="item in order.items" :key="item.id" class="item">
<div class="thumb">
<img v-if="item.productImage" :src="item.productImage" :alt="item.productTitle" />
<span v-else>{{ item.productTitle[0] }}</span>
</div>
<div class="main">
<div class="title">{{ item.productTitle }}</div>
<div class="line">¥{{ item.price.toFixed(2) }} x {{ item.quantity }}</div>
<div v-if="item.appointmentTime" class="line appointment">预约时间:{{ item.appointmentTime }}</div>
<div v-if="pendingArtistChangeForItem(order, item)" class="change-status">
改派申请处理中:{{ pendingArtistChangeForItem(order, item)?.requestedArtistName || '新美甲师' }}
</div>
<div v-else-if="canRequestArtistChange(order, item)" class="change-action">
<span>需要更换服务美甲师</span>
<a-button size="small" @click="openArtistChangeModal(order, item)">申请改派</a-button>
</div>
</div>
</div>
<div class="footer">
<span>当前项目合计:¥{{ order.actualAmount.toFixed(2) }}</span>
<a-button v-if="canComplete(order)" size="small" type="primary" @click="completeOrder(order.id)">
完成服务
</a-button>
</div>
</a-card>
</main>
<a-modal
v-model:open="artistChangeModalOpen"
title="申请改派"
:confirm-loading="artistChangeSubmitting"
@ok="submitArtistChangeRequest"
@cancel="closeArtistChangeModal"
>
<a-form layout="vertical">
<a-form-item label="当前美甲师">
<span>{{ artistChangeOptions?.currentArtistName || activeArtistChangeItem?.artistName || '未分配' }}</span>
</a-form-item>
<a-form-item label="新美甲师" required>
<a-select
v-model:value="artistChangeForm.requestedArtistId"
:loading="artistChangeOptionsLoading"
placeholder="请选择新的美甲师"
>
<a-select-option
v-for="artist in selectableArtistOptions"
:key="String(artist.artistId)"
:value="artist.artistId"
>
{{ artist.artistName }}
</a-select-option>
</a-select>
</a-form-item>
<a-form-item label="改派原因" required>
<a-textarea v-model:value="artistChangeForm.reason" :rows="4" :maxlength="300" placeholder="请说明申请改派的原因" />
</a-form-item>
</a-form>
</a-modal>
</div>
</template>
<script setup lang="ts">
/**
* ArtistOrderView page script.
* Call guide: the template binds the reactive state below, and the methods are triggered by button clicks, lifecycle hooks, or router events.
*/
import { computed, nextTick, onMounted, reactive, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { LeftOutlined } from '@ant-design/icons-vue'
import { message, Modal } from 'ant-design-vue'
import { userApi } from '@/api/user'
import { fileApi } from '../api/index'
import { orderApi } from '../api/order'
import type { OrderArtistChangeOptionsVO, OrderArtistChangeRequestVO } from '../api/order'
import type { IdType } from '@/utils/id'
import { runWithGlobalLoading } from '@/utils/action-loading'
const router = useRouter()
const route = useRoute()
const orders = ref<any[]>([])
const imageIdCache = ref<Record<string, string>>({})
const orderRefs = ref<Record<string, any>>({})
const showCalendarEntry = ref(false)
const currentUserId = ref<IdType>()
const artistChangeModalOpen = ref(false)
const artistChangeSubmitting = ref(false)
const artistChangeOptionsLoading = ref(false)
const artistChangeOptions = ref<OrderArtistChangeOptionsVO | null>(null)
const activeArtistChangeOrder = ref<any | null>(null)
const activeArtistChangeItem = ref<any | null>(null)
const filters = reactive({
keyword: '',
source: 'all',
status: 'all' as 'all' | number,
date: null as any,
minAmount: undefined as number | undefined,
maxAmount: undefined as number | undefined
})
const artistChangeForm = reactive({
requestedArtistId: undefined as IdType | undefined,
reason: ''
})
/** filteredOrders is a reactive state variable used by the template and event handlers. */
const filteredOrders = computed(() => {
const keyword = filters.keyword.trim().toLowerCase()
const targetDate = filters.date?.format?.('YYYY-MM-DD')
return orders.value.filter(order => {
if (filters.source !== 'all' && order.artistOrderSource !== filters.source) return false
if (filters.status !== 'all' && order.status !== filters.status) return false
const amount = Number(order.actualAmount || 0)
if (filters.minAmount !== undefined && amount < filters.minAmount) return false
if (filters.maxAmount !== undefined && amount > filters.maxAmount) return false
const items = order.items || []
if (targetDate && !items.some((item: any) => String(item.appointmentTime || '').slice(0, 10) === targetDate)) {
return false
}
if (keyword) {
const haystack = [
order.orderNo,
order.statusText,
order.artistOrderSourceText,
order.customerNickname,
order.customerPhoneMasked,
...items.flatMap((item: any) => [item.productTitle, item.appointmentTime])
].filter(Boolean).join(' ').toLowerCase()
if (!haystack.includes(keyword)) return false
}
return true
})
})
/** targetOrderId is a local helper used by this page script. */
const targetOrderId = () => String(route.query.orderId || '')
/** isTargetOrder is a local helper used by this page script. */
const isTargetOrder = (orderId: IdType) => targetOrderId() === String(orderId)
/** setOrderRef is a local helper used by this page script. */
const setOrderRef = (orderId: IdType, el: any) => {
if (!el) return
orderRefs.value[String(orderId)] = el
}
/** scrollToTargetOrder is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
const scrollToTargetOrder = async () => {
const id = targetOrderId()
if (!id) return
await nextTick()
const target = orderRefs.value[id]?.$el || orderRefs.value[id]
target?.scrollIntoView?.({ behavior: 'smooth', block: 'center' })
}
/** statusClass is a local helper used by this page script. */
const statusClass = (status: number) => {
if (status === 1) return 'pending'
if (status === 2) return 'completed'
return 'cancelled'
}
/** sourceClass is a local helper used by this page script. */
const sourceClass = (source?: string) => {
if (source === 'SELF') return 'source-self'
if (source === 'BOTH') return 'source-both'
return 'source-service'
}
const sameId = (left: unknown, right: unknown) => {
return left !== undefined && left !== null && right !== undefined && right !== null && String(left) === String(right)
}
const pendingArtistChangeForItem = (order: any, item: any): OrderArtistChangeRequestVO | undefined => {
const itemValue = item?.pendingArtistChangeRequest as OrderArtistChangeRequestVO | undefined
if (itemValue) return itemValue
const orderValue = order?.pendingArtistChangeRequest as OrderArtistChangeRequestVO | undefined
if (!orderValue) return undefined
return sameId(orderValue.orderItemId, item.id) || (order.items || []).length === 1 ? orderValue : undefined
}
/** canComplete is a local helper used by this page script. */
const canComplete = (order: any) => {
return order.status === 1 && order.artistOrderSource !== 'SELF'
}
const canRequestArtistChange = (order: any, item: any) => {
const assignedToCurrentUser = !currentUserId.value || !item.artistId || sameId(item.artistId, currentUserId.value)
return order.status === 1 && item.id && assignedToCurrentUser && !pendingArtistChangeForItem(order, item)
}
const selectableArtistOptions = computed(() => {
const options = artistChangeOptions.value?.artists || []
return options.filter(artist => !sameId(artist.artistId, artistChangeOptions.value?.currentArtistId))
})
/** resetFilters is a local helper used by this page script. */
const resetFilters = () => {
filters.keyword = ''
filters.source = 'all'
filters.status = 'all'
filters.date = null
filters.minAmount = undefined
filters.maxAmount = undefined
}
/** loadOrders is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
const loadOrders = async () => {
try {
orders.value = (await orderApi.getArtistOrders() as unknown as any[]) || []
for (const order of orders.value) {
for (const item of order.items || []) {
const match = item.productImage?.replace(/<[^>]*>/g, '').match(/\/api\/file\/get\/(\d+)/)
if (!match?.[1]) continue
const imageId = match[1]
try {
if (!imageIdCache.value[imageId]) imageIdCache.value[imageId] = await fileApi.getSignedUrl(imageId) as any
item.productImage = imageIdCache.value[imageId]
} catch {
item.productImage = ''
}
}
}
await scrollToTargetOrder()
} catch {
message.error('获取订单列表失败')
}
}
/** loadCalendarAccess is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
const loadCalendarAccess = async () => {
showCalendarEntry.value = false
try {
const currentUser = await userApi.getUserInfo()
currentUserId.value = currentUser.id
if (currentUser.type === 3) {
showCalendarEntry.value = true
return
}
if (currentUser.type === 2) {
const artistInfo = await userApi.getNailArtistInfo(currentUser.id).catch(() => null)
showCalendarEntry.value = Number(artistInfo?.participateProduction) === 1
}
} catch {
currentUserId.value = undefined
showCalendarEntry.value = false
}
}
/** handleRefreshClick is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
const handleRefreshClick = async () => {
await runWithGlobalLoading(() => loadOrders())
}
const openArtistChangeModal = async (order: any, item: any) => {
if (!canRequestArtistChange(order, item)) return
activeArtistChangeOrder.value = order
activeArtistChangeItem.value = item
artistChangeOptions.value = null
artistChangeForm.requestedArtistId = undefined
artistChangeForm.reason = ''
artistChangeModalOpen.value = true
artistChangeOptionsLoading.value = true
try {
artistChangeOptions.value = await orderApi.getArtistChangeOptions(order.id, item.id)
if (!selectableArtistOptions.value.length) {
message.warning('暂无可改派的美甲师')
}
} catch {
message.error('获取可改派美甲师失败')
closeArtistChangeModal()
} finally {
artistChangeOptionsLoading.value = false
}
}
const closeArtistChangeModal = () => {
artistChangeModalOpen.value = false
activeArtistChangeOrder.value = null
activeArtistChangeItem.value = null
artistChangeOptions.value = null
artistChangeForm.requestedArtistId = undefined
artistChangeForm.reason = ''
}
const submitArtistChangeRequest = async () => {
if (!activeArtistChangeOrder.value || !activeArtistChangeItem.value) return
if (!artistChangeForm.requestedArtistId) {
message.warning('请选择新的美甲师')
return
}
const reason = artistChangeForm.reason.trim()
if (!reason) {
message.warning('请填写改派原因')
return
}
if (sameId(artistChangeForm.requestedArtistId, artistChangeOptions.value?.currentArtistId)) {
message.warning('新美甲师不能与当前美甲师相同')
return
}
artistChangeSubmitting.value = true
try {
await orderApi.requestArtistChange({
orderId: activeArtistChangeOrder.value.id,
orderItemId: activeArtistChangeItem.value.id,
requestedArtistId: artistChangeForm.requestedArtistId,
reason
})
message.success('改派申请已提交')
closeArtistChangeModal()
await loadOrders()
} catch {
message.error('提交改派申请失败')
} finally {
artistChangeSubmitting.value = false
}
}
/** completeOrder is a local helper used by this page script. */
const completeOrder = (orderId: IdType) => {
Modal.confirm({
title: '确认完成服务',
content: '确定要完成这个订单吗?',
onOk: async () => {
try {
await runWithGlobalLoading(async () => {
await orderApi.completeArtistOrder(orderId)
await loadOrders()
})
message.success('订单已完成')
} catch (error: any) {
message.error('完成订单失败')
}
}
})
}
onMounted(async () => {
await Promise.all([loadCalendarAccess(), loadOrders()])
})
</script>
<style scoped>
.page { min-height: 100vh; padding-top: 60px; background: linear-gradient(rgba(255, 255, 255, 0.68), rgba(255, 255, 255, 0.92)), var(--theme-background-image), var(--theme-page-bg); background-size: cover, cover, auto; background-position: center top; color: var(--text-primary); }
.header { position: fixed; top: 0; left: 0; right: 0; z-index: 10; display: flex; align-items: center; justify-content: space-between; height: 60px; padding: 10px 15px; background: rgba(255, 255, 255, 0.92); border-bottom: 1px solid var(--theme-border); backdrop-filter: blur(14px); }
.header h1 { margin: 0; font-size: 18px; color: var(--text-primary); }
.placeholder { width: 40px; height: 40px; }
.content { padding: 15px; }
.filter-panel { display: grid; gap: 10px; margin-bottom: 12px; padding: 12px; border-radius: 14px; background: var(--theme-card-bg); border: 1px solid var(--theme-border); box-shadow: 0 14px 34px rgba(122, 67, 88, 0.08); }
.filter-row { display: flex; gap: 8px; align-items: center; }
.filter-select, .filter-date, .filter-number { flex: 1; min-width: 0; }
.filter-actions { display: flex; align-items: center; justify-content: space-between; color: var(--text-secondary); font-size: 12px; }
.order-card { margin-bottom: 12px; border-radius: 14px; border: 1px solid var(--theme-border); background: var(--theme-card-bg); box-shadow: 0 14px 34px rgba(122, 67, 88, 0.08); }
.order-card.is-target { border-color: var(--theme-primary); box-shadow: 0 0 0 2px rgba(198, 89, 125, .16); }
.order-head, .footer { display: flex; align-items: center; justify-content: space-between; }
.order-head { margin-bottom: 12px; padding-bottom: 10px; border-bottom: 1px solid var(--theme-border); }
.order-title { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.source-tag { padding: 3px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.source-service { background: var(--theme-primary-soft); color: var(--theme-primary); }
.source-self { background: var(--theme-primary-soft); color: var(--theme-accent); }
.source-both { background: var(--theme-primary-soft); color: var(--theme-primary); }
.customer-meta { display: flex; flex-wrap: wrap; gap: 8px 16px; margin-bottom: 12px; color: var(--text-secondary); font-size: 13px; }
.status { padding: 4px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.pending { background: var(--theme-primary-soft); color: var(--theme-primary); }
.completed { background: var(--theme-primary-soft); color: var(--theme-primary); }
.cancelled { background: var(--theme-primary-soft); color: var(--theme-accent); }
.item { display: flex; gap: 12px; margin-bottom: 12px; }
.thumb { width: 60px; height: 60px; border-radius: 10px; overflow: hidden; display: flex; align-items: center; justify-content: center; background: var(--theme-primary-soft); color: var(--theme-primary); }
.thumb img { width: 100%; height: 100%; object-fit: cover; }
.main { flex: 1; }
.title { font-weight: 600; color: var(--text-primary); }
.line { margin-top: 4px; color: var(--text-secondary); font-size: 13px; }
.appointment { color: var(--theme-primary); }
.change-status { margin-top: 8px; padding: 8px 10px; border-radius: 8px; background: var(--theme-primary-soft); color: var(--theme-primary); font-size: 13px; }
.change-action { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 8px; padding: 8px 10px; border-radius: 8px; background: var(--theme-primary-soft); color: var(--theme-primary); font-size: 13px; }
:deep(.ant-btn-primary) { background: var(--theme-primary); border-color: var(--theme-primary); }
:deep(.ant-btn-link) { color: var(--theme-primary); }
@media (min-width: 992px) {
.content {
max-width: 980px;
margin: 0 auto;
padding: 24px;
}
}
</style>