http.ts
10 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import { apiUsesSameOrigin, appendLoopbackConnectHint, getApiBase } from "@/config/api";
import { prepareImageForUpload } from "@/utils/prepareUploadImage";
const MP_TOKEN_KEY = "mp_api_token";
const MP_CLIENT_KEY = "mp_client_id";
/** 与 mpSession 一致:guest | weixin | phone */
export const MP_LOGIN_CHANNEL_KEY = "mp_login_channel_v1";
export function getMpToken(): string {
try {
return (uni.getStorageSync(MP_TOKEN_KEY) as string) || "";
} catch {
return "";
}
}
export function setMpAuth(clientId: string, token: string): void {
resetMpUnauthorizedRelaunchGuard();
uni.setStorageSync(MP_CLIENT_KEY, clientId);
uni.setStorageSync(MP_TOKEN_KEY, token);
}
export function clearMpAuth(): void {
try {
uni.removeStorageSync(MP_TOKEN_KEY);
} catch {
/* ignore */
}
}
/** 清除小程序端登录态(含设备标识,下次将分配新游客) */
export function clearMpSession(): void {
clearMpAuth();
try {
uni.removeStorageSync(MP_CLIENT_KEY);
} catch {
/* ignore */
}
try {
uni.removeStorageSync(MP_LOGIN_CHANNEL_KEY);
} catch {
/* ignore */
}
}
function authHeaders(): Record<string, string> {
const t = getMpToken();
return t ? { Authorization: `Bearer ${t}` } : {};
}
/** App 等端对 JSON POST 需传字符串,否则后端可能收不到 body */
function jsonBody(data?: Record<string, unknown>): string {
return JSON.stringify(data ?? {});
}
/** uni.request fail 多为 { errMsg },转成 Error 便于统一提示 */
function rejectAsRequestError(e: unknown, reject: (reason?: unknown) => void): void {
if (e instanceof Error) {
reject(e);
return;
}
if (e && typeof e === "object" && "errMsg" in e) {
const m = (e as { errMsg?: string }).errMsg;
reject(new Error(typeof m === "string" && m.trim() ? m : "网络请求失败"));
return;
}
reject(new Error(typeof e === "string" && e ? e : "网络请求失败"));
}
export interface ApiEnvelope<T> {
/** 后端多为 number;个别网关/序列化可能为字符串 */
code: number | string;
msg: string;
data: T;
}
function isApiSuccessCode(code: unknown): boolean {
return code === 0 || code === "0";
}
/** 后端小程序鉴权失败(token 无效、过期、未登录等) */
function isMpUnauthorizedApiCode(code: unknown): boolean {
return code === 401 || code === "401";
}
let mpUnauthorizedRelaunchScheduled = false;
function resetMpUnauthorizedRelaunchGuard(): void {
mpUnauthorizedRelaunchScheduled = false;
}
function handleMpApiUnauthorized(): void {
if (mpUnauthorizedRelaunchScheduled) {
return;
}
mpUnauthorizedRelaunchScheduled = true;
clearMpSession();
uni.showToast({
title: "登录已过期,请重新登录",
icon: "none",
duration: 2000,
});
uni.reLaunch({ url: "/pages/login/login" });
}
/** 已在 {@link get}/{@link post}/{@link uploadImage} 内触发清 token 与跳转登录;catch 中可据此跳过重复提示 */
export class MpSessionExpiredError extends Error {
override readonly name = "MpSessionExpiredError";
constructor(message = "登录已失效") {
super(message);
}
}
export function isMpSessionExpiredError(e: unknown): boolean {
return e instanceof MpSessionExpiredError;
}
function isApiEnvelopeShape(body: unknown): body is ApiEnvelope<unknown> {
return !!body && typeof body === "object" && "code" in body;
}
/** App 等端偶发把 JSON 当字符串返回,不解析会导致「响应格式错误」 */
function normalizeUniResponseData(data: unknown): unknown {
if (typeof data !== "string") {
return data;
}
const s = data.trim();
if (!s) {
return data;
}
const c0 = s[0];
if (c0 !== "{" && c0 !== "[") {
return data;
}
try {
return JSON.parse(s) as unknown;
} catch {
return data;
}
}
/** 从接口 reject 的 envelope 或 Error 中取出可读提示(不含登录失效) */
export function apiFailureMessage(e: unknown, fallback = "请求失败"): string {
if (isMpSessionExpiredError(e)) {
return "";
}
if (e && typeof e === "object" && "msg" in e) {
const m = (e as { msg?: unknown }).msg;
if (typeof m === "string" && m.trim()) {
return m.trim();
}
}
if (e instanceof Error && e.message.trim()) {
return appendLoopbackConnectHint(e.message.trim());
}
return fallback;
}
/** 弱网 / 真机首次建连可能较慢;过短易误报 request:fail timeout */
const REQUEST_TIMEOUT_MS = 60000;
function toQueryString(
query?: Record<string, string | number | undefined>
): string {
if (!query) {
return "";
}
const parts: string[] = [];
for (const [k, v] of Object.entries(query)) {
if (v === undefined || v === "") {
continue;
}
parts.push(`${encodeURIComponent(k)}=${encodeURIComponent(String(v))}`);
}
return parts.join("&");
}
function rejectIfBadHttpStatus(
res: UniApp.RequestSuccessCallbackResult,
reject: (reason?: unknown) => void
): boolean {
const sc = res.statusCode;
if (sc !== undefined && sc !== 200) {
if (sc === 401) {
const body = normalizeUniResponseData(res.data as unknown);
if (isApiEnvelopeShape(body) && isMpUnauthorizedApiCode(body.code)) {
handleMpApiUnauthorized();
reject(new MpSessionExpiredError());
return true;
}
}
reject(new Error(`网络异常(HTTP ${sc})`));
return true;
}
return false;
}
export function get<T>(
path: string,
query?: Record<string, string | number | undefined>
): Promise<ApiEnvelope<T>> {
const base = getApiBase();
if (!base && !apiUsesSameOrigin()) {
return Promise.reject(new Error("API 未配置"));
}
let url = path.startsWith("http") ? path : `${base}${path}`;
if (query && Object.keys(query).length) {
const s = toQueryString(query);
if (s) {
url += (url.includes("?") ? "&" : "?") + s;
}
}
return new Promise((resolve, reject) => {
uni.request({
url,
method: "GET",
header: { ...authHeaders() },
timeout: REQUEST_TIMEOUT_MS,
dataType: "json",
success: (res) => {
if (rejectIfBadHttpStatus(res, reject)) {
return;
}
const body = normalizeUniResponseData(res.data as unknown);
if (!isApiEnvelopeShape(body)) {
reject(new Error("响应格式错误"));
return;
}
if (!isApiSuccessCode(body.code)) {
if (isMpUnauthorizedApiCode(body.code)) {
handleMpApiUnauthorized();
reject(new MpSessionExpiredError());
return;
}
reject(body);
return;
}
resolve(body as ApiEnvelope<T>);
},
fail: (e) => rejectAsRequestError(e, reject),
});
});
}
export function post<T>(path: string, data?: Record<string, unknown>): Promise<ApiEnvelope<T>> {
const base = getApiBase();
if (!base && !apiUsesSameOrigin()) {
return Promise.reject(new Error("API 未配置"));
}
const url = path.startsWith("http") ? path : `${base}${path}`;
return new Promise((resolve, reject) => {
uni.request({
url,
method: "POST",
header: {
"Content-Type": "application/json",
...authHeaders(),
},
data: jsonBody(data),
timeout: REQUEST_TIMEOUT_MS,
dataType: "json",
success: (res) => {
if (rejectIfBadHttpStatus(res, reject)) {
return;
}
const body = normalizeUniResponseData(res.data as unknown);
if (!isApiEnvelopeShape(body)) {
reject(new Error("响应格式错误"));
return;
}
if (!isApiSuccessCode(body.code)) {
if (isMpUnauthorizedApiCode(body.code)) {
handleMpApiUnauthorized();
reject(new MpSessionExpiredError());
return;
}
reject(body);
return;
}
resolve(body as ApiEnvelope<T>);
},
fail: (e) => rejectAsRequestError(e, reject),
});
});
}
export function uploadImage(filePath: string): Promise<string> {
const base = getApiBase();
if (!base && !apiUsesSameOrigin()) {
return Promise.reject(new Error("API 未配置"));
}
return new Promise((resolve, reject) => {
void (async () => {
let path = filePath;
try {
path = await prepareImageForUpload(filePath);
} catch {
path = filePath;
}
uni.uploadFile({
url: `${base}/api/v1/upload`,
filePath: path,
name: "file",
header: { ...authHeaders() },
timeout: REQUEST_TIMEOUT_MS,
success: (res) => {
const code = (res as { statusCode?: number }).statusCode;
const rawEarly = res.data || "";
if (code !== undefined && code !== 200) {
if (code === 401) {
try {
const parsed =
typeof rawEarly === "string"
? (JSON.parse(rawEarly || "{}") as unknown)
: rawEarly;
if (isApiEnvelopeShape(parsed) && isMpUnauthorizedApiCode(parsed.code)) {
handleMpApiUnauthorized();
reject(new MpSessionExpiredError());
return;
}
} catch {
/* fall through */
}
}
reject(new Error(`上传失败(HTTP ${code})`));
return;
}
const raw = res.data || "";
try {
const body = typeof raw === "string" ? (JSON.parse(raw || "{}") as ApiEnvelope<{ url: string }>) : raw;
if (!isApiEnvelopeShape(body) || !isApiSuccessCode(body.code)) {
if (isApiEnvelopeShape(body) && isMpUnauthorizedApiCode(body.code)) {
handleMpApiUnauthorized();
reject(new MpSessionExpiredError());
return;
}
reject(body);
return;
}
resolve(body.data.url);
} catch {
const tip = typeof raw === "string" && raw.length < 120 ? raw : "上传解析失败,请检查接口与登录态";
reject(new Error(tip));
}
},
fail: (e) => rejectAsRequestError(e, reject),
});
})();
});
}