| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- /** 统一实体详情只读 API。路径规划与响应正规化集中在此,组件/store 不认识后端信封。 */
- import { requestCockpitJson } from './cockpitApi.js'
- import { ENTITY_DETAIL_KIND, entityIdentity, normalizeEntityDetail } from '../domain/entityDetail.js'
- export const ENTITY_DETAIL_PATHS = Object.freeze({
- [ENTITY_DETAIL_KIND.FACILITY]: (id, target) => target.category
- ? `/infrastructure/entities/${encodeURIComponent(id)}/detail?category=${encodeURIComponent(target.category)}` : '',
- [ENTITY_DETAIL_KIND.EMERGENCY]: (id, target) => target.category
- ? `/infrastructure/entities/${encodeURIComponent(id)}/detail?category=${encodeURIComponent(target.category)}` : '',
- [ENTITY_DETAIL_KIND.EVENT]: (id, target) => target.family
- ? `/events/${encodeURIComponent(id)}/detail?family=${encodeURIComponent(target.family)}` : '',
- [ENTITY_DETAIL_KIND.WORK_ORDER]: (id) => `/work-orders/${encodeURIComponent(id)}/detail`,
- })
- /** 工单处理流程:后端按 orderId 返回 workOrder + timelineEntries(票 129)。 */
- export function workOrderTimelinePath(orderId) {
- return orderId ? `/work-orders/${encodeURIComponent(orderId)}/timeline` : ''
- }
- export function entityDetailPath(identityInput) {
- const identity = entityIdentity(identityInput)
- const build = ENTITY_DETAIL_PATHS[identity.kind]
- return identity.valid && build ? build(identity.id, identityInput || {}) : ''
- }
- export async function fetchEntityDetail(identityInput, options = {}) {
- const identity = entityIdentity(identityInput)
- const path = options.path || entityDetailPath(identity)
- if (!identity.valid || !path) return { ok: false, identity, reason: 'invalid-identity', mode: '', data: null }
- // 工单:列表已下发 orderId(票 129),详情 + 处理流程都按主键取。
- // 旧调用方没有 orderId 时仍退回列表 prefill,避免刚升级后端前整卡不可用。
- if (identity.kind === ENTITY_DETAIL_KIND.WORK_ORDER) {
- const prefill = options.prefill && typeof options.prefill === 'object' ? options.prefill : {}
- const orderId = String(prefill.orderId || prefill.identityPayload?.orderId || identity.id || '').trim()
- if (!orderId || !/^\d+$/.test(orderId)) {
- const detail = normalizeEntityDetail(identity, {}, prefill)
- return detail
- ? { ok: true, identity, mode: prefill ? 'prefill' : 'unavailable', path: '', data: detail }
- : { ok: false, identity, mode: '', reason: 'bad-payload', data: null }
- }
- const detailPath = `/work-orders/${encodeURIComponent(orderId)}/detail`
- const timelinePath = workOrderTimelinePath(orderId)
- const [detailResult, timelineResult] = await Promise.all([
- requestCockpitJson(detailPath, { config: options.config, fetchImpl: options.fetchImpl }),
- requestCockpitJson(timelinePath, { config: options.config, fetchImpl: options.fetchImpl }),
- ])
- if (!detailResult.ok && !timelineResult.ok) {
- const fallback = normalizeEntityDetail(identity, {}, prefill)
- return fallback
- ? { ok: true, identity, mode: prefill ? 'prefill' : 'unavailable', path: '', data: fallback }
- : { ok: false, identity, mode: '', reason: detailResult.reason || timelineResult.reason || 'unavailable', data: null }
- }
- const detailData = detailResult.ok && detailResult.data ? detailResult.data : {}
- const timelineData = timelineResult.ok && timelineResult.data ? timelineResult.data : {}
- const order = {
- ...prefill,
- ...(timelineData.workOrder && typeof timelineData.workOrder === 'object' ? timelineData.workOrder : {}),
- ...(detailData.order && typeof detailData.order === 'object' ? detailData.order : {}),
- }
- const source = {
- ...prefill,
- ...detailData,
- ...timelineData,
- order,
- }
- // 时间线端点成功时以它为准;只有它失败才保留 detail 自带的 logs 兜底。
- if (timelineResult.ok) {
- source.timelineEntries = Array.isArray(timelineData.timelineEntries) ? timelineData.timelineEntries : []
- delete source.logs
- }
- const detail = normalizeEntityDetail(identity, source, prefill)
- return detail
- ? {
- ok: true,
- identity,
- mode: detailResult.mode || timelineResult.mode || '',
- path: detailResult.path || detailPath,
- data: detail,
- }
- : { ok: false, identity, mode: '', path: detailPath, reason: 'bad-payload', data: null }
- }
- const result = await requestCockpitJson(path, { config: options.config, fetchImpl: options.fetchImpl })
- if (!result.ok) return { ...result, identity, data: null }
- const detail = normalizeEntityDetail(identity, result.data, options.prefill)
- return detail
- ? { ok: true, identity, mode: result.mode || '', path: result.path || path, data: detail }
- : { ok: false, identity, mode: result.mode || '', path: result.path || path, reason: 'bad-payload', data: null }
- }
|