hifun-tools 1.4.29 → 1.4.36

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.
@@ -30,6 +30,8 @@ declare class InitCls {
30
30
  appGateway?: string;
31
31
  backupAddress?: string[];
32
32
  retry?: boolean;
33
+ lineDictData?: string;
34
+ lineAddressData?: string;
33
35
  }): Promise<(string | null)[]>;
34
36
  getOnlyBackup(): boolean;
35
37
  setOnlyBackup(value: boolean): void;
@@ -49,7 +51,6 @@ declare class InitCls {
49
51
  getImgPath(imgName: string): string;
50
52
  /** 严格初始化租户信息 */
51
53
  private _initializeTenant;
52
- /** 严格同步获取租户信息 */
53
54
  private _getTenantInfoStrictSync;
54
55
  /** 匹配默认租户 */
55
56
  private _matchDefaultTenant;
@@ -27,7 +27,8 @@ class InitCls {
27
27
  tenantDictList = [];
28
28
  /** 初始化配置入口 */
29
29
  async InitConfig(options) {
30
- const { fileType, defaultBaseUrl, localPath, appLine, appTenant, appGateway = "", backupAddress, retry, } = options;
30
+ const { fileType, defaultBaseUrl, localPath, appLine, appTenant, appGateway = "", backupAddress, retry, lineDictData, lineAddressData, // ✨ 提取参数
31
+ } = options;
31
32
  if (defaultBaseUrl)
32
33
  this.defaultBaseUrl = defaultBaseUrl;
33
34
  if (localPath)
@@ -44,11 +45,12 @@ class InitCls {
44
45
  const results = [];
45
46
  if (fileType?.includes("lineDict")) {
46
47
  console.info("初始化 lineDict...");
47
- results.push(await this._initializeTenant());
48
+ results.push(await this._initializeTenant(lineDictData)); // 将参数传递给初始化方法
48
49
  }
49
50
  if (fileType?.includes("lineAddress")) {
50
51
  console.info("初始化 lineAddress...");
51
- results.push(await this._loadGatewayConfig(retry));
52
+ // ✨ 将 lineAddressData 传递给加载方法
53
+ results.push(await this._loadGatewayConfig(retry, lineAddressData));
52
54
  }
53
55
  console.info("✅ 所有初始化完成:", results);
54
56
  return results;
@@ -102,11 +104,11 @@ class InitCls {
102
104
  return getResource(`${this.tenant}/image/${imgName}`);
103
105
  }
104
106
  /** 严格初始化租户信息 */
105
- async _initializeTenant() {
107
+ async _initializeTenant(lineDictData) {
106
108
  if (this.initialized)
107
109
  return this.tenant;
108
110
  try {
109
- const tenantInfo = await this._getTenantInfoStrictSync();
111
+ const tenantInfo = await this._getTenantInfoStrictSync(lineDictData);
110
112
  this.tenant = tenantInfo.tenant;
111
113
  this.tenantConfig = getResource(`${this.tenant}/config.json`);
112
114
  this.initialized = true;
@@ -118,8 +120,39 @@ class InitCls {
118
120
  throw err;
119
121
  }
120
122
  }
121
- /** 严格同步获取租户信息 */
122
- async _getTenantInfoStrictSync() {
123
+ async _getTenantInfoStrictSync(lineDictData) {
124
+ // 尝试使用传入的lineDictData进行匹配
125
+ if (lineDictData) {
126
+ try {
127
+ const data = JSON.parse(AesDecrypt(lineDictData));
128
+ const host = location.host;
129
+ if (this.getIsApp()) {
130
+ this.tenantDict = {
131
+ browserCheck: [],
132
+ line: host,
133
+ lineGroup: this.appLine,
134
+ tenant: this.appTenant,
135
+ };
136
+ this.tenantDictList = [...data, this.tenantDict];
137
+ console.info("🏠 匹配 App 租户:", this.tenantDict);
138
+ return { tenant: this.appTenant };
139
+ }
140
+ const matched = data.find((item) => isDomainMatch([item.line], host));
141
+ if (matched) {
142
+ this.tenantDictList = [...data];
143
+ this.tenantDict = matched;
144
+ console.info("🏠 匹配租户:", matched.tenant);
145
+ return { tenant: matched.tenant };
146
+ }
147
+ // 如果传入的数据没有匹配到,继续执行下面的原始逻辑
148
+ console.info("⚠️ 传入的 lineDictData 未匹配到租户,尝试原始逻辑");
149
+ }
150
+ catch (err) {
151
+ console.warn("⚠️ 解析传入的 lineDictData 失败:", err);
152
+ // 如果传入的数据解析失败,也继续执行下面的原始逻辑
153
+ }
154
+ }
155
+ // 如果没有传入数据或传入数据匹配失败,执行原始逻辑
123
156
  try {
124
157
  const rawText = await this._fetchLineDict();
125
158
  const data = JSON.parse(AesDecrypt(rawText));
@@ -291,82 +324,84 @@ class InitCls {
291
324
  return "";
292
325
  }
293
326
  /** 获取并处理 lineAddress.txt */
294
- async _loadGatewayConfig(retry = false) {
327
+ async _loadGatewayConfig(retry = false, lineAddressData) {
295
328
  if (this.domainBaseUrl && !retry)
296
329
  return this.domainBaseUrl;
297
- /** 设置 domainBaseUrl 并返回 */
298
330
  const setBaseUrl = async (list) => {
299
331
  this.domainBaseUrl = toStandardUrl(await getOptimalDecodedString(list));
300
332
  return this.domainBaseUrl;
301
333
  };
302
- /** 备用 Address 策略 */
303
- const loadBackupAddress = async () => {
304
- if (this.tenant === "t1sport")
305
- this.backupAddress = t1BackGateWay;
306
- const backupList = JSON.parse(AesDecrypt(await this.fetchTxtFile(this.backupAddress)));
307
- this.backupAddressList = backupList || [];
308
- await setBaseUrl(this.backupAddressList);
309
- console.info("✅ 备用远程Address配置成功:", this.domainBaseUrl);
334
+ /** 封装:根据传入的加密字符串执行测速流程 */
335
+ const trySpeedTestWithData = async (encryptedData, label) => {
336
+ try {
337
+ console.info(`🧪 尝试使用 [${label}] 进行解析和测速...`);
338
+ const originBaseUrl = JSON.parse(AesDecrypt(encryptedData));
339
+ this.OriginBaseUrl = originBaseUrl;
340
+ // 计算智能线路
341
+ const tenantDictList = this.getTenantDictList().filter((a) => !this.ErrorDomainUrl.includes(a.lineGroup));
342
+ const currentLineGroup = this.getTenantDict()?.lineGroup;
343
+ let baseUrlList = filterSmartLines(tenantDictList, originBaseUrl, this.ErrorDomainUrl?.length === 0 ? currentLineGroup : "");
344
+ // 执行测速
345
+ await setBaseUrl(baseUrlList);
346
+ console.info(`✅ [${label}] 测速成功:`, this.domainBaseUrl);
347
+ return true;
348
+ }
349
+ catch (e) {
350
+ console.warn(`⚠️ [${label}] 流程失败 (解析或测速未通过)`);
351
+ return false;
352
+ }
310
353
  };
311
354
  this.onlyBackUp = false;
355
+ // --- 核心执行流 ---
356
+ // 1️⃣ 第一优先级:如果有传入 lineAddressData,先跑一遍完整流程
357
+ if (lineAddressData) {
358
+ const success = await trySpeedTestWithData(lineAddressData, "传入参数 lineAddressData");
359
+ if (success) {
360
+ localStorage.removeItem("retryHttpTime");
361
+ return this.domainBaseUrl;
362
+ }
363
+ }
364
+ // 2️⃣ 第二优先级:如果上面失败了或没传,执行原始 fetch 逻辑
312
365
  try {
313
- /** 1️⃣ 读取主配置 */
366
+ console.info("🌐 正在从服务端请求 lineAddress.txt 作为兜底...");
314
367
  const response = await fetch(`/lineAddress.txt?t=${Date.now()}`);
315
368
  const configText = await response.text();
316
- const originBaseUrl = JSON.parse(AesDecrypt(configText));
317
- this.OriginBaseUrl = originBaseUrl;
318
- /** 2️⃣ 计算智能线路 */
319
- const tenantDictList = this.getTenantDictList().filter((a) => !this.ErrorDomainUrl.includes(a.lineGroup));
320
- const currentLineGroup = this.getTenantDict()?.lineGroup;
321
- let baseUrlList = filterSmartLines(tenantDictList, originBaseUrl, this.ErrorDomainUrl?.length === 0 ? currentLineGroup : "");
322
- /** 3️⃣ 主线路测速 */
323
- try {
324
- await setBaseUrl(baseUrlList);
369
+ const success = await trySpeedTestWithData(configText, "远程文件 lineAddress.txt");
370
+ if (success) {
325
371
  localStorage.removeItem("retryHttpTime");
326
- console.info("✅ 成功加载生产环境配置:", this.domainBaseUrl);
327
- }
328
- catch {
329
- /** 4️⃣ 降级策略 */
330
- try {
331
- if (this.ErrorDomainUrl?.length == 0) {
332
- const fallbackList = filterSmartLines(tenantDictList.filter((a) => a.lineGroup !== currentLineGroup), originBaseUrl);
333
- await setBaseUrl(fallbackList);
334
- localStorage.removeItem("retryHttpTime");
335
- }
336
- else {
337
- await loadBackupAddress();
338
- }
339
- }
340
- catch {
341
- try {
342
- await loadBackupAddress();
343
- }
344
- catch {
345
- this.domainBaseUrl = toStandardUrl(originBaseUrl[0]);
346
- localStorage.removeItem("retryHttpTime");
347
- console.warn("⚠️ 备选测速失败,使用备用配置:", this.domainBaseUrl);
348
- this.onlyBackUp = true;
349
- }
350
- }
372
+ return this.domainBaseUrl;
351
373
  }
352
- return this.domainBaseUrl;
353
374
  }
354
375
  catch (err) {
355
- /** 5️⃣ 最终兜底 */
376
+ console.error("❌ 远程 lineAddress.txt 获取失败", err);
377
+ }
378
+ // 3️⃣ 降级策略:如果传入数据和远程文件都测速失败,进入备用/App兜底
379
+ try {
380
+ console.info("🚑 尝试最终降级逻辑 (备用地址/App配置)...");
381
+ // 这里执行你原本代码里的 loadBackupAddress 等逻辑
356
382
  if (this.getIsApp() && this.appGateway) {
357
383
  this.domainBaseUrl = toStandardUrl(this.appGateway);
358
- console.info("🏠 使用app传入网关:", this.domainBaseUrl);
359
384
  return this.domainBaseUrl;
360
385
  }
386
+ // 尝试备用地址
387
+ if (this.tenant === "t1sport")
388
+ this.backupAddress = t1BackGateWay;
389
+ const backupRaw = await this.fetchTxtFile(this.backupAddress);
390
+ if (backupRaw) {
391
+ const backupList = JSON.parse(AesDecrypt(backupRaw));
392
+ this.backupAddressList = backupList || [];
393
+ await setBaseUrl(this.backupAddressList);
394
+ return this.domainBaseUrl;
395
+ }
396
+ }
397
+ catch (finalErr) {
398
+ // 最终无奈的保底:取第一个已知的 URL
361
399
  this.domainBaseUrl =
362
- this.defaultBaseUrl ||
363
- toStandardUrl(this.backupAddressList?.length > 0
364
- ? this.backupAddressList[0]
365
- : this.OriginBaseUrl[0]);
366
- console.info("🏠 使用默认 BaseUrl:", this.domainBaseUrl);
367
- console.error("⚠️ 加载 lineAddress.txt 失败:", err);
368
- return this.domainBaseUrl ?? null;
400
+ this.defaultBaseUrl || toStandardUrl(this.OriginBaseUrl[0] || "");
401
+ this.onlyBackUp = true;
402
+ console.warn("⚠️ 所有链路均不可用,使用最后保底地址:", this.domainBaseUrl);
369
403
  }
404
+ return this.domainBaseUrl || null;
370
405
  }
371
406
  }
372
407
  const HF = new InitCls();
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const VERSION = "1.4.28";
1
+ export declare const VERSION = "1.4.35";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = "1.4.28";
1
+ export const VERSION = "1.4.35";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hifun-tools",
3
- "version": "1.4.29",
3
+ "version": "1.4.36",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",