useOperationApi-BiBzgQM3.mjs
5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { z as createSharedComposable, A as useApiGateway } from './server.mjs';
const OPERATION_TASK_STATUS_VALUES = [
"pending",
"in_progress",
"pending_test",
"failed",
"rework",
"completed",
"skipped"
];
const OPERATION_TASK_ACTION_VALUES = [
"start",
"complete_assembly",
"complete_rework",
"submit_test",
"skip"
];
function isRecord(value) {
return typeof value === "object" && value !== null;
}
function normalizeString(value, fallback = "") {
return typeof value === "string" ? value : fallback;
}
function normalizeNumber(value, fallback = 0) {
const num = Number(value);
return Number.isFinite(num) ? num : fallback;
}
function isStatus(value) {
return typeof value === "string" && OPERATION_TASK_STATUS_VALUES.includes(value);
}
function isAction(value) {
return typeof value === "string" && OPERATION_TASK_ACTION_VALUES.includes(value);
}
function isResult(value) {
return value === "pending" || value === "pass" || value === "fail";
}
function normalizeTasks(payload) {
if (!Array.isArray(payload)) {
return [];
}
return payload.filter(isRecord).map((item) => {
const auditEvents = Array.isArray(item.auditEvents) ? item.auditEvents : [];
return {
id: normalizeNumber(item.id),
workOrderNo: normalizeString(item.workOrderNo),
sn: normalizeString(item.sn),
stepName: normalizeString(item.stepName),
workstation: normalizeString(item.workstation),
device: normalizeString(item.device),
operator: normalizeString(item.operator),
startedAt: normalizeString(item.startedAt, void 0),
endedAt: normalizeString(item.endedAt, void 0),
result: isResult(item.result) ? item.result : "pending",
status: isStatus(item.status) ? item.status : "pending",
nextAction: normalizeString(item.nextAction),
auditEvents: auditEvents.filter(isRecord).map((event) => ({
id: normalizeString(event.id),
action: isAction(event.action) ? event.action : "start",
fromStatus: isStatus(event.fromStatus) ? event.fromStatus : null,
toStatus: isStatus(event.toStatus) ? event.toStatus : "pending",
operator: normalizeString(event.operator),
at: normalizeString(event.at),
remark: normalizeString(event.remark, void 0),
evidencePath: normalizeString(event.evidencePath, void 0),
nextAction: normalizeString(event.nextAction)
}))
};
}).filter((item) => item.id > 0 && item.sn.length > 0);
}
function normalizeMutation(payload, fallbackMessage) {
if (!isRecord(payload)) {
return {
success: false,
errorCode: "REQUEST_FAILED",
message: fallbackMessage
};
}
return {
success: Boolean(payload.success),
errorCode: typeof payload.errorCode === "string" ? payload.errorCode : null,
message: typeof payload.message === "string" && payload.message.length > 0 ? payload.message : fallbackMessage,
state: isStatus(payload.state) ? payload.state : void 0,
nextAction: typeof payload.nextAction === "string" ? payload.nextAction : void 0
};
}
function normalizeUpload(payload, fallbackMessage) {
if (!isRecord(payload)) {
return {
success: false,
errorCode: "REQUEST_FAILED",
message: fallbackMessage
};
}
return {
success: Boolean(payload.success),
errorCode: typeof payload.errorCode === "string" ? payload.errorCode : null,
message: typeof payload.message === "string" && payload.message.length > 0 ? payload.message : fallbackMessage,
relativePath: typeof payload.relativePath === "string" && payload.relativePath.length > 0 ? payload.relativePath : void 0
};
}
function buildQuery(query) {
const params = new URLSearchParams();
if (query.workstation) {
params.set("workstation", query.workstation);
}
if (query.stepName) {
params.set("stepName", query.stepName);
}
if (query.operator) {
params.set("operator", query.operator);
}
if (query.status) {
params.set("status", query.status);
}
if (query.sn) {
params.set("sn", query.sn);
}
const search = params.toString();
return search.length > 0 ? `?${search}` : "";
}
const _useOperationApi = () => {
const api = useApiGateway();
const getOperationTasks = async (query) => {
const result = await api.request(`/api/operations${buildQuery(query)}`, {
method: "GET"
});
return normalizeTasks(result);
};
const runOperationAction = async (id, action, payload) => {
if (!isAction(action)) {
return {
success: false,
errorCode: "VALIDATION_ERROR",
message: "工序任务动作不支持。"
};
}
const actionPath = `/api/operations/${id}/actions/${action}`;
try {
const result = await api.request(actionPath, {
method: "POST",
body: payload,
remotePath: actionPath
});
return normalizeMutation(result, "工序任务状态更新失败。");
} catch {
return {
success: false,
errorCode: "REQUEST_FAILED",
message: "工序任务状态更新失败。"
};
}
};
const uploadOperationEvidence = async (file) => {
const formData = new FormData();
formData.append("file", file);
try {
const result = await api.request("/api/files/operation-evidence", {
method: "POST",
body: formData
});
return normalizeUpload(result, "附件上传失败。");
} catch {
return {
success: false,
errorCode: "REQUEST_FAILED",
message: "附件上传失败。"
};
}
};
return {
getOperationTasks,
runOperationAction,
uploadOperationEvidence
};
};
const useOperationApi = createSharedComposable(_useOperationApi);
export { useOperationApi as u };
//# sourceMappingURL=useOperationApi-BiBzgQM3.mjs.map