a2bei4-utils 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +2 -2
  3. package/dist/a2bei4.utils.cjs.js +2080 -1846
  4. package/dist/a2bei4.utils.cjs.js.map +1 -1
  5. package/dist/a2bei4.utils.cjs.min.js +1 -1
  6. package/dist/a2bei4.utils.cjs.min.js.map +1 -1
  7. package/dist/a2bei4.utils.esm.js +2073 -1843
  8. package/dist/a2bei4.utils.esm.js.map +1 -1
  9. package/dist/a2bei4.utils.esm.min.js +1 -1
  10. package/dist/a2bei4.utils.esm.min.js.map +1 -1
  11. package/dist/a2bei4.utils.umd.js +2080 -1846
  12. package/dist/a2bei4.utils.umd.js.map +1 -1
  13. package/dist/a2bei4.utils.umd.min.js +1 -1
  14. package/dist/a2bei4.utils.umd.min.js.map +1 -1
  15. package/dist/arr.cjs +27 -27
  16. package/dist/arr.cjs.map +1 -1
  17. package/dist/arr.js +27 -27
  18. package/dist/arr.js.map +1 -1
  19. package/dist/audio.cjs +274 -274
  20. package/dist/audio.cjs.map +1 -1
  21. package/dist/audio.js +274 -274
  22. package/dist/audio.js.map +1 -1
  23. package/dist/browser.cjs +52 -52
  24. package/dist/browser.cjs.map +1 -1
  25. package/dist/browser.js +52 -52
  26. package/dist/browser.js.map +1 -1
  27. package/dist/common.cjs +369 -369
  28. package/dist/common.cjs.map +1 -1
  29. package/dist/common.js +369 -369
  30. package/dist/common.js.map +1 -1
  31. package/dist/date.cjs +421 -188
  32. package/dist/date.cjs.map +1 -1
  33. package/dist/date.js +414 -185
  34. package/dist/date.js.map +1 -1
  35. package/dist/download.cjs +102 -102
  36. package/dist/download.cjs.map +1 -1
  37. package/dist/download.js +102 -102
  38. package/dist/download.js.map +1 -1
  39. package/dist/evt.cjs +148 -148
  40. package/dist/evt.cjs.map +1 -1
  41. package/dist/evt.js +148 -148
  42. package/dist/evt.js.map +1 -1
  43. package/dist/id.cjs +68 -68
  44. package/dist/id.cjs.map +1 -1
  45. package/dist/id.js +68 -68
  46. package/dist/id.js.map +1 -1
  47. package/dist/timer.cjs +51 -50
  48. package/dist/timer.cjs.map +1 -1
  49. package/dist/timer.js +51 -50
  50. package/dist/timer.js.map +1 -1
  51. package/dist/tree.cjs +165 -165
  52. package/dist/tree.cjs.map +1 -1
  53. package/dist/tree.js +165 -165
  54. package/dist/tree.js.map +1 -1
  55. package/dist/webSocket.cjs +403 -403
  56. package/dist/webSocket.cjs.map +1 -1
  57. package/dist/webSocket.js +403 -403
  58. package/dist/webSocket.js.map +1 -1
  59. package/package.json +1 -1
  60. package/readme.txt +21 -11
  61. package/types/date.d.ts +243 -45
  62. package/types/index.d.ts +244 -47
