@whitesev/utils 2.0.0 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js.map +1 -1
- package/package.json +5 -3
- package/src/AjaxHookerType.ts +155 -0
- package/src/ColorConversion.ts +130 -0
- package/src/Dictionary.ts +161 -0
- package/src/Event.ts +189 -0
- package/src/GBKEncoder.ts +121 -0
- package/src/Hooks.ts +87 -0
- package/src/Httpx.ts +2524 -0
- package/src/LockFunction.ts +67 -0
- package/src/Log.ts +285 -0
- package/src/Progress.ts +149 -0
- package/src/TryCatch.ts +107 -0
- package/src/Utils.ts +4937 -0
- package/src/UtilsCommon.ts +20 -0
- package/src/UtilsCore.ts +47 -0
- package/src/UtilsGMCookie.ts +262 -0
- package/src/UtilsGMMenu.ts +532 -0
- package/src/VueObject.ts +128 -0
- package/src/ajaxHooker/ajaxHooker.js +566 -0
- package/src/indexedDB.ts +427 -0
package/src/indexedDB.ts
ADDED
|
@@ -0,0 +1,427 @@
|
|
|
1
|
+
import { UtilsCore } from "./UtilsCore";
|
|
2
|
+
|
|
3
|
+
declare interface UtilsIDBOpenErrorResult {
|
|
4
|
+
code: number;
|
|
5
|
+
msg: string;
|
|
6
|
+
event: Event;
|
|
7
|
+
}
|
|
8
|
+
class indexedDB {
|
|
9
|
+
#dbName: string;
|
|
10
|
+
#storeName: string;
|
|
11
|
+
#dbVersion: number;
|
|
12
|
+
/* websql的版本号,由于ios的问题,版本号的写法不一样 */
|
|
13
|
+
// @ts-ignore
|
|
14
|
+
#slqVersion = "1";
|
|
15
|
+
/* 监听IndexDB */
|
|
16
|
+
#indexedDB =
|
|
17
|
+
UtilsCore.window.indexedDB ||
|
|
18
|
+
(UtilsCore.window as any).mozIndexedDB ||
|
|
19
|
+
(UtilsCore.window as any).webkitIndexedDB ||
|
|
20
|
+
(UtilsCore.window as any).msIndexedDB;
|
|
21
|
+
/* 缓存数据库,避免同一个页面重复创建和销毁 */
|
|
22
|
+
#db: {
|
|
23
|
+
[key: string]: IDBDatabase;
|
|
24
|
+
} = {};
|
|
25
|
+
// @ts-ignore
|
|
26
|
+
#store: IDBObjectStore = null as any;
|
|
27
|
+
#errorCode = {
|
|
28
|
+
/* 错误码 */
|
|
29
|
+
success: {
|
|
30
|
+
code: 200,
|
|
31
|
+
msg: "操作成功",
|
|
32
|
+
},
|
|
33
|
+
error: {
|
|
34
|
+
code: 401,
|
|
35
|
+
msg: "操作失败",
|
|
36
|
+
},
|
|
37
|
+
open: { code: 91001, msg: "打开数据库失败" },
|
|
38
|
+
save: { code: 91002, msg: "保存数据失败" },
|
|
39
|
+
get: { code: 91003, msg: "获取数据失败" },
|
|
40
|
+
delete: { code: 91004, msg: "删除数据失败" },
|
|
41
|
+
deleteAll: { code: 91005, msg: "清空数据库失败" },
|
|
42
|
+
regexpGet: { code: 91006, msg: "正则获取数据失败" },
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* @param dbName 数据存储名,默认为:default_db
|
|
46
|
+
* @param storeName 表名,默认为:default_form
|
|
47
|
+
* @param dbVersion indexDB的版本号,默认为:1
|
|
48
|
+
*/
|
|
49
|
+
constructor(
|
|
50
|
+
dbName = "default_db",
|
|
51
|
+
storeName = "default_form",
|
|
52
|
+
dbVersion = 1
|
|
53
|
+
) {
|
|
54
|
+
this.#dbName = dbName;
|
|
55
|
+
this.#storeName = storeName;
|
|
56
|
+
this.#dbVersion = dbVersion;
|
|
57
|
+
if (!this.#indexedDB) {
|
|
58
|
+
alert("很抱歉,您的浏览器不支持indexedDB");
|
|
59
|
+
throw new TypeError("很抱歉,您的浏览器不支持indexedDB");
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* 创建 “表”
|
|
65
|
+
* @param dbName 表名
|
|
66
|
+
*/
|
|
67
|
+
createStore(dbName: string) {
|
|
68
|
+
let txn, store;
|
|
69
|
+
txn = this.#db[dbName].transaction(
|
|
70
|
+
this.#storeName,
|
|
71
|
+
"readwrite"
|
|
72
|
+
) as IDBTransaction;
|
|
73
|
+
/* IndexDB的读写权限 */
|
|
74
|
+
store = txn.objectStore(this.#storeName) as IDBObjectStore;
|
|
75
|
+
this.#store = store;
|
|
76
|
+
return store;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* 打开数据库
|
|
80
|
+
* @param callback 回调
|
|
81
|
+
* @param dbName 数据库名
|
|
82
|
+
*/
|
|
83
|
+
private open(
|
|
84
|
+
callback: (
|
|
85
|
+
idbStore: IDBObjectStore | UtilsIDBOpenErrorResult,
|
|
86
|
+
success: boolean
|
|
87
|
+
) => void,
|
|
88
|
+
dbName: string
|
|
89
|
+
) {
|
|
90
|
+
let that = this;
|
|
91
|
+
/* 打开数据库 */
|
|
92
|
+
/* 如果支持IndexDB */
|
|
93
|
+
if (!that.#db[dbName]) {
|
|
94
|
+
/* 如果缓存中没有,则进行数据库的创建或打开,提高效率 */
|
|
95
|
+
let request = that.#indexedDB.open(dbName, that.#dbVersion);
|
|
96
|
+
request.onerror = function (event: Event) {
|
|
97
|
+
callback(
|
|
98
|
+
{
|
|
99
|
+
code: that.#errorCode.open.code,
|
|
100
|
+
msg: that.#errorCode.open.msg,
|
|
101
|
+
event: event,
|
|
102
|
+
},
|
|
103
|
+
false
|
|
104
|
+
);
|
|
105
|
+
};
|
|
106
|
+
request.onsuccess = function (event: Event) {
|
|
107
|
+
if (!that.#db[dbName]) {
|
|
108
|
+
let target = event.target as IDBRequest;
|
|
109
|
+
that.#db[dbName] = target.result;
|
|
110
|
+
}
|
|
111
|
+
let store = that.createStore(dbName);
|
|
112
|
+
callback(store, true);
|
|
113
|
+
};
|
|
114
|
+
request.onupgradeneeded = function (event: Event) {
|
|
115
|
+
let target = event.target as IDBRequest;
|
|
116
|
+
that.#db[dbName] = target.result;
|
|
117
|
+
let store = that.#db[dbName].createObjectStore(that.#storeName, {
|
|
118
|
+
keyPath: "key",
|
|
119
|
+
});
|
|
120
|
+
store.transaction.oncomplete = function (event: Event) {
|
|
121
|
+
callback(store, true);
|
|
122
|
+
};
|
|
123
|
+
};
|
|
124
|
+
} else {
|
|
125
|
+
/* 如果缓存中已经打开了数据库,就直接使用 */
|
|
126
|
+
let store = that.createStore(dbName);
|
|
127
|
+
callback(store, true);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* 保存数据到数据库
|
|
132
|
+
* @param key 数据key
|
|
133
|
+
* @param value 数据值
|
|
134
|
+
*/
|
|
135
|
+
async save(
|
|
136
|
+
key: string,
|
|
137
|
+
value: any
|
|
138
|
+
): Promise<{
|
|
139
|
+
success: boolean;
|
|
140
|
+
code: number;
|
|
141
|
+
msg: string;
|
|
142
|
+
|
|
143
|
+
event?: Event;
|
|
144
|
+
}> {
|
|
145
|
+
let that = this;
|
|
146
|
+
return new Promise((resolve) => {
|
|
147
|
+
let dbName = that.#dbName;
|
|
148
|
+
let inData = {
|
|
149
|
+
key: key,
|
|
150
|
+
value: value,
|
|
151
|
+
};
|
|
152
|
+
that.open(function (idbStore, success) {
|
|
153
|
+
if (!success) {
|
|
154
|
+
resolve({
|
|
155
|
+
success: false,
|
|
156
|
+
code: that.#errorCode.save.code,
|
|
157
|
+
msg: that.#errorCode.save.msg,
|
|
158
|
+
});
|
|
159
|
+
} else {
|
|
160
|
+
idbStore = idbStore as IDBObjectStore;
|
|
161
|
+
let request = idbStore.put(inData);
|
|
162
|
+
request.onsuccess = function (event: Event) {
|
|
163
|
+
/* 保存成功有success 字段 */
|
|
164
|
+
// @ts-ignore
|
|
165
|
+
let target = event.target as IDBRequest;
|
|
166
|
+
resolve({
|
|
167
|
+
success: true,
|
|
168
|
+
code: that.#errorCode.success.code,
|
|
169
|
+
msg: that.#errorCode.success.msg,
|
|
170
|
+
|
|
171
|
+
event: event,
|
|
172
|
+
});
|
|
173
|
+
};
|
|
174
|
+
request.onerror = function (event: Event) {
|
|
175
|
+
// @ts-ignore
|
|
176
|
+
let target = event.target as IDBRequest;
|
|
177
|
+
resolve({
|
|
178
|
+
success: false,
|
|
179
|
+
|
|
180
|
+
code: that.#errorCode.save.code,
|
|
181
|
+
msg: that.#errorCode.save.msg,
|
|
182
|
+
event: event,
|
|
183
|
+
});
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
}, dbName);
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* 根据key获取值
|
|
192
|
+
* @param key 数据key
|
|
193
|
+
*/
|
|
194
|
+
async get<T extends any>(
|
|
195
|
+
key: string
|
|
196
|
+
): Promise<{
|
|
197
|
+
success: boolean;
|
|
198
|
+
code: number;
|
|
199
|
+
msg: string;
|
|
200
|
+
data: T;
|
|
201
|
+
|
|
202
|
+
event?: Event;
|
|
203
|
+
result?: any;
|
|
204
|
+
}> {
|
|
205
|
+
let that = this;
|
|
206
|
+
return new Promise((resolve) => {
|
|
207
|
+
let dbName = that.#dbName;
|
|
208
|
+
that.open(function (idbStore, success) {
|
|
209
|
+
/* 判断返回的数据中是否有error字段 */
|
|
210
|
+
if (!success) {
|
|
211
|
+
resolve({
|
|
212
|
+
success: false,
|
|
213
|
+
code: that.#errorCode.get.code,
|
|
214
|
+
msg: that.#errorCode.get.msg,
|
|
215
|
+
data: void 0 as any,
|
|
216
|
+
});
|
|
217
|
+
} else {
|
|
218
|
+
idbStore = idbStore as IDBObjectStore;
|
|
219
|
+
let request = idbStore.get(key);
|
|
220
|
+
request.onsuccess = function (event: any) {
|
|
221
|
+
let target = event.target as IDBRequest;
|
|
222
|
+
let result = target.result;
|
|
223
|
+
/* result 返回的是 {key: string, value: any} */
|
|
224
|
+
/* 键值对存储 */
|
|
225
|
+
let data: T = result ? result.value : void 0;
|
|
226
|
+
if (data) {
|
|
227
|
+
resolve({
|
|
228
|
+
success: true,
|
|
229
|
+
code: that.#errorCode.success.code,
|
|
230
|
+
msg: that.#errorCode.success.msg,
|
|
231
|
+
data: data,
|
|
232
|
+
|
|
233
|
+
event: event,
|
|
234
|
+
result: result,
|
|
235
|
+
});
|
|
236
|
+
} else {
|
|
237
|
+
resolve({
|
|
238
|
+
success: false,
|
|
239
|
+
code: that.#errorCode.error.code,
|
|
240
|
+
msg: that.#errorCode.error.msg,
|
|
241
|
+
data: void 0 as any,
|
|
242
|
+
|
|
243
|
+
event: event,
|
|
244
|
+
result: result,
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
request.onerror = function (event: any) {
|
|
249
|
+
// @ts-ignore
|
|
250
|
+
let target = event.target as IDBRequest;
|
|
251
|
+
resolve({
|
|
252
|
+
success: false,
|
|
253
|
+
code: that.#errorCode.get.code,
|
|
254
|
+
msg: that.#errorCode.get.msg,
|
|
255
|
+
data: void 0 as any,
|
|
256
|
+
|
|
257
|
+
event: event,
|
|
258
|
+
});
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
}, dbName);
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* 正则获取数据
|
|
267
|
+
* @param key 数据键
|
|
268
|
+
*/
|
|
269
|
+
async regexpGet<T extends any>(
|
|
270
|
+
key: string
|
|
271
|
+
): Promise<{
|
|
272
|
+
success: boolean;
|
|
273
|
+
code: number;
|
|
274
|
+
msg: string;
|
|
275
|
+
data: T[];
|
|
276
|
+
|
|
277
|
+
event?: Event;
|
|
278
|
+
}> {
|
|
279
|
+
let list: T[] = [];
|
|
280
|
+
let that = this;
|
|
281
|
+
return new Promise((resolve) => {
|
|
282
|
+
/* 正则查询 */
|
|
283
|
+
let dbName = that.#dbName;
|
|
284
|
+
that.open(function (idbStore, success) {
|
|
285
|
+
/* 判断返回的数据中是否有error字段 */
|
|
286
|
+
if (!success) {
|
|
287
|
+
resolve({
|
|
288
|
+
success: false,
|
|
289
|
+
code: that.#errorCode.regexpGet.code,
|
|
290
|
+
msg: that.#errorCode.regexpGet.msg,
|
|
291
|
+
data: [],
|
|
292
|
+
});
|
|
293
|
+
} else {
|
|
294
|
+
idbStore = idbStore as IDBObjectStore;
|
|
295
|
+
let request = idbStore.getAll();
|
|
296
|
+
request.onsuccess = function (event: any) {
|
|
297
|
+
let target = event.target as IDBRequest;
|
|
298
|
+
let result = target.result;
|
|
299
|
+
if (result.length !== 0) {
|
|
300
|
+
result.forEach((item: any, index: any) => {
|
|
301
|
+
if (item["key"].match(key)) {
|
|
302
|
+
let concatList = item["value"];
|
|
303
|
+
concatList["key"] = item["key"];
|
|
304
|
+
list = [...list, concatList];
|
|
305
|
+
}
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
resolve({
|
|
309
|
+
success: true,
|
|
310
|
+
code: that.#errorCode.success.code,
|
|
311
|
+
msg: that.#errorCode.success.msg,
|
|
312
|
+
data: list,
|
|
313
|
+
|
|
314
|
+
event: event,
|
|
315
|
+
});
|
|
316
|
+
};
|
|
317
|
+
request.onerror = function (event: any) {
|
|
318
|
+
// @ts-ignore
|
|
319
|
+
let target = event.target as IDBRequest;
|
|
320
|
+
resolve({
|
|
321
|
+
success: false,
|
|
322
|
+
code: that.#errorCode.get.code,
|
|
323
|
+
msg: that.#errorCode.get.msg,
|
|
324
|
+
data: [],
|
|
325
|
+
|
|
326
|
+
event: event,
|
|
327
|
+
});
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
}, dbName);
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* 删除数据
|
|
336
|
+
* @param {string} key 数据键
|
|
337
|
+
*/
|
|
338
|
+
async delete(key: string): Promise<{
|
|
339
|
+
success: boolean;
|
|
340
|
+
code: number;
|
|
341
|
+
msg: string;
|
|
342
|
+
|
|
343
|
+
event?: Event;
|
|
344
|
+
}> {
|
|
345
|
+
let that = this;
|
|
346
|
+
return new Promise((resolve) => {
|
|
347
|
+
/* 根据key删除某条数据 */
|
|
348
|
+
let dbName = that.#dbName;
|
|
349
|
+
that.open(function (idbStore, success) {
|
|
350
|
+
if (!success) {
|
|
351
|
+
resolve({
|
|
352
|
+
success: false,
|
|
353
|
+
code: that.#errorCode.delete.code,
|
|
354
|
+
msg: that.#errorCode.delete.msg,
|
|
355
|
+
});
|
|
356
|
+
} else {
|
|
357
|
+
idbStore = idbStore as IDBObjectStore;
|
|
358
|
+
let request = idbStore.get(key);
|
|
359
|
+
request.onsuccess = function (event: any) {
|
|
360
|
+
let target = event.target as IDBRequest;
|
|
361
|
+
let recode = target.result;
|
|
362
|
+
if (recode) {
|
|
363
|
+
/* 成功 */
|
|
364
|
+
request = (idbStore as IDBObjectStore).delete(key);
|
|
365
|
+
resolve({
|
|
366
|
+
success: true,
|
|
367
|
+
code: that.#errorCode.success.code,
|
|
368
|
+
msg: that.#errorCode.success.msg,
|
|
369
|
+
});
|
|
370
|
+
} else {
|
|
371
|
+
resolve({
|
|
372
|
+
success: false,
|
|
373
|
+
code: that.#errorCode.error.code,
|
|
374
|
+
msg: that.#errorCode.error.msg,
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
};
|
|
378
|
+
request.onerror = function (event: any) {
|
|
379
|
+
// @ts-ignore
|
|
380
|
+
let target = event.target as IDBRequest;
|
|
381
|
+
resolve({
|
|
382
|
+
success: false,
|
|
383
|
+
code: that.#errorCode.delete.code,
|
|
384
|
+
msg: that.#errorCode.delete.msg,
|
|
385
|
+
|
|
386
|
+
event: event,
|
|
387
|
+
});
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
}, dbName);
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* 删除所有数据
|
|
396
|
+
*/
|
|
397
|
+
async deleteAll(): Promise<{
|
|
398
|
+
success: boolean;
|
|
399
|
+
code: number;
|
|
400
|
+
msg: string;
|
|
401
|
+
}> {
|
|
402
|
+
let that = this;
|
|
403
|
+
return new Promise((resolve) => {
|
|
404
|
+
/* 清空数据库 */
|
|
405
|
+
let dbName = that.#dbName;
|
|
406
|
+
that.open(function (idbStore, success) {
|
|
407
|
+
if (!success) {
|
|
408
|
+
resolve({
|
|
409
|
+
success: false,
|
|
410
|
+
code: that.#errorCode.deleteAll.code,
|
|
411
|
+
msg: that.#errorCode.deleteAll.msg,
|
|
412
|
+
});
|
|
413
|
+
} else {
|
|
414
|
+
idbStore = idbStore as IDBObjectStore;
|
|
415
|
+
idbStore.clear();
|
|
416
|
+
resolve({
|
|
417
|
+
success: true,
|
|
418
|
+
code: that.#errorCode.success.code,
|
|
419
|
+
msg: that.#errorCode.success.msg,
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
}, dbName);
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
export { indexedDB };
|