hifun-tools 1.4.17 → 1.4.19

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.
@@ -6,12 +6,15 @@ declare class InitCls {
6
6
  private appGateway;
7
7
  private appLine;
8
8
  private appTenant;
9
+ private backupAddress;
10
+ private backupAddressList;
9
11
  private defaultBaseUrl;
10
12
  private domainBaseUrl;
11
13
  private ErrorDomainUrl;
12
14
  private initialized;
13
15
  isRefreshNow: boolean;
14
16
  private localPath;
17
+ private OriginBaseUrl;
15
18
  private tenant;
16
19
  private tenantConfig;
17
20
  private tenantDict;
@@ -24,6 +27,7 @@ declare class InitCls {
24
27
  appLine?: string;
25
28
  appTenant?: string;
26
29
  appGateway?: string;
30
+ backupAddress?: string[];
27
31
  }): Promise<(string | null)[]>;
28
32
  /** 是否 App 环境 */
29
33
  getIsApp(): boolean;
@@ -48,6 +52,8 @@ declare class InitCls {
48
52
  refreshHttp(): Promise<void>;
49
53
  /** 获取 lineDict.txt 内容 */
50
54
  private _fetchLineDict;
55
+ /** 按顺序依次请求 txt 文件,成功即返回内容,全部失败返回空字符串 */
56
+ private fetchTxtFile;
51
57
  /** 获取并处理 lineAddress.txt */
52
58
  private _loadGatewayConfig;
53
59
  }
@@ -1,33 +1,38 @@
1
1
  // index.ts
2
2
  import { getResource } from "./getResource";
3
- import { isDomainMatch, filterSmartLines, getOptimalDecodedString, matchBrowser, toStandardUrl, difArr, } from "./utils";
3
+ import { isDomainMatch, filterSmartLines, getOptimalDecodedString, matchBrowser, toStandardUrl, } from "./utils";
4
4
  import { AesDecrypt, AesEncrypt } from "./ende";
5
5
  import { closeLoadingText, loadingText, rewardMsg } from "../msg";
6
6
  import { Cache } from "../utils";
