fix(worker): 打手端实时补丁不再误删"按工单号搜到的行",并同步分页总数

这是后台那个"搜工单号搜不到、必须刷新整页"在打手端的同源副本:
WorkerLayout 的 operation = event.operation === 'remove' || !matches ? 'remove' : 'upsert'
只要前端判"不匹配"就删行,而前端关键词匹配器**没有比对工单号**——
后端搜索表达式的第一项恰恰就是 work_order_no(还有 material_json 里更多前端不知道的字段)。
于是按工单号搜索时,该单一旦有实时变更,行就被从缓存删掉、列表变空。

修法与后台侧对齐:
1. 抽出 utils/worker-realtime-work-orders.ts(可单测、无运行时依赖),关键词匹配补上
   workOrderNo,并改成三态判定——状态/分类等可证伪维度不符才删行;关键词能找到=match
   (允许插入),找不到=unknown(不删不插,只就地更新)。
2. patchWorkerWorkOrderListEnvelope 增删行时同步 data.pagination.total,
   修掉"列表空了、分页器还显示有 N 条"。
3. 大厅无限下拉只在第 1 页允许插入,其余页只就地更新。

验证:前端 86 项单测全绿(新增 6 项,覆盖"按工单号搜索+实时变更不得删行"、
unknown 只就地更新、删/插同步总数、无限下拉页边界)。
This commit is contained in:
yml2213
2026-10-01 13:48:05 +08:00
parent 2d75be702d
commit f4c405d51a
3 changed files with 271 additions and 59 deletions
+19 -59
View File
@@ -14,6 +14,11 @@ import { Outlet, useLocation, useNavigate } from 'react-router'
import { fetchWorkerAfterSalesCases, fetchWorkerProfile, logoutWorker } from '@/services/worker'
import { useIsMobile } from '@/utils/use-is-mobile'
import {
evaluateWorkerKeywordVerdict,
patchWorkerWorkOrderInfiniteEnvelope,
patchWorkerWorkOrderListEnvelope,
} from '@/utils/worker-realtime-work-orders'
import {
clearWorkerSession,
expireWorkerSession,
@@ -337,14 +342,16 @@ function patchWorkerMyOrderCaches(
productName?: unknown
platformOrderId?: unknown
}
const matchesStatus = !status || status === String(item.status || '')
const matchesKeyword = matchesWorkOrderKeyword(item, keyword)
const matches = matchesStatus && matchesKeyword
const shouldInsert = event.operation === 'upsert' && page === 1 && matches
const operation = event.operation === 'remove' || !matches ? 'remove' : 'upsert'
// 状态可证伪:不符才允许删行;关键词不可证伪(后端搜的字段比前端多,含工单号),
// 找不到时只判 unknown —— 不删也不插,就地更新,避免"按工单号搜索时行被删空"。
const statusMatches = !status || status === String(item.status || '')
const keywordVerdict = evaluateWorkerKeywordVerdict(item, keyword)
const shouldInsert =
event.operation === 'upsert' && page === 1 && statusMatches && keywordVerdict === 'match'
const operation = event.operation === 'remove' || !statusMatches ? 'remove' : 'upsert'
queryClient.setQueryData(
queryKey,
patchWorkOrderListEnvelope(current, { ...event, operation }, shouldInsert),
patchWorkerWorkOrderListEnvelope(current, { ...event, operation }, shouldInsert),
)
}
}
@@ -406,15 +413,14 @@ function applyWorkerHallCachePatch(
const keyword = String(queryKey[1] || '')
const categoryId = Number(queryKey[2] || 0)
const matchesCategory = !categoryId || categoryId === Number(item.categoryId || 0)
const matches =
String(item.status || '') === 'open' &&
matchesCategory &&
matchesWorkOrderKeyword(item, keyword)
const shouldInsert = event.operation === 'upsert' && matches
const operation = event.operation === 'remove' || !matches ? 'remove' : 'upsert'
// 大厅是"必须是 open"且分类可证伪:不符就该移出大厅;关键词仍按不可证真处理。
const provableMatch = String(item.status || '') === 'open' && matchesCategory
const keywordVerdict = evaluateWorkerKeywordVerdict(item, keyword)
const shouldInsert = event.operation === 'upsert' && provableMatch && keywordVerdict === 'match'
const operation = event.operation === 'remove' || !provableMatch ? 'remove' : 'upsert'
queryClient.setQueryData(
queryKey,
patchInfiniteWorkOrderEnvelope(
patchWorkerWorkOrderInfiniteEnvelope(
current,
{ ...event, data: workerSnapshot, operation },
shouldInsert,
@@ -476,52 +482,6 @@ function applyWorkerDepositRule(queryClient: ReturnType<typeof useQueryClient>,
}
}
function matchesWorkOrderKeyword(
item: { productName?: unknown; platformOrderId?: unknown; material?: unknown },
keyword: string,
) {
const normalizedKeyword = keyword.trim().toLowerCase()
if (!normalizedKeyword) return true
const material = isRecord(item.material) ? item.material : {}
const collect = isRecord(material.collect) ? material.collect : {}
const fields = isRecord(collect.fields) ? collect.fields : {}
return [
item.productName,
item.platformOrderId,
fields.gameId,
fields.gameNickname,
fields.gameAccount,
fields.roleName,
material.gameId,
material.gameNickname,
]
.map((value) => String(value || '').toLowerCase())
.some((value) => value.includes(normalizedKeyword))
}
function patchWorkOrderListEnvelope(current: unknown, event: RealtimeEvent, insert = false) {
if (!isRecord(current) || !isRecord(current.data) || !Array.isArray(current.data.items)) {
return current
}
const id = Number((event.data as { workOrderId?: unknown }).workOrderId || event.entityId)
const items = current.data.items as unknown[]
const nextItems =
event.operation === 'remove'
? items.filter((entry) => Number((entry as { workOrderId?: unknown })?.workOrderId) !== id)
: upsertById(items, event.data, 'workOrderId', insert)
return { ...current, data: { ...current.data, items: nextItems } }
}
function patchInfiniteWorkOrderEnvelope(current: unknown, event: RealtimeEvent, insert = false) {
if (!isRecord(current) || !Array.isArray(current.pages)) return current
return {
...current,
pages: current.pages.map((page: unknown, index) =>
patchWorkOrderListEnvelope(page, event, insert && index === 0),
),
}
}
function patchFinanceRequestListEnvelope(current: unknown, event: RealtimeEvent) {
if (!isRecord(current) || !isRecord(current.data) || !Array.isArray(current.data.items)) {
return current