locationSupportService.ts
3.94 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
import { ApiError, createApiClient } from "../lib/apiClient";
import type {
LocationSupportCreateInput,
LocationSupportDto,
LocationSupportUpdateInput,
} from "../types/locationSupport";
const api = createApiClient({
getToken: () => {
try {
return localStorage.getItem("access_token") ?? localStorage.getItem("token") ?? null;
} catch {
return null;
}
},
});
function normalizeSupportGetPayload(raw: unknown): LocationSupportDto | null {
if (raw == null) return null;
if (typeof raw !== "object") return null;
const o = raw as Record<string, unknown>;
const id = o.id != null ? String(o.id).trim() : "";
const supportPhone = String(o.supportPhone ?? "").trim();
const supportEmail = String(o.supportEmail ?? "").trim();
if (!id && !supportPhone && !supportEmail) return null;
return {
id,
locationId: o.locationId != null ? String(o.locationId) : null,
locationName: o.locationName != null ? String(o.locationName) : null,
supportPhone,
supportEmail,
};
}
export async function getLocationSupportByLocationId(
locationId: string,
signal?: AbortSignal,
): Promise<LocationSupportDto | null> {
const id = (locationId ?? "").trim();
if (!id) return null;
return api.requestJson<LocationSupportDto | null>({
path: "/location-support/by-location-id",
method: "GET",
query: { locationId: id },
signal,
});
}
function normalizeFirstSupportRecord(raw: unknown): LocationSupportDto | null {
if (!raw) return null;
if (Array.isArray(raw)) {
const first = raw[0] as LocationSupportDto | undefined;
return first ?? null;
}
if (typeof raw === "object") {
const o = raw as Record<string, unknown>;
if (Array.isArray(o.items)) {
const first = o.items[0] as LocationSupportDto | undefined;
return first ?? null;
}
if ("supportPhone" in o || "supportEmail" in o) {
return o as LocationSupportDto;
}
}
return null;
}
/** 兼容旧分页列表拉首条(若后端仍提供列表接口) */
export async function getGlobalLocationSupportFromList(signal?: AbortSignal): Promise<LocationSupportDto | null> {
const raw = await api.requestJson<unknown>({
path: "/location-support",
method: "GET",
query: {
SkipCount: 1,
MaxResultCount: 1,
},
signal,
});
return normalizeFirstSupportRecord(raw);
}
/**
* 接口 6:全局 Support(GET `/api/app/location-support/support`)
* 若路由未部署(404),回退到列表首条。
*/
export async function getGlobalLocationSupport(signal?: AbortSignal): Promise<LocationSupportDto | null> {
try {
const raw = await api.requestJson<unknown>({
path: "/location-support/support",
method: "GET",
signal,
});
return normalizeSupportGetPayload(raw);
} catch (e) {
if (e instanceof ApiError && e.status === 404) {
try {
return await getGlobalLocationSupportFromList(signal);
} catch (e2) {
if (e2 instanceof ApiError && e2.status === 404) return null;
throw e2;
}
}
throw e;
}
}
export async function createLocationSupport(input: LocationSupportCreateInput): Promise<LocationSupportDto> {
const saved = await api.requestJson<unknown>({
path: "/location-support",
method: "POST",
body: {
supportPhone: input.supportPhone,
supportEmail: input.supportEmail,
},
});
const normalized = normalizeSupportGetPayload(saved);
if (normalized) return normalized;
return saved as LocationSupportDto;
}
export async function updateLocationSupport(
id: string,
input: LocationSupportUpdateInput,
): Promise<LocationSupportDto> {
const saved = await api.requestJson<unknown>({
path: `/location-support/${encodeURIComponent(id)}`,
method: "PUT",
body: {
supportPhone: input.supportPhone,
supportEmail: input.supportEmail,
},
});
const normalized = normalizeSupportGetPayload(saved);
if (normalized) return normalized;
return saved as LocationSupportDto;
}