7
- import { difference } from "lodash-es";
7
+ import { uniq } from "lodash-es";
8
8
  class InitCls {
9
9
  AesDecrypt = AesDecrypt;
10
10
  AesEncrypt = AesEncrypt;
11
11
  appGateway = "";
12
12
  appLine = "";
13
13
  appTenant = "";
14
+ backupAddress = [];
15
+ backupAddressList = [];
14
16
  defaultBaseUrl = "";
15
17
  domainBaseUrl = "";
16
18
  ErrorDomainUrl = Cache("ErrorDomainUrl") || [];
17
19
  initialized = false;
18
20
  isRefreshNow = false;
19
21
  localPath = "";
22
+ OriginBaseUrl = [];
20
23
  tenant = "";
21
24
  tenantConfig = null;
22
25
  tenantDict = null;
23
26
  tenantDictList = [];
24
27
  /** 初始化配置入口 */
25
28
  async InitConfig(options) {
26
- const { fileType, defaultBaseUrl, localPath, appLine, appTenant, appGateway = "", } = options;
29
+ const { fileType, defaultBaseUrl, localPath, appLine, appTenant, appGateway = "", backupAddress, } = options;
27
30
  if (defaultBaseUrl)
28
31
  this.defaultBaseUrl = defaultBaseUrl;
29
32
  if (localPath)
30
33
  this.localPath = localPath;
34
+ if (backupAddress)
35
+ this.backupAddress = backupAddress;
31
36
  if (appLine && appTenant) {
32
37
  this.appLine = appLine;
33
38
  this.appTenant = appTenant;
@@ -179,7 +184,7 @@ class InitCls {
179
184
  const tryRequest = async (label) => {
180
185
  log(`${label}:开始请求`);
181
186
  try {
182
- loadingText("网络环境异常,正在重连...");
187
+ loadingText("网络波动,正在优选线路...");
183
188
  await getOptimalDecodedString([this.getBaseUrl()]);
184
189
  success = true;
185
190
  closeLoadingText();
@@ -214,10 +219,12 @@ class InitCls {
214
219
  log("30 秒后仍失败,进入兜底逻辑 ❗");
215
220
  closeLoadingText();
216
221
  this.isRefreshNow = false;
217
- Cache("ErrorDomainUrl", [...this.ErrorDomainUrl, this.domainBaseUrl]);
222
+ const nowLineGroup = this.tenantDictList.find((a) => toStandardUrl(a.line) == toStandardUrl(this.domainBaseUrl))?.lineGroup;
223
+ if (!!nowLineGroup)
224
+ Cache("ErrorDomainUrl", uniq([...this.ErrorDomainUrl, nowLineGroup]));
218
225
  rewardMsg({
219
226
  title: "提示",
220
- text: "网络环境异常,刷新页面以重置",
227
+ text: "优选线路超时,刷新页面以重置" + (nowLineGroup ? "" : "..."),
221
228
  onSubmit() {
222
229
  location.reload();
223
230
  },
@@ -242,6 +249,27 @@ class InitCls {
242
249
  throw new Error(`HTTP 错误: ${response.status}`);
243
250
  return response.text();
244
251
  }
252
+ /** 按顺序依次请求 txt 文件,成功即返回内容,全部失败返回空字符串 */
253
+ async fetchTxtFile(urls) {
254
+ for (const url of urls) {
255
+ try {
256
+ const response = await fetch(`${url}?t=${Date.now()}`, {
257
+ method: "GET",
258
+ cache: "no-store",
259
+ });
260
+ if (!response.ok) {
261
+ throw new Error(`HTTP 错误: ${response.status}`);
262
+ }
263
+ return await response.text();
264
+ }
265
+ catch (err) {
266
+ // 当前 url 失败,继续请求下一个
267
+ continue;
268
+ }
269
+ }
270
+ // 所有 url 都失败
271
+ return "";
272
+ }
245
273
  /** 获取并处理 lineAddress.txt */
246
274
  async _loadGatewayConfig() {
247
275
  if (this.domainBaseUrl)
@@ -250,32 +278,26 @@ class InitCls {
250
278
  const response = await fetch(`/lineAddress.txt?t=${Date.now()}`);
251
279
  const configText = await response.text();
252
280
  const resArray = JSON.parse(AesDecrypt(configText));
253
- let OriginBaseUrl = [];
254
- if (this.ErrorDomainUrl.length < resArray.length) {
255
- OriginBaseUrl = difference(resArray, this.ErrorDomainUrl);
256
- }
257
- else {
258
- OriginBaseUrl = resArray;
259
- }
260
- const dictList = this.getTenantDictList();
281
+ let OriginBaseUrl = resArray;
282
+ this.OriginBaseUrl = OriginBaseUrl;
283
+ const dictList = this.getTenantDictList().filter((a) => !this.ErrorDomainUrl.includes(a.lineGroup));
261
284
  const lineGroup = this.getTenantDict()?.lineGroup;
262
- let baseUrl = filterSmartLines(dictList, OriginBaseUrl, lineGroup);
285
+ let baseUrl = filterSmartLines(dictList, OriginBaseUrl, this.ErrorDomainUrl?.length == 0 ? lineGroup : "");
263
286
  try {
264
287
  this.domainBaseUrl = toStandardUrl(await getOptimalDecodedString(baseUrl));
265
288
  console.info("✅ 成功加载生产环境配置:", this.domainBaseUrl);
266
289
  }
267
- catch {
290
+ catch (error) {
291
+ const backUrl = JSON.parse(AesDecrypt(await this.fetchTxtFile(this.backupAddress)));
292
+ this.backupAddressList = backUrl || [];
268
293
  // 备用策略
269
- let allBaseUrl = filterSmartLines(dictList, OriginBaseUrl);
270
294
  try {
271
- this.domainBaseUrl = toStandardUrl(await getOptimalDecodedString(difArr(allBaseUrl, baseUrl), {
272
- filterIp: false,
273
- }));
295
+ this.domainBaseUrl = toStandardUrl(await getOptimalDecodedString(backUrl));
274
296
  console.info("✅ 备用配置成功:", this.domainBaseUrl);
275
297
  }
276
298
  catch (err) {
277
- this.domainBaseUrl = toStandardUrl(allBaseUrl[0]);
278
- console.warn("⚠️ 备选测速失败,使用备用配置:", allBaseUrl);
299
+ this.domainBaseUrl = toStandardUrl(backUrl?.length > 0 ? backUrl[0] : OriginBaseUrl[0]);
300
+ console.warn("⚠️ 备选测速失败,使用备用配置:", this.domainBaseUrl);
279
301
  }
280
302
  }
281
303
  return this.domainBaseUrl;
@@ -288,13 +310,13 @@ class InitCls {
288
310
  return this.domainBaseUrl;
289
311
  }
290
312
  }
291
- else if ((this.defaultBaseUrl &&
292
- (location.host.includes("iggame") ||
293
- location.host.includes("localhost"))) ||
294
- (!!this.localPath &&
295
- location.origin.includes(this.localPath.replace(/\/+$/, "")))) {
296
- this.domainBaseUrl = toStandardUrl(this.defaultBaseUrl);
297
- console.info("🏠 使用默认 BaseUrl:", this.domainBaseUrl);
313
+ else {
314
+ this.domainBaseUrl =
315
+ this.defaultBaseUrl ||
316
+ toStandardUrl(this.backupAddressList?.length > 0
317
+ ? this.backupAddressList[0]
318
+ : this.OriginBaseUrl[0]);
319
+ console.info("🏠 使用默认 BaseUrl1:", this.domainBaseUrl);
298
320
  return this.domainBaseUrl;
299
321
  }
300
322
  console.error("⚠️ 加载 lineAddress.txt 失败:", err);
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const VERSION = "1.4.16";
1
+ export declare const VERSION = "1.4.18";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = "1.4.16";
1
+ export const VERSION = "1.4.18";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hifun-tools",
3
- "version": "1.4.17",
3
+ "version": "1.4.19",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",