front/src/views/operation/inboundScan/index.vue
zzh 5010213539
All checks were successful
Build and Deploy Front / deploy (push) Successful in 4m3s
feat: 单号截取扫描加速、登录验证码与 SPA 刷新回退
新增单号规则截取模式与本地扫描引擎;登录图形验证码;吉祥物空态;开发态从 Router 自动回退 PC/H5 刷新直链。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-14 19:33:27 +08:00

491 lines
16 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="ops-page">
<div class="ops-col ops-col--main">
<section class="ops-card ops-card--scan">
<div class="ops-card__body ops-scan">
<div class="ops-scan__row">
<a-input
ref="scanInputRef"
v-model="formData.waybillNumber"
:placeholder="$t('operation.inbound.form.placeholder.scanOrInput')"
class="ops-scan-input"
allow-clear
@press-enter="onScanEnter"
@clear="clearLookup"
/>
</div>
<div class="ops-scan__row ops-scan__row--tools">
<div class="ops-scan__opts">
<a-checkbox v-model="keepScanFocus" class="ops-opt">
{{ $t('operation.common.keepScanFocus') }}
</a-checkbox>
<a-checkbox v-model="voiceEnabled" class="ops-opt">
{{ $t('operation.common.voiceBroadcast') }}
</a-checkbox>
</div>
<div class="ops-scan__actions">
<a-button class="ops-btn ops-btn--ghost" @click="handleReset()">
{{ $t('operation.inbound.actions.reset') }}
</a-button>
<a-button
type="primary"
class="ops-btn ops-btn--primary"
:loading="submitting || lookingUp"
@click="handleSubmit()"
>
{{ $t('operation.inbound.actions.inbound') }}
</a-button>
</div>
</div>
<p class="ops-scan-hint">{{ $t('operation.inbound.form.hint.supportedTypes') }}</p>
</div>
</section>
<section class="ops-card ops-card--verify">
<div class="ops-card__body ops-verify">
<p
v-if="feedbackMessage"
class="ops-feedback"
:class="`is-${feedbackType}`"
>
{{ feedbackMessage }}
</p>
<a-spin :loading="lookingUp" class="ops-verify-spin">
<div v-if="formData.orderId" class="ops-panel">
<div class="ops-hero">
<div class="ops-hero__nos">
<div class="ops-hero__waybill">{{ formData.waybillNumber || '-' }}</div>
<div class="ops-hero__meta">
<span>{{ $t('operation.inbound.form.salesProduct') }} {{ formData.salesProductName || '-' }}</span>
<span>{{ $t('operation.inbound.form.destinationCountry') }} {{ formData.destinationCountryName || '-' }}</span>
</div>
</div>
</div>
<div class="ops-metrics">
<div class="ops-metric">
<label class="ops-metric__label">{{ $t('operation.inbound.form.receivedWeight') }}</label>
<div class="ops-metric__weight">
<a-input
v-model="formData.weight"
:placeholder="$t('operation.inbound.form.placeholder.input')"
class="ops-field"
@press-enter="handleSubmit"
/>
<span class="ops-unit">KG</span>
</div>
</div>
<div class="ops-metric ops-metric--dims">
<label class="ops-metric__label">{{ $t('operation.inbound.form.dimensions') }}</label>
<div class="ops-dims">
<a-input
v-model="formData.length"
:placeholder="$t('operation.inbound.form.placeholder.length')"
class="ops-field ops-field--dim"
@press-enter="handleSubmit"
/>
<span class="ops-dims__x">×</span>
<a-input
v-model="formData.width"
:placeholder="$t('operation.inbound.form.placeholder.width')"
class="ops-field ops-field--dim"
@press-enter="handleSubmit"
/>
<span class="ops-dims__x">×</span>
<a-input
v-model="formData.height"
:placeholder="$t('operation.inbound.form.placeholder.height')"
class="ops-field ops-field--dim"
@press-enter="handleSubmit"
/>
</div>
</div>
</div>
</div>
<div v-else class="ops-empty">
<p class="ops-empty__title">{{ $t('operation.inbound.preview.empty') }}</p>
<p class="ops-empty__tip">{{ $t('operation.inbound.tip') }}</p>
</div>
</a-spin>
</div>
</section>
</div>
<aside class="ops-card ops-card--recent-wide">
<div class="ops-list-toolbar">
<div />
<PaginationPro
:total-count="totalCount"
v-model:current-page="currentPage"
v-model:page-size="pageSize"
:page-sizes="[10, 30, 50, 100]"
@page-change="handlePageChange"
@page-size-change="handlePageSizeChange"
/>
</div>
<div ref="tableContainerRef" class="ops-list-wrap">
<HandsontablePro
:columns="hotColumns"
:data="pagedOrders"
:total="totalCount"
:current-page="currentPage"
:page-size="pageSize"
:height="tableHeight"
:loading="listLoading"
:read-only="true"
:stretch-columns="true"
:search-params="headerSearchParams"
@search="handleHeaderSearch"
/>
</div>
</aside>
</div>
</template>
<script lang="ts" setup>
import { useI18n } from 'vue-i18n'
import { ref, reactive, computed, watch, onMounted, onBeforeUnmount } from 'vue'
import HandsontablePro from '@/components/HandsontablePro/index.vue'
import PaginationPro from '@/components/PaginationPro/index.vue'
import { useTableContainerHeight } from '@/composables/useTableContainerHeight'
import { useOperationScanFocus } from '@/composables/useOperationScanFocus'
import { useOperationScanFeedback } from '@/composables/useOperationScanFeedback'
import { inboundCreate, inboundRecentList, inboundOrderLookup } from '@/api/operation'
import { buildStatusTagHtml } from '@/utils/statusTag'
import { withServerFilterColumns } from '@/utils/headerFilterAdapter'
import { applyScanExtract, ensureScanExtractBundle } from '@/utils/scanExtract'
const { t } = useI18n()
const { tableContainerRef, tableHeight } = useTableContainerHeight()
const scanInputRef = ref<{ focus?: () => void } | null>(null)
const { keepScanFocus, maybeRefocusScan } = useOperationScanFocus('inboundScan', scanInputRef)
const {
voiceEnabled,
feedbackType,
feedbackMessage,
clearFeedback,
showError,
showSuccess,
showWarning,
} = useOperationScanFeedback('inboundScan')
defineOptions({ name: 'InboundScan' })
type RecentRow = {
key: string
waybillNumber: string
weight: string
dimensions: string
product: string
country: string
warehouse: string
operateTime: string
operator: string
orderId?: number
orderStatus?: string
orderStatusName?: string
allocationPendingReason?: string
}
const submitting = ref(false)
const lookingUp = ref(false)
const listLoading = ref(false)
const currentPage = ref(1)
const pageSize = ref(50)
const headerSearchParams = reactive<Record<string, string>>({})
const NUMBER_FILTER_KEYS = ['waybillNumber'] as const
let softPollTimer: ReturnType<typeof setInterval> | null = null
let softPollUntil = 0
const formData = reactive({
waybillNumber: '',
weight: '',
length: '',
width: '',
height: '',
salesProductName: '',
destinationCountryName: '',
orderId: undefined as number | undefined,
})
const recentOrders = ref<RecentRow[]>([])
const filteredOrders = computed(() => {
const waybill = String(headerSearchParams.waybillNumber || '')
.trim()
.toLowerCase()
if (!waybill) return recentOrders.value
return recentOrders.value.filter((row) =>
String(row.waybillNumber || '')
.toLowerCase()
.includes(waybill),
)
})
const totalCount = computed(() => filteredOrders.value.length)
const pagedOrders = computed(() => {
const start = (currentPage.value - 1) * pageSize.value
return filteredOrders.value.slice(start, start + pageSize.value)
})
const hotColumns = computed(() =>
withServerFilterColumns(
[
{ key: 'waybillNumber', label: t('operation.inbound.table.waybillNumber'), width: 160 },
{ key: 'weight', label: t('operation.inbound.table.weight'), width: 80 },
{ key: 'dimensions', label: t('operation.inbound.table.dimensions'), width: 120 },
{ key: 'product', label: t('operation.inbound.table.product'), width: 140 },
{ key: 'country', label: t('operation.inbound.table.destCountry'), width: 100 },
{
key: 'orderStatusName',
label: t('operation.inbound.table.orderStatus'),
width: 110,
renderer: (row: RecentRow) => {
const text = row.orderStatusName || row.orderStatus || '-'
return buildStatusTagHtml(text, statusTone(row.orderStatus))
},
},
{ key: 'allocationPendingReason', label: t('operation.inbound.table.reason'), width: 160 },
{ key: 'operateTime', label: t('operation.inbound.table.operateTime'), width: 160 },
],
NUMBER_FILTER_KEYS,
),
)
function statusTone(status?: string) {
switch (status) {
case 'pendingAllocation':
return 'wait'
case 'pendingPayment':
return 'alert'
default:
return 'neutral'
}
}
function mapRecentRow(row: any, idx: number): RecentRow {
const orderId = row.orderId ?? row.OrderId
const orderStatus = row.orderStatus ?? row.OrderStatus ?? ''
const reason = row.allocationPendingReason ?? row.AllocationPendingReason ?? ''
return {
key: String(row.id ?? row.Id ?? `${row.waybillNo ?? ''}-${idx}`),
waybillNumber: row.waybillNo ?? row.waybillNumber ?? '',
weight: row.weight != null ? String(row.weight) : '-',
dimensions:
row.length != null && row.width != null && row.height != null
? `${row.length}×${row.width}×${row.height}`
: '-',
product: row.salesProductName ?? '-',
country: row.destinationCountry ?? '-',
warehouse: row.warehouseName ?? '-',
operateTime: row.operateTime ?? row.createTime ?? '',
operator: row.operatorName ?? row.operator ?? '-',
orderId: orderId != null ? Number(orderId) : undefined,
orderStatus,
orderStatusName: row.orderStatusName ?? row.OrderStatusName ?? '',
allocationPendingReason: typeof reason === 'string' ? reason.trim() : '',
}
}
function stopSoftPoll() {
if (softPollTimer) {
clearInterval(softPollTimer)
softPollTimer = null
}
softPollUntil = 0
}
function startSoftPoll() {
softPollUntil = Date.now() + 20_000
if (softPollTimer) return
softPollTimer = setInterval(async () => {
if (Date.now() > softPollUntil) {
stopSoftPoll()
return
}
await loadRecent(false)
const stillPending = recentOrders.value.some((r) => r.orderStatus === 'pendingAllocation')
if (!stillPending) stopSoftPoll()
}, 3000)
}
async function loadRecent(showLoadError = true) {
listLoading.value = true
try {
const { items } = await inboundRecentList({ limit: 100 })
recentOrders.value = (items || []).map((row: any, idx: number) => mapRecentRow(row, idx))
const maxPage = Math.max(1, Math.ceil(filteredOrders.value.length / pageSize.value) || 1)
if (currentPage.value > maxPage) currentPage.value = maxPage
} catch (e: any) {
if (showLoadError) {
showError(e?.message || t('operation.inbound.message.loadRecentFailed'))
recentOrders.value = []
}
} finally {
listLoading.value = false
}
}
function handleHeaderSearch() {
currentPage.value = 1
}
function handlePageChange(page: number) {
currentPage.value = page
}
function handlePageSizeChange(size: number) {
pageSize.value = size
currentPage.value = 1
}
const lookedUpWaybillNo = ref('')
function clearLookupFields() {
formData.salesProductName = ''
formData.destinationCountryName = ''
formData.orderId = undefined
formData.weight = ''
formData.length = ''
formData.width = ''
formData.height = ''
lookedUpWaybillNo.value = ''
}
function clearLookup() {
clearLookupFields()
clearFeedback()
}
function hasMetrics() {
return !!(formData.weight && formData.length && formData.width && formData.height)
}
/** 扫码后静默查单;不弹「匹配成功」,失败才提示 */
async function ensureOrderLookup(): Promise<boolean> {
const no = formData.waybillNumber?.trim()
if (!no) {
clearLookup()
return false
}
if (formData.orderId && lookedUpWaybillNo.value === no) {
return true
}
lookingUp.value = true
try {
const info = await inboundOrderLookup(no)
if (!info?.orderId) {
throw new Error(t('operation.common.message.orderNotFound'))
}
formData.orderId = info.orderId
formData.salesProductName = info.salesProductName || info.productCode || ''
formData.destinationCountryName =
info.destinationCountryName || info.destinationCountry || ''
lookedUpWaybillNo.value = no
return true
} catch (e: any) {
formData.salesProductName = ''
formData.destinationCountryName = ''
formData.orderId = undefined
lookedUpWaybillNo.value = ''
showError(e?.message || t('operation.common.message.orderNotFound'))
maybeRefocusScan()
return false
} finally {
lookingUp.value = false
}
}
async function onScanEnter() {
formData.waybillNumber = applyScanExtract(formData.waybillNumber)
const ok = await ensureOrderLookup()
if (!ok) return
if (hasMetrics()) {
await handleSubmit()
return
}
clearFeedback()
}
function buildSuccessMessage(result: any) {
const status = result?.orderStatus ?? result?.OrderStatus
const statusName = result?.orderStatusName ?? result?.OrderStatusName
if (status === 'pendingAllocation') {
return t('operation.inbound.message.inboundProcessing')
}
if (statusName) {
return t('operation.inbound.message.inboundSuccessWithStatus', { status: statusName })
}
return t('operation.inbound.message.inboundProcessing')
}
async function handleSubmit() {
if (!formData.waybillNumber?.trim()) {
showWarning(t('operation.inbound.message.waybillNumberRequired'))
maybeRefocusScan()
return
}
const ok = await ensureOrderLookup()
if (!ok) return
if (!hasMetrics()) {
showWarning(t('operation.inbound.message.metricsRequired'))
return
}
submitting.value = true
try {
const result = await inboundCreate({
waybillNo: formData.waybillNumber.trim(),
weight: Number(formData.weight),
length: Number(formData.length),
width: Number(formData.width),
height: Number(formData.height),
})
showSuccess(buildSuccessMessage(result))
handleReset(false)
await loadRecent()
const status = result?.orderStatus ?? result?.OrderStatus
if (status === 'pendingAllocation') {
startSoftPoll()
}
maybeRefocusScan()
} catch (e: any) {
showError(e?.message || t('operation.inbound.message.inboundFailed'))
maybeRefocusScan()
} finally {
submitting.value = false
}
}
watch(
() => formData.waybillNumber,
(value) => {
const no = String(value || '').trim()
if (!no || no !== lookedUpWaybillNo.value) {
formData.orderId = undefined
formData.salesProductName = ''
formData.destinationCountryName = ''
lookedUpWaybillNo.value = ''
}
},
)
function handleReset(clearStatus = true) {
formData.waybillNumber = ''
clearLookupFields()
if (clearStatus) clearFeedback()
maybeRefocusScan()
}
onMounted(() => {
void ensureScanExtractBundle()
loadRecent()
})
onBeforeUnmount(() => {
stopSoftPoll()
})
</script>
<style lang="less" scoped>
@import '@/views/operation/styles/ops-scan-page.less';
</style>