@be-link/request 1.45.1-beta.1 → 1.45.1-beta.3
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 +39 -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 +4 -0
- package/dist/core/constants/index.d.ts.map +1 -1
- package/dist/core/types/interceptor.types.d.ts +2 -5
- package/dist/core/types/interceptor.types.d.ts.map +1 -1
- package/dist/index.cjs.js +890 -1695
- package/dist/index.esm.js +890 -1695
- package/dist/infrastructure/services/TimeSyncService.d.ts +1 -1
- 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,22 +40,26 @@ 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',
|
|
61
65
|
TIME_DIFF: 'x-belink-timeDiff',
|
|
@@ -67,9 +71,9 @@ var STORAGE_KEYS = {
|
|
|
67
71
|
*/
|
|
68
72
|
function request(config) {
|
|
69
73
|
// 创建独立实例,避免拦截器泄漏
|
|
70
|
-
|
|
74
|
+
const instance = axios.create();
|
|
71
75
|
// 解析 HTTP 方法
|
|
72
|
-
|
|
76
|
+
const method = config.method || (config.data ? 'post' : 'get');
|
|
73
77
|
// 应用临时拦截器
|
|
74
78
|
if (config.requestInterceptors && typeof config.requestInterceptors === 'function') {
|
|
75
79
|
instance.interceptors.request.use(config.requestInterceptors);
|
|
@@ -77,18 +81,18 @@ function request(config) {
|
|
|
77
81
|
if (config.responseInterceptors && typeof config.responseInterceptors === 'function') {
|
|
78
82
|
instance.interceptors.response.use(config.responseInterceptors);
|
|
79
83
|
}
|
|
80
|
-
|
|
81
|
-
return new Promise(
|
|
84
|
+
const timeout = config.timeout || DEFAULT_TIMEOUT;
|
|
85
|
+
return new Promise((resolve, reject) => {
|
|
82
86
|
instance
|
|
83
87
|
.request({
|
|
84
|
-
method
|
|
88
|
+
method,
|
|
85
89
|
url: config.url,
|
|
86
90
|
headers: config.headers || {},
|
|
87
91
|
data: config.data,
|
|
88
92
|
params: config.params,
|
|
89
|
-
timeout
|
|
93
|
+
timeout,
|
|
90
94
|
})
|
|
91
|
-
.then(
|
|
95
|
+
.then((result) => {
|
|
92
96
|
if (result.status === 200) {
|
|
93
97
|
resolve(result.data);
|
|
94
98
|
}
|
|
@@ -96,140 +100,15 @@ function request(config) {
|
|
|
96
100
|
reject(result.data);
|
|
97
101
|
}
|
|
98
102
|
})
|
|
99
|
-
.catch(
|
|
100
|
-
var _a, _b;
|
|
103
|
+
.catch((reason) => {
|
|
101
104
|
if (config.catchCallback && typeof config.catchCallback === 'function') {
|
|
102
105
|
config.catchCallback(reason);
|
|
103
106
|
}
|
|
104
|
-
reject(
|
|
107
|
+
reject(reason?.response?.data ?? reason);
|
|
105
108
|
});
|
|
106
109
|
});
|
|
107
110
|
}
|
|
108
111
|
|
|
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
112
|
/**
|
|
234
113
|
* HTTP 方法枚举
|
|
235
114
|
*/
|
|
@@ -245,660 +124,376 @@ var HttpMethod;
|
|
|
245
124
|
/**
|
|
246
125
|
* 基础错误类
|
|
247
126
|
*/
|
|
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;
|
|
127
|
+
class BaseError extends Error {
|
|
128
|
+
constructor(message, code, originalError) {
|
|
129
|
+
super(message);
|
|
130
|
+
this.name = this.constructor.name;
|
|
131
|
+
this.code = code;
|
|
132
|
+
this.originalError = originalError;
|
|
133
|
+
Error.captureStackTrace(this, this.constructor);
|
|
269
134
|
}
|
|
270
|
-
|
|
271
|
-
}(Error));
|
|
135
|
+
}
|
|
272
136
|
/**
|
|
273
137
|
* HTTP 错误
|
|
274
138
|
*/
|
|
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;
|
|
139
|
+
class HttpError extends BaseError {
|
|
140
|
+
constructor(message, status, data, originalError) {
|
|
141
|
+
super(message, 'HTTP_ERROR', originalError);
|
|
142
|
+
this.status = status;
|
|
143
|
+
this.data = data;
|
|
294
144
|
}
|
|
295
|
-
|
|
296
|
-
}(BaseError));
|
|
145
|
+
}
|
|
297
146
|
/**
|
|
298
147
|
* 认证错误
|
|
299
148
|
*/
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
}
|
|
306
|
-
return AuthError;
|
|
307
|
-
}(BaseError));
|
|
149
|
+
class AuthError extends BaseError {
|
|
150
|
+
constructor(message = 'Unauthorized', originalError) {
|
|
151
|
+
super(message, 'AUTH_ERROR', originalError);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
308
154
|
/**
|
|
309
155
|
* 超时错误
|
|
310
156
|
*/
|
|
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;
|
|
157
|
+
class TimeoutError extends BaseError {
|
|
158
|
+
constructor(message, timeout, originalError) {
|
|
159
|
+
super(message, 'TIMEOUT_ERROR', originalError);
|
|
160
|
+
this.timeout = timeout;
|
|
323
161
|
}
|
|
324
|
-
|
|
325
|
-
}(BaseError));
|
|
162
|
+
}
|
|
326
163
|
/**
|
|
327
164
|
* 配置错误
|
|
328
165
|
*/
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
return _super.call(this, message, 'CONFIG_ERROR', originalError) || this;
|
|
166
|
+
class ConfigError extends BaseError {
|
|
167
|
+
constructor(message, originalError) {
|
|
168
|
+
super(message, 'CONFIG_ERROR', originalError);
|
|
333
169
|
}
|
|
334
|
-
|
|
335
|
-
}(BaseError));
|
|
170
|
+
}
|
|
336
171
|
/**
|
|
337
172
|
* 网络错误
|
|
338
173
|
*/
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
if (message === void 0) { message = 'Network error'; }
|
|
343
|
-
return _super.call(this, message, 'NETWORK_ERROR', originalError) || this;
|
|
174
|
+
class NetworkError extends BaseError {
|
|
175
|
+
constructor(message = 'Network error', originalError) {
|
|
176
|
+
super(message, 'NETWORK_ERROR', originalError);
|
|
344
177
|
}
|
|
345
|
-
|
|
346
|
-
}(BaseError));
|
|
178
|
+
}
|
|
347
179
|
|
|
348
180
|
/**
|
|
349
181
|
* Axios 适配器
|
|
350
182
|
* 封装 Axios 实例,提供统一的请求接口
|
|
351
183
|
*/
|
|
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
|
-
});
|
|
184
|
+
class AxiosAdapter {
|
|
185
|
+
constructor(baseURL, timeout) {
|
|
186
|
+
this.interceptorIds = {
|
|
187
|
+
request: [],
|
|
188
|
+
response: [],
|
|
189
|
+
};
|
|
369
190
|
this.instance = axios.create({
|
|
370
|
-
baseURL
|
|
191
|
+
baseURL,
|
|
371
192
|
timeout: timeout || DEFAULT_TIMEOUT,
|
|
372
193
|
});
|
|
373
194
|
}
|
|
374
195
|
/**
|
|
375
196
|
* 发起请求
|
|
376
197
|
*/
|
|
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
|
-
});
|
|
198
|
+
async request(config) {
|
|
199
|
+
const headers = { ...config.headers };
|
|
200
|
+
// 当有请求体时,默认设置 Content-Type 为 application/json
|
|
201
|
+
if (config.data && !headers['Content-Type'] && !headers['content-type']) {
|
|
202
|
+
headers['Content-Type'] = ContentType.JSON;
|
|
410
203
|
}
|
|
411
|
-
|
|
204
|
+
const axiosConfig = {
|
|
205
|
+
method: config.method || (config.data ? HttpMethod.POST : HttpMethod.GET),
|
|
206
|
+
url: config.url,
|
|
207
|
+
headers,
|
|
208
|
+
data: config.data,
|
|
209
|
+
params: config.params,
|
|
210
|
+
timeout: config.timeout || DEFAULT_TIMEOUT,
|
|
211
|
+
...config.axiosConfig,
|
|
212
|
+
};
|
|
213
|
+
try {
|
|
214
|
+
const response = await this.instance.request(axiosConfig);
|
|
215
|
+
if (response.status === 200) {
|
|
216
|
+
return response.data;
|
|
217
|
+
}
|
|
218
|
+
throw new HttpError(`Request failed with status ${response.status}`, response.status, response.data);
|
|
219
|
+
}
|
|
220
|
+
catch (error) {
|
|
221
|
+
throw this.handleError(error);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
412
224
|
/**
|
|
413
225
|
* 添加请求拦截器
|
|
414
226
|
* @returns 拦截器 ID
|
|
415
227
|
*/
|
|
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
|
-
});
|
|
228
|
+
addRequestInterceptor(onFulfilled, onRejected) {
|
|
229
|
+
const id = this.instance.interceptors.request.use(onFulfilled, onRejected);
|
|
230
|
+
this.interceptorIds.request.push(id);
|
|
231
|
+
return id;
|
|
232
|
+
}
|
|
426
233
|
/**
|
|
427
234
|
* 添加响应拦截器
|
|
428
235
|
* @returns 拦截器 ID
|
|
429
236
|
*/
|
|
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
|
-
});
|
|
237
|
+
addResponseInterceptor(onFulfilled, onRejected) {
|
|
238
|
+
const id = this.instance.interceptors.response.use(onFulfilled, onRejected);
|
|
239
|
+
this.interceptorIds.response.push(id);
|
|
240
|
+
return id;
|
|
241
|
+
}
|
|
440
242
|
/**
|
|
441
243
|
* 移除请求拦截器
|
|
442
244
|
*/
|
|
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
|
-
}
|
|
245
|
+
removeRequestInterceptor(id) {
|
|
246
|
+
this.instance.interceptors.request.eject(id);
|
|
247
|
+
const index = this.interceptorIds.request.indexOf(id);
|
|
248
|
+
if (index > -1) {
|
|
249
|
+
this.interceptorIds.request.splice(index, 1);
|
|
453
250
|
}
|
|
454
|
-
}
|
|
251
|
+
}
|
|
455
252
|
/**
|
|
456
253
|
* 移除响应拦截器
|
|
457
254
|
*/
|
|
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
|
-
}
|
|
255
|
+
removeResponseInterceptor(id) {
|
|
256
|
+
this.instance.interceptors.response.eject(id);
|
|
257
|
+
const index = this.interceptorIds.response.indexOf(id);
|
|
258
|
+
if (index > -1) {
|
|
259
|
+
this.interceptorIds.response.splice(index, 1);
|
|
468
260
|
}
|
|
469
|
-
}
|
|
261
|
+
}
|
|
470
262
|
/**
|
|
471
263
|
* 清理所有拦截器
|
|
472
264
|
*/
|
|
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
|
-
});
|
|
265
|
+
clearAllInterceptors() {
|
|
266
|
+
this.interceptorIds.request.forEach((id) => {
|
|
267
|
+
this.instance.interceptors.request.eject(id);
|
|
268
|
+
});
|
|
269
|
+
this.interceptorIds.response.forEach((id) => {
|
|
270
|
+
this.instance.interceptors.response.eject(id);
|
|
271
|
+
});
|
|
272
|
+
this.interceptorIds = { request: [], response: [] };
|
|
273
|
+
}
|
|
488
274
|
/**
|
|
489
275
|
* 获取 Axios 实例
|
|
490
276
|
*/
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
writable: true,
|
|
495
|
-
value: function () {
|
|
496
|
-
return this.instance;
|
|
497
|
-
}
|
|
498
|
-
});
|
|
277
|
+
getInstance() {
|
|
278
|
+
return this.instance;
|
|
279
|
+
}
|
|
499
280
|
/**
|
|
500
281
|
* 设置 BaseURL
|
|
501
282
|
*/
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
writable: true,
|
|
506
|
-
value: function (baseUrl) {
|
|
507
|
-
this.instance.defaults.baseURL = baseUrl;
|
|
508
|
-
}
|
|
509
|
-
});
|
|
283
|
+
setBaseUrl(baseUrl) {
|
|
284
|
+
this.instance.defaults.baseURL = baseUrl;
|
|
285
|
+
}
|
|
510
286
|
/**
|
|
511
287
|
* 处理错误
|
|
512
288
|
*/
|
|
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);
|
|
289
|
+
handleError(error) {
|
|
290
|
+
if (isAxiosError(error)) {
|
|
291
|
+
const axiosError = error;
|
|
292
|
+
if (axiosError.code === 'ECONNABORTED') {
|
|
293
|
+
return new TimeoutError(`Request timeout after ${axiosError.config?.timeout}ms`, axiosError.config?.timeout || DEFAULT_TIMEOUT, axiosError);
|
|
528
294
|
}
|
|
529
|
-
if (
|
|
530
|
-
return error;
|
|
295
|
+
if (!axiosError.response) {
|
|
296
|
+
return new NetworkError('Network error: Unable to reach server', axiosError);
|
|
531
297
|
}
|
|
532
|
-
return new
|
|
298
|
+
return new HttpError(axiosError.message, axiosError.response.status, axiosError.response.data, axiosError);
|
|
533
299
|
}
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
}
|
|
300
|
+
if (error instanceof Error) {
|
|
301
|
+
return error;
|
|
302
|
+
}
|
|
303
|
+
return new Error(String(error));
|
|
304
|
+
}
|
|
305
|
+
}
|
|
537
306
|
|
|
538
307
|
/**
|
|
539
308
|
* 生成请求 ID
|
|
540
309
|
*/
|
|
541
310
|
function generateRequestId() {
|
|
542
|
-
return
|
|
311
|
+
return `req_${Date.now()}_${Math.random().toString(36).substring(2, 11)}`;
|
|
543
312
|
}
|
|
544
313
|
/**
|
|
545
314
|
* HTTP 客户端实现
|
|
546
315
|
* 提供基础的 HTTP 请求功能
|
|
547
316
|
*/
|
|
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
|
-
});
|
|
317
|
+
class HttpClient {
|
|
318
|
+
constructor(adapter) {
|
|
319
|
+
this.requestInterceptors = [];
|
|
320
|
+
this.responseInterceptors = [];
|
|
568
321
|
this.adapter = adapter || new AxiosAdapter();
|
|
569
322
|
}
|
|
570
323
|
/**
|
|
571
324
|
* GET 请求
|
|
572
325
|
*/
|
|
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
|
-
});
|
|
326
|
+
async get(url, config) {
|
|
327
|
+
return this.request({ ...config, url, method: HttpMethod.GET });
|
|
328
|
+
}
|
|
585
329
|
/**
|
|
586
330
|
* POST 请求
|
|
587
331
|
*/
|
|
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
|
-
});
|
|
332
|
+
async post(url, data, config) {
|
|
333
|
+
return this.request({ ...config, url, data, method: HttpMethod.POST });
|
|
334
|
+
}
|
|
600
335
|
/**
|
|
601
336
|
* PUT 请求
|
|
602
337
|
*/
|
|
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
|
-
});
|
|
338
|
+
async put(url, data, config) {
|
|
339
|
+
return this.request({ ...config, url, data, method: HttpMethod.PUT });
|
|
340
|
+
}
|
|
615
341
|
/**
|
|
616
342
|
* DELETE 请求
|
|
617
343
|
*/
|
|
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
|
-
});
|
|
344
|
+
async delete(url, config) {
|
|
345
|
+
return this.request({ ...config, url, method: HttpMethod.DELETE });
|
|
346
|
+
}
|
|
630
347
|
/**
|
|
631
348
|
* 通用请求方法
|
|
632
349
|
*/
|
|
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*/];
|
|
350
|
+
async request(config) {
|
|
351
|
+
// 创建请求上下文
|
|
352
|
+
const context = {
|
|
353
|
+
config: this.normalizeConfig(config),
|
|
354
|
+
startTime: Date.now(),
|
|
355
|
+
requestId: generateRequestId(),
|
|
356
|
+
metadata: new Map(),
|
|
357
|
+
};
|
|
358
|
+
try {
|
|
359
|
+
// 执行请求拦截器
|
|
360
|
+
let processedContext = context;
|
|
361
|
+
for (const interceptor of this.requestInterceptors) {
|
|
362
|
+
processedContext = (await interceptor.onRequest(processedContext));
|
|
363
|
+
}
|
|
364
|
+
// 发起请求
|
|
365
|
+
const response = await this.adapter.request(processedContext.config);
|
|
366
|
+
// 创建响应上下文
|
|
367
|
+
let responseContext = {
|
|
368
|
+
response,
|
|
369
|
+
status: 200,
|
|
370
|
+
headers: {},
|
|
371
|
+
duration: Date.now() - context.startTime,
|
|
372
|
+
requestId: context.requestId,
|
|
373
|
+
};
|
|
374
|
+
// 执行响应拦截器
|
|
375
|
+
for (const interceptor of this.responseInterceptors) {
|
|
376
|
+
responseContext = await interceptor.onResponse(responseContext);
|
|
377
|
+
}
|
|
378
|
+
return responseContext.response;
|
|
379
|
+
}
|
|
380
|
+
catch (error) {
|
|
381
|
+
// 执行错误拦截器
|
|
382
|
+
let processedError = error;
|
|
383
|
+
for (const interceptor of this.responseInterceptors) {
|
|
384
|
+
if (interceptor.onResponseError) {
|
|
385
|
+
try {
|
|
386
|
+
processedError = await interceptor.onResponseError(processedError);
|
|
756
387
|
}
|
|
757
|
-
|
|
758
|
-
|
|
388
|
+
catch (interceptorError) {
|
|
389
|
+
processedError = interceptorError;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
throw processedError;
|
|
759
394
|
}
|
|
760
|
-
}
|
|
395
|
+
}
|
|
761
396
|
/**
|
|
762
397
|
* 添加请求拦截器
|
|
763
398
|
*/
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
if (index > -1) {
|
|
775
|
-
_this.requestInterceptors.splice(index, 1);
|
|
776
|
-
}
|
|
777
|
-
};
|
|
778
|
-
}
|
|
779
|
-
});
|
|
399
|
+
addRequestInterceptor(interceptor) {
|
|
400
|
+
this.requestInterceptors.push(interceptor);
|
|
401
|
+
this.requestInterceptors.sort((a, b) => (a.priority || 0) - (b.priority || 0));
|
|
402
|
+
return () => {
|
|
403
|
+
const index = this.requestInterceptors.indexOf(interceptor);
|
|
404
|
+
if (index > -1) {
|
|
405
|
+
this.requestInterceptors.splice(index, 1);
|
|
406
|
+
}
|
|
407
|
+
};
|
|
408
|
+
}
|
|
780
409
|
/**
|
|
781
410
|
* 添加响应拦截器
|
|
782
411
|
*/
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
if (index > -1) {
|
|
794
|
-
_this.responseInterceptors.splice(index, 1);
|
|
795
|
-
}
|
|
796
|
-
};
|
|
797
|
-
}
|
|
798
|
-
});
|
|
412
|
+
addResponseInterceptor(interceptor) {
|
|
413
|
+
this.responseInterceptors.push(interceptor);
|
|
414
|
+
this.responseInterceptors.sort((a, b) => (a.priority || 0) - (b.priority || 0));
|
|
415
|
+
return () => {
|
|
416
|
+
const index = this.responseInterceptors.indexOf(interceptor);
|
|
417
|
+
if (index > -1) {
|
|
418
|
+
this.responseInterceptors.splice(index, 1);
|
|
419
|
+
}
|
|
420
|
+
};
|
|
421
|
+
}
|
|
799
422
|
/**
|
|
800
423
|
* 克隆客户端
|
|
801
424
|
* 注意:克隆会创建新的拦截器数组,但共享同一个适配器实例
|
|
802
425
|
* 如需独立的适配器,请使用 new HttpClient() 创建新实例
|
|
803
426
|
*/
|
|
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
|
-
});
|
|
427
|
+
clone() {
|
|
428
|
+
const cloned = new HttpClient(this.adapter);
|
|
429
|
+
cloned.requestInterceptors = [...this.requestInterceptors];
|
|
430
|
+
cloned.responseInterceptors = [...this.responseInterceptors];
|
|
431
|
+
return cloned;
|
|
432
|
+
}
|
|
815
433
|
/**
|
|
816
434
|
* 规范化配置
|
|
817
435
|
*/
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
}
|
|
825
|
-
}
|
|
436
|
+
normalizeConfig(config) {
|
|
437
|
+
return {
|
|
438
|
+
method: config.method || (config.data ? HttpMethod.POST : HttpMethod.GET),
|
|
439
|
+
timeout: config.timeout || DEFAULT_TIMEOUT,
|
|
440
|
+
headers: config.headers || {},
|
|
441
|
+
...config,
|
|
442
|
+
};
|
|
443
|
+
}
|
|
826
444
|
/**
|
|
827
445
|
* 获取适配器
|
|
828
446
|
*/
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
writable: true,
|
|
833
|
-
value: function () {
|
|
834
|
-
return this.adapter;
|
|
835
|
-
}
|
|
836
|
-
});
|
|
447
|
+
getAdapter() {
|
|
448
|
+
return this.adapter;
|
|
449
|
+
}
|
|
837
450
|
/**
|
|
838
451
|
* 处理旧版拦截器的请求(向后兼容)
|
|
839
452
|
* 此方法被 BeLinkClient 和 LiveClient 共享
|
|
840
453
|
* @protected 允许子类调用
|
|
841
454
|
*/
|
|
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
|
-
});
|
|
455
|
+
async requestWithLegacyInterceptors(config) {
|
|
456
|
+
// 注册临时拦截器
|
|
457
|
+
const removeRequestInterceptor = config.requestInterceptors
|
|
458
|
+
? this.addRequestInterceptor({
|
|
459
|
+
name: 'LegacyRequest',
|
|
460
|
+
priority: 100,
|
|
461
|
+
onRequest: (ctx) => {
|
|
462
|
+
const newConfig = config.requestInterceptors(ctx.config);
|
|
463
|
+
return { ...ctx, config: newConfig };
|
|
464
|
+
},
|
|
465
|
+
})
|
|
466
|
+
: null;
|
|
467
|
+
const removeResponseInterceptor = config.responseInterceptors
|
|
468
|
+
? this.addResponseInterceptor({
|
|
469
|
+
name: 'LegacyResponse',
|
|
470
|
+
priority: 100,
|
|
471
|
+
onResponse: (ctx) => ctx,
|
|
472
|
+
})
|
|
473
|
+
: null;
|
|
474
|
+
try {
|
|
475
|
+
const response = await this.request(config);
|
|
476
|
+
return response;
|
|
893
477
|
}
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
478
|
+
catch (error) {
|
|
479
|
+
if (config.catchCallback) {
|
|
480
|
+
config.catchCallback(error);
|
|
481
|
+
}
|
|
482
|
+
const err = error;
|
|
483
|
+
throw err?.response?.data || error;
|
|
484
|
+
}
|
|
485
|
+
finally {
|
|
486
|
+
// 清理临时拦截器
|
|
487
|
+
removeRequestInterceptor?.();
|
|
488
|
+
removeResponseInterceptor?.();
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
}
|
|
897
492
|
|
|
898
493
|
/**
|
|
899
494
|
* 默认配置值
|
|
900
495
|
*/
|
|
901
|
-
|
|
496
|
+
const DEFAULT_CONFIG = {
|
|
902
497
|
mode: 'development',
|
|
903
498
|
timeout: 8000,
|
|
904
499
|
encryption: {
|
|
@@ -923,151 +518,100 @@ var DEFAULT_CONFIG = {
|
|
|
923
518
|
* 配置管理器实现
|
|
924
519
|
* 单例模式,集中管理所有配置
|
|
925
520
|
*/
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
enumerable: true,
|
|
930
|
-
configurable: true,
|
|
931
|
-
writable: true,
|
|
932
|
-
value: void 0
|
|
933
|
-
});
|
|
934
|
-
this.config = __assign$4({}, DEFAULT_CONFIG);
|
|
521
|
+
class ConfigManager {
|
|
522
|
+
constructor() {
|
|
523
|
+
this.config = { ...DEFAULT_CONFIG };
|
|
935
524
|
this.loadFromEnvironment();
|
|
936
525
|
}
|
|
937
526
|
/**
|
|
938
527
|
* 获取单例实例
|
|
939
528
|
*/
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
writable: true,
|
|
944
|
-
value: function () {
|
|
945
|
-
if (!ConfigManager.instance) {
|
|
946
|
-
ConfigManager.instance = new ConfigManager();
|
|
947
|
-
}
|
|
948
|
-
return ConfigManager.instance;
|
|
529
|
+
static getInstance() {
|
|
530
|
+
if (!ConfigManager.instance) {
|
|
531
|
+
ConfigManager.instance = new ConfigManager();
|
|
949
532
|
}
|
|
950
|
-
|
|
533
|
+
return ConfigManager.instance;
|
|
534
|
+
}
|
|
951
535
|
/**
|
|
952
536
|
* 初始化配置
|
|
953
537
|
*/
|
|
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
|
-
}
|
|
538
|
+
init(config) {
|
|
539
|
+
this.config = this.mergeConfig(this.config, config);
|
|
540
|
+
if (!this.validate()) {
|
|
541
|
+
console.warn('[ConfigManager] Configuration validation failed');
|
|
963
542
|
}
|
|
964
|
-
}
|
|
543
|
+
}
|
|
965
544
|
/**
|
|
966
545
|
* 获取配置项
|
|
967
546
|
*/
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
writable: true,
|
|
972
|
-
value: function (key) {
|
|
973
|
-
return this.config[key];
|
|
974
|
-
}
|
|
975
|
-
});
|
|
547
|
+
get(key) {
|
|
548
|
+
return this.config[key];
|
|
549
|
+
}
|
|
976
550
|
/**
|
|
977
551
|
* 设置配置项
|
|
978
552
|
*/
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
writable: true,
|
|
983
|
-
value: function (key, value) {
|
|
984
|
-
this.config[key] = value;
|
|
985
|
-
}
|
|
986
|
-
});
|
|
553
|
+
set(key, value) {
|
|
554
|
+
this.config[key] = value;
|
|
555
|
+
}
|
|
987
556
|
/**
|
|
988
557
|
* 获取完整配置
|
|
989
558
|
*/
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
writable: true,
|
|
994
|
-
value: function () {
|
|
995
|
-
return Object.freeze(__assign$4({}, this.config));
|
|
996
|
-
}
|
|
997
|
-
});
|
|
559
|
+
getAll() {
|
|
560
|
+
return Object.freeze({ ...this.config });
|
|
561
|
+
}
|
|
998
562
|
/**
|
|
999
563
|
* 验证配置
|
|
1000
564
|
*/
|
|
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;
|
|
565
|
+
validate() {
|
|
566
|
+
if (!this.config.mode) {
|
|
567
|
+
console.error('[ConfigManager] Mode is required');
|
|
568
|
+
return false;
|
|
1011
569
|
}
|
|
1012
|
-
|
|
570
|
+
return true;
|
|
571
|
+
}
|
|
1013
572
|
/**
|
|
1014
573
|
* 重置配置
|
|
1015
574
|
*/
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
writable: true,
|
|
1020
|
-
value: function () {
|
|
1021
|
-
this.config = __assign$4({}, DEFAULT_CONFIG);
|
|
1022
|
-
}
|
|
1023
|
-
});
|
|
575
|
+
reset() {
|
|
576
|
+
this.config = { ...DEFAULT_CONFIG };
|
|
577
|
+
}
|
|
1024
578
|
/**
|
|
1025
579
|
* 从环境变量加载配置
|
|
1026
580
|
*/
|
|
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
|
-
}
|
|
581
|
+
loadFromEnvironment() {
|
|
582
|
+
const key = this.getEnvVar('BELINK_ENCRYPTION_KEY');
|
|
583
|
+
const iv = this.getEnvVar('BELINK_ENCRYPTION_IV');
|
|
584
|
+
if (key && iv) {
|
|
585
|
+
this.config.encryption = { key, iv };
|
|
1037
586
|
}
|
|
1038
|
-
}
|
|
587
|
+
}
|
|
1039
588
|
/**
|
|
1040
589
|
* 深度合并配置
|
|
1041
590
|
*/
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
591
|
+
mergeConfig(target, source) {
|
|
592
|
+
return {
|
|
593
|
+
...target,
|
|
594
|
+
...source,
|
|
595
|
+
encryption: source.encryption ? { ...target.encryption, ...source.encryption } : target.encryption,
|
|
596
|
+
timeSync: source.timeSync ? { ...target.timeSync, ...source.timeSync } : target.timeSync,
|
|
597
|
+
cloudbase: source.cloudbase ? { ...target.cloudbase, ...source.cloudbase } : target.cloudbase,
|
|
598
|
+
};
|
|
599
|
+
}
|
|
1050
600
|
/**
|
|
1051
601
|
* 获取环境变量
|
|
1052
602
|
*/
|
|
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;
|
|
603
|
+
getEnvVar(key) {
|
|
604
|
+
// Vite 环境
|
|
605
|
+
if (typeof import.meta !== 'undefined' && import.meta.env) {
|
|
606
|
+
return import.meta.env[key];
|
|
1067
607
|
}
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
608
|
+
// Webpack/Node 环境
|
|
609
|
+
if (typeof process !== 'undefined' && process.env) {
|
|
610
|
+
return process.env[key];
|
|
611
|
+
}
|
|
612
|
+
return undefined;
|
|
613
|
+
}
|
|
614
|
+
}
|
|
1071
615
|
|
|
1072
616
|
/**
|
|
1073
617
|
* 检测当前环境
|
|
@@ -1075,12 +619,12 @@ var ConfigManager = /** @class */ (function () {
|
|
|
1075
619
|
function detectEnvironment() {
|
|
1076
620
|
// Vite 环境
|
|
1077
621
|
if (typeof import.meta !== 'undefined' && import.meta.env) {
|
|
1078
|
-
|
|
622
|
+
const env = import.meta.env.VITE_API_ENV || import.meta.env.MODE;
|
|
1079
623
|
return normalizeEnv(env);
|
|
1080
624
|
}
|
|
1081
625
|
// Webpack/Node 环境
|
|
1082
626
|
if (typeof process !== 'undefined' && process.env) {
|
|
1083
|
-
|
|
627
|
+
const env = process.env.API_ENV || process.env.NODE_ENV;
|
|
1084
628
|
return normalizeEnv(env);
|
|
1085
629
|
}
|
|
1086
630
|
console.warn('[EnvUtils] Cannot detect environment, defaulting to "development"');
|
|
@@ -1092,7 +636,7 @@ function detectEnvironment() {
|
|
|
1092
636
|
function normalizeEnv(env) {
|
|
1093
637
|
if (!env)
|
|
1094
638
|
return 'development';
|
|
1095
|
-
|
|
639
|
+
const normalized = env.toLowerCase();
|
|
1096
640
|
if (normalized === 'production' || normalized === 'prod') {
|
|
1097
641
|
return 'production';
|
|
1098
642
|
}
|
|
@@ -1118,951 +662,658 @@ function isNode() {
|
|
|
1118
662
|
* 加密服务实现
|
|
1119
663
|
* 提供 Token 加密/解密功能
|
|
1120
664
|
*/
|
|
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);
|
|
665
|
+
class EncryptionService {
|
|
666
|
+
constructor(config, timeSyncService) {
|
|
667
|
+
this.config = { ...config };
|
|
1136
668
|
this.timeSyncService = timeSyncService;
|
|
1137
669
|
}
|
|
1138
670
|
/**
|
|
1139
671
|
* 加密 Token
|
|
1140
672
|
* 格式: AES(token + "|+|" + timestamp)
|
|
1141
673
|
*/
|
|
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;
|
|
674
|
+
encryptToken(token, timestamp) {
|
|
675
|
+
if (!token) {
|
|
676
|
+
return '';
|
|
1157
677
|
}
|
|
1158
|
-
|
|
678
|
+
const time = timestamp ?? this.getAdjustedTime();
|
|
679
|
+
const data = `${token}|+|${time}`;
|
|
680
|
+
const encrypted = CryptoJS.AES.encrypt(data, CryptoJS.enc.Utf8.parse(this.config.key), {
|
|
681
|
+
iv: CryptoJS.enc.Utf8.parse(this.config.iv),
|
|
682
|
+
mode: CryptoJS.mode.CBC,
|
|
683
|
+
}).toString();
|
|
684
|
+
return encrypted;
|
|
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
|
-
|
|
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') {
|
|
733
|
+
this.synced = false;
|
|
1241
734
|
this.config = config;
|
|
1242
735
|
this.mode = mode;
|
|
1243
736
|
}
|
|
1244
737
|
/**
|
|
1245
738
|
* 确保时间已同步
|
|
1246
739
|
*/
|
|
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
|
-
});
|
|
740
|
+
async ensureSync() {
|
|
741
|
+
if (!this.config.enabled) {
|
|
742
|
+
return;
|
|
1278
743
|
}
|
|
1279
|
-
|
|
744
|
+
if (this.synced) {
|
|
745
|
+
return;
|
|
746
|
+
}
|
|
747
|
+
if (!isBrowser()) {
|
|
748
|
+
return;
|
|
749
|
+
}
|
|
750
|
+
const serverTime = localStorage.getItem(STORAGE_KEYS.SERVER_TIME);
|
|
751
|
+
if (serverTime && serverTime !== 'undefined') {
|
|
752
|
+
this.synced = true;
|
|
753
|
+
return;
|
|
754
|
+
}
|
|
755
|
+
await this.sync();
|
|
756
|
+
}
|
|
1280
757
|
/**
|
|
1281
758
|
* 执行时间同步
|
|
1282
759
|
*/
|
|
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
|
-
});
|
|
760
|
+
async sync() {
|
|
761
|
+
if (!isBrowser()) {
|
|
762
|
+
return;
|
|
1336
763
|
}
|
|
1337
|
-
|
|
764
|
+
try {
|
|
765
|
+
const url = this.getSyncUrl();
|
|
766
|
+
if (!url) {
|
|
767
|
+
console.warn('[TimeSyncService] Sync URL not configured');
|
|
768
|
+
return;
|
|
769
|
+
}
|
|
770
|
+
const response = await fetch(url, { method: 'POST' });
|
|
771
|
+
// 检查响应状态
|
|
772
|
+
if (!response.ok) {
|
|
773
|
+
console.error('[TimeSyncService] Sync failed with status:', response.status);
|
|
774
|
+
this.clear();
|
|
775
|
+
return;
|
|
776
|
+
}
|
|
777
|
+
const res = await response.json();
|
|
778
|
+
const serverTime = res.data;
|
|
779
|
+
if (!serverTime) {
|
|
780
|
+
this.clear();
|
|
781
|
+
return;
|
|
782
|
+
}
|
|
783
|
+
// const currentTime = Date.now();
|
|
784
|
+
// const timeDiff = serverTime - currentTime;
|
|
785
|
+
localStorage.setItem(STORAGE_KEYS.SERVER_TIME, String(serverTime));
|
|
786
|
+
// localStorage.setItem(STORAGE_KEYS.TIME_DIFF, String(timeDiff));
|
|
787
|
+
this.synced = true;
|
|
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
|
-
value: function () {
|
|
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.TIME_DIFF);
|
|
1401
836
|
}
|
|
1402
|
-
|
|
837
|
+
this.synced = false;
|
|
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
857
|
* Token 拦截器
|
|
1433
858
|
* 负责将 Token 加密后注入请求头
|
|
1434
859
|
*/
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
enumerable: true,
|
|
1439
|
-
configurable: true,
|
|
1440
|
-
writable: true,
|
|
1441
|
-
value: 'TokenInterceptor'
|
|
1442
|
-
});
|
|
1443
|
-
Object.defineProperty(this, "priority", {
|
|
1444
|
-
enumerable: true,
|
|
1445
|
-
configurable: true,
|
|
1446
|
-
writable: true,
|
|
1447
|
-
value: void 0
|
|
1448
|
-
});
|
|
1449
|
-
Object.defineProperty(this, "tokenProvider", {
|
|
1450
|
-
enumerable: true,
|
|
1451
|
-
configurable: true,
|
|
1452
|
-
writable: true,
|
|
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
|
-
});
|
|
860
|
+
class TokenInterceptor {
|
|
861
|
+
constructor(tokenProvider, encryptionService, options) {
|
|
862
|
+
this.name = 'TokenInterceptor';
|
|
1467
863
|
this.tokenProvider = tokenProvider;
|
|
1468
864
|
this.encryptionService = encryptionService;
|
|
1469
|
-
this.priority =
|
|
1470
|
-
this.headerName =
|
|
865
|
+
this.priority = options?.priority || 0;
|
|
866
|
+
this.headerName = options?.headerName || TOKEN_HEADER;
|
|
867
|
+
this.userId = options?.userId || '';
|
|
1471
868
|
}
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
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
|
-
});
|
|
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
|
-
return TokenInterceptor;
|
|
1506
|
-
}());
|
|
1507
|
-
|
|
1508
|
-
/**
|
|
1509
|
-
* 时间同步拦截器
|
|
1510
|
-
* 负责同步服务器时间并注入请求头
|
|
1511
|
-
*/
|
|
1512
|
-
var TimeInterceptor = /** @class */ (function () {
|
|
1513
|
-
function TimeInterceptor(timeSyncService, options) {
|
|
1514
|
-
Object.defineProperty(this, "name", {
|
|
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
|
-
});
|
|
1532
|
-
this.timeSyncService = timeSyncService;
|
|
1533
|
-
this.priority = (options === null || options === void 0 ? void 0 : options.priority) || 0;
|
|
879
|
+
return context;
|
|
1534
880
|
}
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
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
|
-
});
|
|
881
|
+
async onRequestError(error) {
|
|
882
|
+
throw error;
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
/**
|
|
887
|
+
* 时间同步拦截器
|
|
888
|
+
* 负责同步服务器时间并注入请求头
|
|
889
|
+
*/
|
|
890
|
+
class TimeInterceptor {
|
|
891
|
+
constructor(timeSyncService, options) {
|
|
892
|
+
this.name = 'TimeInterceptor';
|
|
893
|
+
this.timeSyncService = timeSyncService;
|
|
894
|
+
this.priority = options?.priority || 0;
|
|
895
|
+
}
|
|
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
|
-
var _a;
|
|
1630
|
-
return __generator$3(this, function (_b) {
|
|
1631
|
-
status = ((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) || error.status;
|
|
1632
|
-
if (status === 401) {
|
|
1633
|
-
// 清理时间同步数据
|
|
1634
|
-
this.timeSyncService.clear();
|
|
1635
|
-
loginPath = this.configManager.get('loginPath');
|
|
1636
|
-
if (typeof loginPath === 'string') {
|
|
1637
|
-
if (typeof window !== 'undefined') {
|
|
1638
|
-
window.location.replace(loginPath);
|
|
1639
|
-
}
|
|
1640
|
-
}
|
|
1641
|
-
else if (typeof loginPath === 'function') {
|
|
1642
|
-
loginPath();
|
|
1643
|
-
}
|
|
1644
|
-
throw new AuthError('Unauthorized', error instanceof Error ? error : undefined);
|
|
1645
|
-
}
|
|
1646
|
-
throw error;
|
|
1647
|
-
});
|
|
1648
|
-
});
|
|
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);
|
|
1649
941
|
}
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
}
|
|
942
|
+
throw error;
|
|
943
|
+
}
|
|
944
|
+
}
|
|
1653
945
|
|
|
1654
946
|
/**
|
|
1655
947
|
* BeLink 业务请求客户端
|
|
1656
948
|
* 提供完整的认证、加密、时间同步等企业级功能
|
|
1657
949
|
*/
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
});
|
|
1668
|
-
Object.defineProperty(_this, "encryptionService", {
|
|
1669
|
-
enumerable: true,
|
|
1670
|
-
configurable: true,
|
|
1671
|
-
writable: true,
|
|
1672
|
-
value: void 0
|
|
1673
|
-
});
|
|
1674
|
-
Object.defineProperty(_this, "timeSyncService", {
|
|
1675
|
-
enumerable: true,
|
|
1676
|
-
configurable: true,
|
|
1677
|
-
writable: true,
|
|
1678
|
-
value: void 0
|
|
1679
|
-
});
|
|
1680
|
-
Object.defineProperty(_this, "token", {
|
|
1681
|
-
enumerable: true,
|
|
1682
|
-
configurable: true,
|
|
1683
|
-
writable: true,
|
|
1684
|
-
value: null
|
|
1685
|
-
});
|
|
1686
|
-
Object.defineProperty(_this, "initialized", {
|
|
1687
|
-
enumerable: true,
|
|
1688
|
-
configurable: true,
|
|
1689
|
-
writable: true,
|
|
1690
|
-
value: false
|
|
1691
|
-
});
|
|
1692
|
-
Object.defineProperty(_this, "tokenListenerCleanup", {
|
|
1693
|
-
enumerable: true,
|
|
1694
|
-
configurable: true,
|
|
1695
|
-
writable: true,
|
|
1696
|
-
value: null
|
|
1697
|
-
});
|
|
1698
|
-
Object.defineProperty(_this, "tokenHeaderName", {
|
|
1699
|
-
enumerable: true,
|
|
1700
|
-
configurable: true,
|
|
1701
|
-
writable: true,
|
|
1702
|
-
value: TOKEN_HEADER
|
|
1703
|
-
});
|
|
1704
|
-
Object.defineProperty(_this, "tokenStorageKey", {
|
|
1705
|
-
enumerable: true,
|
|
1706
|
-
configurable: true,
|
|
1707
|
-
writable: true,
|
|
1708
|
-
value: STORAGE_KEYS.TOKEN
|
|
1709
|
-
});
|
|
1710
|
-
_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();
|
|
1711
959
|
// 先初始化 timeSyncService
|
|
1712
|
-
|
|
960
|
+
this.timeSyncService = new TimeSyncService(this.configManager.get('timeSync') || { enabled: true }, this.configManager.get('mode'));
|
|
1713
961
|
// 再初始化 encryptionService,注入 timeSyncService
|
|
1714
|
-
|
|
1715
|
-
return _this;
|
|
962
|
+
this.encryptionService = new EncryptionService(this.configManager.get('encryption') || { key: '214c!dsf1214c!ds', iv: 'icalqai23cfhgj!s' }, this.timeSyncService);
|
|
1716
963
|
}
|
|
1717
964
|
/**
|
|
1718
965
|
* 初始化客户端
|
|
1719
966
|
*/
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
this.timeSyncService.setMode(config.mode);
|
|
1734
|
-
// 设置 baseUrl
|
|
1735
|
-
if (config.baseUrl) {
|
|
1736
|
-
this.adapter.setBaseUrl(config.baseUrl);
|
|
1737
|
-
}
|
|
1738
|
-
// 设置 tokenHeaderName(请求头名称)
|
|
1739
|
-
if (config.tokenHeaderName) {
|
|
1740
|
-
this.tokenHeaderName = config.tokenHeaderName;
|
|
1741
|
-
}
|
|
1742
|
-
// 设置 tokenStorageKey(localStorage 存储键名)
|
|
1743
|
-
if (config.tokenStorageKey) {
|
|
1744
|
-
this.tokenStorageKey = config.tokenStorageKey;
|
|
1745
|
-
}
|
|
1746
|
-
// 设置默认拦截器
|
|
1747
|
-
if (!this.initialized) {
|
|
1748
|
-
this.setupDefaultInterceptors();
|
|
1749
|
-
this.initialized = true;
|
|
1750
|
-
}
|
|
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);
|
|
1751
980
|
}
|
|
1752
|
-
|
|
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
|
+
}
|
|
1753
995
|
/**
|
|
1754
996
|
* 设置 Token
|
|
1755
997
|
*/
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
writable: true,
|
|
1760
|
-
value: function (token) {
|
|
1761
|
-
this.token = token;
|
|
1762
|
-
}
|
|
1763
|
-
});
|
|
998
|
+
setToken(token) {
|
|
999
|
+
this.token = token;
|
|
1000
|
+
}
|
|
1764
1001
|
/**
|
|
1765
1002
|
* 获取当前 Token
|
|
1766
1003
|
*/
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
writable: true,
|
|
1771
|
-
value: function () {
|
|
1772
|
-
return this.token;
|
|
1773
|
-
}
|
|
1774
|
-
});
|
|
1004
|
+
getToken() {
|
|
1005
|
+
return this.token;
|
|
1006
|
+
}
|
|
1775
1007
|
/**
|
|
1776
1008
|
* 设置 Token 监听器
|
|
1777
1009
|
* @returns 清理函数,用于移除监听器
|
|
1778
1010
|
*/
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
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);
|
|
1789
1024
|
}
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
return function () { };
|
|
1025
|
+
else if (typeof tokenKey === 'function') {
|
|
1026
|
+
return tokenKey();
|
|
1793
1027
|
}
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
}
|
|
1801
|
-
return null;
|
|
1802
|
-
};
|
|
1803
|
-
var storageHandler = function (event) {
|
|
1804
|
-
if (typeof tokenKey === 'string' && event.key === tokenKey) {
|
|
1805
|
-
if (event.newValue) {
|
|
1806
|
-
_this.setToken(event.newValue);
|
|
1807
|
-
}
|
|
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);
|
|
1808
1034
|
}
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1035
|
+
}
|
|
1036
|
+
if (typeof tokenKey === 'function') {
|
|
1037
|
+
const token = tokenKey();
|
|
1038
|
+
if (token) {
|
|
1039
|
+
this.setToken(token);
|
|
1814
1040
|
}
|
|
1815
|
-
};
|
|
1816
|
-
window.addEventListener('storage', storageHandler);
|
|
1817
|
-
// 初始化时获取 token
|
|
1818
|
-
var token = getToken();
|
|
1819
|
-
if (token) {
|
|
1820
|
-
this.setToken(token);
|
|
1821
1041
|
}
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1042
|
+
};
|
|
1043
|
+
window.addEventListener('storage', storageHandler);
|
|
1044
|
+
// 初始化时获取 token
|
|
1045
|
+
const token = getToken();
|
|
1046
|
+
if (token) {
|
|
1047
|
+
this.setToken(token);
|
|
1828
1048
|
}
|
|
1829
|
-
|
|
1049
|
+
// 创建并存储清理函数
|
|
1050
|
+
const cleanup = () => {
|
|
1051
|
+
window.removeEventListener('storage', storageHandler);
|
|
1052
|
+
};
|
|
1053
|
+
this.tokenListenerCleanup = cleanup;
|
|
1054
|
+
return cleanup;
|
|
1055
|
+
}
|
|
1830
1056
|
/**
|
|
1831
1057
|
* 设置全局拦截器(向后兼容)
|
|
1832
1058
|
*/
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
writable: true,
|
|
1837
|
-
value: function () {
|
|
1838
|
-
// 已在 init 中自动设置
|
|
1839
|
-
}
|
|
1840
|
-
});
|
|
1059
|
+
setupInterceptors() {
|
|
1060
|
+
// 已在 init 中自动设置
|
|
1061
|
+
}
|
|
1841
1062
|
/**
|
|
1842
1063
|
* 设置认证回调(向后兼容)
|
|
1843
1064
|
*/
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
writable: true,
|
|
1848
|
-
value: function (path) {
|
|
1849
|
-
this.configManager.set('loginPath', path);
|
|
1850
|
-
}
|
|
1851
|
-
});
|
|
1065
|
+
setupAuthorizationCallback(path) {
|
|
1066
|
+
this.configManager.set('loginPath', path);
|
|
1067
|
+
}
|
|
1852
1068
|
/**
|
|
1853
1069
|
* 清除请求时间
|
|
1854
1070
|
*/
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
writable: true,
|
|
1859
|
-
value: function () {
|
|
1860
|
-
return __awaiter$3(this, void 0, void 0, function () {
|
|
1861
|
-
return __generator$3(this, function (_a) {
|
|
1862
|
-
this.timeSyncService.clear();
|
|
1863
|
-
return [2 /*return*/];
|
|
1864
|
-
});
|
|
1865
|
-
});
|
|
1866
|
-
}
|
|
1867
|
-
});
|
|
1071
|
+
async removeRequestTime() {
|
|
1072
|
+
this.timeSyncService.clear();
|
|
1073
|
+
}
|
|
1868
1074
|
/**
|
|
1869
1075
|
* @deprecated 请使用 removeRequestTime(修正拼写后的版本)
|
|
1870
1076
|
* 清除请求时间(向后兼容)
|
|
1871
1077
|
*/
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
writable: true,
|
|
1876
|
-
value: function () {
|
|
1877
|
-
return __awaiter$3(this, void 0, void 0, function () {
|
|
1878
|
-
return __generator$3(this, function (_a) {
|
|
1879
|
-
return [2 /*return*/, this.removeRequestTime()];
|
|
1880
|
-
});
|
|
1881
|
-
});
|
|
1882
|
-
}
|
|
1883
|
-
});
|
|
1078
|
+
async removeReqeustTime() {
|
|
1079
|
+
return this.removeRequestTime();
|
|
1080
|
+
}
|
|
1884
1081
|
/**
|
|
1885
1082
|
* 设置请求时间
|
|
1886
1083
|
*/
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
return __awaiter$3(this, void 0, void 0, function () {
|
|
1893
|
-
var headers;
|
|
1894
|
-
return __generator$3(this, function (_a) {
|
|
1895
|
-
switch (_a.label) {
|
|
1896
|
-
case 0: return [4 /*yield*/, this.timeSyncService.ensureSync()];
|
|
1897
|
-
case 1:
|
|
1898
|
-
_a.sent();
|
|
1899
|
-
headers = this.timeSyncService.getTimeSyncHeaders();
|
|
1900
|
-
if (!config.headers) {
|
|
1901
|
-
config.headers = {};
|
|
1902
|
-
}
|
|
1903
|
-
Object.assign(config.headers, headers);
|
|
1904
|
-
return [2 /*return*/];
|
|
1905
|
-
}
|
|
1906
|
-
});
|
|
1907
|
-
});
|
|
1084
|
+
async setupRequestTime(config) {
|
|
1085
|
+
await this.timeSyncService.ensureSync();
|
|
1086
|
+
const headers = this.timeSyncService.getTimeSyncHeaders();
|
|
1087
|
+
if (!config.headers) {
|
|
1088
|
+
config.headers = {};
|
|
1908
1089
|
}
|
|
1909
|
-
|
|
1090
|
+
Object.assign(config.headers, headers);
|
|
1091
|
+
}
|
|
1910
1092
|
/**
|
|
1911
1093
|
* @deprecated 请使用 setupRequestTime(修正拼写后的版本)
|
|
1912
1094
|
* 设置请求时间(向后兼容)
|
|
1913
1095
|
*/
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
writable: true,
|
|
1918
|
-
value: function (config) {
|
|
1919
|
-
return __awaiter$3(this, void 0, void 0, function () {
|
|
1920
|
-
return __generator$3(this, function (_a) {
|
|
1921
|
-
return [2 /*return*/, this.setupRequestTime(config)];
|
|
1922
|
-
});
|
|
1923
|
-
});
|
|
1924
|
-
}
|
|
1925
|
-
});
|
|
1096
|
+
async setupReqeustTime(config) {
|
|
1097
|
+
return this.setupRequestTime(config);
|
|
1098
|
+
}
|
|
1926
1099
|
/**
|
|
1927
1100
|
* 设置 Token(向后兼容)
|
|
1928
1101
|
*/
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
var token = localStorage.getItem(this.tokenStorageKey);
|
|
1938
|
-
if (token) {
|
|
1939
|
-
if (!config.headers) {
|
|
1940
|
-
config.headers = {};
|
|
1941
|
-
}
|
|
1942
|
-
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 = {};
|
|
1943
1110
|
}
|
|
1111
|
+
config.headers[this.tokenHeaderName] = this.encryptionService.encryptToken(token);
|
|
1944
1112
|
}
|
|
1945
|
-
}
|
|
1113
|
+
}
|
|
1946
1114
|
/**
|
|
1947
1115
|
* 重写请求方法(向后兼容旧版 API)
|
|
1948
1116
|
*/
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
value: function (config) {
|
|
1954
|
-
return __awaiter$3(this, void 0, void 0, function () {
|
|
1955
|
-
return __generator$3(this, function (_a) {
|
|
1956
|
-
// 处理 customToken
|
|
1957
|
-
if (config.customToken) {
|
|
1958
|
-
this.setToken(config.customToken);
|
|
1959
|
-
}
|
|
1960
|
-
// 处理旧版拦截器配置(使用基类的共享方法)
|
|
1961
|
-
if (config.requestInterceptors || config.responseInterceptors) {
|
|
1962
|
-
return [2 /*return*/, this.requestWithLegacyInterceptors(config)];
|
|
1963
|
-
}
|
|
1964
|
-
return [2 /*return*/, _super.prototype.request.call(this, config)];
|
|
1965
|
-
});
|
|
1966
|
-
});
|
|
1117
|
+
async request(config) {
|
|
1118
|
+
// 处理 customToken
|
|
1119
|
+
if (config.customToken) {
|
|
1120
|
+
this.setToken(config.customToken);
|
|
1967
1121
|
}
|
|
1968
|
-
|
|
1122
|
+
// 处理旧版拦截器配置(使用基类的共享方法)
|
|
1123
|
+
if (config.requestInterceptors || config.responseInterceptors) {
|
|
1124
|
+
return this.requestWithLegacyInterceptors(config);
|
|
1125
|
+
}
|
|
1126
|
+
return super.request(config);
|
|
1127
|
+
}
|
|
1969
1128
|
/**
|
|
1970
1129
|
* 设置默认拦截器
|
|
1971
1130
|
*/
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
this.addResponseInterceptor(new AuthInterceptor(this.configManager, this.timeSyncService, {
|
|
1989
|
-
priority: 1,
|
|
1990
|
-
}));
|
|
1991
|
-
}
|
|
1992
|
-
});
|
|
1993
|
-
return BeLinkClient;
|
|
1994
|
-
}(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
|
+
}
|
|
1995
1147
|
|
|
1996
1148
|
// 创建并导出单例实例
|
|
1997
|
-
|
|
1149
|
+
const beLinkRequest = new BeLinkClient();
|
|
1998
1150
|
|
|
1999
1151
|
/**
|
|
2000
|
-
*
|
|
2001
|
-
*
|
|
1152
|
+
* LiveClient 业务请求客户端
|
|
1153
|
+
* 提供完整的认证、加密、时间同步等企业级功能
|
|
2002
1154
|
*/
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
1155
|
+
class LiveClient extends HttpClient {
|
|
1156
|
+
constructor() {
|
|
1157
|
+
super();
|
|
1158
|
+
this.token = null;
|
|
1159
|
+
this.userId = null;
|
|
1160
|
+
this.initialized = false;
|
|
1161
|
+
this.tokenListenerCleanup = null;
|
|
1162
|
+
this.tokenHeaderName = TOKEN_HEADER;
|
|
1163
|
+
this.tokenStorageKey = STORAGE_KEYS.TOKEN;
|
|
1164
|
+
this.configManager = ConfigManager.getInstance();
|
|
1165
|
+
// 先初始化 timeSyncService
|
|
1166
|
+
this.timeSyncService = new TimeSyncService(this.configManager.get('timeSync') || { enabled: true }, this.configManager.get('mode'));
|
|
1167
|
+
// 再初始化 encryptionService,注入 timeSyncService
|
|
1168
|
+
this.encryptionService = new EncryptionService(this.configManager.get('encryption') || { key: '214c!dsf1214c!ds', iv: 'icalqai23cfhgj!s' }, this.timeSyncService);
|
|
2014
1169
|
}
|
|
2015
1170
|
/**
|
|
2016
1171
|
* 初始化客户端
|
|
2017
1172
|
*/
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
1173
|
+
init(config) {
|
|
1174
|
+
this.configManager.init({
|
|
1175
|
+
mode: config.mode,
|
|
1176
|
+
baseUrl: config.baseUrl,
|
|
1177
|
+
timeSync: config.timeSyncConfig,
|
|
1178
|
+
encryption: config.encryption,
|
|
1179
|
+
tokenHeaderName: config.tokenHeaderName,
|
|
1180
|
+
tokenStorageKey: config.tokenStorageKey,
|
|
1181
|
+
});
|
|
1182
|
+
// 更新时间同步服务的模式
|
|
1183
|
+
this.timeSyncService.setMode(config.mode);
|
|
1184
|
+
// 设置 baseUrl
|
|
1185
|
+
if (config.baseUrl) {
|
|
1186
|
+
this.adapter.setBaseUrl(config.baseUrl);
|
|
1187
|
+
}
|
|
1188
|
+
// 设置 tokenHeaderName(请求头名称)
|
|
1189
|
+
if (config.tokenHeaderName) {
|
|
1190
|
+
this.tokenHeaderName = config.tokenHeaderName;
|
|
1191
|
+
}
|
|
1192
|
+
// 设置 tokenStorageKey(localStorage 存储键名)
|
|
1193
|
+
if (config.tokenStorageKey) {
|
|
1194
|
+
this.tokenStorageKey = config.tokenStorageKey;
|
|
1195
|
+
}
|
|
1196
|
+
// 设置默认拦截器
|
|
1197
|
+
if (!this.initialized) {
|
|
1198
|
+
this.setupDefaultInterceptors();
|
|
1199
|
+
this.initialized = true;
|
|
1200
|
+
}
|
|
1201
|
+
}
|
|
1202
|
+
setUserId(userId) {
|
|
1203
|
+
this.userId = userId;
|
|
1204
|
+
}
|
|
1205
|
+
/**
|
|
1206
|
+
* 设置 Token
|
|
1207
|
+
*/
|
|
1208
|
+
setToken(token) {
|
|
1209
|
+
this.token = token;
|
|
1210
|
+
}
|
|
1211
|
+
/**
|
|
1212
|
+
* 获取当前 Token
|
|
1213
|
+
*/
|
|
1214
|
+
getToken() {
|
|
1215
|
+
return this.token;
|
|
1216
|
+
}
|
|
1217
|
+
/**
|
|
1218
|
+
* 设置 Token 监听器
|
|
1219
|
+
* @returns 清理函数,用于移除监听器
|
|
1220
|
+
*/
|
|
1221
|
+
setupTokenListener(tokenKey) {
|
|
1222
|
+
// 清理之前的监听器
|
|
1223
|
+
if (this.tokenListenerCleanup) {
|
|
1224
|
+
this.tokenListenerCleanup();
|
|
1225
|
+
this.tokenListenerCleanup = null;
|
|
1226
|
+
}
|
|
1227
|
+
// 非浏览器环境返回空操作
|
|
1228
|
+
if (!isBrowser()) {
|
|
1229
|
+
return () => { };
|
|
1230
|
+
}
|
|
1231
|
+
const getToken = () => {
|
|
1232
|
+
if (typeof tokenKey === 'string') {
|
|
1233
|
+
return window.localStorage.getItem(tokenKey);
|
|
1234
|
+
}
|
|
1235
|
+
else if (typeof tokenKey === 'function') {
|
|
1236
|
+
return tokenKey();
|
|
1237
|
+
}
|
|
1238
|
+
return null;
|
|
1239
|
+
};
|
|
1240
|
+
const storageHandler = (event) => {
|
|
1241
|
+
if (typeof tokenKey === 'string' && event.key === tokenKey) {
|
|
1242
|
+
if (event.newValue) {
|
|
1243
|
+
this.setToken(event.newValue);
|
|
1244
|
+
}
|
|
2025
1245
|
}
|
|
2026
|
-
if (
|
|
2027
|
-
|
|
1246
|
+
if (typeof tokenKey === 'function') {
|
|
1247
|
+
const token = tokenKey();
|
|
1248
|
+
if (token) {
|
|
1249
|
+
this.setToken(token);
|
|
1250
|
+
}
|
|
2028
1251
|
}
|
|
1252
|
+
};
|
|
1253
|
+
window.addEventListener('storage', storageHandler);
|
|
1254
|
+
// 初始化时获取 token
|
|
1255
|
+
const token = getToken();
|
|
1256
|
+
if (token) {
|
|
1257
|
+
this.setToken(token);
|
|
2029
1258
|
}
|
|
2030
|
-
|
|
1259
|
+
// 创建并存储清理函数
|
|
1260
|
+
const cleanup = () => {
|
|
1261
|
+
window.removeEventListener('storage', storageHandler);
|
|
1262
|
+
};
|
|
1263
|
+
this.tokenListenerCleanup = cleanup;
|
|
1264
|
+
return cleanup;
|
|
1265
|
+
}
|
|
1266
|
+
/**
|
|
1267
|
+
* 清除请求时间
|
|
1268
|
+
*/
|
|
1269
|
+
async removeRequestTime() {
|
|
1270
|
+
this.timeSyncService.clear();
|
|
1271
|
+
}
|
|
2031
1272
|
/**
|
|
2032
|
-
*
|
|
1273
|
+
* 设置请求时间
|
|
2033
1274
|
*/
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
return this.tokenHeaderName;
|
|
1275
|
+
async setupRequestTime(config) {
|
|
1276
|
+
await this.timeSyncService.ensureSync();
|
|
1277
|
+
const headers = this.timeSyncService.getTimeSyncHeaders();
|
|
1278
|
+
if (!config.headers) {
|
|
1279
|
+
config.headers = {};
|
|
2040
1280
|
}
|
|
2041
|
-
|
|
1281
|
+
Object.assign(config.headers, headers);
|
|
1282
|
+
}
|
|
1283
|
+
/**
|
|
1284
|
+
* 设置默认拦截器
|
|
1285
|
+
*/
|
|
1286
|
+
setupDefaultInterceptors() {
|
|
1287
|
+
// 时间同步拦截器
|
|
1288
|
+
this.addRequestInterceptor(new TimeInterceptor(this.timeSyncService, { priority: 1 }));
|
|
1289
|
+
// Token 拦截器
|
|
1290
|
+
if (this.encryptionService) {
|
|
1291
|
+
this.addRequestInterceptor(new TokenInterceptor(() => this.token || localStorage.getItem(this.tokenStorageKey), this.encryptionService, {
|
|
1292
|
+
priority: 2,
|
|
1293
|
+
headerName: this.tokenHeaderName,
|
|
1294
|
+
userId: this.userId,
|
|
1295
|
+
}));
|
|
1296
|
+
}
|
|
1297
|
+
// 认证拦截器(处理 401)
|
|
1298
|
+
this.addResponseInterceptor(new AuthInterceptor(this.configManager, this.timeSyncService, { priority: 1 }));
|
|
1299
|
+
}
|
|
2042
1300
|
/**
|
|
2043
1301
|
* 重写请求方法(向后兼容旧版 API)
|
|
2044
1302
|
*/
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
writable: true,
|
|
2049
|
-
value: function (config) {
|
|
2050
|
-
return __awaiter$3(this, void 0, void 0, function () {
|
|
2051
|
-
return __generator$3(this, function (_a) {
|
|
2052
|
-
// 处理旧版拦截器配置(使用基类的共享方法)
|
|
2053
|
-
if (config.requestInterceptors || config.responseInterceptors) {
|
|
2054
|
-
return [2 /*return*/, this.requestWithLegacyInterceptors(config)];
|
|
2055
|
-
}
|
|
2056
|
-
return [2 /*return*/, _super.prototype.request.call(this, config)];
|
|
2057
|
-
});
|
|
2058
|
-
});
|
|
1303
|
+
async request(config) {
|
|
1304
|
+
if (config.customToken) {
|
|
1305
|
+
this.setToken(config.customToken);
|
|
2059
1306
|
}
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
1307
|
+
// 处理旧版拦截器配置(使用基类的共享方法)
|
|
1308
|
+
if (config.requestInterceptors || config.responseInterceptors) {
|
|
1309
|
+
return this.requestWithLegacyInterceptors(config);
|
|
1310
|
+
}
|
|
1311
|
+
return super.request(config);
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
2063
1314
|
|
|
2064
1315
|
// 创建并导出单例实例
|
|
2065
|
-
|
|
1316
|
+
const beLinkLiveRequest = new LiveClient();
|
|
2066
1317
|
|
|
2067
1318
|
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
|
2068
1319
|
|
|
@@ -6142,167 +5393,111 @@ catch (e) { }
|
|
|
6142
5393
|
* 用户信息服务
|
|
6143
5394
|
* 单例模式,用于存储和获取当前用户信息
|
|
6144
5395
|
*/
|
|
6145
|
-
|
|
6146
|
-
|
|
6147
|
-
|
|
6148
|
-
|
|
6149
|
-
|
|
6150
|
-
writable: true,
|
|
6151
|
-
value: {
|
|
6152
|
-
name: '',
|
|
6153
|
-
}
|
|
6154
|
-
});
|
|
5396
|
+
class UserInfoService {
|
|
5397
|
+
constructor() {
|
|
5398
|
+
this.userInfo = {
|
|
5399
|
+
name: '',
|
|
5400
|
+
};
|
|
6155
5401
|
}
|
|
6156
5402
|
/**
|
|
6157
5403
|
* 初始化用户信息
|
|
6158
5404
|
*/
|
|
6159
|
-
|
|
6160
|
-
|
|
6161
|
-
|
|
6162
|
-
writable: true,
|
|
6163
|
-
value: function (userInfo) {
|
|
6164
|
-
this.userInfo = userInfo || { name: '' };
|
|
6165
|
-
}
|
|
6166
|
-
});
|
|
5405
|
+
init(userInfo) {
|
|
5406
|
+
this.userInfo = userInfo || { name: '' };
|
|
5407
|
+
}
|
|
6167
5408
|
/**
|
|
6168
5409
|
* 获取用户信息
|
|
6169
5410
|
*/
|
|
6170
|
-
|
|
6171
|
-
|
|
6172
|
-
|
|
6173
|
-
writable: true,
|
|
6174
|
-
value: function () {
|
|
6175
|
-
return this.userInfo;
|
|
6176
|
-
}
|
|
6177
|
-
});
|
|
5411
|
+
getParams() {
|
|
5412
|
+
return this.userInfo;
|
|
5413
|
+
}
|
|
6178
5414
|
/**
|
|
6179
5415
|
* 更新用户信息
|
|
6180
5416
|
*/
|
|
6181
|
-
|
|
6182
|
-
|
|
6183
|
-
|
|
6184
|
-
writable: true,
|
|
6185
|
-
value: function (userInfo) {
|
|
6186
|
-
this.userInfo = __assign$4(__assign$4({}, this.userInfo), userInfo);
|
|
6187
|
-
}
|
|
6188
|
-
});
|
|
5417
|
+
update(userInfo) {
|
|
5418
|
+
this.userInfo = { ...this.userInfo, ...userInfo };
|
|
5419
|
+
}
|
|
6189
5420
|
/**
|
|
6190
5421
|
* 清除用户信息
|
|
6191
5422
|
*/
|
|
6192
|
-
|
|
6193
|
-
|
|
6194
|
-
|
|
6195
|
-
|
|
6196
|
-
value: function () {
|
|
6197
|
-
this.userInfo = { name: '' };
|
|
6198
|
-
}
|
|
6199
|
-
});
|
|
6200
|
-
return UserInfoService;
|
|
6201
|
-
}());
|
|
5423
|
+
clear() {
|
|
5424
|
+
this.userInfo = { name: '' };
|
|
5425
|
+
}
|
|
5426
|
+
}
|
|
6202
5427
|
// 导出单例实例
|
|
6203
|
-
|
|
5428
|
+
const beLinkUserInfo = new UserInfoService();
|
|
6204
5429
|
|
|
6205
5430
|
/**
|
|
6206
5431
|
* 云函数客户端
|
|
6207
5432
|
* 封装腾讯云 CloudBase SDK
|
|
6208
5433
|
*/
|
|
6209
|
-
|
|
6210
|
-
|
|
6211
|
-
|
|
6212
|
-
enumerable: true,
|
|
6213
|
-
configurable: true,
|
|
6214
|
-
writable: true,
|
|
6215
|
-
value: null
|
|
6216
|
-
});
|
|
5434
|
+
class CloudFunctionClient {
|
|
5435
|
+
constructor() {
|
|
5436
|
+
this.app = null;
|
|
6217
5437
|
}
|
|
6218
5438
|
/**
|
|
6219
5439
|
* 初始化 CloudBase App
|
|
6220
5440
|
*/
|
|
6221
|
-
|
|
6222
|
-
|
|
6223
|
-
configurable: true,
|
|
6224
|
-
writable: true,
|
|
6225
|
-
value: function () {
|
|
6226
|
-
if (this.app) {
|
|
6227
|
-
return this.app;
|
|
6228
|
-
}
|
|
6229
|
-
var configManager = ConfigManager.getInstance();
|
|
6230
|
-
var cloudbaseConfig = configManager.get('cloudbase');
|
|
6231
|
-
var env = detectEnvironment();
|
|
6232
|
-
var envId = (cloudbaseConfig === null || cloudbaseConfig === void 0 ? void 0 : cloudbaseConfig[env]) || 'dev-1gpp53ju3ceb46c7';
|
|
6233
|
-
var region = (cloudbaseConfig === null || cloudbaseConfig === void 0 ? void 0 : cloudbaseConfig.region) || 'ap-shanghai';
|
|
6234
|
-
var timeout = (cloudbaseConfig === null || cloudbaseConfig === void 0 ? void 0 : cloudbaseConfig.timeout) || 60000;
|
|
6235
|
-
var options = {
|
|
6236
|
-
region: region,
|
|
6237
|
-
env: envId,
|
|
6238
|
-
timeout: timeout,
|
|
6239
|
-
};
|
|
6240
|
-
this.app = cloudbase.init(options);
|
|
5441
|
+
initApp() {
|
|
5442
|
+
if (this.app) {
|
|
6241
5443
|
return this.app;
|
|
6242
5444
|
}
|
|
6243
|
-
|
|
5445
|
+
const configManager = ConfigManager.getInstance();
|
|
5446
|
+
const cloudbaseConfig = configManager.get('cloudbase');
|
|
5447
|
+
const env = detectEnvironment();
|
|
5448
|
+
const envId = cloudbaseConfig?.[env] || 'dev-1gpp53ju3ceb46c7';
|
|
5449
|
+
const region = cloudbaseConfig?.region || 'ap-shanghai';
|
|
5450
|
+
const timeout = cloudbaseConfig?.timeout || 60000;
|
|
5451
|
+
const options = {
|
|
5452
|
+
region,
|
|
5453
|
+
env: envId,
|
|
5454
|
+
timeout,
|
|
5455
|
+
};
|
|
5456
|
+
this.app = cloudbase.init(options);
|
|
5457
|
+
return this.app;
|
|
5458
|
+
}
|
|
6244
5459
|
/**
|
|
6245
5460
|
* 调用云函数
|
|
6246
5461
|
*/
|
|
6247
|
-
|
|
6248
|
-
|
|
6249
|
-
|
|
6250
|
-
|
|
6251
|
-
|
|
6252
|
-
|
|
6253
|
-
|
|
6254
|
-
|
|
6255
|
-
|
|
6256
|
-
case 0:
|
|
6257
|
-
app = this.initApp();
|
|
6258
|
-
data = __assign$4({}, params.data);
|
|
6259
|
-
// 请求拦截:自动注入用户信息
|
|
6260
|
-
if (!data.operateUser) {
|
|
6261
|
-
data.operateUser = __assign$4({}, beLinkUserInfo.getParams());
|
|
6262
|
-
}
|
|
6263
|
-
callParams = __assign$4(__assign$4({}, params), { data: data });
|
|
6264
|
-
return [4 /*yield*/, app.callFunction(callParams)];
|
|
6265
|
-
case 1:
|
|
6266
|
-
result = _a.sent();
|
|
6267
|
-
// 响应拦截
|
|
6268
|
-
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
6269
|
-
var _a;
|
|
6270
|
-
if (!((_a = result.result) === null || _a === void 0 ? void 0 : _a.success)) {
|
|
6271
|
-
reject(result.result);
|
|
6272
|
-
}
|
|
6273
|
-
else {
|
|
6274
|
-
resolve(result.result.data);
|
|
6275
|
-
}
|
|
6276
|
-
})];
|
|
6277
|
-
}
|
|
6278
|
-
});
|
|
6279
|
-
});
|
|
5462
|
+
async call(params) {
|
|
5463
|
+
const app = this.initApp();
|
|
5464
|
+
// 创建数据副本,避免修改原始参数
|
|
5465
|
+
const data = { ...params.data };
|
|
5466
|
+
// 请求拦截:自动注入用户信息
|
|
5467
|
+
if (!data.operateUser) {
|
|
5468
|
+
data.operateUser = {
|
|
5469
|
+
...beLinkUserInfo.getParams(),
|
|
5470
|
+
};
|
|
6280
5471
|
}
|
|
6281
|
-
|
|
5472
|
+
const callParams = {
|
|
5473
|
+
...params,
|
|
5474
|
+
data,
|
|
5475
|
+
};
|
|
5476
|
+
const result = await app.callFunction(callParams);
|
|
5477
|
+
// 响应拦截
|
|
5478
|
+
return new Promise((resolve, reject) => {
|
|
5479
|
+
if (!result.result?.success) {
|
|
5480
|
+
reject(result.result);
|
|
5481
|
+
}
|
|
5482
|
+
else {
|
|
5483
|
+
resolve(result.result.data);
|
|
5484
|
+
}
|
|
5485
|
+
});
|
|
5486
|
+
}
|
|
6282
5487
|
/**
|
|
6283
5488
|
* 重置 App 实例
|
|
6284
5489
|
*/
|
|
6285
|
-
|
|
6286
|
-
|
|
6287
|
-
|
|
6288
|
-
|
|
6289
|
-
value: function () {
|
|
6290
|
-
this.app = null;
|
|
6291
|
-
}
|
|
6292
|
-
});
|
|
6293
|
-
return CloudFunctionClient;
|
|
6294
|
-
}());
|
|
5490
|
+
reset() {
|
|
5491
|
+
this.app = null;
|
|
5492
|
+
}
|
|
5493
|
+
}
|
|
6295
5494
|
// 创建单例
|
|
6296
|
-
|
|
5495
|
+
const cloudFunctionClient = new CloudFunctionClient();
|
|
6297
5496
|
/**
|
|
6298
5497
|
* 云函数调用函数(保持原 API 兼容)
|
|
6299
5498
|
*/
|
|
6300
|
-
function callFunction(params) {
|
|
6301
|
-
return
|
|
6302
|
-
return __generator$3(this, function (_a) {
|
|
6303
|
-
return [2 /*return*/, cloudFunctionClient.call(params)];
|
|
6304
|
-
});
|
|
6305
|
-
});
|
|
5499
|
+
async function callFunction(params) {
|
|
5500
|
+
return cloudFunctionClient.call(params);
|
|
6306
5501
|
}
|
|
6307
5502
|
|
|
6308
5503
|
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 };
|