import { session } from '@/stores/session.js' const EVIDENCE_TYPES = Object.freeze([ 'PHOTO_STEP_1', 'PHOTO_STEP_2', 'PHOTO_STEP_3', 'PHOTO_STEP_4', 'SEAL' ]) // C02 supplies inboundId/inboundVersion and loading.pickingVersion. C04 supplies // inbound-scoped task and evidence operations. No local task is promoted to a // server success when any required identifier or version is absent. function request(options) { const baseUrl = session.apiBase || '/api/platform/v1' const headers = { ...(options.header || {}) } if (session.token) headers.Authorization = `Bearer ${session.token}` if (session.requestId) headers['X-Request-Id'] = session.requestId return new Promise((resolve, reject) => { uni.request({ ...options, url: `${baseUrl}${options.url}`, header: headers, success(response) { const body = response.data || {} const code = String(body.code ?? response.statusCode) if (code !== '200') { const error = new Error(body.message || '服务端请求失败') error.code = body.code || response.statusCode error.statusCode = response.statusCode reject(error) return } resolve(body.data) }, fail(error) { const next = new Error(error.errMsg || '网络连接失败') next.network = true reject(next) } }) }) } function idempotencyKey(operation, inboundId) { return `pda-${operation}-${inboundId}-${Date.now()}-${Math.random().toString(36).slice(2, 8)}` } function tenantQuery(tenantId) { const value = tenantId ?? session.tenantId if (!value) throw new Error('缺少客户隔离标识 tenantId,无法请求现场任务') return String(value) } export function isRemoteReady(inboundId) { return Boolean(session.token && session.tenantId && inboundId) } export function listLoadingOrders({ page = 1, size = 50, keyword = '' } = {}) { const query = [`mainStatus=LOADING`, `page=${page}`, `size=${size}`] if (session.tenantId) query.push(`tenantId=${encodeURIComponent(session.tenantId)}`) if (keyword) query.push(`keyword=${encodeURIComponent(keyword)}`) return request({ url: `/orders?${query.join('&')}`, method: 'GET' }).then((data) => ({ ...data, records: (data && data.records ? data.records : []).map(normalizePdaOrder) })) } export function normalizePdaOrder(row = {}) { row = row || {} return { ...row, inboundId: row.inboundId ?? row.inbound?.id ?? null, inboundVersion: row.inboundVersion ?? row.inbound?.version ?? null, pickingVersion: row.pickingVersion ?? row.loading?.pickingVersion ?? null } } export function normalizePickingTask(task = {}) { task = task || {} return { ...task, pickingVersion: task.pickingVersion ?? task.version ?? null, inboundVersion: task.inboundVersion ?? null } } export function getPickingTask(inboundId, tenantId) { return request({ url: `/inbounds/${encodeURIComponent(inboundId)}/loading/picking?tenantId=${encodeURIComponent(tenantQuery(tenantId))}`, method: 'GET' }) } export function startPicking(inboundId, payload, tenantId) { return request({ url: `/inbounds/${encodeURIComponent(inboundId)}/loading/picking/start?tenantId=${encodeURIComponent(tenantQuery(tenantId))}`, method: 'POST', header: { 'Idempotency-Key': idempotencyKey('start-picking', inboundId) }, data: payload }) } export function completePicking(inboundId, payload, tenantId) { return request({ url: `/inbounds/${encodeURIComponent(inboundId)}/loading/picking/complete?tenantId=${encodeURIComponent(tenantQuery(tenantId))}`, method: 'POST', header: { 'Idempotency-Key': idempotencyKey('complete-picking', inboundId) }, data: payload }) } export function getEvidence(inboundId, tenantId) { return request({ url: `/inbounds/${encodeURIComponent(inboundId)}/loading/evidence?tenantId=${encodeURIComponent(tenantQuery(tenantId))}`, method: 'GET' }) } export function upsertEvidence(inboundId, payload, tenantId) { if (!EVIDENCE_TYPES.includes(payload.evidenceType)) throw new Error('不支持的装柜证据类型') return request({ url: `/inbounds/${encodeURIComponent(inboundId)}/loading/evidence?tenantId=${encodeURIComponent(tenantQuery(tenantId))}`, method: 'POST', header: { 'Idempotency-Key': idempotencyKey(`evidence-${payload.evidenceType}`, inboundId) }, data: payload }) } export function completeLoading(inboundId, payload, tenantId) { return request({ url: `/inbounds/${encodeURIComponent(inboundId)}/loading/complete?tenantId=${encodeURIComponent(tenantQuery(tenantId))}`, method: 'POST', header: { 'Idempotency-Key': idempotencyKey('complete-loading', inboundId) }, data: payload }) } export { EVIDENCE_TYPES }