Blame view

美国版/Food Labeling Management Platform/src/services/labelCategoryService.ts 4.67 KB
0e27ddc8   杨鑫   标签
1
  import { createApiClient } from "../lib/apiClient";
699ea6e8   杨鑫   完善打印逻辑
2
  import { serializeButtonAppearanceForApi } from "../lib/categoryButtonAppearance";
0e27ddc8   杨鑫   标签
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  import type {
    LabelCategoryCreateInput,
    LabelCategoryDto,
    LabelCategoryGetListInput,
    LabelCategoryUpdateInput,
    PagedResultDto,
  } from "../types/labelCategory";
  
  const api = createApiClient({
    getToken: () => {
      try {
        return localStorage.getItem("access_token") ?? localStorage.getItem("token") ?? null;
      } catch {
        return null;
      }
    },
  });
  
  const PATH = "/label-category";
  
ef6b3255   杨鑫   修改BUG
23
24
25
26
27
28
29
30
31
  function normalizeLabelCategoryDto(raw: unknown): LabelCategoryDto {
    const r = (raw && typeof raw === "object" ? raw : {}) as Record<string, unknown>;
    const noRaw = r.noOfLabels ?? r.NoOfLabels;
    const noOfLabels =
      typeof noRaw === "number" && Number.isFinite(noRaw)
        ? noRaw
        : typeof noRaw === "string" && noRaw.trim() !== "" && Number.isFinite(Number(noRaw))
          ? Number(noRaw)
          : null;
540ac0e3   杨鑫   前端修改bug
32
33
34
35
36
37
38
39
    const orderRaw = r.orderNum ?? r.OrderNum;
    const orderNum =
      typeof orderRaw === "number" && Number.isFinite(orderRaw)
        ? orderRaw
        : typeof orderRaw === "string" && orderRaw.trim() !== "" && Number.isFinite(Number(orderRaw))
          ? Number(orderRaw)
          : null;
    return { ...(r as object), id: String(r.id ?? r.Id ?? ""), noOfLabels, orderNum } as LabelCategoryDto;
ef6b3255   杨鑫   修改BUG
40
41
  }
  
0e27ddc8   杨鑫   标签
42
  export async function getLabelCategories(input: LabelCategoryGetListInput, signal?: AbortSignal): Promise<PagedResultDto<LabelCategoryDto>> {
ef6b3255   杨鑫   修改BUG
43
    const res = await api.requestJson<PagedResultDto<LabelCategoryDto> & { items?: unknown[]; Items?: unknown[] }>({
0e27ddc8   杨鑫   标签
44
45
46
47
48
49
50
51
      path: PATH,
      method: "GET",
      query: {
        SkipCount: input.skipCount,
        MaxResultCount: input.maxResultCount,
        Sorting: input.sorting,
        Keyword: input.keyword,
        State: input.state,
83ccb207   杨鑫   最新
52
53
        GroupId: input.groupId,
        LocationId: input.locationId,
0e27ddc8   杨鑫   标签
54
55
56
      },
      signal,
    });
ef6b3255   杨鑫   修改BUG
57
58
59
    const itemsRaw = res.items ?? res.Items ?? [];
    const items = (Array.isArray(itemsRaw) ? itemsRaw : []).map((x) => normalizeLabelCategoryDto(x));
    return { ...res, items, totalCount: res.totalCount ?? (res as { TotalCount?: number }).TotalCount ?? items.length };
0e27ddc8   杨鑫   标签
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  }
  
  export async function getLabelCategory(id: string, signal?: AbortSignal): Promise<LabelCategoryDto> {
    return api.requestJson<LabelCategoryDto>({
      path: `${PATH}/${encodeURIComponent(id)}`,
      method: "GET",
      signal,
    });
  }
  
  export async function createLabelCategory(input: LabelCategoryCreateInput): Promise<LabelCategoryDto> {
    return api.requestJson<LabelCategoryDto>({
      path: PATH,
      method: "POST",
      body: {
699ea6e8   杨鑫   完善打印逻辑
75
        categoryCode: String(input.categoryCode ?? "").trim() || null,
0e27ddc8   杨鑫   标签
76
77
        categoryName: input.categoryName,
        categoryPhotoUrl: input.categoryPhotoUrl,
699ea6e8   杨鑫   完善打印逻辑
78
79
80
81
82
        displayText: (input.displayText ?? "").trim() || null,
        buttonBgColor: (input.buttonBgColor ?? "").trim() || null,
        buttonImageUrl: (input.buttonImageUrl ?? "").trim() || null,
        buttonAppearance: serializeButtonAppearanceForApi(input.buttonAppearance),
        buttonStyleJson: (input.buttonStyleJson ?? "").trim() || null,
0e27ddc8   杨鑫   标签
83
84
        state: input.state ?? true,
        orderNum: input.orderNum,
ef6b3255   杨鑫   修改BUG
85
86
87
88
89
        availabilityType: String(input.availabilityType ?? "ALL").trim().toUpperCase() || "ALL",
        locationIds:
          Array.isArray(input.locationIds) && input.locationIds.length
            ? [...new Set(input.locationIds.map((x) => String(x).trim()).filter(Boolean))]
            : null,
0e27ddc8   杨鑫   标签
90
91
92
93
94
95
96
97
98
      },
    });
  }
  
  export async function updateLabelCategory(id: string, input: LabelCategoryUpdateInput): Promise<LabelCategoryDto> {
    return api.requestJson<LabelCategoryDto>({
      path: `${PATH}/${encodeURIComponent(id)}`,
      method: "PUT",
      body: {
699ea6e8   杨鑫   完善打印逻辑
99
        categoryCode: String(input.categoryCode ?? "").trim() || null,
0e27ddc8   杨鑫   标签
100
101
        categoryName: input.categoryName,
        categoryPhotoUrl: input.categoryPhotoUrl,
699ea6e8   杨鑫   完善打印逻辑
102
103
104
105
106
        displayText: (input.displayText ?? "").trim() || null,
        buttonBgColor: (input.buttonBgColor ?? "").trim() || null,
        buttonImageUrl: (input.buttonImageUrl ?? "").trim() || null,
        buttonAppearance: serializeButtonAppearanceForApi(input.buttonAppearance),
        buttonStyleJson: (input.buttonStyleJson ?? "").trim() || null,
0e27ddc8   杨鑫   标签
107
108
        state: input.state ?? true,
        orderNum: input.orderNum,
ef6b3255   杨鑫   修改BUG
109
110
111
112
113
        availabilityType: String(input.availabilityType ?? "ALL").trim().toUpperCase() || "ALL",
        locationIds:
          Array.isArray(input.locationIds) && input.locationIds.length
            ? [...new Set(input.locationIds.map((x) => String(x).trim()).filter(Boolean))]
            : null,
0e27ddc8   杨鑫   标签
114
115
116
117
118
119
120
121
122
123
      },
    });
  }
  
  export async function deleteLabelCategory(id: string): Promise<void> {
    await api.requestJson<unknown>({
      path: `${PATH}/${encodeURIComponent(id)}`,
      method: "DELETE",
    });
  }