@@ -4,1868 +4,2098 @@
4
4
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.a2bei4Utils = {}));
5
5
  })(this, (function (exports) { 'use strict';
6
6
 
7
- /**
8
- * 使用 Fisher-Yates 算法对数组 **原地** 随机乱序。
9
- * @template T
10
- * @param {T[]} arr - 要乱序的数组
11
- * @returns {T[]} 返回传入的同一数组实例(已乱序)
12
- */
13
- function shuffle(arr) {
14
- // 方式一:
15
- // arr.sort(() => Math.random() - 0.5);
16
- // 方式二:
17
- for (let i = arr.length - 1; i > 0; i--) {
18
- const j = Math.floor(Math.random() * (i + 1));
19
- [arr[i], arr[j]] = [arr[j], arr[i]];
20
- }
21
- return arr;
7
+ /**
8
+ * 使用 Fisher-Yates 算法对数组 **原地** 随机乱序。
9
+ * @template T
10
+ * @param {T[]} arr - 要乱序的数组
11
+ * @returns {T[]} 返回传入的同一数组实例(已乱序)
12
+ */
13
+ function shuffle(arr) {
14
+ // 方式一:
15
+ // arr.sort(() => Math.random() - 0.5);
16
+ // 方式二:
17
+ for (let i = arr.length - 1; i > 0; i--) {
18
+ const j = Math.floor(Math.random() * (i + 1));
19
+ [arr[i], arr[j]] = [arr[j], arr[i]];
20
+ }
21
+ return arr;
22
+ }
23
+
24
+ /**
25
+ * 将数组中的元素从 `fromIndex` 移动到 `toIndex`,**原地** 修改并返回该数组。
26
+ * @template T
27
+ * @param {T[]} arr - 要操作的数组
28
+ * @param {number} fromIndex - 原始下标
29
+ * @param {number} toIndex - 目标下标
30
+ * @returns {T[]} 返回传入的同一数组实例
31
+ */
32
+ function moveItem(arr, fromIndex, toIndex) {
33
+ arr.splice(toIndex, 0, arr.splice(fromIndex, 1)[0]);
22
34
  }
23
35
 
24
- /**
25
- * 将数组中的元素从 `fromIndex` 移动到 `toIndex`,**原地** 修改并返回该数组。
26
- * @template T
27
- * @param {T[]} arr - 要操作的数组
28
- * @param {number} fromIndex - 原始下标
29
- * @param {number} toIndex - 目标下标
30
- * @returns {T[]} 返回传入的同一数组实例
31
- */
32
- function moveItem(arr, fromIndex, toIndex) {
33
- arr.splice(toIndex, 0, arr.splice(fromIndex, 1)[0]);
36
+ const AudioStreamResamplerProcessorCode = `
37
+ class AudioStreamResamplerProcessor extends AudioWorkletProcessor {
38
+ constructor(options) {
39
+ super();
40
+ const config = options.processorOptions || {};
41
+ this.targetSampleRate = config.targetSampleRate || 16000;
42
+ this.sourceSampleRate = sampleRate;
43
+ this.downsampleRatio = this.sourceSampleRate / this.targetSampleRate;
44
+
45
+ // 16000Hz 用 60ms chunk (960 samples),其他用 1024
46
+ this.chunkSize = this.targetSampleRate === 16000 ? 960 : 1024;
47
+
48
+ const sourceBufferSize = config.sourceBufferSize || 16384; // ~340ms @48kHz
49
+ const pcmBufferSize = config.pcmBufferSize || (this.chunkSize * 10); // 更大缓冲,减少溢出概率
50
+
51
+ this.sourceBuffer = new Float32Array(sourceBufferSize);
52
+ this.sourceBufferLength = 0;
53
+
54
+ this.pcmBuffer = new Int16Array(pcmBufferSize);
55
+ this.pcmBufferIndex = 0;
56
+ }
57
+
58
+ process(inputs, outputs, parameters) {
59
+ const input = inputs[0];
60
+ if (!input || input.length === 0 || input[0].length === 0) return true;
61
+ const inputChannel = input[0];
62
+
63
+ // 1. 写入源缓冲区(溢出时覆盖最旧数据)
64
+ const newLength = this.sourceBufferLength + inputChannel.length;
65
+ if (newLength > this.sourceBuffer.length) {
66
+ const overflow = newLength - this.sourceBuffer.length;
67
+ this.sourceBuffer.copyWithin(0, overflow);
68
+ this.sourceBufferLength = this.sourceBuffer.length - inputChannel.length;
69
+ }
70
+ this.sourceBuffer.set(inputChannel, this.sourceBufferLength);
71
+ this.sourceBufferLength += inputChannel.length;
72
+
73
+ // 2. 计算可降采样样本数
74
+ const availableOutputSamples = Math.floor(this.sourceBufferLength / this.downsampleRatio);
75
+ if (availableOutputSamples === 0) return true;
76
+
77
+ // 3. 线性插值降采样
78
+ const downsampled = new Float32Array(availableOutputSamples);
79
+ for (let i = 0; i < availableOutputSamples; i++) {
80
+ const srcIndex = i * this.downsampleRatio;
81
+ const srcIndexInt = Math.floor(srcIndex);
82
+ const fraction = srcIndex - srcIndexInt;
83
+ const val0 = this.sourceBuffer[srcIndexInt];
84
+ const val1 = srcIndexInt + 1 < this.sourceBufferLength ? this.sourceBuffer[srcIndexInt + 1] : val0;
85
+ downsampled[i] = val0 + (val1 - val0) * fraction;
86
+ }
87
+
88
+ // 4. Float32 → Int16 PCM
89
+ const pcmData = this.floatTo16BitPCM(downsampled);
90
+
91
+ // 5. 写入 PCM 缓冲区(空间不足时直接覆盖最旧,缓冲区足够大基本不会触发)
92
+ if (this.pcmBufferIndex + pcmData.length > this.pcmBuffer.length) {
93
+ // 简单策略:从头覆盖(丢弃最旧数据)
94
+ this.pcmBufferIndex = 0;
95
+ }
96
+ this.pcmBuffer.set(pcmData, this.pcmBufferIndex);
97
+ this.pcmBufferIndex += pcmData.length;
98
+
99
+ // 6. 发送所有完整的 chunk(关键:复制到新数组再转移)
100
+ while (this.pcmBufferIndex >= this.chunkSize) {
101
+ // 方法1:推荐,使用 slice(隐式复制到新 ArrayBuffer)
102
+ const chunk = this.pcmBuffer.slice(0, this.chunkSize);
103
+
104
+ // 方法2:等价写法
105
+ // const chunk = new Int16Array(this.pcmBuffer.subarray(0, this.chunkSize));
106
+
107
+ this.port.postMessage(chunk, [chunk.buffer]); // 转移新缓冲区,安全!
108
+
109
+ // 移动剩余数据到开头
110
+ this.pcmBuffer.copyWithin(0, this.chunkSize, this.pcmBufferIndex);
111
+ this.pcmBufferIndex -= this.chunkSize;
112
+ }
113
+
114
+ // 7. 清理已消费的源数据
115
+ const consumedSrc = Math.floor(availableOutputSamples * this.downsampleRatio + 0.5); // 四舍五入更准
116
+ if (consumedSrc > 0) {
117
+ this.sourceBuffer.copyWithin(0, consumedSrc, this.sourceBufferLength);
118
+ this.sourceBufferLength -= consumedSrc;
119
+ }
120
+
121
+ return true;
122
+ }
123
+
124
+ floatTo16BitPCM(input) {
125
+ const output = new Int16Array(input.length);
126
+ for (let i = 0; i < input.length; i++) {
127
+ const s = Math.max(-1, Math.min(1, input[i]));
128
+ output[i] = s < 0 ? s * 0x8000 : s * 0x7FFF;
129
+ }
130
+ return output;
131
+ }
132
+ }
133
+
134
+ registerProcessor('audio-stream-resampler-processor', AudioStreamResamplerProcessor);
135
+ `;
136
+
137
+ /**
138
+ * 浏览器端实时音频流重采样器。
139
+ * 基于 AudioWorklet 将麦克风/媒体流转换为 16 kHz、16-bit、单声道 PCM,
140
+ * 并通过回调逐块输出,可选保存完整 PCM 用于后续合并。
141
+ */
142
+ class AudioStreamResampler {
143
+ /**
144
+ * @param {object} config
145
+ * @param {function(Int16Array)} config.onData - 收到一个 chunk PCM 数据的回调
146
+ * @param {function(string, string)} [config.onStateChange] - 状态变化回调
147
+ * @param {object} [config.processorOptions] - 传递给 AudioWorklet 的选项
148
+ * @param {boolean} [config.saveFullPcm=false] - 是否在内部保存所有 PCM 用于 stop 时合并(长时间录音建议关闭)
149
+ */
150
+ constructor(config) {
151
+ this.onData = config.onData || (() => {});
152
+ this.onStateChange = config.onStateChange || (() => {});
153
+ this.processorOptions = config.processorOptions || {};
154
+ this.saveFullPcm = config.saveFullPcm ?? false;
155
+
156
+ this.audioContext = null;
157
+ this.workletNode = null;
158
+ this.source = null;
159
+ this.workletUrl = null;
160
+ this.fullPcmData = this.saveFullPcm ? [] : null;
161
+
162
+ this.isInitialized = false;
163
+ this.isProcessing = false;
164
+ }
165
+
166
+ /**
167
+ * 初始化 AudioContext 并加载 AudioWorklet。
168
+ * 完成后状态变为 `"ready"`。
169
+ */
170
+ async init() {
171
+ this.onStateChange("initializing", "正在初始化音频环境...");
172
+ try {
173
+ this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
174
+
175
+ const blob = new Blob([AudioStreamResamplerProcessorCode], { type: "application/javascript" });
176
+ this.workletUrl = URL.createObjectURL(blob);
177
+ await this.audioContext.audioWorklet.addModule(this.workletUrl);
178
+
179
+ this.workletNode = new AudioWorkletNode(this.audioContext, "audio-stream-resampler-processor", {
180
+ processorOptions: this.processorOptions
181
+ });
182
+
183
+ this.workletNode.port.onmessage = (event) => {
184
+ const chunk = event.data; // Int16Array
185
+ this.onData(chunk);
186
+ if (this.saveFullPcm) {
187
+ this.fullPcmData.push(chunk);
188
+ }
189
+ };
190
+
191
+ this.isInitialized = true;
192
+ this.onStateChange("ready", "音频环境已就绪");
193
+ } catch (err) {
194
+ console.error("AudioStreamResampler init error:", err);
195
+ this.onStateChange("error", `初始化失败: ${err.message}`);
196
+ }
197
+ }
198
+
199
+ /**
200
+ * 绑定媒体流,开始实时处理。
201
+ * @param {MediaStream} stream - 通过 getUserMedia 或其他方式获得的流
202
+ */
203
+ setMediaStream(stream) {
204
+ if (!this.isInitialized) {
205
+ console.error("请先调用 init()");
206
+ return;
207
+ }
208
+
209
+ if (this.source) {
210
+ this.source.disconnect();
211
+ }
212
+
213
+ this.source = this.audioContext.createMediaStreamSource(stream);
214
+ this.source.connect(this.workletNode);
215
+ // workletNode 默认连接到 destination,可选断开以静音
216
+ // this.workletNode.connect(this.audioContext.destination);
217
+
218
+ this.isProcessing = true;
219
+ this.onStateChange("processing", "正在处理音频流...");
220
+ }
221
+
222
+ /**
223
+ * 停止处理并释放资源。
224
+ * @param {(fullPcm: Int16Array) => void} [callback] - 若构造时 `saveFullPcm=true`,会把合并后的完整 PCM 通过此回调传出
225
+ */
226
+ stop(callback) {
227
+ if (!this.isProcessing) return;
228
+
229
+ this.onStateChange("stopping", "正在停止...");
230
+
231
+ if (this.source) {
232
+ this.source.disconnect();
233
+ this.source = null;
234
+ }
235
+
236
+ this._cleanup();
237
+
238
+ this.onStateChange("stopped", "已停止");
239
+
240
+ if (this.saveFullPcm && callback && typeof callback === "function") {
241
+ const totalLength = this.fullPcmData.reduce((sum, chunk) => sum + chunk.length, 0);
242
+ const combined = new Int16Array(totalLength);
243
+ let offset = 0;
244
+ for (const chunk of this.fullPcmData) {
245
+ combined.set(chunk, offset);
246
+ offset += chunk.length;
247
+ }
248
+ callback(combined);
249
+ }
250
+ }
251
+
252
+ _cleanup() {
253
+ if (this.workletNode) {
254
+ this.workletNode.disconnect();
255
+ this.workletNode.port.close();
256
+ this.workletNode = null;
257
+ }
258
+ if (this.audioContext && this.audioContext.state !== "closed") {
259
+ this.audioContext.close();
260
+ this.audioContext = null;
261
+ }
262
+ if (this.workletUrl) {
263
+ URL.revokeObjectURL(this.workletUrl);
264
+ this.workletUrl = null;
265
+ }
266
+
267
+ this.isProcessing = false;
268
+ this.isInitialized = false;
269
+ if (this.fullPcmData) {
270
+ this.fullPcmData.length = 0;
271
+ }
272
+ }
273
+ }
274
+
275
+ /**
276
+ * 将 16-bit 单声道 PCM 数据封装成标准 WAV Blob。
277
+ *
278
+ * @param {Int16Array} pcmData - PCM 采样数据
279
+ * @param {number} [sampleRate=16000] - 采样率,默认 16 kHz
280
+ * @returns {Blob} audio/wav Blob
281
+ */
282
+ function pcmToWavBlob(pcmData, sampleRate = 16000) {
283
+ const length = pcmData.length;
284
+ const buffer = new ArrayBuffer(44 + length * 2);
285
+ const view = new DataView(buffer);
286
+ const writeString = (offset, string) => {
287
+ for (let i = 0; i < string.length; i++) {
288
+ view.setUint8(offset + i, string.charCodeAt(i));
289
+ }
290
+ };
291
+ writeString(0, "RIFF");
292
+ view.setUint32(4, 36 + length * 2, true);
293
+ writeString(8, "WAVE");
294
+ writeString(12, "fmt ");
295
+ view.setUint32(16, 16, true);
296
+ view.setUint16(20, 1, true);
297
+ view.setUint16(22, 1, true);
298
+ view.setUint32(24, sampleRate, true);
299
+ view.setUint32(28, sampleRate * 2, true);
300
+ view.setUint16(32, 2, true);
301
+ view.setUint16(34, 16, true);
302
+ writeString(36, "data");
303
+ view.setUint32(40, length * 2, true);
304
+ let offset = 44;
305
+ for (let i = 0; i < length; i++) {
306
+ view.setInt16(offset, pcmData[i], true);
307
+ offset += 2;
308
+ }
309
+ return new Blob([buffer], { type: "audio/wav" });
34
310
  }
35
311
 
36
- const AudioStreamResamplerProcessorCode = `
37
- class AudioStreamResamplerProcessor extends AudioWorkletProcessor {
38
- constructor(options) {
39
- super();
40
- const config = options.processorOptions || {};
41
- this.targetSampleRate = config.targetSampleRate || 16000;
42
- this.sourceSampleRate = sampleRate;
43
- this.downsampleRatio = this.sourceSampleRate / this.targetSampleRate;
44
-
45
- // 16000Hz 用 60ms chunk (960 samples),其他用 1024
46
- this.chunkSize = this.targetSampleRate === 16000 ? 960 : 1024;
47
-
48
- const sourceBufferSize = config.sourceBufferSize || 16384; // ~340ms @48kHz
49
- const pcmBufferSize = config.pcmBufferSize || (this.chunkSize * 10); // 更大缓冲,减少溢出概率
50
-
51
- this.sourceBuffer = new Float32Array(sourceBufferSize);
52
- this.sourceBufferLength = 0;
53
-
54
- this.pcmBuffer = new Int16Array(pcmBufferSize);
55
- this.pcmBufferIndex = 0;
56
- }
57
-
58
- process(inputs, outputs, parameters) {
59
- const input = inputs[0];
60
- if (!input || input.length === 0 || input[0].length === 0) return true;
61
- const inputChannel = input[0];
62
-
63
- // 1. 写入源缓冲区(溢出时覆盖最旧数据)
64
- const newLength = this.sourceBufferLength + inputChannel.length;
65
- if (newLength > this.sourceBuffer.length) {
66
- const overflow = newLength - this.sourceBuffer.length;
67
- this.sourceBuffer.copyWithin(0, overflow);
68
- this.sourceBufferLength = this.sourceBuffer.length - inputChannel.length;
69
- }
70
- this.sourceBuffer.set(inputChannel, this.sourceBufferLength);
71
- this.sourceBufferLength += inputChannel.length;
72
-
73
- // 2. 计算可降采样样本数
74
- const availableOutputSamples = Math.floor(this.sourceBufferLength / this.downsampleRatio);
75
- if (availableOutputSamples === 0) return true;
76
-
77
- // 3. 线性插值降采样
78
- const downsampled = new Float32Array(availableOutputSamples);
79
- for (let i = 0; i < availableOutputSamples; i++) {
80
- const srcIndex = i * this.downsampleRatio;
81
- const srcIndexInt = Math.floor(srcIndex);
82
- const fraction = srcIndex - srcIndexInt;
83
- const val0 = this.sourceBuffer[srcIndexInt];
84
- const val1 = srcIndexInt + 1 < this.sourceBufferLength ? this.sourceBuffer[srcIndexInt + 1] : val0;
85
- downsampled[i] = val0 + (val1 - val0) * fraction;
86
- }
87
-
88
- // 4. Float32 → Int16 PCM
89
- const pcmData = this.floatTo16BitPCM(downsampled);
90
-
91
- // 5. 写入 PCM 缓冲区(空间不足时直接覆盖最旧,缓冲区足够大基本不会触发)
92
- if (this.pcmBufferIndex + pcmData.length > this.pcmBuffer.length) {
93
- // 简单策略:从头覆盖(丢弃最旧数据)
94
- this.pcmBufferIndex = 0;
95
- }
96
- this.pcmBuffer.set(pcmData, this.pcmBufferIndex);
97
- this.pcmBufferIndex += pcmData.length;
98
-
99
- // 6. 发送所有完整的 chunk(关键:复制到新数组再转移)
100
- while (this.pcmBufferIndex >= this.chunkSize) {
101
- // 方法1:推荐,使用 slice(隐式复制到新 ArrayBuffer)
102
- const chunk = this.pcmBuffer.slice(0, this.chunkSize);
103
-
104
- // 方法2:等价写法
105
- // const chunk = new Int16Array(this.pcmBuffer.subarray(0, this.chunkSize));
106
-
107
- this.port.postMessage(chunk, [chunk.buffer]); // 转移新缓冲区,安全!
108
-
109
- // 移动剩余数据到开头
110
- this.pcmBuffer.copyWithin(0, this.chunkSize, this.pcmBufferIndex);
111
- this.pcmBufferIndex -= this.chunkSize;
112
- }
113
-
114
- // 7. 清理已消费的源数据
115
- const consumedSrc = Math.floor(availableOutputSamples * this.downsampleRatio + 0.5); // 四舍五入更准
116
- if (consumedSrc > 0) {
117
- this.sourceBuffer.copyWithin(0, consumedSrc, this.sourceBufferLength);
118
- this.sourceBufferLength -= consumedSrc;
119
- }
120
-
121
- return true;
122
- }
123
-
124
- floatTo16BitPCM(input) {
125
- const output = new Int16Array(input.length);
126
- for (let i = 0; i < input.length; i++) {
127
- const s = Math.max(-1, Math.min(1, input[i]));
128
- output[i] = s < 0 ? s * 0x8000 : s * 0x7FFF;
129
- }
130
- return output;
131
- }
132
- }
133
-
134
- registerProcessor('audio-stream-resampler-processor', AudioStreamResamplerProcessor);
135
- `;
136
-
137
- /**
138
- * 浏览器端实时音频流重采样器。
139
- * 基于 AudioWorklet 将麦克风/媒体流转换为 16 kHz、16-bit、单声道 PCM,
140
- * 并通过回调逐块输出,可选保存完整 PCM 用于后续合并。
141
- */
142
- class AudioStreamResampler {
143
- /**
144
- * @param {object} config
145
- * @param {function(Int16Array)} config.onData - 收到一个 chunk PCM 数据的回调
146
- * @param {function(string, string)} [config.onStateChange] - 状态变化回调
147
- * @param {object} [config.processorOptions] - 传递给 AudioWorklet 的选项
148
- * @param {boolean} [config.saveFullPcm=false] - 是否在内部保存所有 PCM 用于 stop 时合并(长时间录音建议关闭)
149
- */
150
- constructor(config) {
151
- this.onData = config.onData || (() => {});
152
- this.onStateChange = config.onStateChange || (() => {});
153
- this.processorOptions = config.processorOptions || {};
154
- this.saveFullPcm = config.saveFullPcm ?? false;
155
-
156
- this.audioContext = null;
157
- this.workletNode = null;
158
- this.source = null;
159
- this.workletUrl = null;
160
- this.fullPcmData = this.saveFullPcm ? [] : null;
161
-
162
- this.isInitialized = false;
163
- this.isProcessing = false;
164
- }
165
-
166
- /**
167
- * 初始化 AudioContext 并加载 AudioWorklet。
168
- * 完成后状态变为 `"ready"`。
169
- */
170
- async init() {
171
- this.onStateChange("initializing", "正在初始化音频环境...");
172
- try {
173
- this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
174
-
175
- const blob = new Blob([AudioStreamResamplerProcessorCode], { type: "application/javascript" });
176
- this.workletUrl = URL.createObjectURL(blob);
177
- await this.audioContext.audioWorklet.addModule(this.workletUrl);
178
-
179
- this.workletNode = new AudioWorkletNode(this.audioContext, "audio-stream-resampler-processor", {
180
- processorOptions: this.processorOptions
181
- });
182
-
183
- this.workletNode.port.onmessage = (event) => {
184
- const chunk = event.data; // Int16Array
185
- this.onData(chunk);
186
- if (this.saveFullPcm) {
187
- this.fullPcmData.push(chunk);
188
- }
189
- };
190
-
191
- this.isInitialized = true;
192
- this.onStateChange("ready", "音频环境已就绪");
193
- } catch (err) {
194
- console.error("AudioStreamResampler init error:", err);
195
- this.onStateChange("error", `初始化失败: ${err.message}`);
196
- }
197
- }
198
-
199
- /**
200
- * 绑定媒体流,开始实时处理。
201
- * @param {MediaStream} stream - 通过 getUserMedia 或其他方式获得的流
202
- */
203
- setMediaStream(stream) {
204
- if (!this.isInitialized) {
205
- console.error("请先调用 init()");
206
- return;
207
- }
208
-
209
- if (this.source) {
210
- this.source.disconnect();
211
- }
212
-
213
- this.source = this.audioContext.createMediaStreamSource(stream);
214
- this.source.connect(this.workletNode);
215
- // workletNode 默认连接到 destination,可选断开以静音
216
- // this.workletNode.connect(this.audioContext.destination);
217
-
218
- this.isProcessing = true;
219
- this.onStateChange("processing", "正在处理音频流...");
220
- }
221
-
222
- /**
223
- * 停止处理并释放资源。
224
- * @param {(fullPcm: Int16Array) => void} [callback] - 若构造时 `saveFullPcm=true`,会把合并后的完整 PCM 通过此回调传出
225
- */
226
- stop(callback) {
227
- if (!this.isProcessing) return;
228
-
229
- this.onStateChange("stopping", "正在停止...");
230
-
231
- if (this.source) {
232
- this.source.disconnect();
233
- this.source = null;
234
- }
235
-
236
- this._cleanup();
237
-
238
- this.onStateChange("stopped", "已停止");
239
-
240
- if (this.saveFullPcm && callback && typeof callback === "function") {
241
- const totalLength = this.fullPcmData.reduce((sum, chunk) => sum + chunk.length, 0);
242
- const combined = new Int16Array(totalLength);
243
- let offset = 0;
244
- for (const chunk of this.fullPcmData) {
245
- combined.set(chunk, offset);
246
- offset += chunk.length;
247
- }
248
- callback(combined);
249
- }
250
- }
251
-
252
- _cleanup() {
253
- if (this.workletNode) {
254
- this.workletNode.disconnect();
255
- this.workletNode.port.close();
256
- this.workletNode = null;
257
- }
258
- if (this.audioContext && this.audioContext.state !== "closed") {
259
- this.audioContext.close();
260
- this.audioContext = null;
261
- }
262
- if (this.workletUrl) {
263
- URL.revokeObjectURL(this.workletUrl);
264
- this.workletUrl = null;
265
- }
266
-
267
- this.isProcessing = false;
268
- this.isInitialized = false;
269
- if (this.fullPcmData) {
270
- this.fullPcmData.length = 0;
271
- }
272
- }
273
- }
274
-
275
- /**
276
- * 将 16-bit 单声道 PCM 数据封装成标准 WAV Blob。
277
- *
278
- * @param {Int16Array} pcmData - PCM 采样数据
279
- * @param {number} [sampleRate=16000] - 采样率,默认 16 kHz
280
- * @returns {Blob} audio/wav Blob
281
- */
282
- function pcmToWavBlob(pcmData, sampleRate = 16000) {
283
- const length = pcmData.length;
284
- const buffer = new ArrayBuffer(44 + length * 2);
285
- const view = new DataView(buffer);
286
- const writeString = (offset, string) => {
287
- for (let i = 0; i < string.length; i++) {
288
- view.setUint8(offset + i, string.charCodeAt(i));
289
- }
290
- };
291
- writeString(0, "RIFF");
292
- view.setUint32(4, 36 + length * 2, true);
293
- writeString(8, "WAVE");
294
- writeString(12, "fmt ");
295
- view.setUint32(16, 16, true);
296
- view.setUint16(20, 1, true);
297
- view.setUint16(22, 1, true);
298
- view.setUint32(24, sampleRate, true);
299
- view.setUint32(28, sampleRate * 2, true);
300
- view.setUint16(32, 2, true);
301
- view.setUint16(34, 16, true);
302
- writeString(36, "data");
303
- view.setUint32(40, length * 2, true);
304
- let offset = 44;
305
- for (let i = 0; i < length; i++) {
306
- view.setInt16(offset, pcmData[i], true);
307
- offset += 2;
308
- }
309
- return new Blob([buffer], { type: "audio/wav" });
310
- }
311
-
312
- /**
313
- * 视口尺寸对象。
314
- * @typedef {Object} ViewportDimensions
315
- * @property {number} w 视口宽度,单位像素。
316
- * @property {number} h 视口高度,单位像素。
317
- */
318
-
319
- /**
320
- * 获取当前视口(viewport)的宽高。
321
- *
322
- * 兼容策略:
323
- * 1. 优先使用 `window.innerWidth/innerHeight`(现代浏览器)。
324
- * 2. 降级到 `document.documentElement.clientWidth/clientHeight`(IE9+ 及怪异模式)。
325
- * 3. 最后降级到 `document.body.clientWidth/clientHeight`(IE6-8 怪异模式)。
326
- *
327
- * @returns {ViewportDimensions} 包含 `w`(宽度)和 `h`(高度)的对象,单位为像素。
328
- *
329
- * @example
330
- * const { w, h } = getViewportSize();
331
- * console.log(`视口尺寸:${w} × ${h}`);
332
- */
333
- function getViewportSize() {
334
- const d = document,
335
- root = d.documentElement,
336
- body = d.body;
337
-
338
- return {
339
- w: window.innerWidth || root.clientWidth || body.clientWidth,
340
- h: window.innerHeight || root.clientHeight || body.clientHeight
341
- };
342
- }
343
-
344
- /**
345
- * 将当前页面 URL 的 query 部分解析成键值对对象。
346
- *
347
- * @returns {Record<string, string>} 所有查询参数组成的平凡对象
348
- * (同名 key 仅保留最后一项)
349
- */
350
- function getAllSearchParams() {
351
- const urlSearchParams = new URLSearchParams(location.search);
352
- return Object.fromEntries(urlSearchParams.entries());
353
- }
354
-
355
- /**
356
- * 根据 key 获取当前页面 URL 中的单个查询参数。
357
- *
358
- * @param {string} key - 要提取的参数名
359
- * @returns {string | undefined} 对应参数值;不存在时返回 `undefined`
360
- */
361
- function getSearchParam(key) {
362
- const params = getAllSearchParams();
363
- return params[key];
364
- }
365
-
366
- //#region 数据类型判断
367
-
368
- /**
369
- * 返回任意值的运行时类型字符串(小写形式)。
370
- *
371
- * @param {*} obj 待检测的值
372
- * @returns {keyof globalThis|"blob"|"file"|"formdata"|string} 小写类型名
373
- */
374
- function getDataType(obj) {
375
- return Object.prototype.toString
376
- .call(obj)
377
- .replace(/^\[object\s(\w+)\]$/, "$1")
378
- .toLowerCase();
379
- }
380
-
381
- /**
382
- * 判断值是否为原生 Blob(含 File)。
383
- *
384
- * @param {*} obj - 待检测的值
385
- * @returns {obj is Blob}
386
- */
387
- function isBlob(obj) {
388
- return getDataType(obj) === "blob";
389
- }
390
-
391
- /**
392
- * 判断值是否为**纯粹**的 Object(即 `{}` 或 `new Object()`,不含数组、null、自定义类等)。
393
- *
394
- * @param {*} obj - 待检测的值
395
- * @returns {obj is Record<PropertyKey, any>}
396
- */
397
- function isPlainObject(obj) {
398
- return getDataType(obj) === "object";
399
- }
400
-
401
- /**
402
- * 判断值是否为 Promise(含 Promise 子类)。
403
- *
404
- * @param {*} obj - 待检测的值
405
- * @returns {obj is Promise<any>}
406
- */
407
- function isPromise(obj) {
408
- return getDataType(obj) === "promise";
409
- }
410
-
411
- /**
412
- * 判断值是否为合法 Date 对象(含 Invalid Date 返回 false)。
413
- *
414
- * @param {*} t - 待检测值
415
- * @returns {t is Date}
416
- */
417
- function isDate(t) {
418
- return getDataType(t) === "date";
419
- }
420
-
421
- /**
422
- * 判断值是否为函数(含异步函数、生成器函数、类)。
423
- *
424
- * @param {*} obj - 待检测的值
425
- * @returns {obj is Function}
426
- */
427
- function isFunction(obj) {
428
- return typeof obj === "function";
429
- }
430
-
431
- /**
432
- * 判断值是否为**非空**字符串。
433
- *
434
- * @param {*} obj - 待检测的值
435
- * @returns {obj is string}
436
- */
437
- function isNonEmptyString(obj) {
438
- return getDataType(obj) === "string" && obj.length > 0;
439
- }
440
-
441
- //#endregion
442
-
443
- //#region 随机数据
444
-
445
- /**
446
- * 在闭区间 [min, max] 内生成一个均匀分布的随机整数。
447
- * 若 min > max 则自动交换。
448
- *
449
- * @param {number} min - 整数下界(包含)
450
- * @param {number} max - 整数上界(包含)
451
- * @returns {number}
452
- * @throws {TypeError} 当 min 或 max 不是整数时抛出
453
- */
454
- function randomIntInRange(min, max) {
455
- if (!Number.isInteger(min) || !Number.isInteger(max)) {
456
- throw new TypeError("Arguments must be integers");
457
- }
458
- if (min > max) [min, max] = [max, min];
459
- // 注意加 1,否则 max 永远取不到;Math.floor 保证均匀
460
- return Math.floor(Math.random() * (max - min + 1)) + min;
461
- }
462
-
463
- /**
464
- * 随机生成一个汉字(可控制范围)。
465
- *
466
- * @param {boolean} [base=true] - 是否启用基本区(0x4E00-0x9FA5)
467
- * @param {boolean} [extA=false] - 是否启用扩展 A 区(0x3400-0x4DBF)
468
- * @param {boolean} [extBH=false] - 是否启用扩展 B~H 区(0x20000-0x2EBEF,代理对)
469
- * @returns {string} 单个汉字字符
470
- * @throws {RangeError} 未启用任何区段时抛出
471
- */
472
- function randomHan(base = true, extA = false, extBH = false) {
473
- // 1. 收集已启用的“区段”
474
- const ranges = [];
475
- if (base) ranges.push({ min: 0x4e00, max: 0x9fa5, surrogate: false });
476
- if (extA) ranges.push({ min: 0x3400, max: 0x4dbf, surrogate: false });
477
- if (extBH) ranges.push({ min: 0x20000, max: 0x2ebef, surrogate: true });
478
-
479
- if (ranges.length === 0) {
480
- throw new RangeError("At least one range must be enabled");
481
- }
482
-
483
- // 2. 按总码位数抽号
484
- const total = ranges.reduce((sum, r) => sum + (r.max - r.min + 1), 0);
485
- let n = randomIntInRange(0, total - 1);
486
-
487
- // 3. 定位落在哪个区段
488
- for (const { min, max, surrogate } of ranges) {
489
- const size = max - min + 1;
490
- if (n < size) {
491
- const code = min + n;
492
- if (!surrogate) return String.fromCharCode(code);
493
- // 代理对
494
- const offset = code - 0x10000;
495
- const hi = (offset >> 10) + 0xd800;
496
- const lo = (offset & 0x3ff) + 0xdc00;
497
- return String.fromCharCode(hi, lo);
498
- }
499
- n -= size;
500
- }
501
- }
502
-
503
- /**
504
- * 随机生成一个英文字母。
505
- *
506
- * @param {'lower'|'upper'} [type] - 指定大小写;留空则随机
507
- * @returns {string} 单个字母
508
- */
509
- function randomEnLetter(type) {
510
- const lower = "abcdefghijklmnopqrstuvwxyz";
511
- const upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
512
- const randomNum = randomIntInRange(0, 25);
513
-
514
- switch (type) {
515
- case "lower":
516
- return lower[randomNum];
517
- case "upper":
518
- return upper[randomNum];
519
- default:
520
- return (Math.random() < 0.5 ? lower : upper)[randomNum];
521
- }
522
- }
523
-
524
- /**
525
- * 生成指定长度的随机“中英混合”字符串。
526
- *
527
- * @param {number} [len=1] - 目标长度(≥1,自动取整)
528
- * @param {number} [zhProb=0.5] - 每个位置选择汉字的概率,默认 0.5
529
- * @returns {string}
530
- */
531
- function randomHanOrEn(len, zhProb = 0.5) {
532
- len = Math.max(1, Math.floor(len));
533
- const buf = [];
534
- for (let i = 0; i < len; i++) {
535
- buf.push(Math.random() < zhProb ? randomHan() : randomEnLetter());
536
- }
537
- return buf.join("");
538
- }
539
-
540
- //#endregion
541
-
542
- //#region 防抖节流
543
-
544
- /**
545
- * 创建 debounced(防抖)函数。
546
- * - 默认 trailing 触发;当 `leading=true` 时,首次调用或超过等待间隔会立即执行。
547
- * - 支持手动取消。
548
- *
549
- * @template {(...args: any[]) => any} T
550
- * @param {T} fn - 要防抖的原始函数
551
- * @param {number} wait - 防抖等待时间(毫秒)
552
- * @param {boolean} [leading=false] - 是否启用立即执行(leading edge)
553
- * @returns {T & { cancel(): void }} 返回经过防抖包装的函数,并附带 `cancel` 方法
554
- * @throws {TypeError} 当 `fn` 不是函数时抛出
555
- */
556
- function debounce(fn, wait, leading = false) {
557
- if (typeof fn !== "function") throw new TypeError("fn must be function");
558
- wait = Math.max(0, Number(wait) || 0);
559
- let timeoutId;
560
- let lastCall = 0; // 0 表示从未调用过
561
-
562
- function debounced(...args) {
563
- const isFirst = lastCall === 0;
564
- const isOverWait = Date.now() - lastCall >= wait;
565
-
566
- clearTimeout(timeoutId);
567
-
568
- // 首次调用 || 已达到等待间隔
569
- if (leading && (isFirst || isOverWait)) {
570
- lastCall = Date.now();
571
- return fn.apply(this, args);
572
- }
573
-
574
- timeoutId = setTimeout(() => {
575
- lastCall = Date.now();
576
- fn.apply(this, args);
577
- }, wait);
578
- }
579
-
580
- debounced.cancel = () => {
581
- clearTimeout(timeoutId);
582
- lastCall = 0; // 恢复初始状态
583
- };
584
-
585
- return debounced;
586
- }
587
-
588
- /**
589
- * 创建 throttled(节流)函数。
590
- * 支持 leading/trailing 边缘触发,可手动取消。
591
- *
592
- * @template {(...args: any[]) => any} T
593
- * @param {T} fn - 要节流的原始函数
594
- * @param {number} wait - 节流间隔(毫秒)
595
- * @param {object} [options] - 配置项
596
- * @param {boolean} [options.leading=true] - 是否在 leading 边缘执行
597
- * @param {boolean} [options.trailing=true] - 是否在 trailing 边缘执行
598
- * @returns {T & { cancel(): void }} 返回经过节流包装的函数,并附带 `cancel` 方法
599
- * @throws {TypeError} 当 `fn` 不是函数时抛出
600
- */
601
- function throttle(fn, wait, { leading = true, trailing = true } = {}) {
602
- if (typeof fn !== "function") throw new TypeError("fn must be function");
603
- wait = Math.max(0, Number(wait) || 0);
604
-
605
- let timeoutId = null,
606
- lastCall = 0;
607
-
608
- function throttled(...args) {
609
- const remaining = wait - (Date.now() - lastCall);
610
-
611
- if (leading && (lastCall === 0 || remaining <= 0)) {
612
- lastCall = Date.now();
613
- fn.apply(this, args);
614
- } else if (trailing && !timeoutId) {
615
- timeoutId = setTimeout(
616
- () => {
617
- timeoutId = null;
618
- lastCall = Date.now();
619
- fn.apply(this, args);
620
- },
621
- remaining > 0 ? remaining : wait
622
- );
623
- }
624
- }
625
-
626
- throttled.cancel = () => {
627
- clearTimeout(timeoutId);
628
- timeoutId = null;
629
- lastCall = 0;
630
- };
631
-
632
- return throttled;
633
- }
634
-
635
- //#endregion
636
-
637
- /**
638
- * 利用 JSON 序列化/反序列化实现**深拷贝**。
639
- * 注意:会丢失 `undefined`、函数、循环引用、特殊包装对象等。
640
- *
641
- * @template T
642
- * @param {T} obj - 待拷贝的 JSON 兼容值
643
- * @returns {T} 深拷贝后的值
644
- */
645
- function deepCloneByJSON(obj) {
646
- return JSON.parse(JSON.stringify(obj));
647
- }
648
-
649
- /**
650
- * **安全**地将源对象中**已存在**的属性赋值到目标对象。
651
- * 不会新增键,也不会复制原型链上的属性。
652
- *
653
- * @template {Record<PropertyKey, any>} T
654
- * @param {T} target - 目标对象(将被就地修改)
655
- * @param {...Partial<T>} sources - 一个或多个源对象
656
- * @returns {T} 修改后的目标对象(即第一个参数本身)
657
- *
658
- * @example
659
- * const defaults = { a: 1, b: 2 };
660
- * assignExisting(defaults, { a: 9, c: 99 }); // defaults 变为 { a: 9, b: 2 }
661
- */
662
- function assignExisting(target, ...sources) {
663
- sources.forEach((source) => {
664
- Object.keys(source).forEach((key) => {
665
- if (target.hasOwnProperty(key)) {
666
- target[key] = source[key];
667
- }
668
- });
669
- });
670
- return target;
671
- }
672
-
673
- /**
674
- * 提取任意函数(含箭头函数、普通函数、async、class 构造器)的形参名称列表。
675
- * 通过源码正则解析,不支持解构参数、默认参数、剩余参数等复杂语法;
676
- * 若出现上述场景将返回空数组或部分名称。
677
- *
678
- * @param {Function} fn - 目标函数
679
- * @returns {string[]} 按声明顺序排列的参数名数组;解析失败时返回空数组
680
- *
681
- * @example
682
- * getFunctionArgNames(function (a, b) {}) // ["a", "b"]
683
- * getFunctionArgNames((foo, bar) => {}) // ["foo", "bar"]
684
- * getFunctionArgNames(async function x({a} = {}) {}) // [] (解构无法识别)
685
- */
686
- function getFunctionArgNames(fn) {
687
- const FN_ARG_SPLIT = /,/,
688
- FN_ARG = /^\s*(_?)(\S+?)\1\s*$/,
689
- FN_ARGS = /^[^(]*\(\s*([^)]*)\)/m,
690
- ARROW_ARG = /^([^(]+?)=>/,
691
- STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/gm;
692
-
693
- const fnText = Function.prototype.toString.call(fn).replace(STRIP_COMMENTS, "");
694
- const argDecl = fnText.match(ARROW_ARG) || fnText.match(FN_ARGS);
695
- const retArgNames = [];
696
- [].forEach.call(argDecl[1].split(FN_ARG_SPLIT), function (arg) {
697
- arg.replace(FN_ARG, function (all, underscore, name) {
698
- retArgNames.push(name);
699
- });
700
- });
701
- return retArgNames;
702
- }
703
-
704
- /**
705
- * 将 Blob(或 File)读取为文本,并可选择自动执行 `JSON.parse`。
706
- * 当 `isParse=true` 且内容非法 JSON 时,会回退为返回原始文本。
707
- *
708
- * @param {Blob} blob - 待读取的 Blob/File 对象
709
- * @param {boolean} [isParse=true] - 是否尝试将结果按 JSON 解析
710
- * @returns {Promise<string | any>} 解析后的 JSON 对象或原始文本
711
- *
712
- * @example
713
- * const json = await readBlobAsText(blob); // 自动 JSON.parse
714
- * const text = await readBlobAsText(blob, false); // 仅返回文本
715
- */
716
- function readBlobAsText(blob, isParse = true) {
717
- return new Promise((resolve, reject) => {
718
- const reader = new FileReader();
719
- reader.onload = (evt) => {
720
- const result = evt.target.result;
721
- if (isParse) {
722
- try {
723
- resolve(JSON.parse(result));
724
- } catch (error) {
725
- console.error(error);
726
- resolve(result);
727
- }
728
- } else {
729
- resolve(result);
730
- }
731
- };
732
- reader.onerror = reject;
733
- reader.readAsText(blob);
734
- });
735
- }
736
-
737
- /**
738
- * 将任意值安全转换为 Date 对象。
739
- * - 数字/数字字符串:视为时间戳
740
- * - 字符串:尝试按 ISO/RFC 格式解析
741
- * - 对象:优先 valueOf(),再 toString()
742
- * - null / undefined / 无效值:返回 null
743
- *
744
- * @param {*} val - 待转换值
745
- * @returns {Date | null} 有效 Date 或 null
746
- */
747
- function toDate(val) {
748
- if (val == null) return null; // null / undefined
749
- if (val instanceof Date) return isNaN(val) ? null : val; // 已是 Date,但需排除 Invalid Date
750
-
751
- // 1. 数字或数字字符串 → 时间戳
752
- if (typeof val === "number" || (typeof val === "string" && /^-?\d+(\.\d+)?$/.test(val.trim()))) {
753
- const d = new Date(+val);
754
- return isNaN(d) ? null : d;
755
- }
756
-
757
- // 2. 标准 ISO 8601 / RFC 2825 等合法字符串
758
- if (typeof val === "string") {
759
- const d = new Date(val);
760
- return isNaN(d) ? null : d; // 非法格式返回 null
761
- }
762
-
763
- // 3. 对象带 valueOf / toString
764
- if (typeof val === "object") {
765
- // 优先调用 valueOf(期望返回数字时间戳)
766
- const prim = val.valueOf ? val.valueOf() : Object.prototype.valueOf.call(val);
767
- if (typeof prim === "number" && !isNaN(prim)) {
768
- const d = new Date(prim);
769
- return isNaN(d) ? null : d;
770
- }
771
- // 兜底用字符串
772
- const str = val.toString ? val.toString() : String(val);
773
- const d = new Date(str);
774
- return isNaN(d) ? null : d;
775
- }
776
-
777
- // 4. 其余情况
778
- return null;
779
- }
780
-
781
- /**
782
- * 在闭区间 [date1, date2] 内随机生成一个日期(含首尾)。
783
- * 若顺序相反则自动交换。
784
- *
785
- * @param {Date} date1 - 起始日期
786
- * @param {Date} date2 - 结束日期
787
- * @returns {Date} 随机日期
788
- */
789
- function randomDateInRange(date1, date2) {
790
- let v1 = date1.getTime(),
791
- v2 = date2.getTime();
792
- if (v1 > v2) [v1, v2] = [v2, v1];
793
- return new Date(v1 + Math.floor(Math.random() * (v2 - v1 + 1)));
794
- }
795
-
796
- /**
797
- * 计算两个时间之间的剩余/已过时长(天-时-分-秒),返回带补零的展示对象。
798
- *
799
- * @param {string|number|Date} originalTime - 原始时间(可转 Date 的任意值)
800
- * @param {Date} [currentTime=new Date()] - 基准时间,默认当前
801
- * @returns {{days:number,hours:string,minutes:string,seconds:string}}
802
- */
803
- function calcTimeDifference(originalTime, currentTime = new Date()) {
804
- // 计算时间差(毫秒)
805
- const diffMs = currentTime - new Date(originalTime);
806
-
807
- // 转换为天、小时、分钟、秒
808
- const diffSeconds = Math.floor(diffMs / 1000);
809
- const days = Math.floor(diffSeconds / (3600 * 24));
810
- const hours = Math.floor((diffSeconds % (3600 * 24)) / 3600);
811
- const minutes = Math.floor((diffSeconds % 3600) / 60);
812
- const seconds = diffSeconds % 60;
813
-
814
- const padZero = (num) => String(num).padStart(2, "0");
815
-
816
- return {
817
- days,
818
- hours: padZero(hours),
819
- minutes: padZero(minutes),
820
- seconds: padZero(seconds)
821
- };
312
+ /**
313
+ * 视口尺寸对象。
314
+ * @typedef {Object} ViewportDimensions
315
+ * @property {number} w 视口宽度,单位像素。
316
+ * @property {number} h 视口高度,单位像素。
317
+ */
318
+
319
+ /**
320
+ * 获取当前视口(viewport)的宽高。
321
+ *
322
+ * 兼容策略:
323
+ * 1. 优先使用 `window.innerWidth/innerHeight`(现代浏览器)。
324
+ * 2. 降级到 `document.documentElement.clientWidth/clientHeight`(IE9+ 及怪异模式)。
325
+ * 3. 最后降级到 `document.body.clientWidth/clientHeight`(IE6-8 怪异模式)。
326
+ *
327
+ * @returns {ViewportDimensions} 包含 `w`(宽度)和 `h`(高度)的对象,单位为像素。
328
+ *
329
+ * @example
330
+ * const { w, h } = getViewportSize();
331
+ * console.log(`视口尺寸:${w} × ${h}`);
332
+ */
333
+ function getViewportSize() {
334
+ const d = document,
335
+ root = d.documentElement,
336
+ body = d.body;
337
+
338
+ return {
339
+ w: window.innerWidth || root.clientWidth || body.clientWidth,
340
+ h: window.innerHeight || root.clientHeight || body.clientHeight
341
+ };
342
+ }
343
+
344
+ /**
345
+ * 将当前页面 URL 的 query 部分解析成键值对对象。
346
+ *
347
+ * @returns {Record<string, string>} 所有查询参数组成的平凡对象
348
+ * (同名 key 仅保留最后一项)
349
+ */
350
+ function getAllSearchParams() {
351
+ const urlSearchParams = new URLSearchParams(location.search);
352
+ return Object.fromEntries(urlSearchParams.entries());
353
+ }
354
+
355
+ /**
356
+ * 根据 key 获取当前页面 URL 中的单个查询参数。
357
+ *
358
+ * @param {string} key - 要提取的参数名
359
+ * @returns {string | undefined} 对应参数值;不存在时返回 `undefined`
360
+ */
361
+ function getSearchParam(key) {
362
+ const params = getAllSearchParams();
363
+ return params[key];
822
364
  }
823
365
 
824
- /**
825
- * 将总秒数格式化成人类可读的时间段文本。
826
- * 固定进制:1 年=365 天,1 月=30 天。
827
- *
828
- * @param {number} totalSeconds - 非负总秒数
829
- * @param {object} [options] - 格式化选项
830
- * @param {Partial<{year:string,month:string,day:string,hour:string,minute:string,second:string}>} [options.labels] - 各单位的自定义文本
831
- * @param {('year'|'month'|'day'|'hour'|'minute'|'second')} [options.maxUnit] - 最大输出单位
832
- * @param {('year'|'month'|'day'|'hour'|'minute'|'second')} [options.minUnit] - 最小输出单位
833
- * @param {boolean} [options.showZero] - 是否强制显示 0 秒
834
- * @returns {string} 拼接后的时长文本,如“1天 02小时 30分钟”
835
- * @throws {TypeError} 当 totalSeconds 为非数字或负数时抛出
836
- */
837
- function formatDuration(totalSeconds, options = {}) {
838
- if (typeof totalSeconds !== "number" || totalSeconds < 0 || !isFinite(totalSeconds)) {
839
- throw new TypeError("totalSeconds 必须是非负数字");
840
- }
841
-
842
- // 1. 默认中文单位
843
- const DEFAULT_LABELS = {
844
- year: "年",
845
- month: "月",
846
- day: "",
847
- hour: "小时",
848
- minute: "分钟",
849
- second: "秒"
850
- };
851
-
852
- // 2. 固定进制表(秒)
853
- const UNIT_TABLE = [
854
- { key: "year", seconds: 365 * 24 * 3600 },
855
- { key: "month", seconds: 30 * 24 * 3600 },
856
- { key: "day", seconds: 24 * 3600 },
857
- { key: "hour", seconds: 3600 },
858
- { key: "minute", seconds: 60 },
859
- { key: "second", seconds: 1 }
860
- ];
861
-
862
- // 3. 合并用户自定义文本
863
- const labels = Object.assign({}, DEFAULT_LABELS, options.labels);
864
-
865
- // 4. 根据 maxUnit / minUnit 截取
866
- let start = 0,
867
- end = UNIT_TABLE.length;
868
- if (options.maxUnit) {
869
- const idx = UNIT_TABLE.findIndex((u) => u.key === options.maxUnit);
870
- if (idx !== -1) start = idx;
871
- }
872
- if (options.minUnit) {
873
- const idx = UNIT_TABLE.findIndex((u) => u.key === options.minUnit);
874
- if (idx !== -1) end = idx + 1;
875
- }
876
- const units = UNIT_TABLE.slice(start, end);
877
- if (!units.length) units.push(UNIT_TABLE[UNIT_TABLE.length - 1]); // 保底秒
878
-
879
- // 5. 逐级计算
880
- let rest = Math.floor(totalSeconds);
881
- const parts = [];
882
-
883
- for (const { key, seconds } of units) {
884
- const val = Math.floor(rest / seconds);
885
- rest %= seconds;
886
-
887
- const shouldShow = val > 0 || (options.showZero && key === "second");
888
- if (shouldShow || (parts.length === 0 && rest === 0)) {
889
- parts.push(`${val}${labels[key]}`);
890
- }
891
- }
892
-
893
- // 6. 兜底
894
- if (parts.length === 0) {
895
- parts.push(`0${labels[units[units.length - 1].key]}`);
896
- }
897
-
898
- return parts.join("");
366
+ //#region 数据类型判断
367
+
368
+ /**
369
+ * 返回任意值的运行时类型字符串(小写形式)。
370
+ *
371
+ * @param {*} obj 待检测的值
372
+ * @returns {keyof globalThis|"blob"|"file"|"formdata"|string} 小写类型名
373
+ */
374
+ function getDataType(obj) {
375
+ return Object.prototype.toString
376
+ .call(obj)
377
+ .replace(/^\[object\s(\w+)\]$/, "$1")
378
+ .toLowerCase();
379
+ }
380
+
381
+ /**
382
+ * 判断值是否为原生 Blob(含 File)。
383
+ *
384
+ * @param {*} obj - 待检测的值
385
+ * @returns {obj is Blob}
386
+ */
387
+ function isBlob(obj) {
388
+ return getDataType(obj) === "blob";
389
+ }
390
+
391
+ /**
392
+ * 判断值是否为**纯粹**的 Object(即 `{}` 或 `new Object()`,不含数组、null、自定义类等)。
393
+ *
394
+ * @param {*} obj - 待检测的值
395
+ * @returns {obj is Record<PropertyKey, any>}
396
+ */
397
+ function isPlainObject(obj) {
398
+ return getDataType(obj) === "object";
399
+ }
400
+
401
+ /**
402
+ * 判断值是否为 Promise(含 Promise 子类)。
403
+ *
404
+ * @param {*} obj - 待检测的值
405
+ * @returns {obj is Promise<any>}
406
+ */
407
+ function isPromise(obj) {
408
+ return getDataType(obj) === "promise";
409
+ }
410
+
411
+ /**
412
+ * 判断值是否为合法 Date 对象(含 Invalid Date 返回 false)。
413
+ *
414
+ * @param {*} t - 待检测值
415
+ * @returns {t is Date}
416
+ */
417
+ function isDate(t) {
418
+ return getDataType(t) === "date";
419
+ }
420
+
421
+ /**
422
+ * 判断值是否为函数(含异步函数、生成器函数、类)。
423
+ *
424
+ * @param {*} obj - 待检测的值
425
+ * @returns {obj is Function}
426
+ */
427
+ function isFunction(obj) {
428
+ return typeof obj === "function";
429
+ }
430
+
431
+ /**
432
+ * 判断值是否为**非空**字符串。
433
+ *
434
+ * @param {*} obj - 待检测的值
435
+ * @returns {obj is string}
436
+ */
437
+ function isNonEmptyString(obj) {
438
+ return getDataType(obj) === "string" && obj.length > 0;
439
+ }
440
+
441
+ //#endregion
442
+
443
+ //#region 随机数据
444
+
445
+ /**
446
+ * 在闭区间 [min, max] 内生成一个均匀分布的随机整数。
447
+ * 若 min > max 则自动交换。
448
+ *
449
+ * @param {number} min - 整数下界(包含)
450
+ * @param {number} max - 整数上界(包含)
451
+ * @returns {number}
452
+ * @throws {TypeError} 当 min 或 max 不是整数时抛出
453
+ */
454
+ function randomIntInRange(min, max) {
455
+ if (!Number.isInteger(min) || !Number.isInteger(max)) {
456
+ throw new TypeError("Arguments must be integers");
457
+ }
458
+ if (min > max) [min, max] = [max, min];
459
+ // 注意加 1,否则 max 永远取不到;Math.floor 保证均匀
460
+ return Math.floor(Math.random() * (max - min + 1)) + min;
461
+ }
462
+
463
+ /**
464
+ * 随机生成一个汉字(可控制范围)。
465
+ *
466
+ * @param {boolean} [base=true] - 是否启用基本区(0x4E00-0x9FA5)
467
+ * @param {boolean} [extA=false] - 是否启用扩展 A 区(0x3400-0x4DBF)
468
+ * @param {boolean} [extBH=false] - 是否启用扩展 B~H 区(0x20000-0x2EBEF,代理对)
469
+ * @returns {string} 单个汉字字符
470
+ * @throws {RangeError} 未启用任何区段时抛出
471
+ */
472
+ function randomHan(base = true, extA = false, extBH = false) {
473
+ // 1. 收集已启用的“区段”
474
+ const ranges = [];
475
+ if (base) ranges.push({ min: 0x4e00, max: 0x9fa5, surrogate: false });
476
+ if (extA) ranges.push({ min: 0x3400, max: 0x4dbf, surrogate: false });
477
+ if (extBH) ranges.push({ min: 0x20000, max: 0x2ebef, surrogate: true });
478
+
479
+ if (ranges.length === 0) {
480
+ throw new RangeError("At least one range must be enabled");
481
+ }
482
+
483
+ // 2. 按总码位数抽号
484
+ const total = ranges.reduce((sum, r) => sum + (r.max - r.min + 1), 0);
485
+ let n = randomIntInRange(0, total - 1);
486
+
487
+ // 3. 定位落在哪个区段
488
+ for (const { min, max, surrogate } of ranges) {
489
+ const size = max - min + 1;
490
+ if (n < size) {
491
+ const code = min + n;
492
+ if (!surrogate) return String.fromCharCode(code);
493
+ // 代理对
494
+ const offset = code - 0x10000;
495
+ const hi = (offset >> 10) + 0xd800;
496
+ const lo = (offset & 0x3ff) + 0xdc00;
497
+ return String.fromCharCode(hi, lo);
498
+ }
499
+ n -= size;
500
+ }
501
+ }
502
+
503
+ /**
504
+ * 随机生成一个英文字母。
505
+ *
506
+ * @param {'lower'|'upper'} [type] - 指定大小写;留空则随机
507
+ * @returns {string} 单个字母
508
+ */
509
+ function randomEnLetter(type) {
510
+ const lower = "abcdefghijklmnopqrstuvwxyz";
511
+ const upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
512
+ const randomNum = randomIntInRange(0, 25);
513
+
514
+ switch (type) {
515
+ case "lower":
516
+ return lower[randomNum];
517
+ case "upper":
518
+ return upper[randomNum];
519
+ default:
520
+ return (Math.random() < 0.5 ? lower : upper)[randomNum];
521
+ }
522
+ }
523
+
524
+ /**
525
+ * 生成指定长度的随机“中英混合”字符串。
526
+ *
527
+ * @param {number} [len=1] - 目标长度(≥1,自动取整)
528
+ * @param {number} [zhProb=0.5] - 每个位置选择汉字的概率,默认 0.5
529
+ * @returns {string}
530
+ */
531
+ function randomHanOrEn(len, zhProb = 0.5) {
532
+ len = Math.max(1, Math.floor(len));
533
+ const buf = [];
534
+ for (let i = 0; i < len; i++) {
535
+ buf.push(Math.random() < zhProb ? randomHan() : randomEnLetter());
536
+ }
537
+ return buf.join("");
538
+ }
539
+
540
+ //#endregion
541
+
542
+ //#region 防抖节流
543
+
544
+ /**
545
+ * 创建 debounced(防抖)函数。
546
+ * - 默认 trailing 触发;当 `leading=true` 时,首次调用或超过等待间隔会立即执行。
547
+ * - 支持手动取消。
548
+ *
549
+ * @template {(...args: any[]) => any} T
550
+ * @param {T} fn - 要防抖的原始函数
551
+ * @param {number} wait - 防抖等待时间(毫秒)
552
+ * @param {boolean} [leading=false] - 是否启用立即执行(leading edge)
553
+ * @returns {T & { cancel(): void }} 返回经过防抖包装的函数,并附带 `cancel` 方法
554
+ * @throws {TypeError} 当 `fn` 不是函数时抛出
555
+ */
556
+ function debounce(fn, wait, leading = false) {
557
+ if (typeof fn !== "function") throw new TypeError("fn must be function");
558
+ wait = Math.max(0, Number(wait) || 0);
559
+ let timeoutId;
560
+ let lastCall = 0; // 0 表示从未调用过
561
+
562
+ function debounced(...args) {
563
+ const isFirst = lastCall === 0;
564
+ const isOverWait = Date.now() - lastCall >= wait;
565
+
566
+ clearTimeout(timeoutId);
567
+
568
+ // 首次调用 || 已达到等待间隔
569
+ if (leading && (isFirst || isOverWait)) {
570
+ lastCall = Date.now();
571
+ return fn.apply(this, args);
572
+ }
573
+
574
+ timeoutId = setTimeout(() => {
575
+ lastCall = Date.now();
576
+ fn.apply(this, args);
577
+ }, wait);
578
+ }
579
+
580
+ debounced.cancel = () => {
581
+ clearTimeout(timeoutId);
582
+ lastCall = 0; // 恢复初始状态
583
+ };
584
+
585
+ return debounced;
586
+ }
587
+
588
+ /**
589
+ * 创建 throttled(节流)函数。
590
+ * 支持 leading/trailing 边缘触发,可手动取消。
591
+ *
592
+ * @template {(...args: any[]) => any} T
593
+ * @param {T} fn - 要节流的原始函数
594
+ * @param {number} wait - 节流间隔(毫秒)
595
+ * @param {object} [options] - 配置项
596
+ * @param {boolean} [options.leading=true] - 是否在 leading 边缘执行
597
+ * @param {boolean} [options.trailing=true] - 是否在 trailing 边缘执行
598
+ * @returns {T & { cancel(): void }} 返回经过节流包装的函数,并附带 `cancel` 方法
599
+ * @throws {TypeError} 当 `fn` 不是函数时抛出
600
+ */
601
+ function throttle(fn, wait, { leading = true, trailing = true } = {}) {
602
+ if (typeof fn !== "function") throw new TypeError("fn must be function");
603
+ wait = Math.max(0, Number(wait) || 0);
604
+
605
+ let timeoutId = null,
606
+ lastCall = 0;
607
+
608
+ function throttled(...args) {
609
+ const remaining = wait - (Date.now() - lastCall);
610
+
611
+ if (leading && (lastCall === 0 || remaining <= 0)) {
612
+ lastCall = Date.now();
613
+ fn.apply(this, args);
614
+ } else if (trailing && !timeoutId) {
615
+ timeoutId = setTimeout(
616
+ () => {
617
+ timeoutId = null;
618
+ lastCall = Date.now();
619
+ fn.apply(this, args);
620
+ },
621
+ remaining > 0 ? remaining : wait
622
+ );
623
+ }
624
+ }
625
+
626
+ throttled.cancel = () => {
627
+ clearTimeout(timeoutId);
628
+ timeoutId = null;
629
+ lastCall = 0;
630
+ };
631
+
632
+ return throttled;
633
+ }
634
+
635
+ //#endregion
636
+
637
+ /**
638
+ * 利用 JSON 序列化/反序列化实现**深拷贝**。
639
+ * 注意:会丢失 `undefined`、函数、循环引用、特殊包装对象等。
640
+ *
641
+ * @template T
642
+ * @param {T} obj - 待拷贝的 JSON 兼容值
643
+ * @returns {T} 深拷贝后的值
644
+ */
645
+ function deepCloneByJSON(obj) {
646
+ return JSON.parse(JSON.stringify(obj));
647
+ }
648
+
649
+ /**
650
+ * **安全**地将源对象中**已存在**的属性赋值到目标对象。
651
+ * 不会新增键,也不会复制原型链上的属性。
652
+ *
653
+ * @template {Record<PropertyKey, any>} T
654
+ * @param {T} target - 目标对象(将被就地修改)
655
+ * @param {...Partial<T>} sources - 一个或多个源对象
656
+ * @returns {T} 修改后的目标对象(即第一个参数本身)
657
+ *
658
+ * @example
659
+ * const defaults = { a: 1, b: 2 };
660
+ * assignExisting(defaults, { a: 9, c: 99 }); // defaults 变为 { a: 9, b: 2 }
661
+ */
662
+ function assignExisting(target, ...sources) {
663
+ sources.forEach((source) => {
664
+ Object.keys(source).forEach((key) => {
665
+ if (target.hasOwnProperty(key)) {
666
+ target[key] = source[key];
667
+ }
668
+ });
669
+ });
670
+ return target;
671
+ }
672
+
673
+ /**
674
+ * 提取任意函数(含箭头函数、普通函数、async、class 构造器)的形参名称列表。
675
+ * 通过源码正则解析,不支持解构参数、默认参数、剩余参数等复杂语法;
676
+ * 若出现上述场景将返回空数组或部分名称。
677
+ *
678
+ * @param {Function} fn - 目标函数
679
+ * @returns {string[]} 按声明顺序排列的参数名数组;解析失败时返回空数组
680
+ *
681
+ * @example
682
+ * getFunctionArgNames(function (a, b) {}) // ["a", "b"]
683
+ * getFunctionArgNames((foo, bar) => {}) // ["foo", "bar"]
684
+ * getFunctionArgNames(async function x({a} = {}) {}) // [] (解构无法识别)
685
+ */
686
+ function getFunctionArgNames(fn) {
687
+ const FN_ARG_SPLIT = /,/,
688
+ FN_ARG = /^\s*(_?)(\S+?)\1\s*$/,
689
+ FN_ARGS = /^[^(]*\(\s*([^)]*)\)/m,
690
+ ARROW_ARG = /^([^(]+?)=>/,
691
+ STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/gm;
692
+
693
+ const fnText = Function.prototype.toString.call(fn).replace(STRIP_COMMENTS, "");
694
+ const argDecl = fnText.match(ARROW_ARG) || fnText.match(FN_ARGS);
695
+ const retArgNames = [];
696
+ [].forEach.call(argDecl[1].split(FN_ARG_SPLIT), function (arg) {
697
+ arg.replace(FN_ARG, function (all, underscore, name) {
698
+ retArgNames.push(name);
699
+ });
700
+ });
701
+ return retArgNames;
702
+ }
703
+
704
+ /**
705
+ * 将 Blob(或 File)读取为文本,并可选择自动执行 `JSON.parse`。
706
+ * 当 `isParse=true` 且内容非法 JSON 时,会回退为返回原始文本。
707
+ *
708
+ * @param {Blob} blob - 待读取的 Blob/File 对象
709
+ * @param {boolean} [isParse=true] - 是否尝试将结果按 JSON 解析
710
+ * @returns {Promise<string | any>} 解析后的 JSON 对象或原始文本
711
+ *
712
+ * @example
713
+ * const json = await readBlobAsText(blob); // 自动 JSON.parse
714
+ * const text = await readBlobAsText(blob, false); // 仅返回文本
715
+ */
716
+ function readBlobAsText(blob, isParse = true) {
717
+ return new Promise((resolve, reject) => {
718
+ const reader = new FileReader();
719
+ reader.onload = (evt) => {
720
+ const result = evt.target.result;
721
+ if (isParse) {
722
+ try {
723
+ resolve(JSON.parse(result));
724
+ } catch (error) {
725
+ console.error(error);
726
+ resolve(result);
727
+ }
728
+ } else {
729
+ resolve(result);
730
+ }
731
+ };
732
+ reader.onerror = reject;
733
+ reader.readAsText(blob);
734
+ });
899
735
  }
900
736
 
901
- /**
902
- * 快捷调用 {@link formatDuration},最大单位到“天”。
903
- *
904
- * @param {number} totalSeconds
905
- * @param {Omit<Parameters<typeof formatDuration>[1],'maxUnit'>} [options]
906
- * @returns {string}
907
- */
908
- function formatDurationMaxDay(totalSeconds, options = {}) {
909
- return formatDuration(totalSeconds, { ...options, maxUnit: "day" });
737
+ function getLocales_TimePeriod() {
738
+ return {
739
+ earlyMorning: "凌晨",
740
+ morning: "上午",
741
+ noon: "中午",
742
+ afternoon: "下午",
743
+ evening: "晚上"
744
+ };
745
+ }
746
+
747
+ /**
748
+ * 将任意值安全转换为 Date 对象。
749
+ * - 数字/数字字符串:视为时间戳
750
+ * - 字符串:尝试按 ISO/RFC 格式解析
751
+ * - 对象:优先 valueOf(),再 toString()
752
+ * - null / undefined / 无效值:返回 null
753
+ *
754
+ * @param {*} val - 待转换值
755
+ * @returns {Date | null} 有效 Date 或 null
756
+ */
757
+ function toDate(val) {
758
+ if (val == null) return null; // null / undefined
759
+ if (val instanceof Date) return isNaN(val) ? null : val; // 已是 Date,但需排除 Invalid Date
760
+
761
+ // 1. 数字或数字字符串 → 时间戳
762
+ if (typeof val === "number" || (typeof val === "string" && /^-?\d+(\.\d+)?$/.test(val.trim()))) {
763
+ const d = new Date(+val);
764
+ return isNaN(d) ? null : d;
765
+ }
766
+
767
+ // 2. 标准 ISO 8601 / RFC 2825 等合法字符串
768
+ if (typeof val === "string") {
769
+ const d = new Date(val);
770
+ return isNaN(d) ? null : d; // 非法格式返回 null
771
+ }
772
+
773
+ // 3. 对象带 valueOf / toString
774
+ if (typeof val === "object") {
775
+ // 优先调用 valueOf(期望返回数字时间戳)
776
+ const prim = val.valueOf ? val.valueOf() : Object.prototype.valueOf.call(val);
777
+ if (typeof prim === "number" && !isNaN(prim)) {
778
+ const d = new Date(prim);
779
+ return isNaN(d) ? null : d;
780
+ }
781
+ // 兜底用字符串
782
+ const str = val.toString ? val.toString() : String(val);
783
+ const d = new Date(str);
784
+ return isNaN(d) ? null : d;
785
+ }
786
+
787
+ // 4. 其余情况
788
+ return null;
789
+ }
790
+
791
+ /**
792
+ * 在闭区间 [date1, date2] 内随机生成一个日期(含首尾)。
793
+ * 若顺序相反则自动交换。
794
+ *
795
+ * @param {Date} date1 - 起始日期
796
+ * @param {Date} date2 - 结束日期
797
+ * @returns {Date} 随机日期
798
+ */
799
+ function randomDateInRange(date1, date2) {
800
+ let v1 = date1.getTime(),
801
+ v2 = date2.getTime();
802
+ if (v1 > v2) [v1, v2] = [v2, v1];
803
+ return new Date(v1 + Math.floor(Math.random() * (v2 - v1 + 1)));
804
+ }
805
+
806
+ //#region 持续时间相关
807
+
808
+ /**
809
+ * 时间持续对象(完整版本,包含年月日时分秒毫秒)
810
+ * @typedef {Object} DurationObject
811
+ * @property {number} years - 年数
812
+ * @property {number} months - 月数(0-11)
813
+ * @property {number} days - 天数(0-29,取决于 monthDays)
814
+ * @property {number} hours - 小时数(0-23)
815
+ * @property {number} minutes - 分钟数(0-59)
816
+ * @property {number} seconds - 秒数(0-59)
817
+ * @property {number} milliseconds - 毫秒数(0-999)
818
+ */
819
+
820
+ /**
821
+ * 时间持续对象(最大单位为天)
822
+ * @typedef {Object} DurationMaxDayObject
823
+ * @property {number} days - 天数
824
+ * @property {number} hours - 小时数(0-23)
825
+ * @property {number} minutes - 分钟数(0-59)
826
+ * @property {number} seconds - 秒数(0-59)
827
+ * @property {number} milliseconds - 毫秒数(0-999)
828
+ */
829
+
830
+ /**
831
+ * 时间持续对象(最大单位为小时)
832
+ * @typedef {Object} DurationMaxHourObject
833
+ * @property {number} hours - 小时数
834
+ * @property {number} minutes - 分钟数(0-59)
835
+ * @property {number} seconds - 秒数(0-59)
836
+ * @property {number} milliseconds - 毫秒数(0-999)
837
+ */
838
+
839
+ /**
840
+ * 将毫秒转换为时间持续对象。
841
+ *
842
+ * @param {number} milliseconds - 毫秒数(非负整数)
843
+ * @param {Object} [options] - 选项对象。
844
+ * @param {number} [options.yearDays=365] - 一年的天数。
845
+ * @param {number} [options.monthDays=30] - 一月的天数。
846
+ * @returns {DurationObject} 时间持续对象
847
+ * @throws {TypeError} 当 milliseconds 不是有效数字
848
+ * @throws {RangeError} 当 milliseconds 为负数
849
+ * @throws {RangeError} 当 yearDays 或 monthDays 不是正整数
850
+ *
851
+ * @example
852
+ * // 基本用法
853
+ * millisecond2Duration(42070000500);
854
+ * // 返回: { years: 1, months: 4, days: 1, hours: 22, minutes: 6, seconds: 40, milliseconds: 500 }
855
+ */
856
+ function millisecond2Duration(milliseconds, options = { yearDays: 365, monthDays: 30 }) {
857
+ // 参数验证
858
+ if (typeof milliseconds !== "number" || isNaN(milliseconds)) {
859
+ throw new TypeError("milliseconds must be a valid number");
860
+ }
861
+ if (milliseconds < 0) {
862
+ throw new RangeError("milliseconds must be a non-negative number");
863
+ }
864
+
865
+ // 默认选项
866
+ const { yearDays = 365, monthDays = 30 } = options;
867
+
868
+ // 选项验证
869
+ if (!Number.isInteger(yearDays) || yearDays <= 0) {
870
+ throw new RangeError("yearDays must be a positive integer");
871
+ }
872
+ if (!Number.isInteger(monthDays) || monthDays <= 0) {
873
+ throw new RangeError("monthDays must be a positive integer");
874
+ }
875
+
876
+ const totalMilliseconds = Math.floor(milliseconds);
877
+ const ms = totalMilliseconds % 1000;
878
+ const diffSeconds = Math.floor(totalMilliseconds / 1000);
879
+
880
+ const seconds = diffSeconds % 60;
881
+ const minutes = Math.floor(diffSeconds / 60) % 60;
882
+ const hours = Math.floor(diffSeconds / 3600) % 24;
883
+
884
+ // 计算年、月、日
885
+ const totalDays = Math.floor(diffSeconds / 3600 / 24);
886
+ const years = Math.floor(totalDays / yearDays);
887
+ const remainingDays = totalDays % yearDays;
888
+ const months = Math.floor(remainingDays / monthDays);
889
+ const days = remainingDays % monthDays;
890
+
891
+ return { years, months, days, hours, minutes, seconds, milliseconds: ms };
892
+ }
893
+
894
+ /**
895
+ * 将毫秒转换为时间持续对象(最大单位为天)。
896
+ * @param {number} milliseconds - 毫秒数(非负整数)
897
+ * @returns {DurationMaxDayObject} 包含天、小时、分钟、秒、毫秒的时间持续对象
898
+ * @throws {TypeError} 当 milliseconds 不是有效数字时抛出
899
+ * @throws {RangeError} 当 milliseconds 为负数时抛出
900
+ * @example
901
+ * // 返回 { days: 486, hours: 22, minutes: 6, seconds: 40, milliseconds: 500 }
902
+ * millisecond2DurationMaxDay(42070000500);
903
+ */
904
+ function millisecond2DurationMaxDay(milliseconds) {
905
+ if (typeof milliseconds !== "number" || isNaN(milliseconds)) {
906
+ throw new TypeError("milliseconds must be a valid number");
907
+ }
908
+ if (milliseconds < 0) {
909
+ throw new RangeError("milliseconds must be a non-negative number");
910
+ }
911
+
912
+ const totalMilliseconds = Math.floor(milliseconds);
913
+ const ms = totalMilliseconds % 1000;
914
+ const diffSeconds = Math.floor(totalMilliseconds / 1000);
915
+
916
+ const seconds = diffSeconds % 60;
917
+ const minutes = Math.floor(diffSeconds / 60) % 60;
918
+ const hours = Math.floor(diffSeconds / 3600) % 24;
919
+ const days = Math.floor(diffSeconds / 3600 / 24);
920
+
921
+ return { days, hours, minutes, seconds, milliseconds: ms };
922
+ }
923
+
924
+ /**
925
+ * 将毫秒转换为时间持续对象(最大单位为小时)。
926
+ * @param {number} milliseconds - 毫秒数(非负整数)
927
+ * @returns {DurationMaxHourObject} 包含小时、分钟、秒、毫秒的时间持续对象
928
+ * @throws {TypeError} 当 milliseconds 不是有效数字时抛出
929
+ * @throws {RangeError} 当 milliseconds 为负数时抛出
930
+ * @example
931
+ * // 返回 { hours: 11686, minutes: 6, seconds: 40, milliseconds: 500 }
932
+ * millisecond2DurationMaxHour(42070000500);
933
+ */
934
+ function millisecond2DurationMaxHour(milliseconds) {
935
+ if (typeof milliseconds !== "number" || isNaN(milliseconds)) {
936
+ throw new TypeError("milliseconds must be a valid number");
937
+ }
938
+ if (milliseconds < 0) {
939
+ throw new RangeError("milliseconds must be a non-negative number");
940
+ }
941
+
942
+ const totalMilliseconds = Math.floor(milliseconds);
943
+ const ms = totalMilliseconds % 1000;
944
+ const diffSeconds = Math.floor(totalMilliseconds / 1000);
945
+
946
+ const seconds = diffSeconds % 60;
947
+ const minutes = Math.floor(diffSeconds / 60) % 60;
948
+ const hours = Math.floor(diffSeconds / 3600);
949
+
950
+ return { hours, minutes, seconds, milliseconds: ms };
951
+ }
952
+
953
+ /**
954
+ * 将秒转换为时间持续对象。
955
+ *
956
+ * @param {number} seconds - 秒数(非负整数)
957
+ * @param {Object} [options] - 选项对象。
958
+ * @param {number} [options.yearDays=365] - 一年的天数。
959
+ * @param {number} [options.monthDays=30] - 一月的天数。
960
+ * @returns {DurationObject} 时间持续对象
961
+ * @throws {TypeError} 当 seconds 不是有效数字
962
+ * @throws {RangeError} 当 seconds 为负数
963
+ * @throws {RangeError} 当 yearDays 或 monthDays 不是正整数
964
+ *
965
+ * @example
966
+ * // 基本用法
967
+ * second2Duration(42070000.5);
968
+ * // 返回: { years: 1, months: 4, days: 1, hours: 22, minutes: 6, seconds: 40, milliseconds: 500 }
969
+ */
970
+ function second2Duration(seconds, options = { yearDays: 365, monthDays: 30 }) {
971
+ if (typeof seconds !== "number" || isNaN(seconds)) {
972
+ throw new TypeError("seconds must be a valid number");
973
+ }
974
+ if (seconds < 0) {
975
+ throw new RangeError("seconds must be a non-negative number");
976
+ }
977
+ return millisecond2Duration(seconds * 1000, options);
978
+ }
979
+
980
+ /**
981
+ * 将秒转换为时间持续对象(最大单位为天)。
982
+ * @param {number} seconds - 秒数(非负整数)
983
+ * @returns {DurationMaxDayObject} 包含天、小时、分钟、秒、毫秒的时间持续对象
984
+ * @throws {TypeError} 当 seconds 不是有效数字时抛出
985
+ * @throws {RangeError} 当 seconds 为负数时抛出
986
+ * @example
987
+ * // 返回 { days: 486, hours: 22, minutes: 6, seconds: 40, milliseconds: 500 }
988
+ * second2DurationMaxDay(42070000.5);
989
+ */
990
+ function second2DurationMaxDay(seconds) {
991
+ if (typeof seconds !== "number" || isNaN(seconds)) {
992
+ throw new TypeError("seconds must be a valid number");
993
+ }
994
+ if (seconds < 0) {
995
+ throw new RangeError("seconds must be a non-negative number");
996
+ }
997
+ return millisecond2DurationMaxDay(seconds * 1000);
998
+ }
999
+
1000
+ /**
1001
+ * 将秒转换为时间持续对象(最大单位为小时)。
1002
+ * @param {number} seconds - 秒数(非负整数)
1003
+ * @returns {DurationMaxHourObject} 包含小时、分钟、秒、毫秒的时间持续对象
1004
+ * @throws {TypeError} 当 seconds 不是有效数字时抛出
1005
+ * @throws {RangeError} 当 seconds 为负数时抛出
1006
+ * @example
1007
+ * // 返回 { hours: 11686, minutes: 6, seconds: 40, milliseconds: 500 }
1008
+ * second2DurationMaxHour(42070000.5);
1009
+ */
1010
+ function second2DurationMaxHour(seconds) {
1011
+ if (typeof seconds !== "number" || isNaN(seconds)) {
1012
+ throw new TypeError("seconds must be a valid number");
1013
+ }
1014
+ if (seconds < 0) {
1015
+ throw new RangeError("seconds must be a non-negative number");
1016
+ }
1017
+ return millisecond2DurationMaxHour(seconds * 1000);
1018
+ }
1019
+
1020
+ //#endregion
1021
+
1022
+ /**
1023
+ * 根据小时数返回对应的时间段名称。
1024
+ *
1025
+ * @param {number} hour - 24 小时制的小时(0-23)
1026
+ * @param {object} [locales] - 自定义时段文案
1027
+ * @param {string} [locales.earlyMorning="凌晨"] - 00-05
1028
+ * @param {string} [locales.morning="上午"] - 06-11
1029
+ * @param {string} [locales.noon="中午"] - 12-13
1030
+ * @param {string} [locales.afternoon="下午"] - 14-17
1031
+ * @param {string} [locales.evening="晚上"] - 18-23
1032
+ * @returns {string} 时段名称
1033
+ * @throws {RangeError} 当 hour 不在 0-23 范围时抛出
1034
+ */
1035
+ function getTimePeriodName(hour, locales = getLocales_TimePeriod()) {
1036
+ if (!Number.isInteger(hour) || hour < 0 || hour > 23) {
1037
+ throw new RangeError("hour 必须是 0-23 的整数");
1038
+ }
1039
+ if (hour >= 0 && hour < 6) return locales.earlyMorning;
1040
+ if (hour < 12) return locales.morning;
1041
+ if (hour < 14) return locales.noon;
1042
+ if (hour < 18) return locales.afternoon;
1043
+ return locales.evening;
1044
+ }
1045
+
1046
+ /**
1047
+ * 格式化时间戳为本地化的时间字符串。
1048
+ *
1049
+ * @param {number} timestamp - 要格式化的时间戳(毫秒)。
1050
+ * @param {Object} [locales] - 本地化配置对象,包含时间相关的本地化字符串。
1051
+ * @param {string} [locales.justNow='刚刚'] - 表示刚刚过去的时间。
1052
+ * @param {string} [locales.today='今天'] - 表示今天。
1053
+ * @param {string} [locales.yesterday='昨天'] - 表示昨天。
1054
+ * @param {string} [locales.beforeYesterday='前天'] - 表示前天。
1055
+ * @param {string} [locales.year='年'] - 年的单位。
1056
+ * @param {string} [locales.month='月'] - 月的单位。
1057
+ * @param {string} [locales.day='日'] - 日的单位。
1058
+ * @param {Object} [locales.timePeriod] - 一天中不同时间段的本地化字符串。
1059
+ * @param {string} [locales.timePeriod.earlyMorning='凌晨'] - 凌晨。
1060
+ * @param {string} [locales.timePeriod.morning='上午'] - 上午。
1061
+ * @param {string} [locales.timePeriod.noon='中午'] - 中午。
1062
+ * @param {string} [locales.timePeriod.afternoon='下午'] - 下午。
1063
+ * @param {string} [locales.timePeriod.evening='晚上'] - 晚上。
1064
+ * @param {Array<string>} [locales.weekDays=['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']] - 星期几的本地化字符串数组。
1065
+ *
1066
+ * @returns {string} - 格式化后的时间字符串。
1067
+ */
1068
+ function formatTimeForLocale(
1069
+ timestamp,
1070
+ locales = {
1071
+ justNow: "刚刚",
1072
+ today: "今天",
1073
+ yesterday: "昨天",
1074
+ beforeYesterday: "前天",
1075
+ year: "年",
1076
+ month: "月",
1077
+ day: "日",
1078
+ timePeriod: getLocales_TimePeriod(),
1079
+ weekDays: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]
1080
+ }
1081
+ ) {
1082
+ const now = new Date();
1083
+ const messageDate = new Date(timestamp);
1084
+
1085
+ const nowTime = now.getTime();
1086
+ const msgTime = messageDate.getTime();
1087
+ const diff = nowTime - msgTime;
1088
+ const diffMinutes = Math.floor(diff / (1000 * 60));
1089
+
1090
+ const year = messageDate.getFullYear();
1091
+ const month = messageDate.getMonth() + 1;
1092
+ const day = messageDate.getDate();
1093
+ const hour = messageDate.getHours();
1094
+ const minute = messageDate.getMinutes().toString().padStart(2, "0");
1095
+
1096
+ // 刚刚:1分钟内
1097
+ if (diffMinutes < 1) {
1098
+ return locales.justNow;
1099
+ }
1100
+
1101
+ // 计算自然天数差(按日期而非时间差)
1102
+ const nowDate = new Date(now.getFullYear(), now.getMonth(), now.getDate());
1103
+ const msgDate = new Date(year, month - 1, day);
1104
+ const diffDays = Math.floor((nowDate.getTime() - msgDate.getTime()) / (1000 * 60 * 60 * 24));
1105
+
1106
+ const period = getTimePeriodName(hour, locales.timePeriod);
1107
+ const timeStr = `${hour}:${minute}`;
1108
+
1109
+ // 今天
1110
+ if (diffDays === 0) {
1111
+ return `${locales.today} ${period}${timeStr}`;
1112
+ }
1113
+
1114
+ // 昨天
1115
+ if (diffDays === 1) {
1116
+ return `${locales.yesterday} ${period}${timeStr}`;
1117
+ }
1118
+
1119
+ // 前天
1120
+ if (diffDays === 2) {
1121
+ return `${locales.beforeYesterday} ${period}${timeStr}`;
1122
+ }
1123
+
1124
+ // 本周内(周一到周日,且不是今天/昨天/前天)
1125
+ // 获取本周一的日期
1126
+ const nowDay = now.getDay() || 7; // 周日转为7
1127
+ const msgDay = messageDate.getDay() || 7;
1128
+ const mondayOfThisWeek = new Date(nowDate.getTime() - (nowDay - 1) * 24 * 60 * 60 * 1000);
1129
+ const mondayOfThatWeek = new Date(msgDate.getTime() - (msgDay - 1) * 24 * 60 * 60 * 1000);
1130
+
1131
+ if (mondayOfThisWeek.getTime() === mondayOfThatWeek.getTime() && diffDays < 7) {
1132
+ const weekDayName = locales.weekDays[messageDate.getDay()];
1133
+ return `${weekDayName}${period} ${timeStr}`;
1134
+ }
1135
+
1136
+ // 本月内(非本周)
1137
+ const nowYear = now.getFullYear();
1138
+ const nowMonth = now.getMonth();
1139
+ if (year === nowYear && messageDate.getMonth() === nowMonth) {
1140
+ return `${month}${locales.month}${day}${locales.day} ${period}${timeStr}`;
1141
+ }
1142
+
1143
+ // 本年内(非本月)
1144
+ if (year === nowYear) {
1145
+ return `${month}${locales.month}${day}${locales.day} ${period}${timeStr}`;
1146
+ }
1147
+
1148
+ // 其他(非本年)
1149
+ return `${year}${locales.year}${month}${locales.month}${day}${locales.day} ${period}${timeStr}`;
910
1150
  }
911
1151
 
912
- /**
913
- * 快捷调用 {@link formatDuration},最大单位到“小时”。
914
- *
915
- * @param {number} totalSeconds
916
- * @param {Omit<Parameters<typeof formatDuration>[1],'maxUnit'>} [options]
917
- * @returns {string}
918
- */
919
- function formatDurationMaxHour(totalSeconds, options = {}) {
920
- return formatDuration(totalSeconds, { ...options, maxUnit: "hour" });
1152
+ /**
1153
+ * 通过动态创建 `<a>` 标签触发浏览器下载。
1154
+ * 注意:此方法可能无法强制下载浏览器原生支持的文件(如图片、PDF),浏览器可能会选择直接打开。
1155
+ *
1156
+ * @param {string} url - 任意可下载地址(同源或允许跨域)
1157
+ * @param {string} [fileName] - 保存到本地的文件名;不传时使用时间戳
1158
+ */
1159
+ function downloadByUrl(url, fileName) {
1160
+ const a = document.createElement("a");
1161
+ a.style.display = "none";
1162
+ a.rel = "noopener";
1163
+ a.href = url;
1164
+ a.download = fileName || Date.now();
1165
+ document.body.appendChild(a);
1166
+ a.click();
1167
+ document.body.removeChild(a);
1168
+ }
1169
+
1170
+ /**
1171
+ * 把 Blob 转成临时 URL 并触发下载,下载完成后立即释放内存。
1172
+ *
1173
+ * @param {Blob} blob - 待下载的 Blob(含 File)
1174
+ * @param {string} [fileName] - 保存到本地的文件名
1175
+ */
1176
+ function downloadByBlob(blob, fileName) {
1177
+ const url = URL.createObjectURL(blob);
1178
+ downloadByUrl(url, fileName);
1179
+ setTimeout(() => URL.revokeObjectURL(url), 0);
1180
+ }
1181
+
1182
+ /**
1183
+ * 将任意数据包装成 Blob 并下载。
1184
+ *
1185
+ * @param {string | ArrayBufferView | ArrayBuffer | Blob} data - 要写入文件的数据
1186
+ * @param {string} [fileName] - 保存到本地的文件名
1187
+ * @param {string} [mimeType] - MIME 类型;默认 `application/octet-stream`
1188
+ */
1189
+ function downloadByData(data, fileName, mimeType = "application/octet-stream") {
1190
+ downloadByBlob(new Blob([data], { type: mimeType }), fileName);
1191
+ }
1192
+
1193
+ /**
1194
+ * 快捷下载 Excel 文件(MIME 已固定)。
1195
+ *
1196
+ * @param {string | ArrayBufferView | ArrayBuffer | Blob} data - Excel 二进制或字符串内容
1197
+ * @param {string} [fileName] - 保存到本地的文件名
1198
+ */
1199
+ function downloadExcel(data, fileName) {
1200
+ downloadByData(data, fileName, "application/vnd.ms-excel");
1201
+ }
1202
+
1203
+ /**
1204
+ * 快捷下载 JSON 文件(MIME 已固定)。
1205
+ * 若传入非字符串数据,会自行 `JSON.stringify`。
1206
+ *
1207
+ * @param {any} data - 要序列化的 JSON 数据
1208
+ * @param {string} [fileName] - 保存到本地的文件名
1209
+ */
1210
+ function downloadJSON(data, fileName) {
1211
+ // downloadByData(typeof data === "string" ? data : JSON.stringify(data, null, 4), fileName, "application/json");
1212
+ downloadByData(data, fileName, "application/json");
1213
+ }
1214
+
1215
+ /**
1216
+ * 通过 `fetch` 获取资源并强制下载,避免浏览器直接打开文件。
1217
+ * 此方法适用于下载图片、PDF 等浏览器默认会打开的文件类型。
1218
+ * 如果 `fetch` 失败(如 CORS 策略阻止),会回退到 `downloadByUrl` 方法作为备选方案。
1219
+ *
1220
+ * @param {string} url - 文件的 URL 地址
1221
+ * @param {string} [fileName] - 保存到本地的文件名。如果不提供,会尝试从 URL 中自动提取
1222
+ * @returns {Promise<void>} 返回一个 Promise,在下载开始或失败后 resolve
1223
+ */
1224
+ async function fetchOrDownloadByUrl(url, fileName) {
1225
+ // 如果未提供文件名,尝试从 URL 路径中提取
1226
+ if (!fileName) {
1227
+ try {
1228
+ const urlPathname = new URL(url).pathname;
1229
+ // 获取路径的最后一部分作为文件名,并移除可能的查询参数
1230
+ fileName = urlPathname.substring(urlPathname.lastIndexOf("/") + 1).split("?")[0];
1231
+ } catch (e) {}
1232
+ // 如果提取后文件名为空(例如 URL 以 '/' 结尾),也使用时间戳
1233
+ if (!fileName) {
1234
+ fileName = Date.now().toString();
1235
+ }
1236
+ }
1237
+
1238
+ try {
1239
+ const response = await fetch(url, {
1240
+ method: "GET",
1241
+ mode: "cors",
1242
+ cache: "no-cache"
1243
+ });
1244
+
1245
+ if (!response.ok) {
1246
+ throw new Error(`HTTP error! ${response.status}: ${response.statusText}`);
1247
+ }
1248
+
1249
+ const blob = await response.blob();
1250
+ downloadByBlob(blob, fileName);
1251
+ } catch (error) {
1252
+ downloadByUrl(url, fileName);
1253
+ }
921
1254
  }
922
1255
 
923
- /**
924
- * 通过动态创建 `<a>` 标签触发浏览器下载。
925
- * 注意:此方法可能无法强制下载浏览器原生支持的文件(如图片、PDF),浏览器可能会选择直接打开。
926
- *
927
- * @param {string} url - 任意可下载地址(同源或允许跨域)
928
- * @param {string} [fileName] - 保存到本地的文件名;不传时使用时间戳
929
- */
930
- function downloadByUrl(url, fileName) {
931
- const a = document.createElement("a");
932
- a.style.display = "none";
933
- a.rel = "noopener";
934
- a.href = url;
935
- a.download = fileName || Date.now();
936
- document.body.appendChild(a);
937
- a.click();
938
- document.body.removeChild(a);
939
- }
940
-
941
- /**
942
- * Blob 转成临时 URL 并触发下载,下载完成后立即释放内存。
943
- *
944
- * @param {Blob} blob - 待下载的 Blob(含 File)
945
- * @param {string} [fileName] - 保存到本地的文件名
946
- */
947
- function downloadByBlob(blob, fileName) {
948
- const url = URL.createObjectURL(blob);
949
- downloadByUrl(url, fileName);
950
- setTimeout(() => URL.revokeObjectURL(url), 0);
951
- }
952
-
953
- /**
954
- * 将任意数据包装成 Blob 并下载。
955
- *
956
- * @param {string | ArrayBufferView | ArrayBuffer | Blob} data - 要写入文件的数据
957
- * @param {string} [fileName] - 保存到本地的文件名
958
- * @param {string} [mimeType] - MIME 类型;默认 `application/octet-stream`
959
- */
960
- function downloadByData(data, fileName, mimeType = "application/octet-stream") {
961
- downloadByBlob(new Blob([data], { type: mimeType }), fileName);
962
- }
963
-
964
- /**
965
- * 快捷下载 Excel 文件(MIME 已固定)。
966
- *
967
- * @param {string | ArrayBufferView | ArrayBuffer | Blob} data - Excel 二进制或字符串内容
968
- * @param {string} [fileName] - 保存到本地的文件名
969
- */
970
- function downloadExcel(data, fileName) {
971
- downloadByData(data, fileName, "application/vnd.ms-excel");
972
- }
973
-
974
- /**
975
- * 快捷下载 JSON 文件(MIME 已固定)。
976
- * 若传入非字符串数据,会自行 `JSON.stringify`。
977
- *
978
- * @param {any} data - 要序列化的 JSON 数据
979
- * @param {string} [fileName] - 保存到本地的文件名
980
- */
981
- function downloadJSON(data, fileName) {
982
- // downloadByData(typeof data === "string" ? data : JSON.stringify(data, null, 4), fileName, "application/json");
983
- downloadByData(data, fileName, "application/json");
984
- }
985
-
986
- /**
987
- * 通过 `fetch` 获取资源并强制下载,避免浏览器直接打开文件。
988
- * 此方法适用于下载图片、PDF 等浏览器默认会打开的文件类型。
989
- * 如果 `fetch` 失败(如 CORS 策略阻止),会回退到 `downloadByUrl` 方法作为备选方案。
990
- *
991
- * @param {string} url - 文件的 URL 地址
992
- * @param {string} [fileName] - 保存到本地的文件名。如果不提供,会尝试从 URL 中自动提取
993
- * @returns {Promise<void>} 返回一个 Promise,在下载开始或失败后 resolve
994
- */
995
- async function fetchOrDownloadByUrl(url, fileName) {
996
- // 如果未提供文件名,尝试从 URL 路径中提取
997
- if (!fileName) {
998
- try {
999
- const urlPathname = new URL(url).pathname;
1000
- // 获取路径的最后一部分作为文件名,并移除可能的查询参数
1001
- fileName = urlPathname.substring(urlPathname.lastIndexOf("/") + 1).split("?")[0];
1002
- } catch (e) {}
1003
- // 如果提取后文件名为空(例如 URL 以 '/' 结尾),也使用时间戳
1004
- if (!fileName) {
1005
- fileName = Date.now().toString();
1006
- }
1007
- }
1008
-
1009
- try {
1010
- const response = await fetch(url, {
1011
- method: "GET",
1012
- mode: "cors",
1013
- cache: "no-cache"
1014
- });
1015
-
1016
- if (!response.ok) {
1017
- throw new Error(`HTTP error! ${response.status}: ${response.statusText}`);
1018
- }
1019
-
1020
- const blob = await response.blob();
1021
- downloadByBlob(blob, fileName);
1022
- } catch (error) {
1023
- downloadByUrl(url, fileName);
1024
- }
1025
- }
1026
-
1027
- /**
1028
- * 简单、高性能的通用事件总线。
1029
- * - 支持命名空间事件
1030
- * - 支持一次性监听器
1031
- * - 返回唯一 flag,用于精确卸载
1032
- * - emit 时可选自定义 this 指向
1033
- */
1034
- class MyEvent {
1035
- constructor() {
1036
- this.evtPool = new Map();
1037
- }
1038
-
1039
- /**
1040
- * 注册事件监听器。
1041
- * @param {string} name - 事件名
1042
- * @param {Function} fn - 回调函数
1043
- * @returns {string} flag - 唯一标识,用于 off
1044
- */
1045
- on(name, fn) {
1046
- let flag = Date.now() + "_" + parseInt(Math.random() * 1e8);
1047
- const evtItem = {
1048
- flag,
1049
- fn
1050
- };
1051
- if (this.evtPool.has(name)) {
1052
- this.evtPool.get(name).push(evtItem);
1053
- } else {
1054
- this.evtPool.set(name, [evtItem]);
1055
- }
1056
- return flag;
1057
- }
1058
-
1059
- /**
1060
- * 注册一次性监听器,触发后自动移除。
1061
- * @param {string} name - 事件名
1062
- * @param {Function} fn - 回调函数
1063
- * @returns {string} flag - 唯一标识
1064
- */
1065
- once(name, fn) {
1066
- const _this = this;
1067
- let wrapper;
1068
- wrapper = function (data) {
1069
- _this.off(name, wrapper);
1070
- fn.call(this, data);
1071
- };
1072
- return this.on(name, wrapper);
1073
- }
1074
-
1075
- /**
1076
- * 移除指定事件监听器。
1077
- * @param {string} name - 事件名
1078
- * @param {Function|string} fnOrFlag - 回调函数或 flag
1079
- */
1080
- off(name, fnOrFlag) {
1081
- if (!this.evtPool.has(name)) return;
1082
- const evtItems = this.evtPool.get(name);
1083
- const filtered = evtItems.filter((item) => item.fn !== fnOrFlag && item.flag !== fnOrFlag);
1084
- if (filtered.length === 0) {
1085
- this.evtPool.delete(name);
1086
- } else {
1087
- this.evtPool.set(name, filtered);
1088
- }
1089
- }
1090
-
1091
- /**
1092
- * 触发事件(同步执行)。
1093
- * @param {string} name - 事件名
1094
- * @param {*} [data] - 任意载荷
1095
- * @param {*} [fnThis] - 回调内部 this 指向,默认 undefined
1096
- */
1097
- emit(name, data, fnThis) {
1098
- if (!this.evtPool.has(name)) return;
1099
- const evtItems = this.evtPool.get(name);
1100
- evtItems.forEach((item) => {
1101
- try {
1102
- item.fn.call(fnThis, data);
1103
- } catch (err) {
1104
- console.error(`Error in event listener for "${name}":`, err);
1105
- }
1106
- });
1107
- }
1108
- }
1109
-
1110
- /**
1111
- * 跨页通信插件:通过 localStorage + storage 事件将当前实例的 emit 广播到其他同源页面。
1112
- * 支持节流、命名空间隔离。
1113
- */
1114
- const MyEvent_CrossPagePlugin = (() => {
1115
- const INSTALLED = new WeakSet(); // 防止重复安装
1116
-
1117
- return {
1118
- /**
1119
- * 为指定 MyEvent 实例安装跨页插件。
1120
- * @param {MyEvent} bus - 事件总线实例
1121
- * @param {Options} [opts] - 配置项
1122
- */
1123
- install(bus, opts = {}) {
1124
- if (INSTALLED.has(bus)) return;
1125
- INSTALLED.add(bus);
1126
-
1127
- const ns = `___my-event-cross-page-${opts.namespace || "default"}___`;
1128
- const delay = opts.throttle || 16;
1129
- let last = 0;
1130
-
1131
- // 1、重写 emit
1132
- const rawEmit = bus.emit;
1133
- bus.emit = function (name, data, fnThis) {
1134
- rawEmit.call(bus, name, data, fnThis); // 本地先执行
1135
- const now = Date.now();
1136
- if (now - last < delay) return;
1137
- last = now;
1138
- const key = ns + name;
1139
- try {
1140
- localStorage.setItem(key, JSON.stringify({ name, data, ts: now }));
1141
- localStorage.removeItem(key); // 触发 storage 事件
1142
- } catch (e) {}
1143
- };
1144
-
1145
- // 2、监听其他页广播
1146
- function onStorageHandler(e) {
1147
- if (!e.key || !e.key.startsWith(ns)) return;
1148
- let payload;
1149
- try {
1150
- payload = JSON.parse(e.newValue || "{}");
1151
- } catch {
1152
- return;
1153
- }
1154
- if (!payload.ts || payload.ts <= last) return;
1155
- rawEmit.call(bus, e.key.slice(ns.length), payload.data); // 仅本地
1156
- }
1157
- addEventListener("storage", onStorageHandler);
1158
-
1159
- // 3、保存卸载器
1160
- bus._uninstallCrossPage = () => {
1161
- removeEventListener("storage", onStorageHandler);
1162
- bus.emit = rawEmit;
1163
- INSTALLED.delete(bus);
1164
- };
1165
- },
1166
-
1167
- /**
1168
- * 卸载插件,恢复原始 emit 并停止监听。
1169
- * @param {MyEvent} bus - 事件总线实例
1170
- */
1171
- uninstall(bus) {
1172
- if (typeof bus._uninstallCrossPage === "function") bus._uninstallCrossPage();
1173
- }
1174
- };
1256
+ /**
1257
+ * 简单、高性能的通用事件总线。
1258
+ * - 支持命名空间事件
1259
+ * - 支持一次性监听器
1260
+ * - 返回唯一 flag,用于精确卸载
1261
+ * - emit 时可选自定义 this 指向
1262
+ */
1263
+ class MyEvent {
1264
+ constructor() {
1265
+ this.evtPool = new Map();
1266
+ }
1267
+
1268
+ /**
1269
+ * 注册事件监听器。
1270
+ * @param {string} name - 事件名
1271
+ * @param {Function} fn - 回调函数
1272
+ * @returns {string} flag - 唯一标识,用于 off
1273
+ */
1274
+ on(name, fn) {
1275
+ let flag = Date.now() + "_" + parseInt(Math.random() * 1e8);
1276
+ const evtItem = {
1277
+ flag,
1278
+ fn
1279
+ };
1280
+ if (this.evtPool.has(name)) {
1281
+ this.evtPool.get(name).push(evtItem);
1282
+ } else {
1283
+ this.evtPool.set(name, [evtItem]);
1284
+ }
1285
+ return flag;
1286
+ }
1287
+
1288
+ /**
1289
+ * 注册一次性监听器,触发后自动移除。
1290
+ * @param {string} name - 事件名
1291
+ * @param {Function} fn - 回调函数
1292
+ * @returns {string} flag - 唯一标识
1293
+ */
1294
+ once(name, fn) {
1295
+ const _this = this;
1296
+ let wrapper;
1297
+ wrapper = function (data) {
1298
+ _this.off(name, wrapper);
1299
+ fn.call(this, data);
1300
+ };
1301
+ return this.on(name, wrapper);
1302
+ }
1303
+
1304
+ /**
1305
+ * 移除指定事件监听器。
1306
+ * @param {string} name - 事件名
1307
+ * @param {Function|string} fnOrFlag - 回调函数或 flag
1308
+ */
1309
+ off(name, fnOrFlag) {
1310
+ if (!this.evtPool.has(name)) return;
1311
+ const evtItems = this.evtPool.get(name);
1312
+ const filtered = evtItems.filter((item) => item.fn !== fnOrFlag && item.flag !== fnOrFlag);
1313
+ if (filtered.length === 0) {
1314
+ this.evtPool.delete(name);
1315
+ } else {
1316
+ this.evtPool.set(name, filtered);
1317
+ }
1318
+ }
1319
+
1320
+ /**
1321
+ * 触发事件(同步执行)。
1322
+ * @param {string} name - 事件名
1323
+ * @param {*} [data] - 任意载荷
1324
+ * @param {*} [fnThis] - 回调内部 this 指向,默认 undefined
1325
+ */
1326
+ emit(name, data, fnThis) {
1327
+ if (!this.evtPool.has(name)) return;
1328
+ const evtItems = this.evtPool.get(name);
1329
+ evtItems.forEach((item) => {
1330
+ try {
1331
+ item.fn.call(fnThis, data);
1332
+ } catch (err) {
1333
+ console.error(`Error in event listener for "${name}":`, err);
1334
+ }
1335
+ });
1336
+ }
1337
+ }
1338
+
1339
+ /**
1340
+ * 跨页通信插件:通过 localStorage + storage 事件将当前实例的 emit 广播到其他同源页面。
1341
+ * 支持节流、命名空间隔离。
1342
+ */
1343
+ const MyEvent_CrossPagePlugin = (() => {
1344
+ const INSTALLED = new WeakSet(); // 防止重复安装
1345
+
1346
+ return {
1347
+ /**
1348
+ * 为指定 MyEvent 实例安装跨页插件。
1349
+ * @param {MyEvent} bus - 事件总线实例
1350
+ * @param {Options} [opts] - 配置项
1351
+ */
1352
+ install(bus, opts = {}) {
1353
+ if (INSTALLED.has(bus)) return;
1354
+ INSTALLED.add(bus);
1355
+
1356
+ const ns = `___my-event-cross-page-${opts.namespace || "default"}___`;
1357
+ const delay = opts.throttle || 16;
1358
+ let last = 0;
1359
+
1360
+ // 1、重写 emit
1361
+ const rawEmit = bus.emit;
1362
+ bus.emit = function (name, data, fnThis) {
1363
+ rawEmit.call(bus, name, data, fnThis); // 本地先执行
1364
+ const now = Date.now();
1365
+ if (now - last < delay) return;
1366
+ last = now;
1367
+ const key = ns + name;
1368
+ try {
1369
+ localStorage.setItem(key, JSON.stringify({ name, data, ts: now }));
1370
+ localStorage.removeItem(key); // 触发 storage 事件
1371
+ } catch (e) {}
1372
+ };
1373
+
1374
+ // 2、监听其他页广播
1375
+ function onStorageHandler(e) {
1376
+ if (!e.key || !e.key.startsWith(ns)) return;
1377
+ let payload;
1378
+ try {
1379
+ payload = JSON.parse(e.newValue || "{}");
1380
+ } catch {
1381
+ return;
1382
+ }
1383
+ if (!payload.ts || payload.ts <= last) return;
1384
+ rawEmit.call(bus, e.key.slice(ns.length), payload.data); // 仅本地
1385
+ }
1386
+ addEventListener("storage", onStorageHandler);
1387
+
1388
+ // 3、保存卸载器
1389
+ bus._uninstallCrossPage = () => {
1390
+ removeEventListener("storage", onStorageHandler);
1391
+ bus.emit = rawEmit;
1392
+ INSTALLED.delete(bus);
1393
+ };
1394
+ },
1395
+
1396
+ /**
1397
+ * 卸载插件,恢复原始 emit 并停止监听。
1398
+ * @param {MyEvent} bus - 事件总线实例
1399
+ */
1400
+ uninstall(bus) {
1401
+ if (typeof bus._uninstallCrossPage === "function") bus._uninstallCrossPage();
1402
+ }
1403
+ };
1175
1404
  })();
1176
1405
 
1177
- /**
1178
- * 生成 RFC4122 版本 4 的 GUID/UUID。
1179
- * 收集来源:《基于mvc的javascript web富应用开发》 书中介绍是Robert Kieffer写的,还留了网址 http://goo.gl/0b0hu ,但实际访问不了。
1180
- * 格式:`xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx`
1181
- *
1182
- * @returns {string} 36 位大写 GUID
1183
- *
1184
- * @example
1185
- * // A2E0F340-6C3B-4D7F-B8C1-1E4F6A8B9C0D
1186
- * console.log(getGUID())
1187
- */
1188
- function getGUID() {
1189
- return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
1190
- let r = (Math.random() * 16) | 0,
1191
- v = c == "x" ? r : (r & 0x3) | 0x8;
1192
- return v.toString(16).toUpperCase();
1193
- });
1194
- }
1195
-
1196
- /**
1197
- * 分布式短 ID 生成器。
1198
- * 格式:`${timestamp}${flag}${serial}`,其中:
1199
- * - timestamp:毫秒级时间戳
1200
- * - flag:客户端标识串(自定义)
1201
- * - serial:同一毫秒内的序号,左补零到固定长度
1202
- */
1203
- class MyId {
1204
- #ts = Date.now(); // 时间戳
1205
- #sn = 0; // 序号(保证同一客户端之间的唯一项)
1206
- #flag = ""; // 客户端标识(保证不同客户端之间的唯一项)
1207
- #len = 5; // 序号位长度(我的电脑测试,同一时间戳内可以for循环执行了1000次左右,没有一次超过3k,所以5位应该够用了)
1208
- // 测试代码
1209
- // let obj = {[Date.now()]:[]}; try { for (let i = 0; i < 100000; i++) { obj[Date.now()].push(i); } } catch { console.log(obj[Object.getOwnPropertyNames(obj)[0]].length); }
1210
-
1211
- /**
1212
- * @param {object} [option]
1213
- * @param {string} [option.flag] - 客户端标识,默认空串
1214
- * @param {number} [option.len=5] - 序号位长度(位数),安全范围 ≥0
1215
- */
1216
- constructor(option = {}) {
1217
- if (option) {
1218
- if (typeof option.flag === "string") {
1219
- this.#flag = option.flag;
1220
- }
1221
- if (Number.isSafeInteger(option.len) && len >= 0) {
1222
- this.#len = option.len;
1223
- }
1224
- }
1225
- }
1226
-
1227
- /**
1228
- * 生成下一个全局唯一字符串 ID。
1229
- * 同一毫秒序号自动递增;序号溢出时会在控制台警告。
1230
- * @returns {string}
1231
- */
1232
- nextId() {
1233
- let ts = Date.now();
1234
- if (ts === this.#ts) {
1235
- this.#sn++;
1236
- if (this.#sn >= 10 ** this.#len) {
1237
- console.log("长度不够用了!!!");
1238
- }
1239
- } else {
1240
- this.#sn = 0;
1241
- this.#ts = ts;
1242
- }
1243
- return ts.toString() + this.#flag + this.#sn.toString().padStart(this.#len, "0");
1244
- }
1245
- }
1246
-
1247
- /**
1248
- * 基于 `setTimeout` 的“间隔循环”定时器。
1249
- * 每次任务执行完成后才计算下一次间隔,避免任务堆积。
1250
- */
1251
- class IntervalTimer {
1252
- /**
1253
- * 创建定时器实例。
1254
- * @param {() => void} fn - 每次间隔要执行的业务函数
1255
- * @param {number} [ms=1000] - 间隔时间(毫秒)
1256
- * @throws {TypeError} 当 `fn` 不是函数时抛出
1257
- */
1258
- constructor(fn, ms = 1000) {
1259
- if (typeof fn !== "function") {
1260
- throw new TypeError("IntervalTimer: 必须传入一个函数");
1261
- }
1262
- this._fn = fn;
1263
- this._ms = ms;
1264
- this._timerId = null;
1265
- }
1266
-
1267
- /**
1268
- * 启动定时器;若已启动则先停止再重新启动。
1269
- * 首次执行会立即触发。
1270
- */
1271
- start() {
1272
- this.stop();
1273
- const loop = () => {
1274
- this._fn(); // 执行业务
1275
- this._timerId = setTimeout(loop, this._ms);
1276
- };
1277
- loop(); // 立即执行第一次
1278
- }
1279
-
1280
- /**
1281
- * 停止定时器。
1282
- */
1283
- stop() {
1284
- if (this._timerId !== null) {
1285
- clearTimeout(this._timerId);
1286
- this._timerId = null;
1287
- }
1288
- }
1289
-
1290
- /**
1291
- * 查询定时器是否正在运行。
1292
- * @returns {boolean}
1293
- */
1294
- isRunning() {
1295
- return this._timerId !== null;
1296
- }
1297
- }
1298
-
1299
- /**
1300
- * 把嵌套树拍平成 `{ [id]: node }` 映射,同时把原 `children` 置为 `null`。
1301
- *
1302
- * @template T extends Record<PropertyKey, any>
1303
- * @param {T[]} data - 嵌套树森林
1304
- * @param {string} [idKey='id'] - 主键字段
1305
- * @param {string} [childrenKey='children'] - 子节点字段
1306
- * @returns {Record<string, T & { [k in typeof childrenKey]: null }>} id→节点的映射表
1307
- */
1308
- function nestedTree2IdMap(data, idKey = "id", childrenKey = "children") {
1309
- const retObj = {};
1310
- function fn(nodes) {
1311
- if (Array.isArray(nodes) && nodes.length > 0) {
1312
- nodes.forEach((node) => {
1313
- retObj[node[idKey]] = { ...node };
1314
- retObj[node[idKey]][childrenKey] = null;
1315
-
1316
- fn(node[childrenKey]);
1317
- });
1318
- }
1319
- }
1320
- fn(data);
1321
- return retObj;
1406
+ /**
1407
+ * 生成 RFC4122 版本 4 的 GUID/UUID。
1408
+ * 收集来源:《基于mvc的javascript web富应用开发》 书中介绍是Robert Kieffer写的,还留了网址 http://goo.gl/0b0hu ,但实际访问不了。
1409
+ * 格式:`xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx`
1410
+ *
1411
+ * @returns {string} 36 位大写 GUID
1412
+ *
1413
+ * @example
1414
+ * // A2E0F340-6C3B-4D7F-B8C1-1E4F6A8B9C0D
1415
+ * console.log(getGUID())
1416
+ */
1417
+ function getGUID() {
1418
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
1419
+ let r = (Math.random() * 16) | 0,
1420
+ v = c == "x" ? r : (r & 0x3) | 0x8;
1421
+ return v.toString(16).toUpperCase();
1422
+ });
1423
+ }
1424
+
1425
+ /**
1426
+ * 分布式短 ID 生成器。
1427
+ * 格式:`${timestamp}${flag}${serial}`,其中:
1428
+ * - timestamp:毫秒级时间戳
1429
+ * - flag:客户端标识串(自定义)
1430
+ * - serial:同一毫秒内的序号,左补零到固定长度
1431
+ */
1432
+ class MyId {
1433
+ #ts = Date.now(); // 时间戳
1434
+ #sn = 0; // 序号(保证同一客户端之间的唯一项)
1435
+ #flag = ""; // 客户端标识(保证不同客户端之间的唯一项)
1436
+ #len = 5; // 序号位长度(我的电脑测试,同一时间戳内可以for循环执行了1000次左右,没有一次超过3k,所以5位应该够用了)
1437
+ // 测试代码
1438
+ // let obj = {[Date.now()]:[]}; try { for (let i = 0; i < 100000; i++) { obj[Date.now()].push(i); } } catch { console.log(obj[Object.getOwnPropertyNames(obj)[0]].length); }
1439
+
1440
+ /**
1441
+ * @param {object} [option]
1442
+ * @param {string} [option.flag] - 客户端标识,默认空串
1443
+ * @param {number} [option.len=5] - 序号位长度(位数),安全范围 ≥0
1444
+ */
1445
+ constructor(option = {}) {
1446
+ if (option) {
1447
+ if (typeof option.flag === "string") {
1448
+ this.#flag = option.flag;
1449
+ }
1450
+ if (Number.isSafeInteger(option.len) && len >= 0) {
1451
+ this.#len = option.len;
1452
+ }
1453
+ }
1454
+ }
1455
+
1456
+ /**
1457
+ * 生成下一个全局唯一字符串 ID。
1458
+ * 同一毫秒序号自动递增;序号溢出时会在控制台警告。
1459
+ * @returns {string}
1460
+ */
1461
+ nextId() {
1462
+ let ts = Date.now();
1463
+ if (ts === this.#ts) {
1464
+ this.#sn++;
1465
+ if (this.#sn >= 10 ** this.#len) {
1466
+ console.log("长度不够用了!!!");
1467
+ }
1468
+ } else {
1469
+ this.#sn = 0;
1470
+ this.#ts = ts;
1471
+ }
1472
+ return ts.toString() + this.#flag + this.#sn.toString().padStart(this.#len, "0");
1473
+ }
1322
1474
  }
1323
1475
 
1324
- /**
1325
- * 把**已包含完整父子关系**的扁平节点列表还原成嵌套树(森林)。
1326
- *
1327
- * @template T extends Record<PropertyKey, any>
1328
- * @param {T[]} nodes - 扁平节点列表(必须包含 id / parentId)
1329
- * @param {number | string} [parentId=0] - 根节点标识值
1330
- * @param {Object} [opts] - 字段映射配置
1331
- * @param {string} [opts.idKey='id'] - 节点主键
1332
- * @param {string} [opts.parentKey='parentId'] - 父节点外键
1333
- * @param {string} [opts.childrenKey='children'] - 存放子节点的字段
1334
- * @returns {(T & { [k in typeof childrenKey]: T[] })[]} 嵌套树森林
1335
- */
1336
- function flatCompleteTree2NestedTree(nodes, parentId = 0, { idKey = "id", parentKey = "parentId", childrenKey = "children" } = {}) {
1337
- const map = new Map(); // id -> node
1338
- const items = []; // 多根森林
1339
-
1340
- // 1. 初始化:保证每个节点都有 children,并存入 map
1341
- for (const item of nodes) {
1342
- const node = { ...item, [childrenKey]: [] };
1343
- map.set(item[idKey], node);
1344
- }
1345
-
1346
- // 2. 建立父子关系
1347
- for (const item of nodes) {
1348
- const node = map.get(item[idKey]);
1349
- const parentIdVal = item[parentKey];
1350
-
1351
- if (parentIdVal === parentId) {
1352
- // 根层
1353
- items.push(node);
1354
- } else {
1355
- // 非根层:找到父节点,把自己挂上去
1356
- const parent = map.get(parentIdVal);
1357
- if (parent) parent[childrenKey].push(node);
1358
- // 如果 parent 不存在,说明数据不完整,可自定义处理
1359
- }
1360
- }
1361
-
1362
- return items;
1476
+ /**
1477
+ * 基于 `setTimeout` 的“间隔循环”定时器。
1478
+ * 每次任务执行完成后才计算下一次间隔,避免任务堆积。
1479
+ */
1480
+ class IntervalTimer {
1481
+
1482
+ /**
1483
+ * 创建定时器实例。
1484
+ * @param {() => void} fn - 每次间隔要执行的业务函数
1485
+ * @param {number} [ms=1000] - 间隔时间(毫秒)
1486
+ * @throws {TypeError} `fn` 不是函数时抛出
1487
+ */
1488
+ constructor(fn, ms = 1000) {
1489
+ if (typeof fn !== "function") {
1490
+ throw new TypeError("IntervalTimer: 必须传入一个函数");
1491
+ }
1492
+ this._fn = fn;
1493
+ this._ms = ms;
1494
+ this._timerId = null;
1495
+ }
1496
+
1497
+ /**
1498
+ * 启动定时器;若已启动则先停止再重新启动。
1499
+ * 首次执行会立即触发。
1500
+ */
1501
+ start() {
1502
+ this.stop();
1503
+ const loop = () => {
1504
+ this._fn(); // 执行业务
1505
+ this._timerId = setTimeout(loop, this._ms);
1506
+ };
1507
+ loop(); // 立即执行第一次
1508
+ }
1509
+
1510
+ /**
1511
+ * 停止定时器。
1512
+ */
1513
+ stop() {
1514
+ if (this._timerId !== null) {
1515
+ clearTimeout(this._timerId);
1516
+ this._timerId = null;
1517
+ }
1518
+ }
1519
+
1520
+ /**
1521
+ * 查询定时器是否正在运行。
1522
+ * @returns {boolean}
1523
+ */
1524
+ isRunning() {
1525
+ return this._timerId !== null;
1526
+ }
1363
1527
  }
1364
1528
 
1365
- /**
1366
- * 在嵌套树中按 `id` 递归查找节点,并返回其指定属性值。
1367
- *
1368
- * @template T extends Record<PropertyKey, any>
1369
- * @param {string | number} id - 要查找的 id
1370
- * @param {T[]} arr - 嵌套树森林
1371
- * @param {string} [resultKey='name'] - 需要返回的字段
1372
- * @param {string} [idKey='id'] - 主键字段
1373
- * @param {string} [childrenKey='children'] - 子节点字段
1374
- * @returns {any} 找到的值;未找到返回 `undefined`
1375
- */
1376
- const findObjAttrValueById = function findObjAttrValueByIdFn(id, arr, resultKey = "name", idKey = "id", childrenKey = "children") {
1377
- if (Array.isArray(arr) && arr.length > 0) {
1378
- for (let i = 0; i < arr.length; i++) {
1379
- const item = arr[i];
1380
- if (item[idKey]?.toString() === id?.toString()) {
1381
- return item[resultKey];
1382
- } else if (Array.isArray(item[childrenKey]) && item[childrenKey].length > 0) {
1383
- const result = findObjAttrValueByIdFn(id, item[childrenKey], resultKey, idKey, childrenKey);
1384
- if (result) {
1385
- return result;
1386
- }
1387
- }
1388
- }
1389
- }
1390
- };
1391
-
1392
- /**
1393
- * 从服务端返回的已选 id 数组里,提取出
1394
- * 1. 叶子节点
1395
- * 2. 所有子节点都被选中的父节点
1396
- * 其余父节点一律丢弃(由前端 Tree 自动算半选)
1397
- *
1398
- * @param {Array} treeData 完整树
1399
- * @param {Array} selectedKeys 后端给的选中 id 数组
1400
- * @param {String} idKey 节点唯一字段
1401
- * @param {String} childrenKey 子节点字段
1402
- * @returns {{checked: string[], halfChecked: string[]}}
1403
- */
1404
- function extractFullyCheckedKeys(treeData, selectedKeys, idKey = "id", childrenKey = "children") {
1405
- const selectedSet = new Set(selectedKeys);
1406
- const checked = new Set();
1407
- const halfChecked = new Set();
1408
-
1409
- /* 返回值含义
1410
- 0 - 未选中
1411
- 1 - 半选
1412
- 2 - 全选
1413
- */
1414
- function dfs(node) {
1415
- const nodeId = node[idKey];
1416
- const children = node[childrenKey] || [];
1417
-
1418
- // 叶子
1419
- if (!children.length) {
1420
- if (selectedSet.has(nodeId)) {
1421
- checked.add(nodeId);
1422
- return 2;
1423
- }
1424
- return 0;
1425
- }
1426
-
1427
- // 非叶子
1428
- let allChecked = true;
1429
- let someChecked = false;
1430
-
1431
- children.forEach((child) => {
1432
- const childState = dfs(child);
1433
- if (childState !== 2) allChecked = false;
1434
- if (childState >= 1) someChecked = true;
1435
- });
1436
-
1437
- // 当前节点本身在 selectedKeys 里,但子节点未全选 只能算半选
1438
- if (selectedSet.has(nodeId)) {
1439
- if (allChecked) {
1440
- checked.add(nodeId);
1441
- return 2;
1442
- }
1443
- halfChecked.add(nodeId);
1444
- return 1;
1445
- }
1446
-
1447
- // 当前节点不在 selectedKeys 里,看子节点
1448
- if (allChecked) {
1449
- checked.add(nodeId);
1450
- return 2;
1451
- }
1452
- if (someChecked) {
1453
- halfChecked.add(nodeId);
1454
- return 1;
1455
- }
1456
- return 0;
1457
- }
1458
-
1459
- treeData.forEach(dfs);
1460
- return {
1461
- checked: [...checked],
1462
- halfChecked: [...halfChecked]
1463
- };
1529
+ /**
1530
+ * 把嵌套树拍平成 `{ [id]: node }` 映射,同时把原 `children` 置为 `null`。
1531
+ *
1532
+ * @template T extends Record<PropertyKey, any>
1533
+ * @param {T[]} data - 嵌套树森林
1534
+ * @param {string} [idKey='id'] - 主键字段
1535
+ * @param {string} [childrenKey='children'] - 子节点字段
1536
+ * @returns {Record<string, T & { [k in typeof childrenKey]: null }>} id→节点的映射表
1537
+ */
1538
+ function nestedTree2IdMap(data, idKey = "id", childrenKey = "children") {
1539
+ const retObj = {};
1540
+ function fn(nodes) {
1541
+ if (Array.isArray(nodes) && nodes.length > 0) {
1542
+ nodes.forEach((node) => {
1543
+ retObj[node[idKey]] = { ...node };
1544
+ retObj[node[idKey]][childrenKey] = null;
1545
+
1546
+ fn(node[childrenKey]);
1547
+ });
1548
+ }
1549
+ }
1550
+ fn(data);
1551
+ return retObj;
1552
+ }
1553
+
1554
+ /**
1555
+ * 把**已包含完整父子关系**的扁平节点列表还原成嵌套树(森林)。
1556
+ *
1557
+ * @template T extends Record<PropertyKey, any>
1558
+ * @param {T[]} nodes - 扁平节点列表(必须包含 id / parentId)
1559
+ * @param {number | string} [parentId=0] - 根节点标识值
1560
+ * @param {Object} [opts] - 字段映射配置
1561
+ * @param {string} [opts.idKey='id'] - 节点主键
1562
+ * @param {string} [opts.parentKey='parentId'] - 父节点外键
1563
+ * @param {string} [opts.childrenKey='children'] - 存放子节点的字段
1564
+ * @returns {(T & { [k in typeof childrenKey]: T[] })[]} 嵌套树森林
1565
+ */
1566
+ function flatCompleteTree2NestedTree(nodes, parentId = 0, { idKey = "id", parentKey = "parentId", childrenKey = "children" } = {}) {
1567
+ const map = new Map(); // id -> node
1568
+ const items = []; // 多根森林
1569
+
1570
+ // 1. 初始化:保证每个节点都有 children,并存入 map
1571
+ for (const item of nodes) {
1572
+ const node = { ...item, [childrenKey]: [] };
1573
+ map.set(item[idKey], node);
1574
+ }
1575
+
1576
+ // 2. 建立父子关系
1577
+ for (const item of nodes) {
1578
+ const node = map.get(item[idKey]);
1579
+ const parentIdVal = item[parentKey];
1580
+
1581
+ if (parentIdVal === parentId) {
1582
+ // 根层
1583
+ items.push(node);
1584
+ } else {
1585
+ // 非根层:找到父节点,把自己挂上去
1586
+ const parent = map.get(parentIdVal);
1587
+ if (parent) parent[childrenKey].push(node);
1588
+ // 如果 parent 不存在,说明数据不完整,可自定义处理
1589
+ }
1590
+ }
1591
+
1592
+ return items;
1593
+ }
1594
+
1595
+ /**
1596
+ * 在嵌套树中按 `id` 递归查找节点,并返回其指定属性值。
1597
+ *
1598
+ * @template T extends Record<PropertyKey, any>
1599
+ * @param {string | number} id - 要查找的 id
1600
+ * @param {T[]} arr - 嵌套树森林
1601
+ * @param {string} [resultKey='name'] - 需要返回的字段
1602
+ * @param {string} [idKey='id'] - 主键字段
1603
+ * @param {string} [childrenKey='children'] - 子节点字段
1604
+ * @returns {any} 找到的值;未找到返回 `undefined`
1605
+ */
1606
+ const findObjAttrValueById = function findObjAttrValueByIdFn(id, arr, resultKey = "name", idKey = "id", childrenKey = "children") {
1607
+ if (Array.isArray(arr) && arr.length > 0) {
1608
+ for (let i = 0; i < arr.length; i++) {
1609
+ const item = arr[i];
1610
+ if (item[idKey]?.toString() === id?.toString()) {
1611
+ return item[resultKey];
1612
+ } else if (Array.isArray(item[childrenKey]) && item[childrenKey].length > 0) {
1613
+ const result = findObjAttrValueByIdFn(id, item[childrenKey], resultKey, idKey, childrenKey);
1614
+ if (result) {
1615
+ return result;
1616
+ }
1617
+ }
1618
+ }
1619
+ }
1620
+ };
1621
+
1622
+ /**
1623
+ * 从服务端返回的已选 id 数组里,提取出
1624
+ * 1. 叶子节点
1625
+ * 2. 所有子节点都被选中的父节点
1626
+ * 其余父节点一律丢弃(由前端 Tree 自动算半选)
1627
+ *
1628
+ * @param {Array} treeData 完整树
1629
+ * @param {Array} selectedKeys 后端给的选中 id 数组
1630
+ * @param {String} idKey 节点唯一字段
1631
+ * @param {String} childrenKey 子节点字段
1632
+ * @returns {{checked: string[], halfChecked: string[]}}
1633
+ */
1634
+ function extractFullyCheckedKeys(treeData, selectedKeys, idKey = "id", childrenKey = "children") {
1635
+ const selectedSet = new Set(selectedKeys);
1636
+ const checked = new Set();
1637
+ const halfChecked = new Set();
1638
+
1639
+ /* 返回值含义
1640
+ 0 - 未选中
1641
+ 1 - 半选
1642
+ 2 - 全选
1643
+ */
1644
+ function dfs(node) {
1645
+ const nodeId = node[idKey];
1646
+ const children = node[childrenKey] || [];
1647
+
1648
+ // 叶子
1649
+ if (!children.length) {
1650
+ if (selectedSet.has(nodeId)) {
1651
+ checked.add(nodeId);
1652
+ return 2;
1653
+ }
1654
+ return 0;
1655
+ }
1656
+
1657
+ // 非叶子
1658
+ let allChecked = true;
1659
+ let someChecked = false;
1660
+
1661
+ children.forEach((child) => {
1662
+ const childState = dfs(child);
1663
+ if (childState !== 2) allChecked = false;
1664
+ if (childState >= 1) someChecked = true;
1665
+ });
1666
+
1667
+ // 当前节点本身在 selectedKeys 里,但子节点未全选 → 只能算半选
1668
+ if (selectedSet.has(nodeId)) {
1669
+ if (allChecked) {
1670
+ checked.add(nodeId);
1671
+ return 2;
1672
+ }
1673
+ halfChecked.add(nodeId);
1674
+ return 1;
1675
+ }
1676
+
1677
+ // 当前节点不在 selectedKeys 里,看子节点
1678
+ if (allChecked) {
1679
+ checked.add(nodeId);
1680
+ return 2;
1681
+ }
1682
+ if (someChecked) {
1683
+ halfChecked.add(nodeId);
1684
+ return 1;
1685
+ }
1686
+ return 0;
1687
+ }
1688
+
1689
+ treeData.forEach(dfs);
1690
+ return {
1691
+ checked: [...checked],
1692
+ halfChecked: [...halfChecked]
1693
+ };
1464
1694
  }
1465
1695
 
1466
- /**
1467
- * @class WebSocketManager - 一个纯粹、强大的 WebSocket 连接管理引擎
1468
- *
1469
- * @features
1470
- * - 智能断线重连 (指数退避算法)
1471
- * - 网络状态监听
1472
- * - 高度可定制的心跳保活机制 (通过注入函数实现)
1473
- * - 页面可见性 API 集成
1474
- * - 消息发送队列
1475
- * - 清晰的生命周期管理
1476
- * - 优雅的资源销毁
1477
- * - 可配置的数据序列化/反序列化
1478
- * - 纯粹的消息传递,不关心消息内容
1479
- */
1480
- class WebSocketManager {
1481
- /**
1482
- * @param {string} url - WebSocket 服务器的地址
1483
- * @param {object} [options={}] - 配置选项
1484
- * @param {number} [options.heartbeatInterval=30000] - 心跳间隔 (毫秒)
1485
- * @param {number} [options.heartbeatTimeout=10000] - 心跳超时时间 (毫秒)
1486
- * @param {number} [options.reconnectBaseInterval=1000] - 重连基础间隔 (毫秒)
1487
- * @param {number} [options.maxReconnectInterval=30000] - 最大重连间隔 (毫秒)
1488
- * @param {number} [options.maxReconnectAttempts=Infinity] - 最大重连次数
1489
- * @param {boolean} [options.autoConnect=true] - 是否在实例化后自动连接
1490
- * @param {boolean} [options.serializeData=false] - 发送数据时是否自动序列化为JSON字符串
1491
- * @param {boolean} [options.deserializeData=false] - 接收数据时是否自动反序列化为JSON对象
1492
- * @param {function|null} [options.getPingMessage=null] - 返回要发送的 ping 消息内容的函数。如果为 null,则不发送心跳。
1493
- * @param {function|null} [options.isPongMessage=null] - 判断接收到的消息是否为 pong 的函数。接收 MessageEvent 对象作为参数,返回布尔值。
1494
- * @param {object} [options.protocols] - WebSocket 协议
1495
- */
1496
- constructor(url, options = {}) {
1497
- this.url = url;
1498
-
1499
- // 默认的心跳实现,用于向后兼容
1500
- const defaultGetPingMessage = () => JSON.stringify({ type: "ping" });
1501
- const defaultIsPongMessage = (event) => {
1502
- try {
1503
- const message = JSON.parse(event.data);
1504
- return message.type === "pong";
1505
- } catch (e) {
1506
- return false;
1507
- }
1508
- };
1509
-
1510
- this.options = {
1511
- heartbeatInterval: 30000,
1512
- heartbeatTimeout: 10000,
1513
- reconnectBaseInterval: 1000,
1514
- maxReconnectInterval: 30000,
1515
- maxReconnectAttempts: Infinity,
1516
- autoConnect: true,
1517
- serializeData: false,
1518
- deserializeData: false,
1519
- getPingMessage: defaultGetPingMessage, // 默认提供 ping 消息生成器
1520
- isPongMessage: defaultIsPongMessage, // 默认提供 pong 消息判断器
1521
- ...options
1522
- };
1523
-
1524
- // WebSocket 实例
1525
- this.ws = null;
1526
-
1527
- // 状态管理
1528
- this.readyState = WebSocket.CLOSED;
1529
- this.reconnectAttempts = 0;
1530
- this.forcedClose = false;
1531
- this.isReconnecting = false;
1532
-
1533
- // 定时器
1534
- this.heartbeatTimer = null;
1535
- this.heartbeatTimeoutTimer = null;
1536
- this.reconnectTimer = null;
1537
-
1538
- // 消息队列
1539
- this.messageQueue = [];
1540
-
1541
- // 事件监听器
1542
- this.listeners = new Map();
1543
-
1544
- // 绑定方法上下文
1545
- this._onOpen = this._onOpen.bind(this);
1546
- this._onMessage = this._onMessage.bind(this);
1547
- this._onClose = this._onClose.bind(this);
1548
- this._onError = this._onError.bind(this);
1549
- this._handleVisibilityChange = this._handleVisibilityChange.bind(this);
1550
- this._handleOnline = this._handleOnline.bind(this);
1551
- this._handleOffline = this._handleOffline.bind(this);
1552
-
1553
- this._setupEventListeners();
1554
-
1555
- if (this.options.autoConnect) {
1556
- this.connect();
1557
- }
1558
- }
1559
-
1560
- // --- 公共 API ---
1561
-
1562
- /**
1563
- * 连接到 WebSocket 服务器
1564
- */
1565
- connect() {
1566
- if (this.ws && (this.ws.readyState === WebSocket.CONNECTING || this.ws.readyState === WebSocket.OPEN)) {
1567
- return;
1568
- }
1569
-
1570
- this.forcedClose = false;
1571
- this._updateReadyState(WebSocket.CONNECTING);
1572
- console.log(`[WS] 正在连接到 ${this.url}...`);
1573
- this._emit("connecting");
1574
-
1575
- try {
1576
- this.ws = new WebSocket(this.url, this.options.protocols);
1577
- this.ws.onopen = this._onOpen;
1578
- this.ws.onmessage = this._onMessage;
1579
- this.ws.onclose = this._onClose;
1580
- this.ws.onerror = this._onError;
1581
- } catch (error) {
1582
- console.error("[WS] 连接失败:", error);
1583
- this._onError(error);
1584
- }
1585
- }
1586
-
1587
- /**
1588
- * 发送数据
1589
- * @param {string|object|ArrayBuffer|Blob} data - 要发送的数据
1590
- */
1591
- send(data) {
1592
- if (this.readyState === WebSocket.OPEN) {
1593
- let message;
1594
- // 根据配置决定是否序列化数据
1595
- if (this.options.serializeData) {
1596
- message = JSON.stringify(data);
1597
- } else {
1598
- message = data;
1599
- }
1600
- this.ws.send(message);
1601
- console.log("[WS] 消息已发送:", message);
1602
- } else {
1603
- console.warn("[WS] 连接未打开,消息已加入队列:", data);
1604
- this.messageQueue.push(data);
1605
- }
1606
- }
1607
-
1608
- /**
1609
- * 主动关闭连接
1610
- * @param {number} [code=1000] - 关闭代码
1611
- * @param {string} [reason=''] - 关闭原因
1612
- */
1613
- close(code = 1000, reason = "Normal closure") {
1614
- this.forcedClose = true;
1615
- this._stopHeartbeat();
1616
- this._clearReconnectTimer();
1617
- if (this.ws && this.ws.readyState === WebSocket.OPEN) {
1618
- this.ws.close(code, reason);
1619
- } else {
1620
- this._updateReadyState(WebSocket.CLOSED);
1621
- this._emit("close", { code, reason, wasClean: true });
1622
- }
1623
- }
1624
-
1625
- /**
1626
- * 彻底销毁实例,清理所有资源
1627
- */
1628
- destroy() {
1629
- console.log("[WS] 正在销毁实例...");
1630
- this.close(1000, "Instance destroyed");
1631
- this._removeEventListeners();
1632
- this.messageQueue = [];
1633
- this.listeners.clear();
1634
- this.ws = null;
1635
- }
1636
-
1637
- /**
1638
- * 添加事件监听器
1639
- * @param {string} eventName - 事件名 (e.g., 'open', 'message', 'close', 'error', 'reconnect')
1640
- * @param {function} callback - 回调函数
1641
- */
1642
- on(eventName, callback) {
1643
- if (!this.listeners.has(eventName)) {
1644
- this.listeners.set(eventName, []);
1645
- }
1646
- this.listeners.get(eventName).push(callback);
1647
- }
1648
-
1649
- /**
1650
- * 移除事件监听器
1651
- * @param {string} eventName - 事件名
1652
- * @param {function} callback - 回调函数
1653
- */
1654
- off(eventName, callback) {
1655
- if (this.listeners.has(eventName)) {
1656
- const callbacks = this.listeners.get(eventName);
1657
- const index = callbacks.indexOf(callback);
1658
- if (index > -1) {
1659
- callbacks.splice(index, 1);
1660
- }
1661
- }
1662
- }
1663
-
1664
- // --- 内部方法 ---
1665
-
1666
- _onOpen(event) {
1667
- console.log("[WS] 连接已建立");
1668
- this._updateReadyState(WebSocket.OPEN);
1669
- this.reconnectAttempts = 0;
1670
- this.isReconnecting = false;
1671
-
1672
- // 如果配置了 getPingMessage,则启动心跳
1673
- if (this.options.getPingMessage) {
1674
- this._startHeartbeat();
1675
- }
1676
-
1677
- this._flushMessageQueue();
1678
- this._emit("open", event);
1679
- }
1680
-
1681
- /**
1682
- * 纯粹的消息处理方法:处理心跳,然后将数据交给使用者
1683
- * @param {MessageEvent} event
1684
- */
1685
- _onMessage(event) {
1686
- // --- 步骤 1: 使用注入的函数优先处理内部心跳机制 ---
1687
- if (this.options.isPongMessage && this.options.isPongMessage(event)) {
1688
- this._handlePong();
1689
- return; // 是心跳消息,处理完毕,直接返回
1690
- }
1691
-
1692
- // --- 步骤 2: 根据用户配置处理业务消息 ---
1693
- if (this.options.deserializeData) {
1694
- try {
1695
- const parsedMessage = JSON.parse(event.data);
1696
- this._emit("message", parsedMessage, event);
1697
- } catch (e) {
1698
- this._emit("message", event.data, event);
1699
- }
1700
- } else {
1701
- this._emit("message", event.data, event);
1702
- }
1703
- }
1704
-
1705
- _onClose(event) {
1706
- console.log("[WS] 连接已关闭", event);
1707
- this._updateReadyState(WebSocket.CLOSED);
1708
- this._stopHeartbeat();
1709
- this._emit("close", event);
1710
-
1711
- if (!this.forcedClose) {
1712
- this._scheduleReconnect();
1713
- }
1714
- }
1715
-
1716
- _onError(event) {
1717
- console.error("[WS] 连接发生错误:", event);
1718
- this._emit("error", event);
1719
- }
1720
-
1721
- // --- 心跳机制 ---
1722
- _startHeartbeat() {
1723
- // 如果没有配置 getPingMessage,则无法启动心跳
1724
- if (!this.options.getPingMessage) {
1725
- return;
1726
- }
1727
-
1728
- this._stopHeartbeat();
1729
- this.heartbeatTimer = setInterval(() => {
1730
- if (this.ws && this.ws.readyState === WebSocket.OPEN) {
1731
- try {
1732
- // 调用注入的函数获取消息内容并发送
1733
- const pingMessage = this.options.getPingMessage();
1734
- this.ws.send(pingMessage);
1735
- console.log("[WS] 发送 Ping:", pingMessage);
1736
- this._setHeartbeatTimeout();
1737
- } catch (error) {
1738
- console.error("[WS] 发送 Ping 消息失败:", error);
1739
- }
1740
- }
1741
- }, this.options.heartbeatInterval);
1742
- }
1743
-
1744
- _stopHeartbeat() {
1745
- if (this.heartbeatTimer) {
1746
- clearInterval(this.heartbeatTimer);
1747
- this.heartbeatTimer = null;
1748
- }
1749
- this._clearHeartbeatTimeout();
1750
- }
1751
-
1752
- _setHeartbeatTimeout() {
1753
- this._clearHeartbeatTimeout();
1754
- this.heartbeatTimeoutTimer = setTimeout(() => {
1755
- console.error("[WS] 心跳超时,主动断开连接");
1756
- this.ws.close(1006, "Heartbeat timeout");
1757
- }, this.options.heartbeatTimeout);
1758
- }
1759
-
1760
- _clearHeartbeatTimeout() {
1761
- if (this.heartbeatTimeoutTimer) {
1762
- clearTimeout(this.heartbeatTimeoutTimer);
1763
- this.heartbeatTimeoutTimer = null;
1764
- }
1765
- }
1766
-
1767
- _handlePong() {
1768
- console.log("[WS] 收到 Pong");
1769
- this._clearHeartbeatTimeout();
1770
- }
1771
-
1772
- // --- 重连机制 ---
1773
- _scheduleReconnect() {
1774
- if (this.forcedClose || this.isReconnecting || this.reconnectAttempts >= this.options.maxReconnectAttempts) {
1775
- if (this.reconnectAttempts >= this.options.maxReconnectAttempts) {
1776
- console.error("[WS] 已达到最大重连次数,停止重连");
1777
- this._emit("reconnect-failed");
1778
- }
1779
- return;
1780
- }
1781
-
1782
- this.isReconnecting = true;
1783
- const interval = Math.min(this.options.reconnectBaseInterval * Math.pow(2, this.reconnectAttempts), this.options.maxReconnectInterval);
1784
-
1785
- console.log(`[WS] ${interval / 1000}秒后将尝试第 ${this.reconnectAttempts + 1} 次重连...`);
1786
- this._emit("reconnect-attempt", { attempt: this.reconnectAttempts + 1, interval });
1787
-
1788
- this.reconnectTimer = setTimeout(() => {
1789
- this.reconnectAttempts++;
1790
- this.connect();
1791
- }, interval);
1792
- }
1793
-
1794
- _clearReconnectTimer() {
1795
- if (this.reconnectTimer) {
1796
- clearTimeout(this.reconnectTimer);
1797
- this.reconnectTimer = null;
1798
- }
1799
- }
1800
-
1801
- // --- 消息队列 ---
1802
- _flushMessageQueue() {
1803
- if (this.messageQueue.length === 0) return;
1804
- console.log(`[WS] 发送队列中的 ${this.messageQueue.length} 条消息`);
1805
- const queue = [...this.messageQueue];
1806
- this.messageQueue = [];
1807
- queue.forEach((data) => this.send(data));
1808
- }
1809
-
1810
- // --- 事件系统 ---
1811
- _emit(eventName, ...args) {
1812
- if (this.listeners.has(eventName)) {
1813
- this.listeners.get(eventName).forEach((callback) => callback(...args));
1814
- }
1815
- }
1816
-
1817
- // --- 状态与监听器管理 ---
1818
- _updateReadyState(newState) {
1819
- this.readyState = newState;
1820
- this._emit("ready-state-change", newState);
1821
- }
1822
-
1823
- _setupEventListeners() {
1824
- document.addEventListener("visibilitychange", this._handleVisibilityChange);
1825
- window.addEventListener("online", this._handleOnline);
1826
- window.addEventListener("offline", this._handleOffline);
1827
- }
1828
-
1829
- _removeEventListeners() {
1830
- document.removeEventListener("visibilitychange", this._handleVisibilityChange);
1831
- window.removeEventListener("online", this._handleOnline);
1832
- window.removeEventListener("offline", this._handleOffline);
1833
- }
1834
-
1835
- _handleVisibilityChange() {
1836
- // 如果未启用心跳,不需要处理页面可见性变化
1837
- if (!this.options.getPingMessage) {
1838
- return;
1839
- }
1840
-
1841
- if (document.hidden) {
1842
- console.log("[WS] 页面隐藏,停止心跳");
1843
- this._stopHeartbeat();
1844
- } else {
1845
- console.log("[WS] 页面可见,检查连接状态");
1846
- if (this.ws && this.ws.readyState === WebSocket.OPEN) {
1847
- this._startHeartbeat();
1848
- } else if (!this.forcedClose && !this.isReconnecting) {
1849
- this.connect();
1850
- }
1851
- }
1852
- }
1853
-
1854
- _handleOnline() {
1855
- console.log("[WS] 网络已恢复,尝试重连");
1856
- if (!this.forcedClose && this.readyState !== WebSocket.OPEN) {
1857
- this._clearReconnectTimer(); // 清除当前的重连计划
1858
- this.connect(); // 立即尝试连接
1859
- }
1860
- }
1861
-
1862
- _handleOffline() {
1863
- console.log("[WS] 网络已断开");
1864
- this._clearReconnectTimer(); // 停止重连尝试
1865
- // ws.onclose 会被触发,从而启动重连逻辑,但我们已经停止了
1866
- // 所以这里可以手动触发一次 close 事件来通知应用层
1867
- if (this.ws) this.ws.onclose({ code: 1006, reason: "Network offline" });
1868
- }
1696
+ /**
1697
+ * @class WebSocketManager - 一个纯粹、强大的 WebSocket 连接管理引擎
1698
+ *
1699
+ * @features
1700
+ * - 智能断线重连 (指数退避算法)
1701
+ * - 网络状态监听
1702
+ * - 高度可定制的心跳保活机制 (通过注入函数实现)
1703
+ * - 页面可见性 API 集成
1704
+ * - 消息发送队列
1705
+ * - 清晰的生命周期管理
1706
+ * - 优雅的资源销毁
1707
+ * - 可配置的数据序列化/反序列化
1708
+ * - 纯粹的消息传递,不关心消息内容
1709
+ */
1710
+ class WebSocketManager {
1711
+ /**
1712
+ * @param {string} url - WebSocket 服务器的地址
1713
+ * @param {object} [options={}] - 配置选项
1714
+ * @param {number} [options.heartbeatInterval=30000] - 心跳间隔 (毫秒)
1715
+ * @param {number} [options.heartbeatTimeout=10000] - 心跳超时时间 (毫秒)
1716
+ * @param {number} [options.reconnectBaseInterval=1000] - 重连基础间隔 (毫秒)
1717
+ * @param {number} [options.maxReconnectInterval=30000] - 最大重连间隔 (毫秒)
1718
+ * @param {number} [options.maxReconnectAttempts=Infinity] - 最大重连次数
1719
+ * @param {boolean} [options.autoConnect=true] - 是否在实例化后自动连接
1720
+ * @param {boolean} [options.serializeData=false] - 发送数据时是否自动序列化为JSON字符串
1721
+ * @param {boolean} [options.deserializeData=false] - 接收数据时是否自动反序列化为JSON对象
1722
+ * @param {function|null} [options.getPingMessage=null] - 返回要发送的 ping 消息内容的函数。如果为 null,则不发送心跳。
1723
+ * @param {function|null} [options.isPongMessage=null] - 判断接收到的消息是否为 pong 的函数。接收 MessageEvent 对象作为参数,返回布尔值。
1724
+ * @param {object} [options.protocols] - WebSocket 协议
1725
+ */
1726
+ constructor(url, options = {}) {
1727
+ this.url = url;
1728
+
1729
+ // 默认的心跳实现,用于向后兼容
1730
+ const defaultGetPingMessage = () => JSON.stringify({ type: "ping" });
1731
+ const defaultIsPongMessage = (event) => {
1732
+ try {
1733
+ const message = JSON.parse(event.data);
1734
+ return message.type === "pong";
1735
+ } catch (e) {
1736
+ return false;
1737
+ }
1738
+ };
1739
+
1740
+ this.options = {
1741
+ heartbeatInterval: 30000,
1742
+ heartbeatTimeout: 10000,
1743
+ reconnectBaseInterval: 1000,
1744
+ maxReconnectInterval: 30000,
1745
+ maxReconnectAttempts: Infinity,
1746
+ autoConnect: true,
1747
+ serializeData: false,
1748
+ deserializeData: false,
1749
+ getPingMessage: defaultGetPingMessage, // 默认提供 ping 消息生成器
1750
+ isPongMessage: defaultIsPongMessage, // 默认提供 pong 消息判断器
1751
+ ...options
1752
+ };
1753
+
1754
+ // WebSocket 实例
1755
+ this.ws = null;
1756
+
1757
+ // 状态管理
1758
+ this.readyState = WebSocket.CLOSED;
1759
+ this.reconnectAttempts = 0;
1760
+ this.forcedClose = false;
1761
+ this.isReconnecting = false;
1762
+
1763
+ // 定时器
1764
+ this.heartbeatTimer = null;
1765
+ this.heartbeatTimeoutTimer = null;
1766
+ this.reconnectTimer = null;
1767
+
1768
+ // 消息队列
1769
+ this.messageQueue = [];
1770
+
1771
+ // 事件监听器
1772
+ this.listeners = new Map();
1773
+
1774
+ // 绑定方法上下文
1775
+ this._onOpen = this._onOpen.bind(this);
1776
+ this._onMessage = this._onMessage.bind(this);
1777
+ this._onClose = this._onClose.bind(this);
1778
+ this._onError = this._onError.bind(this);
1779
+ this._handleVisibilityChange = this._handleVisibilityChange.bind(this);
1780
+ this._handleOnline = this._handleOnline.bind(this);
1781
+ this._handleOffline = this._handleOffline.bind(this);
1782
+
1783
+ this._setupEventListeners();
1784
+
1785
+ if (this.options.autoConnect) {
1786
+ this.connect();
1787
+ }
1788
+ }
1789
+
1790
+ // --- 公共 API ---
1791
+
1792
+ /**
1793
+ * 连接到 WebSocket 服务器
1794
+ */
1795
+ connect() {
1796
+ if (this.ws && (this.ws.readyState === WebSocket.CONNECTING || this.ws.readyState === WebSocket.OPEN)) {
1797
+ return;
1798
+ }
1799
+
1800
+ this.forcedClose = false;
1801
+ this._updateReadyState(WebSocket.CONNECTING);
1802
+ console.log(`[WS] 正在连接到 ${this.url}...`);
1803
+ this._emit("connecting");
1804
+
1805
+ try {
1806
+ this.ws = new WebSocket(this.url, this.options.protocols);
1807
+ this.ws.onopen = this._onOpen;
1808
+ this.ws.onmessage = this._onMessage;
1809
+ this.ws.onclose = this._onClose;
1810
+ this.ws.onerror = this._onError;
1811
+ } catch (error) {
1812
+ console.error("[WS] 连接失败:", error);
1813
+ this._onError(error);
1814
+ }
1815
+ }
1816
+
1817
+ /**
1818
+ * 发送数据
1819
+ * @param {string|object|ArrayBuffer|Blob} data - 要发送的数据
1820
+ */
1821
+ send(data) {
1822
+ if (this.readyState === WebSocket.OPEN) {
1823
+ let message;
1824
+ // 根据配置决定是否序列化数据
1825
+ if (this.options.serializeData) {
1826
+ message = JSON.stringify(data);
1827
+ } else {
1828
+ message = data;
1829
+ }
1830
+ this.ws.send(message);
1831
+ console.log("[WS] 消息已发送:", message);
1832
+ } else {
1833
+ console.warn("[WS] 连接未打开,消息已加入队列:", data);
1834
+ this.messageQueue.push(data);
1835
+ }
1836
+ }
1837
+
1838
+ /**
1839
+ * 主动关闭连接
1840
+ * @param {number} [code=1000] - 关闭代码
1841
+ * @param {string} [reason=''] - 关闭原因
1842
+ */
1843
+ close(code = 1000, reason = "Normal closure") {
1844
+ this.forcedClose = true;
1845
+ this._stopHeartbeat();
1846
+ this._clearReconnectTimer();
1847
+ if (this.ws && this.ws.readyState === WebSocket.OPEN) {
1848
+ this.ws.close(code, reason);
1849
+ } else {
1850
+ this._updateReadyState(WebSocket.CLOSED);
1851
+ this._emit("close", { code, reason, wasClean: true });
1852
+ }
1853
+ }
1854
+
1855
+ /**
1856
+ * 彻底销毁实例,清理所有资源
1857
+ */
1858
+ destroy() {
1859
+ console.log("[WS] 正在销毁实例...");
1860
+ this.close(1000, "Instance destroyed");
1861
+ this._removeEventListeners();
1862
+ this.messageQueue = [];
1863
+ this.listeners.clear();
1864
+ this.ws = null;
1865
+ }
1866
+
1867
+ /**
1868
+ * 添加事件监听器
1869
+ * @param {string} eventName - 事件名 (e.g., 'open', 'message', 'close', 'error', 'reconnect')
1870
+ * @param {function} callback - 回调函数
1871
+ */
1872
+ on(eventName, callback) {
1873
+ if (!this.listeners.has(eventName)) {
1874
+ this.listeners.set(eventName, []);
1875
+ }
1876
+ this.listeners.get(eventName).push(callback);
1877
+ }
1878
+
1879
+ /**
1880
+ * 移除事件监听器
1881
+ * @param {string} eventName - 事件名
1882
+ * @param {function} callback - 回调函数
1883
+ */
1884
+ off(eventName, callback) {
1885
+ if (this.listeners.has(eventName)) {
1886
+ const callbacks = this.listeners.get(eventName);
1887
+ const index = callbacks.indexOf(callback);
1888
+ if (index > -1) {
1889
+ callbacks.splice(index, 1);
1890
+ }
1891
+ }
1892
+ }
1893
+
1894
+ // --- 内部方法 ---
1895
+
1896
+ _onOpen(event) {
1897
+ console.log("[WS] 连接已建立");
1898
+ this._updateReadyState(WebSocket.OPEN);
1899
+ this.reconnectAttempts = 0;
1900
+ this.isReconnecting = false;
1901
+
1902
+ // 如果配置了 getPingMessage,则启动心跳
1903
+ if (this.options.getPingMessage) {
1904
+ this._startHeartbeat();
1905
+ }
1906
+
1907
+ this._flushMessageQueue();
1908
+ this._emit("open", event);
1909
+ }
1910
+
1911
+ /**
1912
+ * 纯粹的消息处理方法:处理心跳,然后将数据交给使用者
1913
+ * @param {MessageEvent} event
1914
+ */
1915
+ _onMessage(event) {
1916
+ // --- 步骤 1: 使用注入的函数优先处理内部心跳机制 ---
1917
+ if (this.options.isPongMessage && this.options.isPongMessage(event)) {
1918
+ this._handlePong();
1919
+ return; // 是心跳消息,处理完毕,直接返回
1920
+ }
1921
+
1922
+ // --- 步骤 2: 根据用户配置处理业务消息 ---
1923
+ if (this.options.deserializeData) {
1924
+ try {
1925
+ const parsedMessage = JSON.parse(event.data);
1926
+ this._emit("message", parsedMessage, event);
1927
+ } catch (e) {
1928
+ this._emit("message", event.data, event);
1929
+ }
1930
+ } else {
1931
+ this._emit("message", event.data, event);
1932
+ }
1933
+ }
1934
+
1935
+ _onClose(event) {
1936
+ console.log("[WS] 连接已关闭", event);
1937
+ this._updateReadyState(WebSocket.CLOSED);
1938
+ this._stopHeartbeat();
1939
+ this._emit("close", event);
1940
+
1941
+ if (!this.forcedClose) {
1942
+ this._scheduleReconnect();
1943
+ }
1944
+ }
1945
+
1946
+ _onError(event) {
1947
+ console.error("[WS] 连接发生错误:", event);
1948
+ this._emit("error", event);
1949
+ }
1950
+
1951
+ // --- 心跳机制 ---
1952
+ _startHeartbeat() {
1953
+ // 如果没有配置 getPingMessage,则无法启动心跳
1954
+ if (!this.options.getPingMessage) {
1955
+ return;
1956
+ }
1957
+
1958
+ this._stopHeartbeat();
1959
+ this.heartbeatTimer = setInterval(() => {
1960
+ if (this.ws && this.ws.readyState === WebSocket.OPEN) {
1961
+ try {
1962
+ // 调用注入的函数获取消息内容并发送
1963
+ const pingMessage = this.options.getPingMessage();
1964
+ this.ws.send(pingMessage);
1965
+ console.log("[WS] 发送 Ping:", pingMessage);
1966
+ this._setHeartbeatTimeout();
1967
+ } catch (error) {
1968
+ console.error("[WS] 发送 Ping 消息失败:", error);
1969
+ }
1970
+ }
1971
+ }, this.options.heartbeatInterval);
1972
+ }
1973
+
1974
+ _stopHeartbeat() {
1975
+ if (this.heartbeatTimer) {
1976
+ clearInterval(this.heartbeatTimer);
1977
+ this.heartbeatTimer = null;
1978
+ }
1979
+ this._clearHeartbeatTimeout();
1980
+ }
1981
+
1982
+ _setHeartbeatTimeout() {
1983
+ this._clearHeartbeatTimeout();
1984
+ this.heartbeatTimeoutTimer = setTimeout(() => {
1985
+ console.error("[WS] 心跳超时,主动断开连接");
1986
+ this.ws.close(1006, "Heartbeat timeout");
1987
+ }, this.options.heartbeatTimeout);
1988
+ }
1989
+
1990
+ _clearHeartbeatTimeout() {
1991
+ if (this.heartbeatTimeoutTimer) {
1992
+ clearTimeout(this.heartbeatTimeoutTimer);
1993
+ this.heartbeatTimeoutTimer = null;
1994
+ }
1995
+ }
1996
+
1997
+ _handlePong() {
1998
+ console.log("[WS] 收到 Pong");
1999
+ this._clearHeartbeatTimeout();
2000
+ }
2001
+
2002
+ // --- 重连机制 ---
2003
+ _scheduleReconnect() {
2004
+ if (this.forcedClose || this.isReconnecting || this.reconnectAttempts >= this.options.maxReconnectAttempts) {
2005
+ if (this.reconnectAttempts >= this.options.maxReconnectAttempts) {
2006
+ console.error("[WS] 已达到最大重连次数,停止重连");
2007
+ this._emit("reconnect-failed");
2008
+ }
2009
+ return;
2010
+ }
2011
+
2012
+ this.isReconnecting = true;
2013
+ const interval = Math.min(this.options.reconnectBaseInterval * Math.pow(2, this.reconnectAttempts), this.options.maxReconnectInterval);
2014
+
2015
+ console.log(`[WS] ${interval / 1000}秒后将尝试第 ${this.reconnectAttempts + 1} 次重连...`);
2016
+ this._emit("reconnect-attempt", { attempt: this.reconnectAttempts + 1, interval });
2017
+
2018
+ this.reconnectTimer = setTimeout(() => {
2019
+ this.reconnectAttempts++;
2020
+ this.connect();
2021
+ }, interval);
2022
+ }
2023
+
2024
+ _clearReconnectTimer() {
2025
+ if (this.reconnectTimer) {
2026
+ clearTimeout(this.reconnectTimer);
2027
+ this.reconnectTimer = null;
2028
+ }
2029
+ }
2030
+
2031
+ // --- 消息队列 ---
2032
+ _flushMessageQueue() {
2033
+ if (this.messageQueue.length === 0) return;
2034
+ console.log(`[WS] 发送队列中的 ${this.messageQueue.length} 条消息`);
2035
+ const queue = [...this.messageQueue];
2036
+ this.messageQueue = [];
2037
+ queue.forEach((data) => this.send(data));
2038
+ }
2039
+
2040
+ // --- 事件系统 ---
2041
+ _emit(eventName, ...args) {
2042
+ if (this.listeners.has(eventName)) {
2043
+ this.listeners.get(eventName).forEach((callback) => callback(...args));
2044
+ }
2045
+ }
2046
+
2047
+ // --- 状态与监听器管理 ---
2048
+ _updateReadyState(newState) {
2049
+ this.readyState = newState;
2050
+ this._emit("ready-state-change", newState);
2051
+ }
2052
+
2053
+ _setupEventListeners() {
2054
+ document.addEventListener("visibilitychange", this._handleVisibilityChange);
2055
+ window.addEventListener("online", this._handleOnline);
2056
+ window.addEventListener("offline", this._handleOffline);
2057
+ }
2058
+
2059
+ _removeEventListeners() {
2060
+ document.removeEventListener("visibilitychange", this._handleVisibilityChange);
2061
+ window.removeEventListener("online", this._handleOnline);
2062
+ window.removeEventListener("offline", this._handleOffline);
2063
+ }
2064
+
2065
+ _handleVisibilityChange() {
2066
+ // 如果未启用心跳,不需要处理页面可见性变化
2067
+ if (!this.options.getPingMessage) {
2068
+ return;
2069
+ }
2070
+
2071
+ if (document.hidden) {
2072
+ console.log("[WS] 页面隐藏,停止心跳");
2073
+ this._stopHeartbeat();
2074
+ } else {
2075
+ console.log("[WS] 页面可见,检查连接状态");
2076
+ if (this.ws && this.ws.readyState === WebSocket.OPEN) {
2077
+ this._startHeartbeat();
2078
+ } else if (!this.forcedClose && !this.isReconnecting) {
2079
+ this.connect();
2080
+ }
2081
+ }
2082
+ }
2083
+
2084
+ _handleOnline() {
2085
+ console.log("[WS] 网络已恢复,尝试重连");
2086
+ if (!this.forcedClose && this.readyState !== WebSocket.OPEN) {
2087
+ this._clearReconnectTimer(); // 清除当前的重连计划
2088
+ this.connect(); // 立即尝试连接
2089
+ }
2090
+ }
2091
+
2092
+ _handleOffline() {
2093
+ console.log("[WS] 网络已断开");
2094
+ this._clearReconnectTimer(); // 停止重连尝试
2095
+ // ws.onclose 会被触发,从而启动重连逻辑,但我们已经停止了
2096
+ // 所以这里可以手动触发一次 close 事件来通知应用层
2097
+ if (this.ws) this.ws.onclose({ code: 1006, reason: "Network offline" });
2098
+ }
1869
2099
  }
1870
2100
 
1871
2101
  exports.AudioStreamResampler = AudioStreamResampler;
@@ -1875,7 +2105,6 @@ registerProcessor('audio-stream-resampler-processor', AudioStreamResamplerProces
1875
2105
  exports.MyId = MyId;
1876
2106
  exports.WebSocketManager = WebSocketManager;
1877
2107
  exports.assignExisting = assignExisting;
1878
- exports.calcTimeDifference = calcTimeDifference;
1879
2108
  exports.debounce = debounce;
1880
2109
  exports.deepCloneByJSON = deepCloneByJSON;
1881
2110
  exports.downloadByBlob = downloadByBlob;
@@ -1887,14 +2116,13 @@ registerProcessor('audio-stream-resampler-processor', AudioStreamResamplerProces
1887
2116
  exports.fetchOrDownloadByUrl = fetchOrDownloadByUrl;
1888
2117
  exports.findObjAttrValueById = findObjAttrValueById;
1889
2118
  exports.flatCompleteTree2NestedTree = flatCompleteTree2NestedTree;
1890
- exports.formatDuration = formatDuration;
1891
- exports.formatDurationMaxDay = formatDurationMaxDay;
1892
- exports.formatDurationMaxHour = formatDurationMaxHour;
2119
+ exports.formatTimeForLocale = formatTimeForLocale;
1893
2120
  exports.getAllSearchParams = getAllSearchParams;
1894
2121
  exports.getDataType = getDataType;
1895
2122
  exports.getFunctionArgNames = getFunctionArgNames;
1896
2123
  exports.getGUID = getGUID;
1897
2124
  exports.getSearchParam = getSearchParam;
2125
+ exports.getTimePeriodName = getTimePeriodName;
1898
2126
  exports.getViewportSize = getViewportSize;
1899
2127
  exports.isBlob = isBlob;
1900
2128
  exports.isDate = isDate;
@@ -1902,6 +2130,9 @@ registerProcessor('audio-stream-resampler-processor', AudioStreamResamplerProces
1902
2130
  exports.isNonEmptyString = isNonEmptyString;
1903
2131
  exports.isPlainObject = isPlainObject;
1904
2132
  exports.isPromise = isPromise;
2133
+ exports.millisecond2Duration = millisecond2Duration;
2134
+ exports.millisecond2DurationMaxDay = millisecond2DurationMaxDay;
2135
+ exports.millisecond2DurationMaxHour = millisecond2DurationMaxHour;
1905
2136
  exports.moveItem = moveItem;
1906
2137
  exports.nestedTree2IdMap = nestedTree2IdMap;
1907
2138
  exports.pcmToWavBlob = pcmToWavBlob;
@@ -1911,6 +2142,9 @@ registerProcessor('audio-stream-resampler-processor', AudioStreamResamplerProces
1911
2142
  exports.randomHanOrEn = randomHanOrEn;
1912
2143
  exports.randomIntInRange = randomIntInRange;
1913
2144
  exports.readBlobAsText = readBlobAsText;
2145
+ exports.second2Duration = second2Duration;
2146
+ exports.second2DurationMaxDay = second2DurationMaxDay;
2147
+ exports.second2DurationMaxHour = second2DurationMaxHour;
1914
2148
  exports.shuffle = shuffle;
1915
2149
  exports.throttle = throttle;
1916
2150
  exports.toDate = toDate;