0e27ddc8
杨鑫
标签
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
import { createApiClient } from "../lib/apiClient";
import type {
LabelCreateInput,
LabelDto,
LabelGetListInput,
LabelUpdateInput,
PagedResultDto,
} from "../types/label";
const api = createApiClient({
getToken: () => {
try {
return localStorage.getItem("access_token") ?? localStorage.getItem("token") ?? null;
} catch {
return null;
}
},
});
const PATH = "/label";
|
540ac0e3
杨鑫
前端修改bug
|
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
|
function parseIdList(v: unknown): string[] | undefined {
if (!Array.isArray(v)) return undefined;
return [...new Set(v.map((x) => String(x).trim()).filter(Boolean))];
}
export function normalizeLabelDto(raw: unknown): LabelDto {
const r = (raw && typeof raw === "object" ? raw : {}) as Record<string, unknown>;
const regionIds = parseIdList(r.regionIds ?? r.RegionIds);
const groupIds = parseIdList(r.groupIds ?? r.GroupIds);
const mergedRegionIds = [...new Set([...(regionIds ?? []), ...(groupIds ?? [])])];
const appliedRaw = r.appliedRegionType ?? r.AppliedRegionType;
const appliedRegionType =
typeof appliedRaw === "string" && appliedRaw.trim()
? appliedRaw.trim().toUpperCase()
: mergedRegionIds.length > 0
? "SPECIFIED"
: null;
const regionRaw = r.region ?? r.Region;
return {
...(r as object),
id: String(r.id ?? r.Id ?? r.labelCode ?? r.LabelCode ?? ""),
regionIds: mergedRegionIds.length ? mergedRegionIds : regionIds,
groupIds: mergedRegionIds.length ? mergedRegionIds : groupIds,
appliedRegionType: appliedRegionType as LabelDto["appliedRegionType"],
region: typeof regionRaw === "string" ? regionRaw : null,
} as LabelDto;
}
function scopeBodyFromInput(input: LabelCreateInput | LabelUpdateInput): Record<string, unknown> {
const regionIds = [
...new Set(
[...(input.regionIds ?? []), ...(input.groupIds ?? [])]
.map((x) => String(x).trim())
.filter(Boolean),
),
];
const applied =
String(input.appliedRegionType ?? "").trim().toUpperCase() ||
(regionIds.length > 0 ? "SPECIFIED" : "");
const body: Record<string, unknown> = {};
if (applied === "ALL" || applied === "SPECIFIED") {
body.appliedRegionType = applied;
}
if (regionIds.length) {
body.regionIds = regionIds;
body.groupIds = regionIds;
} else if (applied === "ALL") {
body.regionIds = [];
body.groupIds = [];
}
return body;
}
|
0e27ddc8
杨鑫
标签
|
75
|
export async function getLabels(input: LabelGetListInput, signal?: AbortSignal): Promise<PagedResultDto<LabelDto>> {
|
540ac0e3
杨鑫
前端修改bug
|
76
|
const res = await api.requestJson<PagedResultDto<LabelDto> & { items?: unknown[]; Items?: unknown[] }>({
|
0e27ddc8
杨鑫
标签
|
77
78
79
80
81
82
83
|
path: PATH,
method: "GET",
query: {
SkipCount: input.skipCount,
MaxResultCount: input.maxResultCount,
Sorting: input.sorting,
Keyword: input.keyword,
|
540ac0e3
杨鑫
前端修改bug
|
84
85
|
PartnerId: input.partnerId,
GroupId: input.groupId,
|
0e27ddc8
杨鑫
标签
|
86
87
88
89
90
91
92
93
94
|
LocationId: input.locationId,
ProductId: input.productId,
LabelCategoryId: input.labelCategoryId,
LabelTypeId: input.labelTypeId,
TemplateCode: input.templateCode,
State: input.state,
},
signal,
});
|
540ac0e3
杨鑫
前端修改bug
|
95
96
97
98
99
100
101
|
const itemsRaw = res.items ?? res.Items ?? [];
const items = (Array.isArray(itemsRaw) ? itemsRaw : []).map((x) => normalizeLabelDto(x));
return {
...res,
items,
totalCount: res.totalCount ?? (res as { TotalCount?: number }).TotalCount ?? items.length,
};
|
0e27ddc8
杨鑫
标签
|
102
103
104
|
}
export async function getLabel(labelCode: string, signal?: AbortSignal): Promise<LabelDto> {
|
540ac0e3
杨鑫
前端修改bug
|
105
|
const raw = await api.requestJson<unknown>({
|
0e27ddc8
杨鑫
标签
|
106
107
108
109
|
path: `${PATH}/${encodeURIComponent(labelCode)}`,
method: "GET",
signal,
});
|
540ac0e3
杨鑫
前端修改bug
|
110
|
return normalizeLabelDto(raw);
|
0e27ddc8
杨鑫
标签
|
111
112
113
|
}
export async function createLabel(input: LabelCreateInput): Promise<LabelDto> {
|
540ac0e3
杨鑫
前端修改bug
|
114
|
const raw = await api.requestJson<unknown>({
|
0e27ddc8
杨鑫
标签
|
115
116
117
|
path: PATH,
method: "POST",
body: {
|
699ea6e8
杨鑫
完善打印逻辑
|
118
|
labelCode: String(input.labelCode ?? "").trim() || null,
|
0e27ddc8
杨鑫
标签
|
119
120
121
122
123
124
125
126
|
labelName: input.labelName,
templateCode: input.templateCode,
locationId: input.locationId,
labelCategoryId: input.labelCategoryId,
labelTypeId: input.labelTypeId,
productIds: input.productIds,
labelInfoJson: input.labelInfoJson,
state: input.state ?? true,
|
540ac0e3
杨鑫
前端修改bug
|
127
|
...scopeBodyFromInput(input),
|
0e27ddc8
杨鑫
标签
|
128
129
|
},
});
|
540ac0e3
杨鑫
前端修改bug
|
130
|
return normalizeLabelDto(raw);
|
0e27ddc8
杨鑫
标签
|
131
132
133
|
}
export async function updateLabel(labelCode: string, input: LabelUpdateInput): Promise<LabelDto> {
|
540ac0e3
杨鑫
前端修改bug
|
134
|
const raw = await api.requestJson<unknown>({
|
0e27ddc8
杨鑫
标签
|
135
136
137
138
139
140
141
142
143
144
145
|
path: `${PATH}/${encodeURIComponent(labelCode)}`,
method: "PUT",
body: {
labelName: input.labelName,
templateCode: input.templateCode,
locationId: input.locationId,
labelCategoryId: input.labelCategoryId,
labelTypeId: input.labelTypeId,
productIds: input.productIds,
labelInfoJson: input.labelInfoJson,
state: input.state ?? true,
|
540ac0e3
杨鑫
前端修改bug
|
146
|
...scopeBodyFromInput(input),
|
0e27ddc8
杨鑫
标签
|
147
148
|
},
});
|
540ac0e3
杨鑫
前端修改bug
|
149
|
return normalizeLabelDto(raw);
|
0e27ddc8
杨鑫
标签
|
150
151
152
153
154
155
156
157
|
}
export async function deleteLabel(labelCode: string): Promise<void> {
await api.requestJson<unknown>({
path: `${PATH}/${encodeURIComponent(labelCode)}`,
method: "DELETE",
});
}
|