hifun-tools 1.4.11 → 1.4.13
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/dist/init/index.d.ts +2 -0
- package/dist/init/index.js +87 -11
- package/dist/msg/index.d.ts +2 -0
- package/dist/msg/index.js +51 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +34 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +3 -1
package/dist/init/index.d.ts
CHANGED
package/dist/init/index.js
CHANGED
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
import { getResource } from "./getResource";
|
|
3
3
|
import { isDomainMatch, filterSmartLines, getOptimalDecodedString, matchBrowser, toStandardUrl, difArr, } from "./utils";
|
|
4
4
|
import { AesDecrypt, AesEncrypt } from "./ende";
|
|
5
|
-
import { rewardMsg } from "../msg";
|
|
5
|
+
import { closeLoadingText, loadingText, rewardMsg } from "../msg";
|
|
6
|
+
import { Cache } from "../utils";
|
|
7
|
+
import { difference } from "lodash-es";
|
|
6
8
|
class InitCls {
|
|
7
9
|
AesDecrypt = AesDecrypt;
|
|
8
10
|
AesEncrypt = AesEncrypt;
|
|
@@ -11,7 +13,9 @@ class InitCls {
|
|
|
11
13
|
appTenant = "";
|
|
12
14
|
defaultBaseUrl = "";
|
|
13
15
|
domainBaseUrl = "";
|
|
16
|
+
ErrorDomainUrl = Cache("ErrorDomainUrl") || [];
|
|
14
17
|
initialized = false;
|
|
18
|
+
isRefreshNow = false;
|
|
15
19
|
localPath = "";
|
|
16
20
|
tenant = "";
|
|
17
21
|
tenantConfig = null;
|
|
@@ -150,17 +154,82 @@ class InitCls {
|
|
|
150
154
|
throw new Error("无法获取有效的租户信息");
|
|
151
155
|
}
|
|
152
156
|
async refreshHttp() {
|
|
157
|
+
const startTime = Date.now();
|
|
158
|
+
const log = (msg) => {
|
|
159
|
+
const t = ((Date.now() - startTime) / 1000).toFixed(1);
|
|
160
|
+
console.log(`[refreshHttp +${t}s] ${msg}`);
|
|
161
|
+
};
|
|
162
|
+
this.isRefreshNow = true;
|
|
163
|
+
log("进入函数,isRefreshNow = true");
|
|
164
|
+
let success = false;
|
|
165
|
+
let finished = false;
|
|
166
|
+
// ⏱ 每秒心跳日志
|
|
167
|
+
const ticker = setInterval(() => {
|
|
168
|
+
const t = Math.floor((Date.now() - startTime) / 1000);
|
|
169
|
+
console.log(`[refreshHttp ⏱ ${t}s] alive`);
|
|
170
|
+
}, 1000);
|
|
171
|
+
const clearTicker = () => {
|
|
172
|
+
if (!finished) {
|
|
173
|
+
finished = true;
|
|
174
|
+
clearInterval(ticker);
|
|
175
|
+
log("停止每秒日志");
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
const retrySchedule = [5, 15, 30]; // 秒(绝对时间点)
|
|
179
|
+
const tryRequest = async (label) => {
|
|
180
|
+
log(`${label}:开始请求`);
|
|
181
|
+
try {
|
|
182
|
+
loadingText("网络环境异常,正在重连...");
|
|
183
|
+
await getOptimalDecodedString([this.getBaseUrl()]);
|
|
184
|
+
success = true;
|
|
185
|
+
closeLoadingText();
|
|
186
|
+
this.isRefreshNow = false;
|
|
187
|
+
log(`${label}:请求成功 ✅`);
|
|
188
|
+
clearTicker();
|
|
189
|
+
return true;
|
|
190
|
+
}
|
|
191
|
+
catch (err) {
|
|
192
|
+
log(`${label}:请求失败 ❌`);
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
195
|
+
};
|
|
153
196
|
try {
|
|
154
|
-
|
|
197
|
+
// 立即请求
|
|
198
|
+
await tryRequest("首次");
|
|
199
|
+
// 按 5 / 15 / 30 秒重试
|
|
200
|
+
for (const at of retrySchedule) {
|
|
201
|
+
if (success)
|
|
202
|
+
break;
|
|
203
|
+
const wait = startTime + at * 1000 - Date.now();
|
|
204
|
+
if (wait > 0) {
|
|
205
|
+
log(`等待到第 ${at}s(剩余 ${Math.ceil(wait / 1000)}s)`);
|
|
206
|
+
await new Promise((r) => setTimeout(r, wait));
|
|
207
|
+
}
|
|
208
|
+
if (success)
|
|
209
|
+
break;
|
|
210
|
+
await tryRequest(`第 ${at}s 重试`);
|
|
211
|
+
}
|
|
212
|
+
// 30 秒后仍失败
|
|
213
|
+
if (!success) {
|
|
214
|
+
log("30 秒后仍失败,进入兜底逻辑 ❗");
|
|
215
|
+
closeLoadingText();
|
|
216
|
+
this.isRefreshNow = false;
|
|
217
|
+
Cache("ErrorDomainUrl", [...this.ErrorDomainUrl, this.domainBaseUrl]);
|
|
218
|
+
rewardMsg({
|
|
219
|
+
title: "提示",
|
|
220
|
+
text: "网络环境异常,刷新页面以重置",
|
|
221
|
+
onSubmit() {
|
|
222
|
+
location.reload();
|
|
223
|
+
},
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
catch (e) {
|
|
228
|
+
log("发生异常:" + e);
|
|
229
|
+
this.isRefreshNow = false;
|
|
155
230
|
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
title: "提示",
|
|
159
|
-
text: "网络环境异常,刷新页面以重置",
|
|
160
|
-
onSubmit() {
|
|
161
|
-
location.reload();
|
|
162
|
-
},
|
|
163
|
-
});
|
|
231
|
+
finally {
|
|
232
|
+
clearTicker();
|
|
164
233
|
}
|
|
165
234
|
}
|
|
166
235
|
/** 获取 lineDict.txt 内容 */
|
|
@@ -180,7 +249,14 @@ class InitCls {
|
|
|
180
249
|
try {
|
|
181
250
|
const response = await fetch(`/lineAddress.txt?t=${Date.now()}`);
|
|
182
251
|
const configText = await response.text();
|
|
183
|
-
const
|
|
252
|
+
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
|
+
}
|
|
184
260
|
const dictList = this.getTenantDictList();
|
|
185
261
|
const lineGroup = this.getTenantDict()?.lineGroup;
|
|
186
262
|
let baseUrl = filterSmartLines(dictList, OriginBaseUrl, lineGroup);
|
package/dist/msg/index.d.ts
CHANGED
package/dist/msg/index.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
let isRewardShowing = false;
|
|
2
|
+
// --------------------------- loadingText ---------------------------
|
|
3
|
+
let loadingMask = null;
|
|
4
|
+
let loadingTextEl = null;
|
|
2
5
|
// --------------------------- 普通消息 ---------------------------
|
|
3
6
|
let messageStackContainer = null;
|
|
4
7
|
// --------------------------- reward 弹窗 ---------------------------
|
|
@@ -268,3 +271,51 @@ export function rewardMsg(options) {
|
|
|
268
271
|
rewardQueue.push(options);
|
|
269
272
|
showNextReward();
|
|
270
273
|
}
|
|
274
|
+
export function loadingText(text) {
|
|
275
|
+
if (text) {
|
|
276
|
+
if (!loadingMask) {
|
|
277
|
+
loadingMask = document.createElement("div");
|
|
278
|
+
Object.assign(loadingMask.style, {
|
|
279
|
+
position: "fixed",
|
|
280
|
+
top: "0",
|
|
281
|
+
left: "0",
|
|
282
|
+
width: "100vw",
|
|
283
|
+
height: "100vh",
|
|
284
|
+
background: "rgba(0,0,0,0)", // 透明蒙层
|
|
285
|
+
zIndex: "1000000",
|
|
286
|
+
pointerEvents: "auto", // 阻止所有操作
|
|
287
|
+
display: "flex",
|
|
288
|
+
justifyContent: "center",
|
|
289
|
+
alignItems: "flex-start",
|
|
290
|
+
});
|
|
291
|
+
loadingTextEl = document.createElement("div");
|
|
292
|
+
Object.assign(loadingTextEl.style, {
|
|
293
|
+
marginTop: "20px",
|
|
294
|
+
padding: "8px 16px",
|
|
295
|
+
background: "rgba(0,0,0,0.6)",
|
|
296
|
+
color: "#fff",
|
|
297
|
+
borderRadius: "8px",
|
|
298
|
+
fontSize: "14px",
|
|
299
|
+
fontWeight: "500",
|
|
300
|
+
maxWidth: "80%",
|
|
301
|
+
textAlign: "center",
|
|
302
|
+
wordBreak: "break-word",
|
|
303
|
+
boxShadow: "0 4px 12px rgba(0,0,0,0.2)",
|
|
304
|
+
});
|
|
305
|
+
loadingMask.appendChild(loadingTextEl);
|
|
306
|
+
document.body.appendChild(loadingMask);
|
|
307
|
+
}
|
|
308
|
+
loadingTextEl.textContent = text;
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
// 如果没传 text,则关闭
|
|
312
|
+
closeLoadingText();
|
|
313
|
+
}
|
|
314
|
+
// 专门关闭函数
|
|
315
|
+
export function closeLoadingText() {
|
|
316
|
+
if (loadingMask) {
|
|
317
|
+
loadingMask.remove();
|
|
318
|
+
loadingMask = null;
|
|
319
|
+
loadingTextEl = null;
|
|
320
|
+
}
|
|
321
|
+
}
|
package/dist/utils/index.d.ts
CHANGED
package/dist/utils/index.js
CHANGED
|
@@ -32,6 +32,40 @@ export function Local(name, value, time) {
|
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
|
+
export function Cache(name, value, time) {
|
|
36
|
+
const date = Number(new Date().getTime() / 1000);
|
|
37
|
+
if (value === null) {
|
|
38
|
+
sessionStorage.removeItem(name);
|
|
39
|
+
}
|
|
40
|
+
else if (value !== undefined) {
|
|
41
|
+
if (time) {
|
|
42
|
+
sessionStorage.setItem(name, JSON.stringify({ time: date + time, value }));
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
sessionStorage.setItem(name, JSON.stringify({ value }));
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
const v = sessionStorage.getItem(name);
|
|
50
|
+
if (v) {
|
|
51
|
+
if (JSON.parse(v).time) {
|
|
52
|
+
if (JSON.parse(v).time - date > 0) {
|
|
53
|
+
return JSON.parse(v).value;
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
sessionStorage.removeItem(name);
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
return JSON.parse(v).value;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
35
69
|
/**
|
|
36
70
|
* 获取链接参数
|
|
37
71
|
* @param name
|
package/dist/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const VERSION = "1.4.
|
|
1
|
+
export declare const VERSION = "1.4.12";
|
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const VERSION = "1.4.
|
|
1
|
+
export const VERSION = "1.4.12";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hifun-tools",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.13",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"module": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -20,9 +20,11 @@
|
|
|
20
20
|
"@babel/core": "^7.28.5",
|
|
21
21
|
"@babel/preset-typescript": "^7.28.5",
|
|
22
22
|
"@types/jest": "^30.0.0",
|
|
23
|
+
"@types/lodash-es": "^4.17.12",
|
|
23
24
|
"jest": "^30.2.0",
|
|
24
25
|
"jest-environment-jsdom": "^30.2.0",
|
|
25
26
|
"jscodeshift": "^17.3.0",
|
|
27
|
+
"lodash-es": "^4.17.22",
|
|
26
28
|
"prettier": "^3.6.2",
|
|
27
29
|
"ts-jest": "^29.4.5",
|
|
28
30
|
"typescript": "^5.6.3"
|