0e27ddc8
杨鑫
标签
|
1
|
import { createApiClient } from "../lib/apiClient";
|
58d2e61c
杨鑫
最新代码
|
2
3
|
import {
stripLabelConfigPrefixes,
|
540ac0e3
杨鑫
前端修改bug
|
4
5
|
normalizeTemplateBorder,
normalizePrintOrientation,
|
58d2e61c
杨鑫
最新代码
|
6
7
8
9
10
11
12
|
type LabelElement,
type LabelTemplateCreateInput,
type LabelTemplateDto,
type LabelTemplateGetListInput,
type LabelTemplateProductDefaultDto,
type LabelTemplateUpdateInput,
type PagedResultDto,
|
0e27ddc8
杨鑫
标签
|
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
} from "../types/labelTemplate";
const api = createApiClient({
getToken: () => {
try {
return localStorage.getItem("access_token") ?? localStorage.getItem("token") ?? null;
} catch {
return null;
}
},
});
const PATH = "/label-template";
function normalizeTemplateCode(raw: unknown): string {
const r = raw as Record<string, unknown> | null | undefined;
if (!r || typeof r !== "object") return "";
const id = r.id ?? r.templateCode ?? r.TemplateCode;
return typeof id === "string" ? id.trim() : String(id ?? "").trim();
}
|
143afd59
杨鑫
打印,标签
|
34
35
36
37
38
39
40
41
42
|
/** 详情/列表里的 elements 兼容 PascalCase(如 InputKey) */
function normalizeTemplateElements(list: unknown): LabelElement[] {
if (!Array.isArray(list)) return [];
return list.map((raw) => {
const e = raw as Record<string, unknown> & {
InputKey?: unknown;
inputKey?: unknown;
ElementName?: unknown;
elementName?: unknown;
|
58d2e61c
杨鑫
最新代码
|
43
44
|
TypeAdd?: unknown;
typeAdd?: unknown;
|
143afd59
杨鑫
打印,标签
|
45
46
47
48
49
|
LibraryCategory?: unknown;
libraryCategory?: unknown;
};
const ik = e.inputKey ?? e.InputKey;
const nameRaw = e.elementName ?? e.ElementName;
|
58d2e61c
杨鑫
最新代码
|
50
|
const typeAddRaw = e.typeAdd ?? e.TypeAdd;
|
143afd59
杨鑫
打印,标签
|
51
52
53
54
55
56
|
const lcRaw = e.libraryCategory ?? e.LibraryCategory;
let libraryCategory: LabelElement["libraryCategory"];
if (typeof lcRaw === "string") {
const t = lcRaw.trim();
if (t) libraryCategory = t;
}
|
58d2e61c
杨鑫
最新代码
|
57
58
59
60
|
const rawCfg =
e.config && typeof e.config === "object" && !Array.isArray(e.config)
? (e.config as Record<string, unknown>)
: {};
|
143afd59
杨鑫
打印,标签
|
61
62
63
64
|
return {
...(e as object),
elementName:
typeof nameRaw === "string" ? nameRaw.trim() : undefined,
|
58d2e61c
杨鑫
最新代码
|
65
|
typeAdd: typeof typeAddRaw === "string" ? typeAddRaw.trim() : undefined,
|
143afd59
杨鑫
打印,标签
|
66
67
|
inputKey: typeof ik === "string" ? ik : e.inputKey ?? null,
libraryCategory,
|
20554770
杨鑫
更新bug
|
68
|
border: normalizeTemplateBorder(e.border ?? e.Border ?? e.BorderType ?? e.borderType),
|
58d2e61c
杨鑫
最新代码
|
69
|
config: stripLabelConfigPrefixes(rawCfg) as LabelElement["config"],
|
143afd59
杨鑫
打印,标签
|
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
|
} as LabelElement;
});
}
function normalizeDefaultValuesJson(raw: unknown): Record<string, string> {
if (raw == null || typeof raw !== "object" || Array.isArray(raw)) return {};
const out: Record<string, string> = {};
for (const [k, v] of Object.entries(raw as Record<string, unknown>)) {
if (v == null) out[k] = "";
else if (typeof v === "string") out[k] = v;
else if (typeof v === "number" || typeof v === "boolean") out[k] = String(v);
else out[k] = JSON.stringify(v);
}
return out;
}
function normalizeTemplateProductDefaultsList(raw: unknown): LabelTemplateProductDefaultDto[] {
if (!Array.isArray(raw)) return [];
return raw.map((item, index) => {
const o = item as Record<string, unknown>;
const dv = o.defaultValues ?? o.DefaultValues ?? o.defaultValuesJson ?? o.DefaultValuesJson;
return {
productId: String(o.productId ?? o.ProductId ?? "").trim(),
labelTypeId: String(o.labelTypeId ?? o.LabelTypeId ?? "").trim(),
defaultValues: normalizeDefaultValuesJson(dv),
orderNum: Number(o.orderNum ?? o.OrderNum ?? index + 1) || index + 1,
};
});
}
|
540ac0e3
杨鑫
前端修改bug
|
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
|
function normalizeListContentItems(raw: unknown): string[] {
const r = raw as Record<string, unknown> | null | undefined;
if (!r || typeof r !== "object") return [];
const list = r.contentItems ?? r.items ?? r.Items ?? r.itemNames ?? r.ItemNames;
/** 列表接口 items 常为后端拼好的逗号分隔字符串 */
if (typeof list === "string") {
const t = list.trim();
return t ? [t] : [];
}
if (!Array.isArray(list)) return [];
const out: string[] = [];
for (const x of list) {
if (typeof x === "string") {
const t = x.trim();
if (t) out.push(t);
continue;
}
if (x != null && typeof x === "object" && !Array.isArray(x)) {
const o = x as Record<string, unknown>;
const name = String(o.elementName ?? o.ElementName ?? o.name ?? o.Name ?? "").trim();
if (name) out.push(name);
}
}
return out;
}
/** 列表 Contents 展示文案:优先接口 items 字符串,否则数组 join */
function normalizeListContentsText(raw: unknown, contentItems: string[]): string | null {
const r = raw as Record<string, unknown> | null | undefined;
if (!r || typeof r !== "object") return contentItems.length ? contentItems.join(", ") : null;
const list = r.contentItems ?? r.items ?? r.Items ?? r.itemNames ?? r.ItemNames;
if (typeof list === "string") {
const t = list.trim();
return t || null;
}
if (contentItems.length) return contentItems.join(", ");
return null;
}
|
0e27ddc8
杨鑫
标签
|
139
140
141
142
143
144
145
|
function normalizeLabelTemplateDto(raw: unknown): LabelTemplateDto {
const r = raw as Record<string, unknown>;
const ids =
(Array.isArray(r.appliedLocationIds) ? r.appliedLocationIds : null) ??
(Array.isArray(r.AppliedLocationIds) ? r.AppliedLocationIds : null) ??
[];
const appliedLocationIds = ids.map((x) => String(x));
|
83ccb207
杨鑫
最新
|
146
147
148
149
150
151
152
|
const parseIdList = (v: unknown): string[] => {
if (!Array.isArray(v)) return [];
return [...new Set(v.map((x) => String(x).trim()).filter(Boolean))];
};
const regionIds = parseIdList(r.regionIds ?? r.RegionIds);
const groupIds = parseIdList(r.groupIds ?? r.GroupIds);
const locationIds = parseIdList(r.locationIds ?? r.LocationIds);
|
540ac0e3
杨鑫
前端修改bug
|
153
|
const partnerIds = parseIdList(r.partnerIds ?? r.PartnerIds ?? r.companyIds ?? r.CompanyIds);
|
83ccb207
杨鑫
最新
|
154
155
156
157
|
const mergedRegionIds = [...new Set([...regionIds, ...groupIds])];
const mergedLocationIds = [
...new Set([...locationIds, ...appliedLocationIds]),
];
|
540ac0e3
杨鑫
前端修改bug
|
158
159
160
161
162
163
164
165
|
const mergedPartnerIds = partnerIds;
const appliedPartnerTypeRaw = r.appliedPartnerType ?? r.AppliedPartnerType;
const appliedRegionTypeRaw = r.appliedRegionType ?? r.AppliedRegionType;
const appliedLocationTypeRaw =
r.appliedLocationType ?? r.AppliedLocationType ?? r.appliedLocation ?? r.AppliedLocation;
const companyTextVo = r.company ?? r.Company;
const regionTextVo = r.region ?? r.Region;
const locationFieldVo = r.location ?? r.Location;
|
0e27ddc8
杨鑫
标签
|
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
const id = normalizeTemplateCode(raw);
const templateNameVo = r.templateName ?? r.TemplateName;
const templateCodeVo = r.templateCode ?? r.TemplateCode;
const locationTextVo = r.locationText ?? r.LocationText;
const sizeTextVo = r.sizeText ?? r.SizeText;
const ccRaw = r.contentsCount ?? r.ContentsCount;
const contentsCountVo = typeof ccRaw === "number" ? ccRaw : undefined;
const lastEditedVo = r.lastEdited ?? r.LastEdited;
const nameFromList =
(typeof r.name === "string" && r.name.trim() ? r.name : null) ??
(typeof templateNameVo === "string" && String(templateNameVo).trim() ? String(templateNameVo) : null);
|
540ac0e3
杨鑫
前端修改bug
|
180
181
182
|
const contentItems = normalizeListContentItems(r);
const contentsText = normalizeListContentsText(r, contentItems);
|
0e27ddc8
杨鑫
标签
|
183
184
185
186
187
188
189
|
return {
...(r as object),
id,
name: nameFromList ?? (r.name as LabelTemplateDto["name"]),
templateName: (typeof templateNameVo === "string" ? templateNameVo : null) ?? (r.templateName as string | null),
templateCode: (typeof templateCodeVo === "string" ? templateCodeVo : null) ?? (r.templateCode as string | null),
locationText: (typeof locationTextVo === "string" ? locationTextVo : null) ?? (r.locationText as string | null),
|
540ac0e3
杨鑫
前端修改bug
|
190
191
192
193
194
195
196
197
198
199
200
201
|
company: (typeof companyTextVo === "string" ? companyTextVo : null) ?? (r.company as string | null),
region: (typeof regionTextVo === "string" ? regionTextVo : null) ?? (r.region as string | null),
location: (typeof locationFieldVo === "string" ? locationFieldVo : null) ?? (r.location as string | null),
appliedPartnerType:
(typeof appliedPartnerTypeRaw === "string" ? appliedPartnerTypeRaw : null) ??
(r.appliedPartnerType as string | null),
appliedRegionType:
(typeof appliedRegionTypeRaw === "string" ? appliedRegionTypeRaw : null) ??
(r.appliedRegionType as string | null),
appliedLocationType:
(typeof appliedLocationTypeRaw === "string" ? appliedLocationTypeRaw : null) ??
(r.appliedLocationType as string | null),
|
0e27ddc8
杨鑫
标签
|
202
203
|
sizeText: (typeof sizeTextVo === "string" ? sizeTextVo : null) ?? (r.sizeText as string | null),
contentsCount: contentsCountVo ?? (r.contentsCount as number | null),
|
540ac0e3
杨鑫
前端修改bug
|
204
205
206
|
contentItems,
contentsText,
items: contentsText ?? (contentItems.length ? contentItems.join(", ") : null),
|
0e27ddc8
杨鑫
标签
|
207
|
lastEdited: (typeof lastEditedVo === "string" ? lastEditedVo : null) ?? (r.lastEdited as string | null),
|
83ccb207
杨鑫
最新
|
208
|
appliedLocationIds: mergedLocationIds.length ? mergedLocationIds : appliedLocationIds,
|
540ac0e3
杨鑫
前端修改bug
|
209
210
|
partnerIds: mergedPartnerIds,
companyIds: mergedPartnerIds,
|
83ccb207
杨鑫
最新
|
211
212
213
|
regionIds: mergedRegionIds,
groupIds: mergedRegionIds,
locationIds: mergedLocationIds,
|
143afd59
杨鑫
打印,标签
|
214
215
216
217
|
elements: normalizeTemplateElements(r.elements),
templateProductDefaults: normalizeTemplateProductDefaultsList(
r.templateProductDefaults ?? r.TemplateProductDefaults,
),
|
540ac0e3
杨鑫
前端修改bug
|
218
219
|
border: normalizeTemplateBorder(r.border ?? r.BorderType ?? r.borderType),
printOrientation: normalizePrintOrientation(r.printOrientation ?? r.PrintOrientation),
|
0e27ddc8
杨鑫
标签
|
220
221
222
223
224
225
226
227
228
229
230
231
|
} as LabelTemplateDto;
}
export async function getLabelTemplates(input: LabelTemplateGetListInput, signal?: AbortSignal): Promise<PagedResultDto<LabelTemplateDto>> {
const res = await api.requestJson<PagedResultDto<LabelTemplateDto>>({
path: PATH,
method: "GET",
query: {
SkipCount: input.skipCount,
MaxResultCount: input.maxResultCount,
Sorting: input.sorting,
Keyword: input.keyword,
|
540ac0e3
杨鑫
前端修改bug
|
232
|
PartnerId: input.partnerId,
|
83ccb207
杨鑫
最新
|
233
|
GroupId: input.groupId,
|
0e27ddc8
杨鑫
标签
|
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
LocationId: input.locationId,
LabelType: input.labelType,
State: input.state,
},
signal,
});
const items = (res.items ?? []).map((x) => normalizeLabelTemplateDto(x));
return { ...res, items };
}
export async function getLabelTemplate(templateCode: string, signal?: AbortSignal): Promise<LabelTemplateDto> {
const raw = await api.requestJson<LabelTemplateDto>({
path: `${PATH}/${encodeURIComponent(templateCode)}`,
method: "GET",
signal,
});
return normalizeLabelTemplateDto(raw);
}
|
83ccb207
杨鑫
最新
|
253
|
function buildLabelTemplateScopeBody(input: LabelTemplateCreateInput): Record<string, unknown> {
|
540ac0e3
杨鑫
前端修改bug
|
254
255
256
257
258
259
260
|
const partnerIds = [
...new Set(
[...(input.partnerIds ?? []), ...(input.companyIds ?? [])]
.map((x) => String(x).trim())
.filter(Boolean),
),
];
|
83ccb207
杨鑫
最新
|
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
const regionIds = [
...new Set(
[...(input.regionIds ?? []), ...(input.groupIds ?? [])]
.map((x) => String(x).trim())
.filter(Boolean),
),
];
const locationIds = [
...new Set(
[...(input.locationIds ?? []), ...(input.appliedLocationIds ?? [])]
.map((x) => String(x).trim())
.filter(Boolean),
),
];
|
540ac0e3
杨鑫
前端修改bug
|
275
276
277
278
|
const appliedPartnerType = String(input.appliedPartnerType ?? "").trim().toUpperCase() || (partnerIds.length ? "SPECIFIED" : "ALL");
const appliedRegionType = String(input.appliedRegionType ?? "").trim().toUpperCase() || (regionIds.length ? "SPECIFIED" : "ALL");
const appliedLocation = String(input.appliedLocation ?? "").trim().toUpperCase() || (locationIds.length ? "SPECIFIED" : "ALL");
|
143afd59
杨鑫
打印,标签
|
279
280
281
282
283
284
285
|
const body: Record<string, unknown> = {
id: input.id,
name: input.name,
labelType: input.labelType,
unit: input.unit,
width: input.width,
height: input.height,
|
540ac0e3
杨鑫
前端修改bug
|
286
287
288
|
appliedPartnerType,
appliedRegionType,
appliedLocation,
|
143afd59
杨鑫
打印,标签
|
289
290
|
showRuler: input.showRuler ?? true,
showGrid: input.showGrid ?? true,
|
540ac0e3
杨鑫
前端修改bug
|
291
|
border: normalizeTemplateBorder(input.border),
|
4cb354d4
杨鑫
提交
|
292
|
printOrientation: normalizePrintOrientation(input.printOrientation),
|
143afd59
杨鑫
打印,标签
|
293
294
|
state: input.state ?? true,
elements: input.elements,
|
143afd59
杨鑫
打印,标签
|
295
|
};
|
540ac0e3
杨鑫
前端修改bug
|
296
297
298
299
300
301
302
303
304
305
|
if (appliedPartnerType === "SPECIFIED" && partnerIds.length) {
body.partnerIds = partnerIds;
body.companyIds = partnerIds;
} else {
body.partnerIds = [];
body.companyIds = [];
}
if (appliedRegionType === "SPECIFIED" && regionIds.length) {
|
83ccb207
杨鑫
最新
|
306
307
|
body.regionIds = regionIds;
body.groupIds = regionIds;
|
540ac0e3
杨鑫
前端修改bug
|
308
309
310
|
} else {
body.regionIds = [];
body.groupIds = [];
|
83ccb207
杨鑫
最新
|
311
|
}
|
540ac0e3
杨鑫
前端修改bug
|
312
313
|
if (appliedLocation === "SPECIFIED" && locationIds.length) {
|
83ccb207
杨鑫
最新
|
314
315
316
|
body.locationIds = locationIds;
body.appliedLocationIds = locationIds;
} else {
|
540ac0e3
杨鑫
前端修改bug
|
317
|
body.locationIds = [];
|
83ccb207
杨鑫
最新
|
318
319
|
body.appliedLocationIds = [];
}
|
540ac0e3
杨鑫
前端修改bug
|
320
|
|
83ccb207
杨鑫
最新
|
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
return body;
}
export async function createLabelTemplate(input: LabelTemplateCreateInput): Promise<LabelTemplateDto> {
const created = await api.requestJson<LabelTemplateDto>({
path: PATH,
method: "POST",
body: buildLabelTemplateScopeBody(input),
});
return normalizeLabelTemplateDto(created);
}
export async function updateLabelTemplate(templateCode: string, input: LabelTemplateUpdateInput): Promise<LabelTemplateDto> {
const body = buildLabelTemplateScopeBody(input);
|
143afd59
杨鑫
打印,标签
|
335
336
337
338
339
340
341
342
|
if (input.templateProductDefaults !== undefined) {
body.templateProductDefaults = input.templateProductDefaults.map((row, i) => ({
productId: row.productId,
labelTypeId: row.labelTypeId,
defaultValues: row.defaultValues,
orderNum: row.orderNum ?? i + 1,
}));
}
|
0e27ddc8
杨鑫
标签
|
343
344
345
|
const updated = await api.requestJson<LabelTemplateDto>({
path: `${PATH}/${encodeURIComponent(templateCode)}`,
method: "PUT",
|
143afd59
杨鑫
打印,标签
|
346
|
body,
|
0e27ddc8
杨鑫
标签
|
347
348
349
350
351
352
353
354
355
356
|
});
return normalizeLabelTemplateDto(updated);
}
export async function deleteLabelTemplate(templateCode: string): Promise<void> {
await api.requestJson<unknown>({
path: `${PATH}/${encodeURIComponent(templateCode)}`,
method: "DELETE",
});
}
|