@be-link/request 1.45.1-beta.2 → 1.45.1-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +187 -65
- package/dist/application/clients/LiveClient.d.ts +31 -7
- package/dist/application/clients/LiveClient.d.ts.map +1 -1
- package/dist/application/interceptors/TokenInterceptor.d.ts +2 -0
- package/dist/application/interceptors/TokenInterceptor.d.ts.map +1 -1
- package/dist/core/constants/index.d.ts +5 -0
- package/dist/core/constants/index.d.ts.map +1 -1
- package/dist/core/types/config.types.d.ts +2 -0
- package/dist/core/types/config.types.d.ts.map +1 -1
- package/dist/index.cjs.js +839 -1696
- package/dist/index.esm.js +839 -1696
- package/dist/infrastructure/services/EncryptionService.d.ts.map +1 -1
- package/dist/infrastructure/services/TimeSyncService.d.ts +1 -2
- package/dist/infrastructure/services/TimeSyncService.d.ts.map +1 -1
- package/package.json +6 -2
package/dist/index.esm.js
CHANGED
|
@@ -4,7 +4,7 @@ import CryptoJS from 'crypto-js';
|
|
|
4
4
|
/**
|
|
5
5
|
* HTTP 状态码常量
|
|
6
6
|
*/
|
|
7
|
-
|
|
7
|
+
const HTTP_STATUS = {
|
|
8
8
|
OK: 200,
|
|
9
9
|
CREATED: 201,
|
|
10
10
|
NO_CONTENT: 204,
|
|
@@ -40,24 +40,29 @@ var ContentType;
|
|
|
40
40
|
/**
|
|
41
41
|
* 默认超时时间
|
|
42
42
|
*/
|
|
43
|
-
|
|
43
|
+
const DEFAULT_TIMEOUT = 8000;
|
|
44
44
|
/**
|
|
45
45
|
* Token Header 名称
|
|
46
46
|
*/
|
|
47
|
-
|
|
47
|
+
const TOKEN_HEADER = 'x-belink-authorization';
|
|
48
|
+
/**
|
|
49
|
+
* UserID Header 名称
|
|
50
|
+
*/
|
|
51
|
+
const USERID_HEADER = 'x-belink-userid';
|
|
48
52
|
/**
|
|
49
53
|
* 时间同步 Header
|
|
50
54
|
*/
|
|
51
|
-
|
|
55
|
+
const TIME_SYNC_HEADERS = {
|
|
52
56
|
SERVER_TIME: 'x-belink-serverTime',
|
|
53
57
|
TIME_DIFF: 'x-belink-timeDiff',
|
|
54
58
|
};
|
|
55
59
|
/**
|
|
56
60
|
* LocalStorage 键
|
|
57
61
|
*/
|
|
58
|
-
|
|
62
|
+
const STORAGE_KEYS = {
|
|
59
63
|
TOKEN: 'token',
|
|
60
64
|
SERVER_TIME: 'x-belink-serverTime',
|
|
65
|
+
CLIENT_TIME: 'x-belink-clientTime',
|
|
61
66
|
TIME_DIFF: 'x-belink-timeDiff',
|
|
62
67
|
};
|
|
63
68
|
|
|
@@ -67,9 +72,9 @@ var STORAGE_KEYS = {
|
|
|
67
72
|
*/
|
|
68
73
|
function request(config) {
|
|
69
74
|
// 创建独立实例,避免拦截器泄漏
|
|
70
|
-
|
|
75
|
+
const instance = axios.create();
|
|
71
76
|
// 解析 HTTP 方法
|
|
72
|
-
|
|
77
|
+
const method = config.method || (config.data ? 'post' : 'get');
|
|
73
78
|
// 应用临时拦截器
|
|
74
79
|
if (config.requestInterceptors && typeof config.requestInterceptors === 'function') {
|
|
75
80
|
instance.interceptors.request.use(config.requestInterceptors);
|
|
@@ -77,18 +82,18 @@ function request(config) {
|
|
|
77
82
|
if (config.responseInterceptors && typeof config.responseInterceptors === 'function') {
|
|
78
83
|
instance.interceptors.response.use(config.responseInterceptors);
|
|
79
84
|
}
|
|
80
|
-
|
|
81
|
-
return new Promise(
|
|
85
|
+
const timeout = config.timeout || DEFAULT_TIMEOUT;
|
|
86
|
+
return new Promise((resolve, reject) => {
|
|
82
87
|
instance
|
|
83
88
|
.request({
|
|
84
|
-
method
|
|
89
|
+
method,
|
|
85
90
|
url: config.url,
|
|
86
91
|
headers: config.headers || {},
|
|
87
92
|
data: config.data,
|
|
88
93
|
params: config.params,
|
|
89
|
-
timeout
|
|
94
|
+
timeout,
|
|
90
95
|
})
|
|
91
|
-
.then(
|
|
96
|
+
.then((result) => {
|
|
92
97
|
if (result.status === 200) {
|
|
93
98
|
resolve(result.data);
|
|
94
99
|
}
|
|
@@ -96,140 +101,15 @@ function request(config) {
|
|
|
96
101
|
reject(result.data);
|
|
97
102
|
}
|
|
98
103
|
})
|
|
99
|
-
.catch(
|
|
100
|
-
var _a, _b;
|
|
104
|
+
.catch((reason) => {
|
|
101
105
|
if (config.catchCallback && typeof config.catchCallback === 'function') {
|
|
102
106
|
config.catchCallback(reason);
|
|
103
107
|
}
|
|
104
|
-
reject(
|
|
108
|
+
reject(reason?.response?.data ?? reason);
|
|
105
109
|
});
|
|
106
110
|
});
|
|
107
111
|
}
|
|
108
112
|
|
|
109
|
-
/******************************************************************************
|
|
110
|
-
Copyright (c) Microsoft Corporation.
|
|
111
|
-
|
|
112
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
113
|
-
purpose with or without fee is hereby granted.
|
|
114
|
-
|
|
115
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
116
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
117
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
118
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
119
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
120
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
121
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
122
|
-
***************************************************************************** */
|
|
123
|
-
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
|
|
124
|
-
|
|
125
|
-
var extendStatics = function(d, b) {
|
|
126
|
-
extendStatics = Object.setPrototypeOf ||
|
|
127
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
128
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
129
|
-
return extendStatics(d, b);
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
function __extends$1(d, b) {
|
|
133
|
-
if (typeof b !== "function" && b !== null)
|
|
134
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
135
|
-
extendStatics(d, b);
|
|
136
|
-
function __() { this.constructor = d; }
|
|
137
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
var __assign$4 = function() {
|
|
141
|
-
__assign$4 = Object.assign || function __assign(t) {
|
|
142
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
143
|
-
s = arguments[i];
|
|
144
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
145
|
-
}
|
|
146
|
-
return t;
|
|
147
|
-
};
|
|
148
|
-
return __assign$4.apply(this, arguments);
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
function __awaiter$3(thisArg, _arguments, P, generator) {
|
|
152
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
153
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
154
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
155
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
156
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
157
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
function __generator$3(thisArg, body) {
|
|
162
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
163
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
164
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
165
|
-
function step(op) {
|
|
166
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
167
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
168
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
169
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
170
|
-
switch (op[0]) {
|
|
171
|
-
case 0: case 1: t = op; break;
|
|
172
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
173
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
174
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
175
|
-
default:
|
|
176
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
177
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
178
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
179
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
180
|
-
if (t[2]) _.ops.pop();
|
|
181
|
-
_.trys.pop(); continue;
|
|
182
|
-
}
|
|
183
|
-
op = body.call(thisArg, _);
|
|
184
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
185
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
function __values(o) {
|
|
190
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
191
|
-
if (m) return m.call(o);
|
|
192
|
-
if (o && typeof o.length === "number") return {
|
|
193
|
-
next: function () {
|
|
194
|
-
if (o && i >= o.length) o = void 0;
|
|
195
|
-
return { value: o && o[i++], done: !o };
|
|
196
|
-
}
|
|
197
|
-
};
|
|
198
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
function __read(o, n) {
|
|
202
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
203
|
-
if (!m) return o;
|
|
204
|
-
var i = m.call(o), r, ar = [], e;
|
|
205
|
-
try {
|
|
206
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
207
|
-
}
|
|
208
|
-
catch (error) { e = { error: error }; }
|
|
209
|
-
finally {
|
|
210
|
-
try {
|
|
211
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
212
|
-
}
|
|
213
|
-
finally { if (e) throw e.error; }
|
|
214
|
-
}
|
|
215
|
-
return ar;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
function __spreadArray$1(to, from, pack) {
|
|
219
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
220
|
-
if (ar || !(i in from)) {
|
|
221
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
222
|
-
ar[i] = from[i];
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
229
|
-
var e = new Error(message);
|
|
230
|
-
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
231
|
-
};
|
|
232
|
-
|
|
233
113
|
/**
|
|
234
114
|
* HTTP 方法枚举
|
|
235
115
|
*/
|
|
@@ -245,660 +125,376 @@ var HttpMethod;
|
|
|
245
125
|
/**
|
|
246
126
|
* 基础错误类
|
|
247
127
|
*/
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
writable: true,
|
|
256
|
-
value: void 0
|
|
257
|
-
});
|
|
258
|
-
Object.defineProperty(_this, "originalError", {
|
|
259
|
-
enumerable: true,
|
|
260
|
-
configurable: true,
|
|
261
|
-
writable: true,
|
|
262
|
-
value: void 0
|
|
263
|
-
});
|
|
264
|
-
_this.name = _this.constructor.name;
|
|
265
|
-
_this.code = code;
|
|
266
|
-
_this.originalError = originalError;
|
|
267
|
-
Error.captureStackTrace(_this, _this.constructor);
|
|
268
|
-
return _this;
|
|
128
|
+
class BaseError extends Error {
|
|
129
|
+
constructor(message, code, originalError) {
|
|
130
|
+
super(message);
|
|
131
|
+
this.name = this.constructor.name;
|
|
132
|
+
this.code = code;
|
|
133
|
+
this.originalError = originalError;
|
|
134
|
+
Error.captureStackTrace(this, this.constructor);
|
|
269
135
|
}
|
|
270
|
-
|
|
271
|
-
}(Error));
|
|
136
|
+
}
|
|
272
137
|
/**
|
|
273
138
|
* HTTP 错误
|
|
274
139
|
*/
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
enumerable: true,
|
|
281
|
-
configurable: true,
|
|
282
|
-
writable: true,
|
|
283
|
-
value: void 0
|
|
284
|
-
});
|
|
285
|
-
Object.defineProperty(_this, "data", {
|
|
286
|
-
enumerable: true,
|
|
287
|
-
configurable: true,
|
|
288
|
-
writable: true,
|
|
289
|
-
value: void 0
|
|
290
|
-
});
|
|
291
|
-
_this.status = status;
|
|
292
|
-
_this.data = data;
|
|
293
|
-
return _this;
|
|
140
|
+
class HttpError extends BaseError {
|
|
141
|
+
constructor(message, status, data, originalError) {
|
|
142
|
+
super(message, 'HTTP_ERROR', originalError);
|
|
143
|
+
this.status = status;
|
|
144
|
+
this.data = data;
|
|
294
145
|
}
|
|
295
|
-
|
|
296
|
-
}(BaseError));
|
|
146
|
+
}
|
|
297
147
|
/**
|
|
298
148
|
* 认证错误
|
|
299
149
|
*/
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
}
|
|
306
|
-
return AuthError;
|
|
307
|
-
}(BaseError));
|
|
150
|
+
class AuthError extends BaseError {
|
|
151
|
+
constructor(message = 'Unauthorized', originalError) {
|
|
152
|
+
super(message, 'AUTH_ERROR', originalError);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
308
155
|
/**
|
|
309
156
|
* 超时错误
|
|
310
157
|
*/
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
Object.defineProperty(_this, "timeout", {
|
|
316
|
-
enumerable: true,
|
|
317
|
-
configurable: true,
|
|
318
|
-
writable: true,
|
|
319
|
-
value: void 0
|
|
320
|
-
});
|
|
321
|
-
_this.timeout = timeout;
|
|
322
|
-
return _this;
|
|
158
|
+
class TimeoutError extends BaseError {
|
|
159
|
+
constructor(message, timeout, originalError) {
|
|
160
|
+
super(message, 'TIMEOUT_ERROR', originalError);
|
|
161
|
+
this.timeout = timeout;
|
|
323
162
|
}
|
|
324
|
-
|
|
325
|
-
}(BaseError));
|
|
163
|
+
}
|
|
326
164
|
/**
|
|
327
165
|
* 配置错误
|
|
328
166
|
*/
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
return _super.call(this, message, 'CONFIG_ERROR', originalError) || this;
|
|
167
|
+
class ConfigError extends BaseError {
|
|
168
|
+
constructor(message, originalError) {
|
|
169
|
+
super(message, 'CONFIG_ERROR', originalError);
|
|
333
170
|
}
|
|
334
|
-
|
|
335
|
-
}(BaseError));
|
|
171
|
+
}
|
|
336
172
|
/**
|
|
337
173
|
* 网络错误
|
|
338
174
|
*/
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
if (message === void 0) { message = 'Network error'; }
|
|
343
|
-
return _super.call(this, message, 'NETWORK_ERROR', originalError) || this;
|
|
175
|
+
class NetworkError extends BaseError {
|
|
176
|
+
constructor(message = 'Network error', originalError) {
|
|
177
|
+
super(message, 'NETWORK_ERROR', originalError);
|
|
344
178
|
}
|
|
345
|
-
|
|
346
|
-
}(BaseError));
|
|
179
|
+
}
|
|
347
180
|
|
|
348
181
|
/**
|
|
349
182
|
* Axios 适配器
|
|
350
183
|
* 封装 Axios 实例,提供统一的请求接口
|
|
351
184
|
*/
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
value: void 0
|
|
359
|
-
});
|
|
360
|
-
Object.defineProperty(this, "interceptorIds", {
|
|
361
|
-
enumerable: true,
|
|
362
|
-
configurable: true,
|
|
363
|
-
writable: true,
|
|
364
|
-
value: {
|
|
365
|
-
request: [],
|
|
366
|
-
response: [],
|
|
367
|
-
}
|
|
368
|
-
});
|
|
185
|
+
class AxiosAdapter {
|
|
186
|
+
constructor(baseURL, timeout) {
|
|
187
|
+
this.interceptorIds = {
|
|
188
|
+
request: [],
|
|
189
|
+
response: [],
|
|
190
|
+
};
|
|
369
191
|
this.instance = axios.create({
|
|
370
|
-
baseURL
|
|
192
|
+
baseURL,
|
|
371
193
|
timeout: timeout || DEFAULT_TIMEOUT,
|
|
372
194
|
});
|
|
373
195
|
}
|
|
374
196
|
/**
|
|
375
197
|
* 发起请求
|
|
376
198
|
*/
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
return __awaiter$3(this, void 0, void 0, function () {
|
|
383
|
-
var headers, axiosConfig, response, error_1;
|
|
384
|
-
return __generator$3(this, function (_a) {
|
|
385
|
-
switch (_a.label) {
|
|
386
|
-
case 0:
|
|
387
|
-
headers = __assign$4({}, config.headers);
|
|
388
|
-
// 当有请求体时,默认设置 Content-Type 为 application/json
|
|
389
|
-
if (config.data && !headers['Content-Type'] && !headers['content-type']) {
|
|
390
|
-
headers['Content-Type'] = ContentType.JSON;
|
|
391
|
-
}
|
|
392
|
-
axiosConfig = __assign$4({ method: config.method || (config.data ? HttpMethod.POST : HttpMethod.GET), url: config.url, headers: headers, data: config.data, params: config.params, timeout: config.timeout || DEFAULT_TIMEOUT }, config.axiosConfig);
|
|
393
|
-
_a.label = 1;
|
|
394
|
-
case 1:
|
|
395
|
-
_a.trys.push([1, 3, , 4]);
|
|
396
|
-
return [4 /*yield*/, this.instance.request(axiosConfig)];
|
|
397
|
-
case 2:
|
|
398
|
-
response = _a.sent();
|
|
399
|
-
if (response.status === 200) {
|
|
400
|
-
return [2 /*return*/, response.data];
|
|
401
|
-
}
|
|
402
|
-
throw new HttpError("Request failed with status ".concat(response.status), response.status, response.data);
|
|
403
|
-
case 3:
|
|
404
|
-
error_1 = _a.sent();
|
|
405
|
-
throw this.handleError(error_1);
|
|
406
|
-
case 4: return [2 /*return*/];
|
|
407
|
-
}
|
|
408
|
-
});
|
|
409
|
-
});
|
|
199
|
+
async request(config) {
|
|
200
|
+
const headers = { ...config.headers };
|
|
201
|
+
// 当有请求体时,默认设置 Content-Type 为 application/json
|
|
202
|
+
if (config.data && !headers['Content-Type'] && !headers['content-type']) {
|
|
203
|
+
headers['Content-Type'] = ContentType.JSON;
|
|
410
204
|
}
|
|
411
|
-
|
|
205
|
+
const axiosConfig = {
|
|
206
|
+
method: config.method || (config.data ? HttpMethod.POST : HttpMethod.GET),
|
|
207
|
+
url: config.url,
|
|
208
|
+
headers,
|
|
209
|
+
data: config.data,
|
|
210
|
+
params: config.params,
|
|
211
|
+
timeout: config.timeout || DEFAULT_TIMEOUT,
|
|
212
|
+
...config.axiosConfig,
|
|
213
|
+
};
|
|
214
|
+
try {
|
|
215
|
+
const response = await this.instance.request(axiosConfig);
|
|
216
|
+
if (response.status === 200) {
|
|
217
|
+
return response.data;
|
|
218
|
+
}
|
|
219
|
+
throw new HttpError(`Request failed with status ${response.status}`, response.status, response.data);
|
|
220
|
+
}
|
|
221
|
+
catch (error) {
|
|
222
|
+
throw this.handleError(error);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
412
225
|
/**
|
|
413
226
|
* 添加请求拦截器
|
|
414
227
|
* @returns 拦截器 ID
|
|
415
228
|
*/
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
var id = this.instance.interceptors.request.use(onFulfilled, onRejected);
|
|
422
|
-
this.interceptorIds.request.push(id);
|
|
423
|
-
return id;
|
|
424
|
-
}
|
|
425
|
-
});
|
|
229
|
+
addRequestInterceptor(onFulfilled, onRejected) {
|
|
230
|
+
const id = this.instance.interceptors.request.use(onFulfilled, onRejected);
|
|
231
|
+
this.interceptorIds.request.push(id);
|
|
232
|
+
return id;
|
|
233
|
+
}
|
|
426
234
|
/**
|
|
427
235
|
* 添加响应拦截器
|
|
428
236
|
* @returns 拦截器 ID
|
|
429
237
|
*/
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
var id = this.instance.interceptors.response.use(onFulfilled, onRejected);
|
|
436
|
-
this.interceptorIds.response.push(id);
|
|
437
|
-
return id;
|
|
438
|
-
}
|
|
439
|
-
});
|
|
238
|
+
addResponseInterceptor(onFulfilled, onRejected) {
|
|
239
|
+
const id = this.instance.interceptors.response.use(onFulfilled, onRejected);
|
|
240
|
+
this.interceptorIds.response.push(id);
|
|
241
|
+
return id;
|
|
242
|
+
}
|
|
440
243
|
/**
|
|
441
244
|
* 移除请求拦截器
|
|
442
245
|
*/
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
this.instance.interceptors.request.eject(id);
|
|
449
|
-
var index = this.interceptorIds.request.indexOf(id);
|
|
450
|
-
if (index > -1) {
|
|
451
|
-
this.interceptorIds.request.splice(index, 1);
|
|
452
|
-
}
|
|
246
|
+
removeRequestInterceptor(id) {
|
|
247
|
+
this.instance.interceptors.request.eject(id);
|
|
248
|
+
const index = this.interceptorIds.request.indexOf(id);
|
|
249
|
+
if (index > -1) {
|
|
250
|
+
this.interceptorIds.request.splice(index, 1);
|
|
453
251
|
}
|
|
454
|
-
}
|
|
252
|
+
}
|
|
455
253
|
/**
|
|
456
254
|
* 移除响应拦截器
|
|
457
255
|
*/
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
this.instance.interceptors.response.eject(id);
|
|
464
|
-
var index = this.interceptorIds.response.indexOf(id);
|
|
465
|
-
if (index > -1) {
|
|
466
|
-
this.interceptorIds.response.splice(index, 1);
|
|
467
|
-
}
|
|
256
|
+
removeResponseInterceptor(id) {
|
|
257
|
+
this.instance.interceptors.response.eject(id);
|
|
258
|
+
const index = this.interceptorIds.response.indexOf(id);
|
|
259
|
+
if (index > -1) {
|
|
260
|
+
this.interceptorIds.response.splice(index, 1);
|
|
468
261
|
}
|
|
469
|
-
}
|
|
262
|
+
}
|
|
470
263
|
/**
|
|
471
264
|
* 清理所有拦截器
|
|
472
265
|
*/
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
this.interceptorIds.response.forEach(function (id) {
|
|
483
|
-
_this.instance.interceptors.response.eject(id);
|
|
484
|
-
});
|
|
485
|
-
this.interceptorIds = { request: [], response: [] };
|
|
486
|
-
}
|
|
487
|
-
});
|
|
266
|
+
clearAllInterceptors() {
|
|
267
|
+
this.interceptorIds.request.forEach((id) => {
|
|
268
|
+
this.instance.interceptors.request.eject(id);
|
|
269
|
+
});
|
|
270
|
+
this.interceptorIds.response.forEach((id) => {
|
|
271
|
+
this.instance.interceptors.response.eject(id);
|
|
272
|
+
});
|
|
273
|
+
this.interceptorIds = { request: [], response: [] };
|
|
274
|
+
}
|
|
488
275
|
/**
|
|
489
276
|
* 获取 Axios 实例
|
|
490
277
|
*/
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
writable: true,
|
|
495
|
-
value: function () {
|
|
496
|
-
return this.instance;
|
|
497
|
-
}
|
|
498
|
-
});
|
|
278
|
+
getInstance() {
|
|
279
|
+
return this.instance;
|
|
280
|
+
}
|
|
499
281
|
/**
|
|
500
282
|
* 设置 BaseURL
|
|
501
283
|
*/
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
writable: true,
|
|
506
|
-
value: function (baseUrl) {
|
|
507
|
-
this.instance.defaults.baseURL = baseUrl;
|
|
508
|
-
}
|
|
509
|
-
});
|
|
284
|
+
setBaseUrl(baseUrl) {
|
|
285
|
+
this.instance.defaults.baseURL = baseUrl;
|
|
286
|
+
}
|
|
510
287
|
/**
|
|
511
288
|
* 处理错误
|
|
512
289
|
*/
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
var _a, _b;
|
|
519
|
-
if (isAxiosError(error)) {
|
|
520
|
-
var axiosError = error;
|
|
521
|
-
if (axiosError.code === 'ECONNABORTED') {
|
|
522
|
-
return new TimeoutError("Request timeout after ".concat((_a = axiosError.config) === null || _a === void 0 ? void 0 : _a.timeout, "ms"), ((_b = axiosError.config) === null || _b === void 0 ? void 0 : _b.timeout) || DEFAULT_TIMEOUT, axiosError);
|
|
523
|
-
}
|
|
524
|
-
if (!axiosError.response) {
|
|
525
|
-
return new NetworkError('Network error: Unable to reach server', axiosError);
|
|
526
|
-
}
|
|
527
|
-
return new HttpError(axiosError.message, axiosError.response.status, axiosError.response.data, axiosError);
|
|
290
|
+
handleError(error) {
|
|
291
|
+
if (isAxiosError(error)) {
|
|
292
|
+
const axiosError = error;
|
|
293
|
+
if (axiosError.code === 'ECONNABORTED') {
|
|
294
|
+
return new TimeoutError(`Request timeout after ${axiosError.config?.timeout}ms`, axiosError.config?.timeout || DEFAULT_TIMEOUT, axiosError);
|
|
528
295
|
}
|
|
529
|
-
if (
|
|
530
|
-
return error;
|
|
296
|
+
if (!axiosError.response) {
|
|
297
|
+
return new NetworkError('Network error: Unable to reach server', axiosError);
|
|
531
298
|
}
|
|
532
|
-
return new
|
|
299
|
+
return new HttpError(axiosError.message, axiosError.response.status, axiosError.response.data, axiosError);
|
|
533
300
|
}
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
}
|
|
301
|
+
if (error instanceof Error) {
|
|
302
|
+
return error;
|
|
303
|
+
}
|
|
304
|
+
return new Error(String(error));
|
|
305
|
+
}
|
|
306
|
+
}
|
|
537
307
|
|
|
538
308
|
/**
|
|
539
309
|
* 生成请求 ID
|
|
540
310
|
*/
|
|
541
311
|
function generateRequestId() {
|
|
542
|
-
return
|
|
312
|
+
return `req_${Date.now()}_${Math.random().toString(36).substring(2, 11)}`;
|
|
543
313
|
}
|
|
544
314
|
/**
|
|
545
315
|
* HTTP 客户端实现
|
|
546
316
|
* 提供基础的 HTTP 请求功能
|
|
547
317
|
*/
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
configurable: true,
|
|
553
|
-
writable: true,
|
|
554
|
-
value: void 0
|
|
555
|
-
});
|
|
556
|
-
Object.defineProperty(this, "requestInterceptors", {
|
|
557
|
-
enumerable: true,
|
|
558
|
-
configurable: true,
|
|
559
|
-
writable: true,
|
|
560
|
-
value: []
|
|
561
|
-
});
|
|
562
|
-
Object.defineProperty(this, "responseInterceptors", {
|
|
563
|
-
enumerable: true,
|
|
564
|
-
configurable: true,
|
|
565
|
-
writable: true,
|
|
566
|
-
value: []
|
|
567
|
-
});
|
|
318
|
+
class HttpClient {
|
|
319
|
+
constructor(adapter) {
|
|
320
|
+
this.requestInterceptors = [];
|
|
321
|
+
this.responseInterceptors = [];
|
|
568
322
|
this.adapter = adapter || new AxiosAdapter();
|
|
569
323
|
}
|
|
570
324
|
/**
|
|
571
325
|
* GET 请求
|
|
572
326
|
*/
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
writable: true,
|
|
577
|
-
value: function (url, config) {
|
|
578
|
-
return __awaiter$3(this, void 0, void 0, function () {
|
|
579
|
-
return __generator$3(this, function (_a) {
|
|
580
|
-
return [2 /*return*/, this.request(__assign$4(__assign$4({}, config), { url: url, method: HttpMethod.GET }))];
|
|
581
|
-
});
|
|
582
|
-
});
|
|
583
|
-
}
|
|
584
|
-
});
|
|
327
|
+
async get(url, config) {
|
|
328
|
+
return this.request({ ...config, url, method: HttpMethod.GET });
|
|
329
|
+
}
|
|
585
330
|
/**
|
|
586
331
|
* POST 请求
|
|
587
332
|
*/
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
writable: true,
|
|
592
|
-
value: function (url, data, config) {
|
|
593
|
-
return __awaiter$3(this, void 0, void 0, function () {
|
|
594
|
-
return __generator$3(this, function (_a) {
|
|
595
|
-
return [2 /*return*/, this.request(__assign$4(__assign$4({}, config), { url: url, data: data, method: HttpMethod.POST }))];
|
|
596
|
-
});
|
|
597
|
-
});
|
|
598
|
-
}
|
|
599
|
-
});
|
|
333
|
+
async post(url, data, config) {
|
|
334
|
+
return this.request({ ...config, url, data, method: HttpMethod.POST });
|
|
335
|
+
}
|
|
600
336
|
/**
|
|
601
337
|
* PUT 请求
|
|
602
338
|
*/
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
writable: true,
|
|
607
|
-
value: function (url, data, config) {
|
|
608
|
-
return __awaiter$3(this, void 0, void 0, function () {
|
|
609
|
-
return __generator$3(this, function (_a) {
|
|
610
|
-
return [2 /*return*/, this.request(__assign$4(__assign$4({}, config), { url: url, data: data, method: HttpMethod.PUT }))];
|
|
611
|
-
});
|
|
612
|
-
});
|
|
613
|
-
}
|
|
614
|
-
});
|
|
339
|
+
async put(url, data, config) {
|
|
340
|
+
return this.request({ ...config, url, data, method: HttpMethod.PUT });
|
|
341
|
+
}
|
|
615
342
|
/**
|
|
616
343
|
* DELETE 请求
|
|
617
344
|
*/
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
writable: true,
|
|
622
|
-
value: function (url, config) {
|
|
623
|
-
return __awaiter$3(this, void 0, void 0, function () {
|
|
624
|
-
return __generator$3(this, function (_a) {
|
|
625
|
-
return [2 /*return*/, this.request(__assign$4(__assign$4({}, config), { url: url, method: HttpMethod.DELETE }))];
|
|
626
|
-
});
|
|
627
|
-
});
|
|
628
|
-
}
|
|
629
|
-
});
|
|
345
|
+
async delete(url, config) {
|
|
346
|
+
return this.request({ ...config, url, method: HttpMethod.DELETE });
|
|
347
|
+
}
|
|
630
348
|
/**
|
|
631
349
|
* 通用请求方法
|
|
632
350
|
*/
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
case 7:
|
|
671
|
-
e_1_1 = _k.sent();
|
|
672
|
-
e_1 = { error: e_1_1 };
|
|
673
|
-
return [3 /*break*/, 9];
|
|
674
|
-
case 8:
|
|
675
|
-
try {
|
|
676
|
-
if (_b && !_b.done && (_g = _a.return)) _g.call(_a);
|
|
677
|
-
}
|
|
678
|
-
finally { if (e_1) throw e_1.error; }
|
|
679
|
-
return [7 /*endfinally*/];
|
|
680
|
-
case 9: return [4 /*yield*/, this.adapter.request(processedContext.config)];
|
|
681
|
-
case 10:
|
|
682
|
-
response = _k.sent();
|
|
683
|
-
responseContext = {
|
|
684
|
-
response: response,
|
|
685
|
-
status: 200,
|
|
686
|
-
headers: {},
|
|
687
|
-
duration: Date.now() - context.startTime,
|
|
688
|
-
requestId: context.requestId,
|
|
689
|
-
};
|
|
690
|
-
_k.label = 11;
|
|
691
|
-
case 11:
|
|
692
|
-
_k.trys.push([11, 16, 17, 18]);
|
|
693
|
-
_c = __values(this.responseInterceptors), _d = _c.next();
|
|
694
|
-
_k.label = 12;
|
|
695
|
-
case 12:
|
|
696
|
-
if (!!_d.done) return [3 /*break*/, 15];
|
|
697
|
-
interceptor = _d.value;
|
|
698
|
-
return [4 /*yield*/, interceptor.onResponse(responseContext)];
|
|
699
|
-
case 13:
|
|
700
|
-
responseContext = _k.sent();
|
|
701
|
-
_k.label = 14;
|
|
702
|
-
case 14:
|
|
703
|
-
_d = _c.next();
|
|
704
|
-
return [3 /*break*/, 12];
|
|
705
|
-
case 15: return [3 /*break*/, 18];
|
|
706
|
-
case 16:
|
|
707
|
-
e_2_1 = _k.sent();
|
|
708
|
-
e_2 = { error: e_2_1 };
|
|
709
|
-
return [3 /*break*/, 18];
|
|
710
|
-
case 17:
|
|
711
|
-
try {
|
|
712
|
-
if (_d && !_d.done && (_h = _c.return)) _h.call(_c);
|
|
713
|
-
}
|
|
714
|
-
finally { if (e_2) throw e_2.error; }
|
|
715
|
-
return [7 /*endfinally*/];
|
|
716
|
-
case 18: return [2 /*return*/, responseContext.response];
|
|
717
|
-
case 19:
|
|
718
|
-
error_1 = _k.sent();
|
|
719
|
-
processedError = error_1;
|
|
720
|
-
_k.label = 20;
|
|
721
|
-
case 20:
|
|
722
|
-
_k.trys.push([20, 27, 28, 29]);
|
|
723
|
-
_e = __values(this.responseInterceptors), _f = _e.next();
|
|
724
|
-
_k.label = 21;
|
|
725
|
-
case 21:
|
|
726
|
-
if (!!_f.done) return [3 /*break*/, 26];
|
|
727
|
-
interceptor = _f.value;
|
|
728
|
-
if (!interceptor.onResponseError) return [3 /*break*/, 25];
|
|
729
|
-
_k.label = 22;
|
|
730
|
-
case 22:
|
|
731
|
-
_k.trys.push([22, 24, , 25]);
|
|
732
|
-
return [4 /*yield*/, interceptor.onResponseError(processedError)];
|
|
733
|
-
case 23:
|
|
734
|
-
processedError = _k.sent();
|
|
735
|
-
return [3 /*break*/, 25];
|
|
736
|
-
case 24:
|
|
737
|
-
interceptorError_1 = _k.sent();
|
|
738
|
-
processedError = interceptorError_1;
|
|
739
|
-
return [3 /*break*/, 25];
|
|
740
|
-
case 25:
|
|
741
|
-
_f = _e.next();
|
|
742
|
-
return [3 /*break*/, 21];
|
|
743
|
-
case 26: return [3 /*break*/, 29];
|
|
744
|
-
case 27:
|
|
745
|
-
e_3_1 = _k.sent();
|
|
746
|
-
e_3 = { error: e_3_1 };
|
|
747
|
-
return [3 /*break*/, 29];
|
|
748
|
-
case 28:
|
|
749
|
-
try {
|
|
750
|
-
if (_f && !_f.done && (_j = _e.return)) _j.call(_e);
|
|
751
|
-
}
|
|
752
|
-
finally { if (e_3) throw e_3.error; }
|
|
753
|
-
return [7 /*endfinally*/];
|
|
754
|
-
case 29: throw processedError;
|
|
755
|
-
case 30: return [2 /*return*/];
|
|
351
|
+
async request(config) {
|
|
352
|
+
// 创建请求上下文
|
|
353
|
+
const context = {
|
|
354
|
+
config: this.normalizeConfig(config),
|
|
355
|
+
startTime: Date.now(),
|
|
356
|
+
requestId: generateRequestId(),
|
|
357
|
+
metadata: new Map(),
|
|
358
|
+
};
|
|
359
|
+
try {
|
|
360
|
+
// 执行请求拦截器
|
|
361
|
+
let processedContext = context;
|
|
362
|
+
for (const interceptor of this.requestInterceptors) {
|
|
363
|
+
processedContext = (await interceptor.onRequest(processedContext));
|
|
364
|
+
}
|
|
365
|
+
// 发起请求
|
|
366
|
+
const response = await this.adapter.request(processedContext.config);
|
|
367
|
+
// 创建响应上下文
|
|
368
|
+
let responseContext = {
|
|
369
|
+
response,
|
|
370
|
+
status: 200,
|
|
371
|
+
headers: {},
|
|
372
|
+
duration: Date.now() - context.startTime,
|
|
373
|
+
requestId: context.requestId,
|
|
374
|
+
};
|
|
375
|
+
// 执行响应拦截器
|
|
376
|
+
for (const interceptor of this.responseInterceptors) {
|
|
377
|
+
responseContext = await interceptor.onResponse(responseContext);
|
|
378
|
+
}
|
|
379
|
+
return responseContext.response;
|
|
380
|
+
}
|
|
381
|
+
catch (error) {
|
|
382
|
+
// 执行错误拦截器
|
|
383
|
+
let processedError = error;
|
|
384
|
+
for (const interceptor of this.responseInterceptors) {
|
|
385
|
+
if (interceptor.onResponseError) {
|
|
386
|
+
try {
|
|
387
|
+
processedError = await interceptor.onResponseError(processedError);
|
|
756
388
|
}
|
|
757
|
-
|
|
758
|
-
|
|
389
|
+
catch (interceptorError) {
|
|
390
|
+
processedError = interceptorError;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
throw processedError;
|
|
759
395
|
}
|
|
760
|
-
}
|
|
396
|
+
}
|
|
761
397
|
/**
|
|
762
398
|
* 添加请求拦截器
|
|
763
399
|
*/
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
if (index > -1) {
|
|
775
|
-
_this.requestInterceptors.splice(index, 1);
|
|
776
|
-
}
|
|
777
|
-
};
|
|
778
|
-
}
|
|
779
|
-
});
|
|
400
|
+
addRequestInterceptor(interceptor) {
|
|
401
|
+
this.requestInterceptors.push(interceptor);
|
|
402
|
+
this.requestInterceptors.sort((a, b) => (a.priority || 0) - (b.priority || 0));
|
|
403
|
+
return () => {
|
|
404
|
+
const index = this.requestInterceptors.indexOf(interceptor);
|
|
405
|
+
if (index > -1) {
|
|
406
|
+
this.requestInterceptors.splice(index, 1);
|
|
407
|
+
}
|
|
408
|
+
};
|
|
409
|
+
}
|
|
780
410
|
/**
|
|
781
411
|
* 添加响应拦截器
|
|
782
412
|
*/
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
if (index > -1) {
|
|
794
|
-
_this.responseInterceptors.splice(index, 1);
|
|
795
|
-
}
|
|
796
|
-
};
|
|
797
|
-
}
|
|
798
|
-
});
|
|
413
|
+
addResponseInterceptor(interceptor) {
|
|
414
|
+
this.responseInterceptors.push(interceptor);
|
|
415
|
+
this.responseInterceptors.sort((a, b) => (a.priority || 0) - (b.priority || 0));
|
|
416
|
+
return () => {
|
|
417
|
+
const index = this.responseInterceptors.indexOf(interceptor);
|
|
418
|
+
if (index > -1) {
|
|
419
|
+
this.responseInterceptors.splice(index, 1);
|
|
420
|
+
}
|
|
421
|
+
};
|
|
422
|
+
}
|
|
799
423
|
/**
|
|
800
424
|
* 克隆客户端
|
|
801
425
|
* 注意:克隆会创建新的拦截器数组,但共享同一个适配器实例
|
|
802
426
|
* 如需独立的适配器,请使用 new HttpClient() 创建新实例
|
|
803
427
|
*/
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
cloned.requestInterceptors = __spreadArray$1([], __read(this.requestInterceptors), false);
|
|
811
|
-
cloned.responseInterceptors = __spreadArray$1([], __read(this.responseInterceptors), false);
|
|
812
|
-
return cloned;
|
|
813
|
-
}
|
|
814
|
-
});
|
|
428
|
+
clone() {
|
|
429
|
+
const cloned = new HttpClient(this.adapter);
|
|
430
|
+
cloned.requestInterceptors = [...this.requestInterceptors];
|
|
431
|
+
cloned.responseInterceptors = [...this.responseInterceptors];
|
|
432
|
+
return cloned;
|
|
433
|
+
}
|
|
815
434
|
/**
|
|
816
435
|
* 规范化配置
|
|
817
436
|
*/
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
}
|
|
825
|
-
}
|
|
437
|
+
normalizeConfig(config) {
|
|
438
|
+
return {
|
|
439
|
+
method: config.method || (config.data ? HttpMethod.POST : HttpMethod.GET),
|
|
440
|
+
timeout: config.timeout || DEFAULT_TIMEOUT,
|
|
441
|
+
headers: config.headers || {},
|
|
442
|
+
...config,
|
|
443
|
+
};
|
|
444
|
+
}
|
|
826
445
|
/**
|
|
827
446
|
* 获取适配器
|
|
828
447
|
*/
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
writable: true,
|
|
833
|
-
value: function () {
|
|
834
|
-
return this.adapter;
|
|
835
|
-
}
|
|
836
|
-
});
|
|
448
|
+
getAdapter() {
|
|
449
|
+
return this.adapter;
|
|
450
|
+
}
|
|
837
451
|
/**
|
|
838
452
|
* 处理旧版拦截器的请求(向后兼容)
|
|
839
453
|
* 此方法被 BeLinkClient 和 LiveClient 共享
|
|
840
454
|
* @protected 允许子类调用
|
|
841
455
|
*/
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
? this.addResponseInterceptor({
|
|
865
|
-
name: 'LegacyResponse',
|
|
866
|
-
priority: 100,
|
|
867
|
-
onResponse: function (ctx) { return ctx; },
|
|
868
|
-
})
|
|
869
|
-
: null;
|
|
870
|
-
_b.label = 1;
|
|
871
|
-
case 1:
|
|
872
|
-
_b.trys.push([1, 3, 4, 5]);
|
|
873
|
-
return [4 /*yield*/, this.request(config)];
|
|
874
|
-
case 2:
|
|
875
|
-
response = _b.sent();
|
|
876
|
-
return [2 /*return*/, response];
|
|
877
|
-
case 3:
|
|
878
|
-
error_2 = _b.sent();
|
|
879
|
-
if (config.catchCallback) {
|
|
880
|
-
config.catchCallback(error_2);
|
|
881
|
-
}
|
|
882
|
-
err = error_2;
|
|
883
|
-
throw ((_a = err === null || err === void 0 ? void 0 : err.response) === null || _a === void 0 ? void 0 : _a.data) || error_2;
|
|
884
|
-
case 4:
|
|
885
|
-
// 清理临时拦截器
|
|
886
|
-
removeRequestInterceptor === null || removeRequestInterceptor === void 0 ? void 0 : removeRequestInterceptor();
|
|
887
|
-
removeResponseInterceptor === null || removeResponseInterceptor === void 0 ? void 0 : removeResponseInterceptor();
|
|
888
|
-
return [7 /*endfinally*/];
|
|
889
|
-
case 5: return [2 /*return*/];
|
|
890
|
-
}
|
|
891
|
-
});
|
|
892
|
-
});
|
|
456
|
+
async requestWithLegacyInterceptors(config) {
|
|
457
|
+
// 注册临时拦截器
|
|
458
|
+
const removeRequestInterceptor = config.requestInterceptors
|
|
459
|
+
? this.addRequestInterceptor({
|
|
460
|
+
name: 'LegacyRequest',
|
|
461
|
+
priority: 100,
|
|
462
|
+
onRequest: (ctx) => {
|
|
463
|
+
const newConfig = config.requestInterceptors(ctx.config);
|
|
464
|
+
return { ...ctx, config: newConfig };
|
|
465
|
+
},
|
|
466
|
+
})
|
|
467
|
+
: null;
|
|
468
|
+
const removeResponseInterceptor = config.responseInterceptors
|
|
469
|
+
? this.addResponseInterceptor({
|
|
470
|
+
name: 'LegacyResponse',
|
|
471
|
+
priority: 100,
|
|
472
|
+
onResponse: (ctx) => ctx,
|
|
473
|
+
})
|
|
474
|
+
: null;
|
|
475
|
+
try {
|
|
476
|
+
const response = await this.request(config);
|
|
477
|
+
return response;
|
|
893
478
|
}
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
479
|
+
catch (error) {
|
|
480
|
+
if (config.catchCallback) {
|
|
481
|
+
config.catchCallback(error);
|
|
482
|
+
}
|
|
483
|
+
const err = error;
|
|
484
|
+
throw err?.response?.data || error;
|
|
485
|
+
}
|
|
486
|
+
finally {
|
|
487
|
+
// 清理临时拦截器
|
|
488
|
+
removeRequestInterceptor?.();
|
|
489
|
+
removeResponseInterceptor?.();
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
}
|
|
897
493
|
|
|
898
494
|
/**
|
|
899
495
|
* 默认配置值
|
|
900
496
|
*/
|
|
901
|
-
|
|
497
|
+
const DEFAULT_CONFIG = {
|
|
902
498
|
mode: 'development',
|
|
903
499
|
timeout: 8000,
|
|
904
500
|
encryption: {
|
|
@@ -923,151 +519,100 @@ var DEFAULT_CONFIG = {
|
|
|
923
519
|
* 配置管理器实现
|
|
924
520
|
* 单例模式,集中管理所有配置
|
|
925
521
|
*/
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
enumerable: true,
|
|
930
|
-
configurable: true,
|
|
931
|
-
writable: true,
|
|
932
|
-
value: void 0
|
|
933
|
-
});
|
|
934
|
-
this.config = __assign$4({}, DEFAULT_CONFIG);
|
|
522
|
+
class ConfigManager {
|
|
523
|
+
constructor() {
|
|
524
|
+
this.config = { ...DEFAULT_CONFIG };
|
|
935
525
|
this.loadFromEnvironment();
|
|
936
526
|
}
|
|
937
527
|
/**
|
|
938
528
|
* 获取单例实例
|
|
939
529
|
*/
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
writable: true,
|
|
944
|
-
value: function () {
|
|
945
|
-
if (!ConfigManager.instance) {
|
|
946
|
-
ConfigManager.instance = new ConfigManager();
|
|
947
|
-
}
|
|
948
|
-
return ConfigManager.instance;
|
|
530
|
+
static getInstance() {
|
|
531
|
+
if (!ConfigManager.instance) {
|
|
532
|
+
ConfigManager.instance = new ConfigManager();
|
|
949
533
|
}
|
|
950
|
-
|
|
534
|
+
return ConfigManager.instance;
|
|
535
|
+
}
|
|
951
536
|
/**
|
|
952
537
|
* 初始化配置
|
|
953
538
|
*/
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
value: function (config) {
|
|
959
|
-
this.config = this.mergeConfig(this.config, config);
|
|
960
|
-
if (!this.validate()) {
|
|
961
|
-
console.warn('[ConfigManager] Configuration validation failed');
|
|
962
|
-
}
|
|
539
|
+
init(config) {
|
|
540
|
+
this.config = this.mergeConfig(this.config, config);
|
|
541
|
+
if (!this.validate()) {
|
|
542
|
+
console.warn('[ConfigManager] Configuration validation failed');
|
|
963
543
|
}
|
|
964
|
-
}
|
|
544
|
+
}
|
|
965
545
|
/**
|
|
966
546
|
* 获取配置项
|
|
967
547
|
*/
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
writable: true,
|
|
972
|
-
value: function (key) {
|
|
973
|
-
return this.config[key];
|
|
974
|
-
}
|
|
975
|
-
});
|
|
548
|
+
get(key) {
|
|
549
|
+
return this.config[key];
|
|
550
|
+
}
|
|
976
551
|
/**
|
|
977
552
|
* 设置配置项
|
|
978
553
|
*/
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
writable: true,
|
|
983
|
-
value: function (key, value) {
|
|
984
|
-
this.config[key] = value;
|
|
985
|
-
}
|
|
986
|
-
});
|
|
554
|
+
set(key, value) {
|
|
555
|
+
this.config[key] = value;
|
|
556
|
+
}
|
|
987
557
|
/**
|
|
988
558
|
* 获取完整配置
|
|
989
559
|
*/
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
writable: true,
|
|
994
|
-
value: function () {
|
|
995
|
-
return Object.freeze(__assign$4({}, this.config));
|
|
996
|
-
}
|
|
997
|
-
});
|
|
560
|
+
getAll() {
|
|
561
|
+
return Object.freeze({ ...this.config });
|
|
562
|
+
}
|
|
998
563
|
/**
|
|
999
564
|
* 验证配置
|
|
1000
565
|
*/
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
value: function () {
|
|
1006
|
-
if (!this.config.mode) {
|
|
1007
|
-
console.error('[ConfigManager] Mode is required');
|
|
1008
|
-
return false;
|
|
1009
|
-
}
|
|
1010
|
-
return true;
|
|
566
|
+
validate() {
|
|
567
|
+
if (!this.config.mode) {
|
|
568
|
+
console.error('[ConfigManager] Mode is required');
|
|
569
|
+
return false;
|
|
1011
570
|
}
|
|
1012
|
-
|
|
571
|
+
return true;
|
|
572
|
+
}
|
|
1013
573
|
/**
|
|
1014
574
|
* 重置配置
|
|
1015
575
|
*/
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
writable: true,
|
|
1020
|
-
value: function () {
|
|
1021
|
-
this.config = __assign$4({}, DEFAULT_CONFIG);
|
|
1022
|
-
}
|
|
1023
|
-
});
|
|
576
|
+
reset() {
|
|
577
|
+
this.config = { ...DEFAULT_CONFIG };
|
|
578
|
+
}
|
|
1024
579
|
/**
|
|
1025
580
|
* 从环境变量加载配置
|
|
1026
581
|
*/
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
var key = this.getEnvVar('BELINK_ENCRYPTION_KEY');
|
|
1033
|
-
var iv = this.getEnvVar('BELINK_ENCRYPTION_IV');
|
|
1034
|
-
if (key && iv) {
|
|
1035
|
-
this.config.encryption = { key: key, iv: iv };
|
|
1036
|
-
}
|
|
582
|
+
loadFromEnvironment() {
|
|
583
|
+
const key = this.getEnvVar('BELINK_ENCRYPTION_KEY');
|
|
584
|
+
const iv = this.getEnvVar('BELINK_ENCRYPTION_IV');
|
|
585
|
+
if (key && iv) {
|
|
586
|
+
this.config.encryption = { key, iv };
|
|
1037
587
|
}
|
|
1038
|
-
}
|
|
588
|
+
}
|
|
1039
589
|
/**
|
|
1040
590
|
* 深度合并配置
|
|
1041
591
|
*/
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
592
|
+
mergeConfig(target, source) {
|
|
593
|
+
return {
|
|
594
|
+
...target,
|
|
595
|
+
...source,
|
|
596
|
+
encryption: source.encryption ? { ...target.encryption, ...source.encryption } : target.encryption,
|
|
597
|
+
timeSync: source.timeSync ? { ...target.timeSync, ...source.timeSync } : target.timeSync,
|
|
598
|
+
cloudbase: source.cloudbase ? { ...target.cloudbase, ...source.cloudbase } : target.cloudbase,
|
|
599
|
+
};
|
|
600
|
+
}
|
|
1050
601
|
/**
|
|
1051
602
|
* 获取环境变量
|
|
1052
603
|
*/
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
value: function (key) {
|
|
1058
|
-
// Vite 环境
|
|
1059
|
-
if (typeof import.meta !== 'undefined' && import.meta.env) {
|
|
1060
|
-
return import.meta.env[key];
|
|
1061
|
-
}
|
|
1062
|
-
// Webpack/Node 环境
|
|
1063
|
-
if (typeof process !== 'undefined' && process.env) {
|
|
1064
|
-
return process.env[key];
|
|
1065
|
-
}
|
|
1066
|
-
return undefined;
|
|
604
|
+
getEnvVar(key) {
|
|
605
|
+
// Vite 环境
|
|
606
|
+
if (typeof import.meta !== 'undefined' && import.meta.env) {
|
|
607
|
+
return import.meta.env[key];
|
|
1067
608
|
}
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
609
|
+
// Webpack/Node 环境
|
|
610
|
+
if (typeof process !== 'undefined' && process.env) {
|
|
611
|
+
return process.env[key];
|
|
612
|
+
}
|
|
613
|
+
return undefined;
|
|
614
|
+
}
|
|
615
|
+
}
|
|
1071
616
|
|
|
1072
617
|
/**
|
|
1073
618
|
* 检测当前环境
|
|
@@ -1075,12 +620,12 @@ var ConfigManager = /** @class */ (function () {
|
|
|
1075
620
|
function detectEnvironment() {
|
|
1076
621
|
// Vite 环境
|
|
1077
622
|
if (typeof import.meta !== 'undefined' && import.meta.env) {
|
|
1078
|
-
|
|
623
|
+
const env = import.meta.env.VITE_API_ENV || import.meta.env.MODE;
|
|
1079
624
|
return normalizeEnv(env);
|
|
1080
625
|
}
|
|
1081
626
|
// Webpack/Node 环境
|
|
1082
627
|
if (typeof process !== 'undefined' && process.env) {
|
|
1083
|
-
|
|
628
|
+
const env = process.env.API_ENV || process.env.NODE_ENV;
|
|
1084
629
|
return normalizeEnv(env);
|
|
1085
630
|
}
|
|
1086
631
|
console.warn('[EnvUtils] Cannot detect environment, defaulting to "development"');
|
|
@@ -1092,7 +637,7 @@ function detectEnvironment() {
|
|
|
1092
637
|
function normalizeEnv(env) {
|
|
1093
638
|
if (!env)
|
|
1094
639
|
return 'development';
|
|
1095
|
-
|
|
640
|
+
const normalized = env.toLowerCase();
|
|
1096
641
|
if (normalized === 'production' || normalized === 'prod') {
|
|
1097
642
|
return 'production';
|
|
1098
643
|
}
|
|
@@ -1118,950 +663,604 @@ function isNode() {
|
|
|
1118
663
|
* 加密服务实现
|
|
1119
664
|
* 提供 Token 加密/解密功能
|
|
1120
665
|
*/
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
enumerable: true,
|
|
1125
|
-
configurable: true,
|
|
1126
|
-
writable: true,
|
|
1127
|
-
value: void 0
|
|
1128
|
-
});
|
|
1129
|
-
Object.defineProperty(this, "timeSyncService", {
|
|
1130
|
-
enumerable: true,
|
|
1131
|
-
configurable: true,
|
|
1132
|
-
writable: true,
|
|
1133
|
-
value: void 0
|
|
1134
|
-
});
|
|
1135
|
-
this.config = __assign$4({}, config);
|
|
666
|
+
class EncryptionService {
|
|
667
|
+
constructor(config, timeSyncService) {
|
|
668
|
+
this.config = { ...config };
|
|
1136
669
|
this.timeSyncService = timeSyncService;
|
|
1137
670
|
}
|
|
1138
671
|
/**
|
|
1139
672
|
* 加密 Token
|
|
1140
673
|
* 格式: AES(token + "|+|" + timestamp)
|
|
1141
674
|
*/
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
writable: true,
|
|
1146
|
-
value: function (token, timestamp) {
|
|
1147
|
-
if (!token) {
|
|
1148
|
-
return '';
|
|
1149
|
-
}
|
|
1150
|
-
var time = timestamp !== null && timestamp !== void 0 ? timestamp : this.getAdjustedTime();
|
|
1151
|
-
var data = "".concat(token, "|+|").concat(time);
|
|
1152
|
-
var encrypted = CryptoJS.AES.encrypt(data, CryptoJS.enc.Utf8.parse(this.config.key), {
|
|
1153
|
-
iv: CryptoJS.enc.Utf8.parse(this.config.iv),
|
|
1154
|
-
mode: CryptoJS.mode.CBC,
|
|
1155
|
-
}).toString();
|
|
1156
|
-
return encrypted;
|
|
675
|
+
encryptToken(token, timestamp) {
|
|
676
|
+
if (!token) {
|
|
677
|
+
return '';
|
|
1157
678
|
}
|
|
1158
|
-
|
|
679
|
+
const time = timestamp ?? this.getAdjustedTime();
|
|
680
|
+
const data = `${token}|+|${time}`;
|
|
681
|
+
return CryptoJS.AES.encrypt(data, CryptoJS.enc.Utf8.parse(this.config.key), {
|
|
682
|
+
iv: CryptoJS.enc.Utf8.parse(this.config.iv),
|
|
683
|
+
mode: CryptoJS.mode.CBC,
|
|
684
|
+
}).toString();
|
|
685
|
+
}
|
|
1159
686
|
/**
|
|
1160
687
|
* 解密 Token
|
|
1161
688
|
*/
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
token: parts[0],
|
|
1174
|
-
timestamp: parseInt(parts[1], 10),
|
|
1175
|
-
};
|
|
1176
|
-
}
|
|
1177
|
-
});
|
|
689
|
+
decryptToken(encryptedToken) {
|
|
690
|
+
const decrypted = CryptoJS.AES.decrypt(encryptedToken, CryptoJS.enc.Utf8.parse(this.config.key), {
|
|
691
|
+
iv: CryptoJS.enc.Utf8.parse(this.config.iv),
|
|
692
|
+
mode: CryptoJS.mode.CBC,
|
|
693
|
+
}).toString(CryptoJS.enc.Utf8);
|
|
694
|
+
const parts = decrypted.split('|+|');
|
|
695
|
+
return {
|
|
696
|
+
token: parts[0],
|
|
697
|
+
timestamp: parseInt(parts[1], 10),
|
|
698
|
+
};
|
|
699
|
+
}
|
|
1178
700
|
/**
|
|
1179
701
|
* 更新加密配置
|
|
1180
702
|
*/
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
writable: true,
|
|
1185
|
-
value: function (config) {
|
|
1186
|
-
this.config = __assign$4(__assign$4({}, this.config), config);
|
|
1187
|
-
}
|
|
1188
|
-
});
|
|
703
|
+
updateConfig(config) {
|
|
704
|
+
this.config = { ...this.config, ...config };
|
|
705
|
+
}
|
|
1189
706
|
/**
|
|
1190
707
|
* 获取调整后的时间戳(考虑服务器时间差)
|
|
1191
708
|
* 优先使用注入的 TimeSyncService
|
|
1192
709
|
*/
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
// 优先使用注入的 TimeSyncService
|
|
1200
|
-
if (this.timeSyncService) {
|
|
1201
|
-
var timeDiff = this.timeSyncService.getTimeDiff();
|
|
1202
|
-
return timeDiff !== null ? currentTime + timeDiff : currentTime;
|
|
1203
|
-
}
|
|
1204
|
-
// 降级方案:直接读取 localStorage(向后兼容)
|
|
1205
|
-
// 需要环境检查,防止 Node.js 环境崩溃
|
|
1206
|
-
if (isBrowser()) {
|
|
1207
|
-
var timeDiff = localStorage.getItem(STORAGE_KEYS.TIME_DIFF);
|
|
1208
|
-
return timeDiff ? currentTime + Number(timeDiff) : currentTime;
|
|
1209
|
-
}
|
|
1210
|
-
return currentTime;
|
|
710
|
+
getAdjustedTime() {
|
|
711
|
+
const currentTime = Date.now();
|
|
712
|
+
// 优先使用注入的 TimeSyncService
|
|
713
|
+
if (this.timeSyncService) {
|
|
714
|
+
const timeDiff = this.timeSyncService.getTimeDiff();
|
|
715
|
+
return timeDiff !== null ? currentTime + timeDiff : currentTime;
|
|
1211
716
|
}
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
717
|
+
// 降级方案:直接读取 localStorage(向后兼容)
|
|
718
|
+
// 需要环境检查,防止 Node.js 环境崩溃
|
|
719
|
+
if (isBrowser()) {
|
|
720
|
+
const timeDiff = localStorage.getItem(STORAGE_KEYS.TIME_DIFF);
|
|
721
|
+
return timeDiff ? currentTime + Number(timeDiff) : currentTime;
|
|
722
|
+
}
|
|
723
|
+
return currentTime;
|
|
724
|
+
}
|
|
725
|
+
}
|
|
1215
726
|
|
|
1216
727
|
/**
|
|
1217
728
|
* 时间同步服务
|
|
1218
729
|
* 负责同步客户端和服务器时间
|
|
1219
730
|
*/
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
if (mode === void 0) { mode = 'development'; }
|
|
1223
|
-
Object.defineProperty(this, "config", {
|
|
1224
|
-
enumerable: true,
|
|
1225
|
-
configurable: true,
|
|
1226
|
-
writable: true,
|
|
1227
|
-
value: void 0
|
|
1228
|
-
});
|
|
1229
|
-
Object.defineProperty(this, "mode", {
|
|
1230
|
-
enumerable: true,
|
|
1231
|
-
configurable: true,
|
|
1232
|
-
writable: true,
|
|
1233
|
-
value: void 0
|
|
1234
|
-
});
|
|
1235
|
-
Object.defineProperty(this, "synced", {
|
|
1236
|
-
enumerable: true,
|
|
1237
|
-
configurable: true,
|
|
1238
|
-
writable: true,
|
|
1239
|
-
value: false
|
|
1240
|
-
});
|
|
731
|
+
class TimeSyncService {
|
|
732
|
+
constructor(config, mode = 'development') {
|
|
1241
733
|
this.config = config;
|
|
1242
734
|
this.mode = mode;
|
|
1243
735
|
}
|
|
1244
736
|
/**
|
|
1245
737
|
* 确保时间已同步
|
|
1246
738
|
*/
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
writable: true,
|
|
1251
|
-
value: function () {
|
|
1252
|
-
return __awaiter$3(this, void 0, void 0, function () {
|
|
1253
|
-
var serverTime;
|
|
1254
|
-
return __generator$3(this, function (_a) {
|
|
1255
|
-
switch (_a.label) {
|
|
1256
|
-
case 0:
|
|
1257
|
-
if (!this.config.enabled) {
|
|
1258
|
-
return [2 /*return*/];
|
|
1259
|
-
}
|
|
1260
|
-
if (this.synced) {
|
|
1261
|
-
return [2 /*return*/];
|
|
1262
|
-
}
|
|
1263
|
-
if (!isBrowser()) {
|
|
1264
|
-
return [2 /*return*/];
|
|
1265
|
-
}
|
|
1266
|
-
serverTime = localStorage.getItem(STORAGE_KEYS.SERVER_TIME);
|
|
1267
|
-
if (serverTime && serverTime !== 'undefined') {
|
|
1268
|
-
this.synced = true;
|
|
1269
|
-
return [2 /*return*/];
|
|
1270
|
-
}
|
|
1271
|
-
return [4 /*yield*/, this.sync()];
|
|
1272
|
-
case 1:
|
|
1273
|
-
_a.sent();
|
|
1274
|
-
return [2 /*return*/];
|
|
1275
|
-
}
|
|
1276
|
-
});
|
|
1277
|
-
});
|
|
739
|
+
async ensureSync() {
|
|
740
|
+
if (!this.config.enabled) {
|
|
741
|
+
return;
|
|
1278
742
|
}
|
|
1279
|
-
|
|
743
|
+
if (!isBrowser()) {
|
|
744
|
+
return;
|
|
745
|
+
}
|
|
746
|
+
const serverTime = localStorage.getItem(STORAGE_KEYS.SERVER_TIME);
|
|
747
|
+
const clientTime = localStorage.getItem(STORAGE_KEYS.CLIENT_TIME);
|
|
748
|
+
const currentTime = Date.now();
|
|
749
|
+
if (serverTime && serverTime !== 'undefined' && clientTime && clientTime !== 'undefined') {
|
|
750
|
+
const diff = Math.abs(Number(clientTime) - currentTime);
|
|
751
|
+
if (diff <= (this.config.syncGapTime || 50 * 1000)) {
|
|
752
|
+
console.log('服务器时间无需同步,差值:', diff);
|
|
753
|
+
return;
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
await this.sync();
|
|
757
|
+
}
|
|
1280
758
|
/**
|
|
1281
759
|
* 执行时间同步
|
|
1282
760
|
*/
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
writable: true,
|
|
1287
|
-
value: function () {
|
|
1288
|
-
return __awaiter$3(this, void 0, void 0, function () {
|
|
1289
|
-
var url, response, res, serverTime, currentTime, timeDiff, error_1;
|
|
1290
|
-
return __generator$3(this, function (_a) {
|
|
1291
|
-
switch (_a.label) {
|
|
1292
|
-
case 0:
|
|
1293
|
-
if (!isBrowser()) {
|
|
1294
|
-
return [2 /*return*/];
|
|
1295
|
-
}
|
|
1296
|
-
_a.label = 1;
|
|
1297
|
-
case 1:
|
|
1298
|
-
_a.trys.push([1, 4, , 5]);
|
|
1299
|
-
url = this.getSyncUrl();
|
|
1300
|
-
if (!url) {
|
|
1301
|
-
console.warn('[TimeSyncService] Sync URL not configured');
|
|
1302
|
-
return [2 /*return*/];
|
|
1303
|
-
}
|
|
1304
|
-
return [4 /*yield*/, fetch(url, { method: 'POST' })];
|
|
1305
|
-
case 2:
|
|
1306
|
-
response = _a.sent();
|
|
1307
|
-
// 检查响应状态
|
|
1308
|
-
if (!response.ok) {
|
|
1309
|
-
console.error('[TimeSyncService] Sync failed with status:', response.status);
|
|
1310
|
-
this.clear();
|
|
1311
|
-
return [2 /*return*/];
|
|
1312
|
-
}
|
|
1313
|
-
return [4 /*yield*/, response.json()];
|
|
1314
|
-
case 3:
|
|
1315
|
-
res = _a.sent();
|
|
1316
|
-
serverTime = res.data;
|
|
1317
|
-
if (!serverTime) {
|
|
1318
|
-
this.clear();
|
|
1319
|
-
return [2 /*return*/];
|
|
1320
|
-
}
|
|
1321
|
-
currentTime = Date.now();
|
|
1322
|
-
timeDiff = serverTime - currentTime;
|
|
1323
|
-
localStorage.setItem(STORAGE_KEYS.SERVER_TIME, String(serverTime));
|
|
1324
|
-
localStorage.setItem(STORAGE_KEYS.TIME_DIFF, String(timeDiff));
|
|
1325
|
-
this.synced = true;
|
|
1326
|
-
return [3 /*break*/, 5];
|
|
1327
|
-
case 4:
|
|
1328
|
-
error_1 = _a.sent();
|
|
1329
|
-
console.error('[TimeSyncService] Failed to sync time:', error_1);
|
|
1330
|
-
this.clear();
|
|
1331
|
-
return [3 /*break*/, 5];
|
|
1332
|
-
case 5: return [2 /*return*/];
|
|
1333
|
-
}
|
|
1334
|
-
});
|
|
1335
|
-
});
|
|
761
|
+
async sync() {
|
|
762
|
+
if (!isBrowser()) {
|
|
763
|
+
return;
|
|
1336
764
|
}
|
|
1337
|
-
|
|
765
|
+
try {
|
|
766
|
+
console.log('服务器时间无需同步');
|
|
767
|
+
const url = this.getSyncUrl();
|
|
768
|
+
if (!url) {
|
|
769
|
+
console.warn('[TimeSyncService] Sync URL not configured');
|
|
770
|
+
return;
|
|
771
|
+
}
|
|
772
|
+
const response = await fetch(url, { method: 'POST' });
|
|
773
|
+
// 检查响应状态
|
|
774
|
+
if (!response.ok) {
|
|
775
|
+
console.error('[TimeSyncService] Sync failed with status:', response.status);
|
|
776
|
+
this.clear();
|
|
777
|
+
return;
|
|
778
|
+
}
|
|
779
|
+
const res = await response.json();
|
|
780
|
+
const serverTime = res.data;
|
|
781
|
+
if (!serverTime) {
|
|
782
|
+
this.clear();
|
|
783
|
+
return;
|
|
784
|
+
}
|
|
785
|
+
console.log(111111113331312);
|
|
786
|
+
localStorage.setItem(STORAGE_KEYS.SERVER_TIME, String(serverTime));
|
|
787
|
+
localStorage.setItem(STORAGE_KEYS.CLIENT_TIME, String(Date.now()));
|
|
788
|
+
}
|
|
789
|
+
catch (error) {
|
|
790
|
+
console.error('[TimeSyncService] Failed to sync time:', error);
|
|
791
|
+
this.clear();
|
|
792
|
+
}
|
|
793
|
+
}
|
|
1338
794
|
/**
|
|
1339
795
|
* 获取服务器时间
|
|
1340
796
|
*/
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
writable: true,
|
|
1345
|
-
value: function () {
|
|
1346
|
-
if (!isBrowser()) {
|
|
1347
|
-
return null;
|
|
1348
|
-
}
|
|
1349
|
-
var serverTime = localStorage.getItem(STORAGE_KEYS.SERVER_TIME);
|
|
1350
|
-
return serverTime ? Number(serverTime) : null;
|
|
797
|
+
getServerTime() {
|
|
798
|
+
if (!isBrowser()) {
|
|
799
|
+
return null;
|
|
1351
800
|
}
|
|
1352
|
-
|
|
801
|
+
const serverTime = localStorage.getItem(STORAGE_KEYS.SERVER_TIME);
|
|
802
|
+
return serverTime ? Number(serverTime) : null;
|
|
803
|
+
}
|
|
1353
804
|
/**
|
|
1354
805
|
* 获取时间差
|
|
1355
806
|
*/
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
writable: true,
|
|
1360
|
-
value: function () {
|
|
1361
|
-
if (!isBrowser()) {
|
|
1362
|
-
return null;
|
|
1363
|
-
}
|
|
1364
|
-
var timeDiff = localStorage.getItem(STORAGE_KEYS.TIME_DIFF);
|
|
1365
|
-
return timeDiff ? Number(timeDiff) : null;
|
|
807
|
+
getTimeDiff() {
|
|
808
|
+
if (!isBrowser()) {
|
|
809
|
+
return null;
|
|
1366
810
|
}
|
|
1367
|
-
|
|
811
|
+
const serverTime = this.getServerTime();
|
|
812
|
+
return serverTime ? serverTime - Date.now() : null;
|
|
813
|
+
}
|
|
1368
814
|
/**
|
|
1369
815
|
* 获取时间同步请求头
|
|
1370
816
|
*/
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
var serverTime = this.getServerTime();
|
|
1378
|
-
var timeDiff = this.getTimeDiff();
|
|
1379
|
-
if (serverTime) {
|
|
1380
|
-
headers['x-belink-serverTime'] = String(serverTime);
|
|
1381
|
-
}
|
|
1382
|
-
if (timeDiff !== null) {
|
|
1383
|
-
headers['x-belink-timeDiff'] = String(timeDiff);
|
|
1384
|
-
}
|
|
1385
|
-
return headers;
|
|
817
|
+
getTimeSyncHeaders() {
|
|
818
|
+
const headers = {};
|
|
819
|
+
const serverTime = this.getServerTime();
|
|
820
|
+
const timeDiff = this.getTimeDiff();
|
|
821
|
+
if (serverTime) {
|
|
822
|
+
headers['x-belink-serverTime'] = String(serverTime);
|
|
1386
823
|
}
|
|
1387
|
-
|
|
824
|
+
if (timeDiff !== null) {
|
|
825
|
+
headers['x-belink-timeDiff'] = String(timeDiff);
|
|
826
|
+
}
|
|
827
|
+
return headers;
|
|
828
|
+
}
|
|
1388
829
|
/**
|
|
1389
830
|
* 清理时间同步数据
|
|
1390
831
|
*/
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
if (isBrowser()) {
|
|
1397
|
-
localStorage.removeItem(STORAGE_KEYS.SERVER_TIME);
|
|
1398
|
-
localStorage.removeItem(STORAGE_KEYS.TIME_DIFF);
|
|
1399
|
-
}
|
|
1400
|
-
this.synced = false;
|
|
832
|
+
clear() {
|
|
833
|
+
if (isBrowser()) {
|
|
834
|
+
localStorage.removeItem(STORAGE_KEYS.SERVER_TIME);
|
|
835
|
+
localStorage.removeItem(STORAGE_KEYS.CLIENT_TIME);
|
|
836
|
+
localStorage.removeItem(STORAGE_KEYS.TIME_DIFF);
|
|
1401
837
|
}
|
|
1402
|
-
}
|
|
838
|
+
}
|
|
1403
839
|
/**
|
|
1404
840
|
* 更新模式
|
|
1405
841
|
*/
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
writable: true,
|
|
1410
|
-
value: function (mode) {
|
|
1411
|
-
this.mode = mode;
|
|
1412
|
-
}
|
|
1413
|
-
});
|
|
842
|
+
setMode(mode) {
|
|
843
|
+
this.mode = mode;
|
|
844
|
+
}
|
|
1414
845
|
/**
|
|
1415
846
|
* 获取同步 URL
|
|
1416
847
|
*/
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
writable: true,
|
|
1421
|
-
value: function () {
|
|
1422
|
-
if (this.mode === 'production') {
|
|
1423
|
-
return this.config.productionUrl || '';
|
|
1424
|
-
}
|
|
1425
|
-
return this.config.developmentUrl || '';
|
|
848
|
+
getSyncUrl() {
|
|
849
|
+
if (this.mode === 'production') {
|
|
850
|
+
return this.config.productionUrl || '';
|
|
1426
851
|
}
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
}
|
|
852
|
+
return this.config.developmentUrl || '';
|
|
853
|
+
}
|
|
854
|
+
}
|
|
1430
855
|
|
|
1431
856
|
/**
|
|
1432
|
-
* Token 拦截器
|
|
1433
|
-
* 负责将 Token 加密后注入请求头
|
|
1434
|
-
*/
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
value: void 0
|
|
1454
|
-
});
|
|
1455
|
-
Object.defineProperty(this, "encryptionService", {
|
|
1456
|
-
enumerable: true,
|
|
1457
|
-
configurable: true,
|
|
1458
|
-
writable: true,
|
|
1459
|
-
value: void 0
|
|
1460
|
-
});
|
|
1461
|
-
Object.defineProperty(this, "headerName", {
|
|
1462
|
-
enumerable: true,
|
|
1463
|
-
configurable: true,
|
|
1464
|
-
writable: true,
|
|
1465
|
-
value: void 0
|
|
1466
|
-
});
|
|
1467
|
-
this.tokenProvider = tokenProvider;
|
|
1468
|
-
this.encryptionService = encryptionService;
|
|
1469
|
-
this.priority = (options === null || options === void 0 ? void 0 : options.priority) || 0;
|
|
1470
|
-
this.headerName = (options === null || options === void 0 ? void 0 : options.headerName) || TOKEN_HEADER;
|
|
1471
|
-
}
|
|
1472
|
-
Object.defineProperty(TokenInterceptor.prototype, "onRequest", {
|
|
1473
|
-
enumerable: false,
|
|
1474
|
-
configurable: true,
|
|
1475
|
-
writable: true,
|
|
1476
|
-
value: function (context) {
|
|
1477
|
-
return __awaiter$3(this, void 0, void 0, function () {
|
|
1478
|
-
var token, encryptedToken;
|
|
1479
|
-
return __generator$3(this, function (_a) {
|
|
1480
|
-
token = this.tokenProvider();
|
|
1481
|
-
if (token) {
|
|
1482
|
-
encryptedToken = this.encryptionService.encryptToken(token);
|
|
1483
|
-
if (!context.config.headers) {
|
|
1484
|
-
context.config.headers = {};
|
|
1485
|
-
}
|
|
1486
|
-
context.config.headers[this.headerName] = encryptedToken;
|
|
1487
|
-
}
|
|
1488
|
-
return [2 /*return*/, context];
|
|
1489
|
-
});
|
|
1490
|
-
});
|
|
1491
|
-
}
|
|
1492
|
-
});
|
|
1493
|
-
Object.defineProperty(TokenInterceptor.prototype, "onRequestError", {
|
|
1494
|
-
enumerable: false,
|
|
1495
|
-
configurable: true,
|
|
1496
|
-
writable: true,
|
|
1497
|
-
value: function (error) {
|
|
1498
|
-
return __awaiter$3(this, void 0, void 0, function () {
|
|
1499
|
-
return __generator$3(this, function (_a) {
|
|
1500
|
-
throw error;
|
|
1501
|
-
});
|
|
1502
|
-
});
|
|
857
|
+
* Token 拦截器
|
|
858
|
+
* 负责将 Token 加密后注入请求头
|
|
859
|
+
*/
|
|
860
|
+
class TokenInterceptor {
|
|
861
|
+
constructor(tokenProvider, encryptionService, options) {
|
|
862
|
+
this.name = 'TokenInterceptor';
|
|
863
|
+
this.tokenProvider = tokenProvider;
|
|
864
|
+
this.encryptionService = encryptionService;
|
|
865
|
+
this.priority = options?.priority || 0;
|
|
866
|
+
this.headerName = options?.headerName || TOKEN_HEADER;
|
|
867
|
+
this.userId = options?.userId || '';
|
|
868
|
+
}
|
|
869
|
+
async onRequest(context) {
|
|
870
|
+
const token = this.tokenProvider();
|
|
871
|
+
if (token) {
|
|
872
|
+
const encryptedToken = this.encryptionService.encryptToken(token);
|
|
873
|
+
if (!context.config.headers) {
|
|
874
|
+
context.config.headers = {};
|
|
875
|
+
}
|
|
876
|
+
context.config.headers[this.headerName] = encryptedToken;
|
|
877
|
+
context.config.headers[USERID_HEADER] = this.userId || '';
|
|
1503
878
|
}
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
879
|
+
return context;
|
|
880
|
+
}
|
|
881
|
+
async onRequestError(error) {
|
|
882
|
+
throw error;
|
|
883
|
+
}
|
|
884
|
+
}
|
|
1507
885
|
|
|
1508
886
|
/**
|
|
1509
887
|
* 时间同步拦截器
|
|
1510
888
|
* 负责同步服务器时间并注入请求头
|
|
1511
889
|
*/
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
enumerable: true,
|
|
1516
|
-
configurable: true,
|
|
1517
|
-
writable: true,
|
|
1518
|
-
value: 'TimeInterceptor'
|
|
1519
|
-
});
|
|
1520
|
-
Object.defineProperty(this, "priority", {
|
|
1521
|
-
enumerable: true,
|
|
1522
|
-
configurable: true,
|
|
1523
|
-
writable: true,
|
|
1524
|
-
value: void 0
|
|
1525
|
-
});
|
|
1526
|
-
Object.defineProperty(this, "timeSyncService", {
|
|
1527
|
-
enumerable: true,
|
|
1528
|
-
configurable: true,
|
|
1529
|
-
writable: true,
|
|
1530
|
-
value: void 0
|
|
1531
|
-
});
|
|
890
|
+
class TimeInterceptor {
|
|
891
|
+
constructor(timeSyncService, options) {
|
|
892
|
+
this.name = 'TimeInterceptor';
|
|
1532
893
|
this.timeSyncService = timeSyncService;
|
|
1533
|
-
this.priority =
|
|
894
|
+
this.priority = options?.priority || 0;
|
|
1534
895
|
}
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
return __generator$3(this, function (_a) {
|
|
1543
|
-
switch (_a.label) {
|
|
1544
|
-
case 0:
|
|
1545
|
-
// 确保时间已同步
|
|
1546
|
-
return [4 /*yield*/, this.timeSyncService.ensureSync()];
|
|
1547
|
-
case 1:
|
|
1548
|
-
// 确保时间已同步
|
|
1549
|
-
_a.sent();
|
|
1550
|
-
timeSyncHeaders = this.timeSyncService.getTimeSyncHeaders();
|
|
1551
|
-
if (!context.config.headers) {
|
|
1552
|
-
context.config.headers = {};
|
|
1553
|
-
}
|
|
1554
|
-
Object.assign(context.config.headers, timeSyncHeaders);
|
|
1555
|
-
return [2 /*return*/, context];
|
|
1556
|
-
}
|
|
1557
|
-
});
|
|
1558
|
-
});
|
|
1559
|
-
}
|
|
1560
|
-
});
|
|
1561
|
-
Object.defineProperty(TimeInterceptor.prototype, "onRequestError", {
|
|
1562
|
-
enumerable: false,
|
|
1563
|
-
configurable: true,
|
|
1564
|
-
writable: true,
|
|
1565
|
-
value: function (error) {
|
|
1566
|
-
return __awaiter$3(this, void 0, void 0, function () {
|
|
1567
|
-
return __generator$3(this, function (_a) {
|
|
1568
|
-
throw error;
|
|
1569
|
-
});
|
|
1570
|
-
});
|
|
896
|
+
async onRequest(context) {
|
|
897
|
+
// 确保时间已同步
|
|
898
|
+
await this.timeSyncService.ensureSync();
|
|
899
|
+
// 获取时间同步头
|
|
900
|
+
const timeSyncHeaders = this.timeSyncService.getTimeSyncHeaders();
|
|
901
|
+
if (!context.config.headers) {
|
|
902
|
+
context.config.headers = {};
|
|
1571
903
|
}
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
}
|
|
904
|
+
Object.assign(context.config.headers, timeSyncHeaders);
|
|
905
|
+
return context;
|
|
906
|
+
}
|
|
907
|
+
async onRequestError(error) {
|
|
908
|
+
throw error;
|
|
909
|
+
}
|
|
910
|
+
}
|
|
1575
911
|
|
|
1576
912
|
/**
|
|
1577
913
|
* 认证拦截器
|
|
1578
914
|
* 负责处理 401 未授权响应
|
|
1579
915
|
*/
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
enumerable: true,
|
|
1584
|
-
configurable: true,
|
|
1585
|
-
writable: true,
|
|
1586
|
-
value: 'AuthInterceptor'
|
|
1587
|
-
});
|
|
1588
|
-
Object.defineProperty(this, "priority", {
|
|
1589
|
-
enumerable: true,
|
|
1590
|
-
configurable: true,
|
|
1591
|
-
writable: true,
|
|
1592
|
-
value: void 0
|
|
1593
|
-
});
|
|
1594
|
-
Object.defineProperty(this, "configManager", {
|
|
1595
|
-
enumerable: true,
|
|
1596
|
-
configurable: true,
|
|
1597
|
-
writable: true,
|
|
1598
|
-
value: void 0
|
|
1599
|
-
});
|
|
1600
|
-
Object.defineProperty(this, "timeSyncService", {
|
|
1601
|
-
enumerable: true,
|
|
1602
|
-
configurable: true,
|
|
1603
|
-
writable: true,
|
|
1604
|
-
value: void 0
|
|
1605
|
-
});
|
|
916
|
+
class AuthInterceptor {
|
|
917
|
+
constructor(configManager, timeSyncService, options) {
|
|
918
|
+
this.name = 'AuthInterceptor';
|
|
1606
919
|
this.configManager = configManager;
|
|
1607
920
|
this.timeSyncService = timeSyncService;
|
|
1608
|
-
this.priority =
|
|
921
|
+
this.priority = options?.priority || 0;
|
|
1609
922
|
}
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
var status, loginPath;
|
|
1629
|
-
return __generator$3(this, function (_a) {
|
|
1630
|
-
status = error.status;
|
|
1631
|
-
if (status === 401) {
|
|
1632
|
-
// 清理时间同步数据
|
|
1633
|
-
this.timeSyncService.clear();
|
|
1634
|
-
loginPath = this.configManager.get('loginPath');
|
|
1635
|
-
if (typeof loginPath === 'string') {
|
|
1636
|
-
if (typeof window !== 'undefined') {
|
|
1637
|
-
window.location.replace(loginPath);
|
|
1638
|
-
}
|
|
1639
|
-
}
|
|
1640
|
-
else if (typeof loginPath === 'function') {
|
|
1641
|
-
loginPath();
|
|
1642
|
-
}
|
|
1643
|
-
throw new AuthError('Unauthorized', error instanceof Error ? error : undefined);
|
|
1644
|
-
}
|
|
1645
|
-
throw error;
|
|
1646
|
-
});
|
|
1647
|
-
});
|
|
923
|
+
async onResponse(context) {
|
|
924
|
+
return context;
|
|
925
|
+
}
|
|
926
|
+
async onResponseError(error) {
|
|
927
|
+
const status = error.status;
|
|
928
|
+
if (status === 401) {
|
|
929
|
+
// 清理时间同步数据
|
|
930
|
+
this.timeSyncService.clear();
|
|
931
|
+
const loginPath = this.configManager.get('loginPath');
|
|
932
|
+
if (typeof loginPath === 'string') {
|
|
933
|
+
if (typeof window !== 'undefined') {
|
|
934
|
+
window.location.replace(loginPath);
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
else if (typeof loginPath === 'function') {
|
|
938
|
+
loginPath();
|
|
939
|
+
}
|
|
940
|
+
throw new AuthError('Unauthorized', error instanceof Error ? error : undefined);
|
|
1648
941
|
}
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
}
|
|
942
|
+
throw error;
|
|
943
|
+
}
|
|
944
|
+
}
|
|
1652
945
|
|
|
1653
946
|
/**
|
|
1654
947
|
* BeLink 业务请求客户端
|
|
1655
948
|
* 提供完整的认证、加密、时间同步等企业级功能
|
|
1656
949
|
*/
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
});
|
|
1667
|
-
Object.defineProperty(_this, "encryptionService", {
|
|
1668
|
-
enumerable: true,
|
|
1669
|
-
configurable: true,
|
|
1670
|
-
writable: true,
|
|
1671
|
-
value: void 0
|
|
1672
|
-
});
|
|
1673
|
-
Object.defineProperty(_this, "timeSyncService", {
|
|
1674
|
-
enumerable: true,
|
|
1675
|
-
configurable: true,
|
|
1676
|
-
writable: true,
|
|
1677
|
-
value: void 0
|
|
1678
|
-
});
|
|
1679
|
-
Object.defineProperty(_this, "token", {
|
|
1680
|
-
enumerable: true,
|
|
1681
|
-
configurable: true,
|
|
1682
|
-
writable: true,
|
|
1683
|
-
value: null
|
|
1684
|
-
});
|
|
1685
|
-
Object.defineProperty(_this, "initialized", {
|
|
1686
|
-
enumerable: true,
|
|
1687
|
-
configurable: true,
|
|
1688
|
-
writable: true,
|
|
1689
|
-
value: false
|
|
1690
|
-
});
|
|
1691
|
-
Object.defineProperty(_this, "tokenListenerCleanup", {
|
|
1692
|
-
enumerable: true,
|
|
1693
|
-
configurable: true,
|
|
1694
|
-
writable: true,
|
|
1695
|
-
value: null
|
|
1696
|
-
});
|
|
1697
|
-
Object.defineProperty(_this, "tokenHeaderName", {
|
|
1698
|
-
enumerable: true,
|
|
1699
|
-
configurable: true,
|
|
1700
|
-
writable: true,
|
|
1701
|
-
value: TOKEN_HEADER
|
|
1702
|
-
});
|
|
1703
|
-
Object.defineProperty(_this, "tokenStorageKey", {
|
|
1704
|
-
enumerable: true,
|
|
1705
|
-
configurable: true,
|
|
1706
|
-
writable: true,
|
|
1707
|
-
value: STORAGE_KEYS.TOKEN
|
|
1708
|
-
});
|
|
1709
|
-
_this.configManager = ConfigManager.getInstance();
|
|
950
|
+
class BeLinkClient extends HttpClient {
|
|
951
|
+
constructor() {
|
|
952
|
+
super();
|
|
953
|
+
this.token = null;
|
|
954
|
+
this.initialized = false;
|
|
955
|
+
this.tokenListenerCleanup = null;
|
|
956
|
+
this.tokenHeaderName = TOKEN_HEADER;
|
|
957
|
+
this.tokenStorageKey = STORAGE_KEYS.TOKEN;
|
|
958
|
+
this.configManager = ConfigManager.getInstance();
|
|
1710
959
|
// 先初始化 timeSyncService
|
|
1711
|
-
|
|
960
|
+
this.timeSyncService = new TimeSyncService(this.configManager.get('timeSync') || { enabled: true }, this.configManager.get('mode'));
|
|
1712
961
|
// 再初始化 encryptionService,注入 timeSyncService
|
|
1713
|
-
|
|
1714
|
-
return _this;
|
|
962
|
+
this.encryptionService = new EncryptionService(this.configManager.get('encryption') || { key: '214c!dsf1214c!ds', iv: 'icalqai23cfhgj!s' }, this.timeSyncService);
|
|
1715
963
|
}
|
|
1716
964
|
/**
|
|
1717
965
|
* 初始化客户端
|
|
1718
966
|
*/
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
this.timeSyncService.setMode(config.mode);
|
|
1733
|
-
// 设置 baseUrl
|
|
1734
|
-
if (config.baseUrl) {
|
|
1735
|
-
this.adapter.setBaseUrl(config.baseUrl);
|
|
1736
|
-
}
|
|
1737
|
-
// 设置 tokenHeaderName(请求头名称)
|
|
1738
|
-
if (config.tokenHeaderName) {
|
|
1739
|
-
this.tokenHeaderName = config.tokenHeaderName;
|
|
1740
|
-
}
|
|
1741
|
-
// 设置 tokenStorageKey(localStorage 存储键名)
|
|
1742
|
-
if (config.tokenStorageKey) {
|
|
1743
|
-
this.tokenStorageKey = config.tokenStorageKey;
|
|
1744
|
-
}
|
|
1745
|
-
// 设置默认拦截器
|
|
1746
|
-
if (!this.initialized) {
|
|
1747
|
-
this.setupDefaultInterceptors();
|
|
1748
|
-
this.initialized = true;
|
|
1749
|
-
}
|
|
967
|
+
init(config) {
|
|
968
|
+
this.configManager.init({
|
|
969
|
+
mode: config.mode,
|
|
970
|
+
loginPath: config.loginPath,
|
|
971
|
+
baseUrl: config.baseUrl,
|
|
972
|
+
tokenHeaderName: config.tokenHeaderName,
|
|
973
|
+
tokenStorageKey: config.tokenStorageKey,
|
|
974
|
+
});
|
|
975
|
+
// 更新时间同步服务的模式
|
|
976
|
+
this.timeSyncService.setMode(config.mode);
|
|
977
|
+
// 设置 baseUrl
|
|
978
|
+
if (config.baseUrl) {
|
|
979
|
+
this.adapter.setBaseUrl(config.baseUrl);
|
|
1750
980
|
}
|
|
1751
|
-
|
|
981
|
+
// 设置 tokenHeaderName(请求头名称)
|
|
982
|
+
if (config.tokenHeaderName) {
|
|
983
|
+
this.tokenHeaderName = config.tokenHeaderName;
|
|
984
|
+
}
|
|
985
|
+
// 设置 tokenStorageKey(localStorage 存储键名)
|
|
986
|
+
if (config.tokenStorageKey) {
|
|
987
|
+
this.tokenStorageKey = config.tokenStorageKey;
|
|
988
|
+
}
|
|
989
|
+
// 设置默认拦截器
|
|
990
|
+
if (!this.initialized) {
|
|
991
|
+
this.setupDefaultInterceptors();
|
|
992
|
+
this.initialized = true;
|
|
993
|
+
}
|
|
994
|
+
}
|
|
1752
995
|
/**
|
|
1753
996
|
* 设置 Token
|
|
1754
997
|
*/
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
writable: true,
|
|
1759
|
-
value: function (token) {
|
|
1760
|
-
this.token = token;
|
|
1761
|
-
}
|
|
1762
|
-
});
|
|
998
|
+
setToken(token) {
|
|
999
|
+
this.token = token;
|
|
1000
|
+
}
|
|
1763
1001
|
/**
|
|
1764
1002
|
* 获取当前 Token
|
|
1765
1003
|
*/
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
writable: true,
|
|
1770
|
-
value: function () {
|
|
1771
|
-
return this.token;
|
|
1772
|
-
}
|
|
1773
|
-
});
|
|
1004
|
+
getToken() {
|
|
1005
|
+
return this.token;
|
|
1006
|
+
}
|
|
1774
1007
|
/**
|
|
1775
1008
|
* 设置 Token 监听器
|
|
1776
1009
|
* @returns 清理函数,用于移除监听器
|
|
1777
1010
|
*/
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1011
|
+
setupTokenListener(tokenKey) {
|
|
1012
|
+
// 清理之前的监听器
|
|
1013
|
+
if (this.tokenListenerCleanup) {
|
|
1014
|
+
this.tokenListenerCleanup();
|
|
1015
|
+
this.tokenListenerCleanup = null;
|
|
1016
|
+
}
|
|
1017
|
+
// 非浏览器环境返回空操作
|
|
1018
|
+
if (!isBrowser()) {
|
|
1019
|
+
return () => { };
|
|
1020
|
+
}
|
|
1021
|
+
const getToken = () => {
|
|
1022
|
+
if (typeof tokenKey === 'string') {
|
|
1023
|
+
return window.localStorage.getItem(tokenKey);
|
|
1788
1024
|
}
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
return function () { };
|
|
1025
|
+
else if (typeof tokenKey === 'function') {
|
|
1026
|
+
return tokenKey();
|
|
1792
1027
|
}
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
}
|
|
1800
|
-
return null;
|
|
1801
|
-
};
|
|
1802
|
-
var storageHandler = function (event) {
|
|
1803
|
-
if (typeof tokenKey === 'string' && event.key === tokenKey) {
|
|
1804
|
-
if (event.newValue) {
|
|
1805
|
-
_this.setToken(event.newValue);
|
|
1806
|
-
}
|
|
1028
|
+
return null;
|
|
1029
|
+
};
|
|
1030
|
+
const storageHandler = (event) => {
|
|
1031
|
+
if (typeof tokenKey === 'string' && event.key === tokenKey) {
|
|
1032
|
+
if (event.newValue) {
|
|
1033
|
+
this.setToken(event.newValue);
|
|
1807
1034
|
}
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1035
|
+
}
|
|
1036
|
+
if (typeof tokenKey === 'function') {
|
|
1037
|
+
const token = tokenKey();
|
|
1038
|
+
if (token) {
|
|
1039
|
+
this.setToken(token);
|
|
1813
1040
|
}
|
|
1814
|
-
};
|
|
1815
|
-
window.addEventListener('storage', storageHandler);
|
|
1816
|
-
// 初始化时获取 token
|
|
1817
|
-
var token = getToken();
|
|
1818
|
-
if (token) {
|
|
1819
|
-
this.setToken(token);
|
|
1820
1041
|
}
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1042
|
+
};
|
|
1043
|
+
window.addEventListener('storage', storageHandler);
|
|
1044
|
+
// 初始化时获取 token
|
|
1045
|
+
const token = getToken();
|
|
1046
|
+
if (token) {
|
|
1047
|
+
this.setToken(token);
|
|
1827
1048
|
}
|
|
1828
|
-
|
|
1049
|
+
// 创建并存储清理函数
|
|
1050
|
+
const cleanup = () => {
|
|
1051
|
+
window.removeEventListener('storage', storageHandler);
|
|
1052
|
+
};
|
|
1053
|
+
this.tokenListenerCleanup = cleanup;
|
|
1054
|
+
return cleanup;
|
|
1055
|
+
}
|
|
1829
1056
|
/**
|
|
1830
1057
|
* 设置全局拦截器(向后兼容)
|
|
1831
1058
|
*/
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
writable: true,
|
|
1836
|
-
value: function () {
|
|
1837
|
-
// 已在 init 中自动设置
|
|
1838
|
-
}
|
|
1839
|
-
});
|
|
1059
|
+
setupInterceptors() {
|
|
1060
|
+
// 已在 init 中自动设置
|
|
1061
|
+
}
|
|
1840
1062
|
/**
|
|
1841
1063
|
* 设置认证回调(向后兼容)
|
|
1842
1064
|
*/
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
writable: true,
|
|
1847
|
-
value: function (path) {
|
|
1848
|
-
this.configManager.set('loginPath', path);
|
|
1849
|
-
}
|
|
1850
|
-
});
|
|
1065
|
+
setupAuthorizationCallback(path) {
|
|
1066
|
+
this.configManager.set('loginPath', path);
|
|
1067
|
+
}
|
|
1851
1068
|
/**
|
|
1852
1069
|
* 清除请求时间
|
|
1853
1070
|
*/
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
writable: true,
|
|
1858
|
-
value: function () {
|
|
1859
|
-
return __awaiter$3(this, void 0, void 0, function () {
|
|
1860
|
-
return __generator$3(this, function (_a) {
|
|
1861
|
-
this.timeSyncService.clear();
|
|
1862
|
-
return [2 /*return*/];
|
|
1863
|
-
});
|
|
1864
|
-
});
|
|
1865
|
-
}
|
|
1866
|
-
});
|
|
1071
|
+
async removeRequestTime() {
|
|
1072
|
+
this.timeSyncService.clear();
|
|
1073
|
+
}
|
|
1867
1074
|
/**
|
|
1868
1075
|
* @deprecated 请使用 removeRequestTime(修正拼写后的版本)
|
|
1869
1076
|
* 清除请求时间(向后兼容)
|
|
1870
1077
|
*/
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
writable: true,
|
|
1875
|
-
value: function () {
|
|
1876
|
-
return __awaiter$3(this, void 0, void 0, function () {
|
|
1877
|
-
return __generator$3(this, function (_a) {
|
|
1878
|
-
return [2 /*return*/, this.removeRequestTime()];
|
|
1879
|
-
});
|
|
1880
|
-
});
|
|
1881
|
-
}
|
|
1882
|
-
});
|
|
1078
|
+
async removeReqeustTime() {
|
|
1079
|
+
return this.removeRequestTime();
|
|
1080
|
+
}
|
|
1883
1081
|
/**
|
|
1884
1082
|
* 设置请求时间
|
|
1885
1083
|
*/
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
return __awaiter$3(this, void 0, void 0, function () {
|
|
1892
|
-
var headers;
|
|
1893
|
-
return __generator$3(this, function (_a) {
|
|
1894
|
-
switch (_a.label) {
|
|
1895
|
-
case 0: return [4 /*yield*/, this.timeSyncService.ensureSync()];
|
|
1896
|
-
case 1:
|
|
1897
|
-
_a.sent();
|
|
1898
|
-
headers = this.timeSyncService.getTimeSyncHeaders();
|
|
1899
|
-
if (!config.headers) {
|
|
1900
|
-
config.headers = {};
|
|
1901
|
-
}
|
|
1902
|
-
Object.assign(config.headers, headers);
|
|
1903
|
-
return [2 /*return*/];
|
|
1904
|
-
}
|
|
1905
|
-
});
|
|
1906
|
-
});
|
|
1084
|
+
async setupRequestTime(config) {
|
|
1085
|
+
await this.timeSyncService.ensureSync();
|
|
1086
|
+
const headers = this.timeSyncService.getTimeSyncHeaders();
|
|
1087
|
+
if (!config.headers) {
|
|
1088
|
+
config.headers = {};
|
|
1907
1089
|
}
|
|
1908
|
-
|
|
1090
|
+
Object.assign(config.headers, headers);
|
|
1091
|
+
}
|
|
1909
1092
|
/**
|
|
1910
1093
|
* @deprecated 请使用 setupRequestTime(修正拼写后的版本)
|
|
1911
1094
|
* 设置请求时间(向后兼容)
|
|
1912
1095
|
*/
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
writable: true,
|
|
1917
|
-
value: function (config) {
|
|
1918
|
-
return __awaiter$3(this, void 0, void 0, function () {
|
|
1919
|
-
return __generator$3(this, function (_a) {
|
|
1920
|
-
return [2 /*return*/, this.setupRequestTime(config)];
|
|
1921
|
-
});
|
|
1922
|
-
});
|
|
1923
|
-
}
|
|
1924
|
-
});
|
|
1096
|
+
async setupReqeustTime(config) {
|
|
1097
|
+
return this.setupRequestTime(config);
|
|
1098
|
+
}
|
|
1925
1099
|
/**
|
|
1926
1100
|
* 设置 Token(向后兼容)
|
|
1927
1101
|
*/
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
var token = localStorage.getItem(this.tokenStorageKey);
|
|
1937
|
-
if (token) {
|
|
1938
|
-
if (!config.headers) {
|
|
1939
|
-
config.headers = {};
|
|
1940
|
-
}
|
|
1941
|
-
config.headers[this.tokenHeaderName] = this.encryptionService.encryptToken(token);
|
|
1102
|
+
setupToken(config) {
|
|
1103
|
+
if (!isBrowser()) {
|
|
1104
|
+
return;
|
|
1105
|
+
}
|
|
1106
|
+
const token = localStorage.getItem(this.tokenStorageKey);
|
|
1107
|
+
if (token) {
|
|
1108
|
+
if (!config.headers) {
|
|
1109
|
+
config.headers = {};
|
|
1942
1110
|
}
|
|
1111
|
+
config.headers[this.tokenHeaderName] = this.encryptionService.encryptToken(token);
|
|
1943
1112
|
}
|
|
1944
|
-
}
|
|
1113
|
+
}
|
|
1945
1114
|
/**
|
|
1946
1115
|
* 重写请求方法(向后兼容旧版 API)
|
|
1947
1116
|
*/
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
value: function (config) {
|
|
1953
|
-
return __awaiter$3(this, void 0, void 0, function () {
|
|
1954
|
-
return __generator$3(this, function (_a) {
|
|
1955
|
-
// 处理 customToken
|
|
1956
|
-
if (config.customToken) {
|
|
1957
|
-
this.setToken(config.customToken);
|
|
1958
|
-
}
|
|
1959
|
-
// 处理旧版拦截器配置(使用基类的共享方法)
|
|
1960
|
-
if (config.requestInterceptors || config.responseInterceptors) {
|
|
1961
|
-
return [2 /*return*/, this.requestWithLegacyInterceptors(config)];
|
|
1962
|
-
}
|
|
1963
|
-
return [2 /*return*/, _super.prototype.request.call(this, config)];
|
|
1964
|
-
});
|
|
1965
|
-
});
|
|
1117
|
+
async request(config) {
|
|
1118
|
+
// 处理 customToken
|
|
1119
|
+
if (config.customToken) {
|
|
1120
|
+
this.setToken(config.customToken);
|
|
1966
1121
|
}
|
|
1967
|
-
|
|
1122
|
+
// 处理旧版拦截器配置(使用基类的共享方法)
|
|
1123
|
+
if (config.requestInterceptors || config.responseInterceptors) {
|
|
1124
|
+
return this.requestWithLegacyInterceptors(config);
|
|
1125
|
+
}
|
|
1126
|
+
return super.request(config);
|
|
1127
|
+
}
|
|
1968
1128
|
/**
|
|
1969
1129
|
* 设置默认拦截器
|
|
1970
1130
|
*/
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
this.addResponseInterceptor(new AuthInterceptor(this.configManager, this.timeSyncService, {
|
|
1988
|
-
priority: 1,
|
|
1989
|
-
}));
|
|
1990
|
-
}
|
|
1991
|
-
});
|
|
1992
|
-
return BeLinkClient;
|
|
1993
|
-
}(HttpClient));
|
|
1131
|
+
setupDefaultInterceptors() {
|
|
1132
|
+
// 时间同步拦截器
|
|
1133
|
+
this.addRequestInterceptor(new TimeInterceptor(this.timeSyncService, {
|
|
1134
|
+
priority: 1,
|
|
1135
|
+
}));
|
|
1136
|
+
// Token 拦截器
|
|
1137
|
+
this.addRequestInterceptor(new TokenInterceptor(() => this.token || localStorage.getItem(this.tokenStorageKey), this.encryptionService, {
|
|
1138
|
+
priority: 2,
|
|
1139
|
+
headerName: this.tokenHeaderName,
|
|
1140
|
+
}));
|
|
1141
|
+
// 认证拦截器(处理 401)
|
|
1142
|
+
this.addResponseInterceptor(new AuthInterceptor(this.configManager, this.timeSyncService, {
|
|
1143
|
+
priority: 1,
|
|
1144
|
+
}));
|
|
1145
|
+
}
|
|
1146
|
+
}
|
|
1994
1147
|
|
|
1995
1148
|
// 创建并导出单例实例
|
|
1996
|
-
|
|
1149
|
+
const beLinkRequest = new BeLinkClient();
|
|
1997
1150
|
|
|
1998
1151
|
/**
|
|
1999
|
-
*
|
|
2000
|
-
*
|
|
1152
|
+
* LiveClient 业务请求客户端
|
|
1153
|
+
* 提供完整的认证、加密、时间同步等企业级功能
|
|
2001
1154
|
*/
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
1155
|
+
class LiveClient extends HttpClient {
|
|
1156
|
+
constructor() {
|
|
1157
|
+
super();
|
|
1158
|
+
this.token = '';
|
|
1159
|
+
this.userId = '';
|
|
1160
|
+
this.initialized = false;
|
|
1161
|
+
this.tokenHeaderName = TOKEN_HEADER;
|
|
1162
|
+
this.tokenStorageKey = STORAGE_KEYS.TOKEN;
|
|
1163
|
+
this.configManager = ConfigManager.getInstance();
|
|
1164
|
+
// 先初始化 timeSyncService
|
|
1165
|
+
this.timeSyncService = new TimeSyncService(this.configManager.get('timeSync') || { enabled: true }, this.configManager.get('mode'));
|
|
1166
|
+
// 再初始化 encryptionService,注入 timeSyncService
|
|
1167
|
+
this.encryptionService = new EncryptionService(this.configManager.get('encryption') || { key: '214c!dsf1214c!ds', iv: 'icalqai23cfhgj!s' }, this.timeSyncService);
|
|
2013
1168
|
}
|
|
2014
1169
|
/**
|
|
2015
1170
|
* 初始化客户端
|
|
2016
1171
|
*/
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
1172
|
+
init(config) {
|
|
1173
|
+
this.configManager.init({
|
|
1174
|
+
mode: config.mode,
|
|
1175
|
+
baseUrl: config.baseUrl,
|
|
1176
|
+
timeSync: config.timeSyncConfig,
|
|
1177
|
+
encryption: config.encryption,
|
|
1178
|
+
tokenHeaderName: config.tokenHeaderName,
|
|
1179
|
+
tokenStorageKey: config.tokenStorageKey,
|
|
1180
|
+
});
|
|
1181
|
+
// 更新时间同步服务的模式
|
|
1182
|
+
this.timeSyncService.setMode(config.mode);
|
|
1183
|
+
// 设置 baseUrl
|
|
1184
|
+
if (config.baseUrl) {
|
|
1185
|
+
this.adapter.setBaseUrl(config.baseUrl);
|
|
2028
1186
|
}
|
|
2029
|
-
|
|
1187
|
+
// 设置 tokenHeaderName(请求头名称)
|
|
1188
|
+
if (config.tokenHeaderName) {
|
|
1189
|
+
this.tokenHeaderName = config.tokenHeaderName;
|
|
1190
|
+
}
|
|
1191
|
+
// 设置 tokenStorageKey(localStorage 存储键名)
|
|
1192
|
+
if (config.tokenStorageKey) {
|
|
1193
|
+
this.tokenStorageKey = config.tokenStorageKey;
|
|
1194
|
+
}
|
|
1195
|
+
if (config.token) {
|
|
1196
|
+
this.token = config.token;
|
|
1197
|
+
}
|
|
1198
|
+
if (config.userId) {
|
|
1199
|
+
this.userId = config.userId;
|
|
1200
|
+
}
|
|
1201
|
+
// 设置默认拦截器
|
|
1202
|
+
if (!this.initialized) {
|
|
1203
|
+
this.setupDefaultInterceptors();
|
|
1204
|
+
this.initialized = true;
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
setUserId(userId) {
|
|
1208
|
+
this.userId = userId;
|
|
1209
|
+
}
|
|
2030
1210
|
/**
|
|
2031
|
-
*
|
|
1211
|
+
* 获取当前 Token
|
|
2032
1212
|
*/
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
1213
|
+
getToken() {
|
|
1214
|
+
return this.token;
|
|
1215
|
+
}
|
|
1216
|
+
/**
|
|
1217
|
+
* 清除请求时间
|
|
1218
|
+
*/
|
|
1219
|
+
async removeRequestTime() {
|
|
1220
|
+
this.timeSyncService.clear();
|
|
1221
|
+
}
|
|
1222
|
+
/**
|
|
1223
|
+
* 设置请求时间
|
|
1224
|
+
*/
|
|
1225
|
+
async setupRequestTime(config) {
|
|
1226
|
+
await this.timeSyncService.ensureSync();
|
|
1227
|
+
const headers = this.timeSyncService.getTimeSyncHeaders();
|
|
1228
|
+
if (!config.headers) {
|
|
1229
|
+
config.headers = {};
|
|
2039
1230
|
}
|
|
2040
|
-
|
|
1231
|
+
Object.assign(config.headers, headers);
|
|
1232
|
+
}
|
|
1233
|
+
/**
|
|
1234
|
+
* 设置默认拦截器
|
|
1235
|
+
*/
|
|
1236
|
+
setupDefaultInterceptors() {
|
|
1237
|
+
// 时间同步拦截器
|
|
1238
|
+
this.addRequestInterceptor(new TimeInterceptor(this.timeSyncService, { priority: 1 }));
|
|
1239
|
+
// Token 拦截器
|
|
1240
|
+
if (this.encryptionService) {
|
|
1241
|
+
this.addRequestInterceptor(new TokenInterceptor(() => this.token || localStorage.getItem(this.tokenStorageKey), this.encryptionService, {
|
|
1242
|
+
priority: 2,
|
|
1243
|
+
headerName: this.tokenHeaderName,
|
|
1244
|
+
userId: this.userId,
|
|
1245
|
+
}));
|
|
1246
|
+
}
|
|
1247
|
+
// 认证拦截器(处理 401)
|
|
1248
|
+
this.addResponseInterceptor(new AuthInterceptor(this.configManager, this.timeSyncService, { priority: 1 }));
|
|
1249
|
+
}
|
|
2041
1250
|
/**
|
|
2042
1251
|
* 重写请求方法(向后兼容旧版 API)
|
|
2043
1252
|
*/
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
value: function (config) {
|
|
2049
|
-
return __awaiter$3(this, void 0, void 0, function () {
|
|
2050
|
-
return __generator$3(this, function (_a) {
|
|
2051
|
-
// 处理旧版拦截器配置(使用基类的共享方法)
|
|
2052
|
-
if (config.requestInterceptors || config.responseInterceptors) {
|
|
2053
|
-
return [2 /*return*/, this.requestWithLegacyInterceptors(config)];
|
|
2054
|
-
}
|
|
2055
|
-
return [2 /*return*/, _super.prototype.request.call(this, config)];
|
|
2056
|
-
});
|
|
2057
|
-
});
|
|
1253
|
+
async request(config) {
|
|
1254
|
+
// 处理旧版拦截器配置(使用基类的共享方法)
|
|
1255
|
+
if (config.requestInterceptors || config.responseInterceptors) {
|
|
1256
|
+
return this.requestWithLegacyInterceptors(config);
|
|
2058
1257
|
}
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
}
|
|
1258
|
+
return super.request(config);
|
|
1259
|
+
}
|
|
1260
|
+
}
|
|
2062
1261
|
|
|
2063
1262
|
// 创建并导出单例实例
|
|
2064
|
-
|
|
1263
|
+
const beLinkLiveRequest = new LiveClient();
|
|
2065
1264
|
|
|
2066
1265
|
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
|
2067
1266
|
|
|
@@ -6141,167 +5340,111 @@ catch (e) { }
|
|
|
6141
5340
|
* 用户信息服务
|
|
6142
5341
|
* 单例模式,用于存储和获取当前用户信息
|
|
6143
5342
|
*/
|
|
6144
|
-
|
|
6145
|
-
|
|
6146
|
-
|
|
6147
|
-
|
|
6148
|
-
|
|
6149
|
-
writable: true,
|
|
6150
|
-
value: {
|
|
6151
|
-
name: '',
|
|
6152
|
-
}
|
|
6153
|
-
});
|
|
5343
|
+
class UserInfoService {
|
|
5344
|
+
constructor() {
|
|
5345
|
+
this.userInfo = {
|
|
5346
|
+
name: '',
|
|
5347
|
+
};
|
|
6154
5348
|
}
|
|
6155
5349
|
/**
|
|
6156
5350
|
* 初始化用户信息
|
|
6157
5351
|
*/
|
|
6158
|
-
|
|
6159
|
-
|
|
6160
|
-
|
|
6161
|
-
writable: true,
|
|
6162
|
-
value: function (userInfo) {
|
|
6163
|
-
this.userInfo = userInfo || { name: '' };
|
|
6164
|
-
}
|
|
6165
|
-
});
|
|
5352
|
+
init(userInfo) {
|
|
5353
|
+
this.userInfo = userInfo || { name: '' };
|
|
5354
|
+
}
|
|
6166
5355
|
/**
|
|
6167
5356
|
* 获取用户信息
|
|
6168
5357
|
*/
|
|
6169
|
-
|
|
6170
|
-
|
|
6171
|
-
|
|
6172
|
-
writable: true,
|
|
6173
|
-
value: function () {
|
|
6174
|
-
return this.userInfo;
|
|
6175
|
-
}
|
|
6176
|
-
});
|
|
5358
|
+
getParams() {
|
|
5359
|
+
return this.userInfo;
|
|
5360
|
+
}
|
|
6177
5361
|
/**
|
|
6178
5362
|
* 更新用户信息
|
|
6179
5363
|
*/
|
|
6180
|
-
|
|
6181
|
-
|
|
6182
|
-
|
|
6183
|
-
writable: true,
|
|
6184
|
-
value: function (userInfo) {
|
|
6185
|
-
this.userInfo = __assign$4(__assign$4({}, this.userInfo), userInfo);
|
|
6186
|
-
}
|
|
6187
|
-
});
|
|
5364
|
+
update(userInfo) {
|
|
5365
|
+
this.userInfo = { ...this.userInfo, ...userInfo };
|
|
5366
|
+
}
|
|
6188
5367
|
/**
|
|
6189
5368
|
* 清除用户信息
|
|
6190
5369
|
*/
|
|
6191
|
-
|
|
6192
|
-
|
|
6193
|
-
|
|
6194
|
-
|
|
6195
|
-
value: function () {
|
|
6196
|
-
this.userInfo = { name: '' };
|
|
6197
|
-
}
|
|
6198
|
-
});
|
|
6199
|
-
return UserInfoService;
|
|
6200
|
-
}());
|
|
5370
|
+
clear() {
|
|
5371
|
+
this.userInfo = { name: '' };
|
|
5372
|
+
}
|
|
5373
|
+
}
|
|
6201
5374
|
// 导出单例实例
|
|
6202
|
-
|
|
5375
|
+
const beLinkUserInfo = new UserInfoService();
|
|
6203
5376
|
|
|
6204
5377
|
/**
|
|
6205
5378
|
* 云函数客户端
|
|
6206
5379
|
* 封装腾讯云 CloudBase SDK
|
|
6207
5380
|
*/
|
|
6208
|
-
|
|
6209
|
-
|
|
6210
|
-
|
|
6211
|
-
enumerable: true,
|
|
6212
|
-
configurable: true,
|
|
6213
|
-
writable: true,
|
|
6214
|
-
value: null
|
|
6215
|
-
});
|
|
5381
|
+
class CloudFunctionClient {
|
|
5382
|
+
constructor() {
|
|
5383
|
+
this.app = null;
|
|
6216
5384
|
}
|
|
6217
5385
|
/**
|
|
6218
5386
|
* 初始化 CloudBase App
|
|
6219
5387
|
*/
|
|
6220
|
-
|
|
6221
|
-
|
|
6222
|
-
configurable: true,
|
|
6223
|
-
writable: true,
|
|
6224
|
-
value: function () {
|
|
6225
|
-
if (this.app) {
|
|
6226
|
-
return this.app;
|
|
6227
|
-
}
|
|
6228
|
-
var configManager = ConfigManager.getInstance();
|
|
6229
|
-
var cloudbaseConfig = configManager.get('cloudbase');
|
|
6230
|
-
var env = detectEnvironment();
|
|
6231
|
-
var envId = (cloudbaseConfig === null || cloudbaseConfig === void 0 ? void 0 : cloudbaseConfig[env]) || 'dev-1gpp53ju3ceb46c7';
|
|
6232
|
-
var region = (cloudbaseConfig === null || cloudbaseConfig === void 0 ? void 0 : cloudbaseConfig.region) || 'ap-shanghai';
|
|
6233
|
-
var timeout = (cloudbaseConfig === null || cloudbaseConfig === void 0 ? void 0 : cloudbaseConfig.timeout) || 60000;
|
|
6234
|
-
var options = {
|
|
6235
|
-
region: region,
|
|
6236
|
-
env: envId,
|
|
6237
|
-
timeout: timeout,
|
|
6238
|
-
};
|
|
6239
|
-
this.app = cloudbase.init(options);
|
|
5388
|
+
initApp() {
|
|
5389
|
+
if (this.app) {
|
|
6240
5390
|
return this.app;
|
|
6241
5391
|
}
|
|
6242
|
-
|
|
5392
|
+
const configManager = ConfigManager.getInstance();
|
|
5393
|
+
const cloudbaseConfig = configManager.get('cloudbase');
|
|
5394
|
+
const env = detectEnvironment();
|
|
5395
|
+
const envId = cloudbaseConfig?.[env] || 'dev-1gpp53ju3ceb46c7';
|
|
5396
|
+
const region = cloudbaseConfig?.region || 'ap-shanghai';
|
|
5397
|
+
const timeout = cloudbaseConfig?.timeout || 60000;
|
|
5398
|
+
const options = {
|
|
5399
|
+
region,
|
|
5400
|
+
env: envId,
|
|
5401
|
+
timeout,
|
|
5402
|
+
};
|
|
5403
|
+
this.app = cloudbase.init(options);
|
|
5404
|
+
return this.app;
|
|
5405
|
+
}
|
|
6243
5406
|
/**
|
|
6244
5407
|
* 调用云函数
|
|
6245
5408
|
*/
|
|
6246
|
-
|
|
6247
|
-
|
|
6248
|
-
|
|
6249
|
-
|
|
6250
|
-
|
|
6251
|
-
|
|
6252
|
-
|
|
6253
|
-
|
|
6254
|
-
|
|
6255
|
-
case 0:
|
|
6256
|
-
app = this.initApp();
|
|
6257
|
-
data = __assign$4({}, params.data);
|
|
6258
|
-
// 请求拦截:自动注入用户信息
|
|
6259
|
-
if (!data.operateUser) {
|
|
6260
|
-
data.operateUser = __assign$4({}, beLinkUserInfo.getParams());
|
|
6261
|
-
}
|
|
6262
|
-
callParams = __assign$4(__assign$4({}, params), { data: data });
|
|
6263
|
-
return [4 /*yield*/, app.callFunction(callParams)];
|
|
6264
|
-
case 1:
|
|
6265
|
-
result = _a.sent();
|
|
6266
|
-
// 响应拦截
|
|
6267
|
-
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
6268
|
-
var _a;
|
|
6269
|
-
if (!((_a = result.result) === null || _a === void 0 ? void 0 : _a.success)) {
|
|
6270
|
-
reject(result.result);
|
|
6271
|
-
}
|
|
6272
|
-
else {
|
|
6273
|
-
resolve(result.result.data);
|
|
6274
|
-
}
|
|
6275
|
-
})];
|
|
6276
|
-
}
|
|
6277
|
-
});
|
|
6278
|
-
});
|
|
5409
|
+
async call(params) {
|
|
5410
|
+
const app = this.initApp();
|
|
5411
|
+
// 创建数据副本,避免修改原始参数
|
|
5412
|
+
const data = { ...params.data };
|
|
5413
|
+
// 请求拦截:自动注入用户信息
|
|
5414
|
+
if (!data.operateUser) {
|
|
5415
|
+
data.operateUser = {
|
|
5416
|
+
...beLinkUserInfo.getParams(),
|
|
5417
|
+
};
|
|
6279
5418
|
}
|
|
6280
|
-
|
|
5419
|
+
const callParams = {
|
|
5420
|
+
...params,
|
|
5421
|
+
data,
|
|
5422
|
+
};
|
|
5423
|
+
const result = await app.callFunction(callParams);
|
|
5424
|
+
// 响应拦截
|
|
5425
|
+
return new Promise((resolve, reject) => {
|
|
5426
|
+
if (!result.result?.success) {
|
|
5427
|
+
reject(result.result);
|
|
5428
|
+
}
|
|
5429
|
+
else {
|
|
5430
|
+
resolve(result.result.data);
|
|
5431
|
+
}
|
|
5432
|
+
});
|
|
5433
|
+
}
|
|
6281
5434
|
/**
|
|
6282
5435
|
* 重置 App 实例
|
|
6283
5436
|
*/
|
|
6284
|
-
|
|
6285
|
-
|
|
6286
|
-
|
|
6287
|
-
|
|
6288
|
-
value: function () {
|
|
6289
|
-
this.app = null;
|
|
6290
|
-
}
|
|
6291
|
-
});
|
|
6292
|
-
return CloudFunctionClient;
|
|
6293
|
-
}());
|
|
5437
|
+
reset() {
|
|
5438
|
+
this.app = null;
|
|
5439
|
+
}
|
|
5440
|
+
}
|
|
6294
5441
|
// 创建单例
|
|
6295
|
-
|
|
5442
|
+
const cloudFunctionClient = new CloudFunctionClient();
|
|
6296
5443
|
/**
|
|
6297
5444
|
* 云函数调用函数(保持原 API 兼容)
|
|
6298
5445
|
*/
|
|
6299
|
-
function callFunction(params) {
|
|
6300
|
-
return
|
|
6301
|
-
return __generator$3(this, function (_a) {
|
|
6302
|
-
return [2 /*return*/, cloudFunctionClient.call(params)];
|
|
6303
|
-
});
|
|
6304
|
-
});
|
|
5446
|
+
async function callFunction(params) {
|
|
5447
|
+
return cloudFunctionClient.call(params);
|
|
6305
5448
|
}
|
|
6306
5449
|
|
|
6307
5450
|
export { AuthError, AuthInterceptor, AxiosAdapter, BaseError, BeLinkClient, ConfigError, ConfigManager, ContentType, DEFAULT_TIMEOUT, EncryptionService, HTTP_STATUS, HttpClient, HttpError, HttpMethod, LiveClient, NetworkError, STORAGE_KEYS, TIME_SYNC_HEADERS, TOKEN_HEADER, TimeInterceptor, TimeSyncService, TimeoutError, TokenInterceptor, beLinkLiveRequest, beLinkRequest, beLinkUserInfo, callFunction, detectEnvironment, isBrowser, isNode, request };
|