@whitesev/utils 2.0.0 → 2.0.1
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/package.json +4 -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
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { Utils } from "./Utils";
|
|
2
|
+
|
|
3
|
+
class LockFunction<K extends (...args: any[]) => any | Promise<any> | void> {
|
|
4
|
+
#flag: boolean = false;
|
|
5
|
+
#delayTime: number = 0;
|
|
6
|
+
#callback: K;
|
|
7
|
+
#context: typeof Utils;
|
|
8
|
+
lock: () => void;
|
|
9
|
+
unlock: () => void;
|
|
10
|
+
run: (...args: any[]) => Promise<void>;
|
|
11
|
+
isLock: () => boolean;
|
|
12
|
+
/**
|
|
13
|
+
* @param callback 需要执行的函数
|
|
14
|
+
* @param delayTime (可选)延迟xx毫秒后解锁,默认:0
|
|
15
|
+
*/
|
|
16
|
+
constructor(callback: K, delayTime?: number);
|
|
17
|
+
/**
|
|
18
|
+
* @param callback 需要执行的函数
|
|
19
|
+
* @param context (可选)函数作用域,默认:this(Utils)
|
|
20
|
+
* @param delayTime (可选)延迟xx毫秒后解锁,默认:0
|
|
21
|
+
*/
|
|
22
|
+
constructor(callback: K, context?: any, delayTime?: number);
|
|
23
|
+
constructor(callback: K, context?: any, delayTime?: number) {
|
|
24
|
+
let that = this;
|
|
25
|
+
this.#callback = callback;
|
|
26
|
+
if (typeof context === "number") {
|
|
27
|
+
this.#delayTime = context;
|
|
28
|
+
this.#context = Utils;
|
|
29
|
+
} else {
|
|
30
|
+
this.#delayTime = delayTime as number;
|
|
31
|
+
this.#context = context;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* 锁
|
|
35
|
+
*/
|
|
36
|
+
this.lock = function () {
|
|
37
|
+
that.#flag = true;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* 解锁
|
|
41
|
+
*/
|
|
42
|
+
this.unlock = function () {
|
|
43
|
+
setTimeout(() => {
|
|
44
|
+
that.#flag = false;
|
|
45
|
+
}, that.#delayTime);
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* 判断是否被锁
|
|
49
|
+
*/
|
|
50
|
+
this.isLock = function () {
|
|
51
|
+
return that.#flag;
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* 执行
|
|
55
|
+
*/
|
|
56
|
+
this.run = async function (...args: any[]) {
|
|
57
|
+
if (that.isLock()) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
that.lock();
|
|
61
|
+
await that.#callback.apply(that.#context, args);
|
|
62
|
+
that.unlock();
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export { LockFunction };
|
package/src/Log.ts
ADDED
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import type { AnyObject } from "./Utils";
|
|
2
|
+
import { UtilsCore } from "./UtilsCore";
|
|
3
|
+
|
|
4
|
+
/** Utils.Log的初始化配置 */
|
|
5
|
+
declare interface UtilsLogOptions {
|
|
6
|
+
/** 是否输出Tag,false的话其它的颜色也不输出,默认为true */
|
|
7
|
+
tag: boolean;
|
|
8
|
+
/** log.success的颜色,默认#0000FF */
|
|
9
|
+
successColor: string;
|
|
10
|
+
/** log.warn的颜色,默认0 */
|
|
11
|
+
warnColor: string;
|
|
12
|
+
/** log.error的颜色,默认#FF0000 */
|
|
13
|
+
errorColor: string;
|
|
14
|
+
/** log.info的颜色,默认0 */
|
|
15
|
+
infoColor: string;
|
|
16
|
+
/** 是否开启debug模式,true会在控制台每次调用时输出调用函数的所在位置,false不会输出位置,默认false */
|
|
17
|
+
debug: boolean;
|
|
18
|
+
/** 当console输出超过logMaxCount数量自动清理控制台,默认false */
|
|
19
|
+
autoClearConsole: boolean;
|
|
20
|
+
/** console输出的最高数量,autoClearConsole开启则生效,默认999 */
|
|
21
|
+
logMaxCount: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
class Log {
|
|
25
|
+
/** 是否禁用输出的flag */
|
|
26
|
+
#disable: boolean = false;
|
|
27
|
+
/** 前面的TAG标志 */
|
|
28
|
+
tag: string = "Utils.Log";
|
|
29
|
+
/* 使用的console函数 */
|
|
30
|
+
#console: Console = null as any;
|
|
31
|
+
/* 当前输出的数量 */
|
|
32
|
+
#logCount = 0;
|
|
33
|
+
/* 配置 */
|
|
34
|
+
#details: UtilsLogOptions = {
|
|
35
|
+
tag: true,
|
|
36
|
+
successColor: "#0000FF",
|
|
37
|
+
errorColor: "#FF0000",
|
|
38
|
+
infoColor: "0",
|
|
39
|
+
warnColor: "0",
|
|
40
|
+
debug: false,
|
|
41
|
+
autoClearConsole: false,
|
|
42
|
+
logMaxCount: 999,
|
|
43
|
+
};
|
|
44
|
+
#msgColorDetails = [
|
|
45
|
+
"font-weight: bold; color: cornflowerblue",
|
|
46
|
+
"font-weight: bold; color: cornflowerblue",
|
|
47
|
+
"font-weight: bold; color: darkorange",
|
|
48
|
+
"font-weight: bold; color: cornflowerblue",
|
|
49
|
+
];
|
|
50
|
+
/**
|
|
51
|
+
* @param _GM_info_ 油猴管理器的API GM_info,或者是一个对象,如{"script":{name:"Utils.Log"}},或者直接是一个字符串
|
|
52
|
+
* @param console 可指定console对象为unsafeWindow下的console或者是油猴window下的console
|
|
53
|
+
*/
|
|
54
|
+
constructor(
|
|
55
|
+
_GM_info_?:
|
|
56
|
+
| {
|
|
57
|
+
script: {
|
|
58
|
+
name: string;
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
| string,
|
|
62
|
+
console: Console = UtilsCore.window.console
|
|
63
|
+
) {
|
|
64
|
+
if (typeof _GM_info_ === "string") {
|
|
65
|
+
this.tag = _GM_info_;
|
|
66
|
+
} else if (
|
|
67
|
+
typeof _GM_info_ === "object" &&
|
|
68
|
+
typeof _GM_info_?.script?.name === "string"
|
|
69
|
+
) {
|
|
70
|
+
this.tag = _GM_info_.script.name;
|
|
71
|
+
}
|
|
72
|
+
this.#console = console;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 解析Error的堆栈获取实际调用者的函数名及函数所在的位置
|
|
77
|
+
* @param stack
|
|
78
|
+
*/
|
|
79
|
+
private parseErrorStack(stack: string[]) {
|
|
80
|
+
let result = {
|
|
81
|
+
name: "",
|
|
82
|
+
position: "",
|
|
83
|
+
};
|
|
84
|
+
for (let stackString of stack) {
|
|
85
|
+
stackString = stackString.trim();
|
|
86
|
+
let stackFunctionNameMatch = stackString.match(/^at[\s]+(.+?)[\s]+/i);
|
|
87
|
+
let stackFunctionNamePositionMatch = stackString.match(
|
|
88
|
+
/^at[\s]+.+[\s]+\((.+?)\)/i
|
|
89
|
+
);
|
|
90
|
+
if (stackFunctionNameMatch == null) {
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
if (stackFunctionNamePositionMatch == null) {
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
/* 获取最后一个,因为第一个是包含了at */
|
|
97
|
+
let stackFunctionName =
|
|
98
|
+
stackFunctionNameMatch[stackFunctionNameMatch.length - 1];
|
|
99
|
+
let stackFunctionNamePosition =
|
|
100
|
+
stackFunctionNamePositionMatch[
|
|
101
|
+
stackFunctionNamePositionMatch.length - 1
|
|
102
|
+
];
|
|
103
|
+
if (
|
|
104
|
+
stackFunctionName === "" ||
|
|
105
|
+
stackFunctionName.match(
|
|
106
|
+
/^(Utils\.|)Log(\.|)|.<anonymous>$|^Function.each|^NodeList.forEach|^k.fn.init.each/g
|
|
107
|
+
)
|
|
108
|
+
) {
|
|
109
|
+
continue;
|
|
110
|
+
} else {
|
|
111
|
+
result.name = stackFunctionName;
|
|
112
|
+
result.position = stackFunctionNamePosition;
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
if (result.position === "") {
|
|
117
|
+
let lastStackString = stack[stack.length - 1].trim();
|
|
118
|
+
if (lastStackString.startsWith("at chrome-extension://")) {
|
|
119
|
+
let lastStackMatch = lastStackString.match(/^at[\s]+(.+)/);
|
|
120
|
+
if (lastStackMatch) {
|
|
121
|
+
result.position = lastStackMatch[lastStackMatch.length - 1];
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (result.position === "") {
|
|
126
|
+
result.position = stack[stack.length - 1].trim().replace(/^at[\s]*/g, "");
|
|
127
|
+
}
|
|
128
|
+
return result;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* 检测清理控制台
|
|
132
|
+
*/
|
|
133
|
+
private checkClearConsole() {
|
|
134
|
+
this.#logCount++;
|
|
135
|
+
if (
|
|
136
|
+
this.#details.autoClearConsole &&
|
|
137
|
+
this.#logCount > this.#details.logMaxCount
|
|
138
|
+
) {
|
|
139
|
+
this.#console.clear();
|
|
140
|
+
this.#logCount = 0;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* 输出内容
|
|
146
|
+
* @param msg 需要输出的内容
|
|
147
|
+
* @param color 颜色
|
|
148
|
+
* @param otherStyle 其它CSS
|
|
149
|
+
*/
|
|
150
|
+
private printContent(msg: any, color: string, otherStyle?: string) {
|
|
151
|
+
this.checkClearConsole();
|
|
152
|
+
otherStyle = otherStyle || "";
|
|
153
|
+
let stackSplit = new Error()!.stack!.split("\n");
|
|
154
|
+
stackSplit.splice(0, 2);
|
|
155
|
+
let { name: callerName, position: callerPosition } =
|
|
156
|
+
this.parseErrorStack(stackSplit);
|
|
157
|
+
let tagName = this.tag;
|
|
158
|
+
let that = this;
|
|
159
|
+
function consoleMsg(_msg_: any) {
|
|
160
|
+
if (typeof _msg_ === "string") {
|
|
161
|
+
that.#console.log(
|
|
162
|
+
`%c[${tagName}%c-%c${callerName}%c]%c %s`,
|
|
163
|
+
...that.#msgColorDetails,
|
|
164
|
+
`color: ${color};${otherStyle}`,
|
|
165
|
+
_msg_
|
|
166
|
+
);
|
|
167
|
+
} else if (typeof _msg_ === "number") {
|
|
168
|
+
that.#console.log(
|
|
169
|
+
`%c[${tagName}%c-%c${callerName}%c]%c %d`,
|
|
170
|
+
...that.#msgColorDetails,
|
|
171
|
+
`color: ${color};${otherStyle}`,
|
|
172
|
+
_msg_
|
|
173
|
+
);
|
|
174
|
+
} else if (typeof _msg_ === "object") {
|
|
175
|
+
that.#console.log(
|
|
176
|
+
`%c[${tagName}%c-%c${callerName}%c]%c %o`,
|
|
177
|
+
...that.#msgColorDetails,
|
|
178
|
+
`color: ${color};${otherStyle}`,
|
|
179
|
+
_msg_
|
|
180
|
+
);
|
|
181
|
+
} else {
|
|
182
|
+
that.#console.log(_msg_);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
if (Array.isArray(msg)) {
|
|
186
|
+
msg.forEach((item) => {
|
|
187
|
+
consoleMsg(item);
|
|
188
|
+
});
|
|
189
|
+
} else {
|
|
190
|
+
consoleMsg(msg);
|
|
191
|
+
}
|
|
192
|
+
if (this.#details.debug) {
|
|
193
|
+
/* 如果开启调试模式,输出堆栈位置 */
|
|
194
|
+
this.#console.log(callerPosition);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* 控制台-普通输出
|
|
199
|
+
* @param msg 需要输出的内容,如果想输出多个,修改成数组,且数组内的长度最大值为4个
|
|
200
|
+
* @param color 输出的颜色
|
|
201
|
+
* @param otherStyle 其它CSS
|
|
202
|
+
*/
|
|
203
|
+
info(msg: any, color: string = this.#details.infoColor, otherStyle?: string) {
|
|
204
|
+
if (this.#disable) return;
|
|
205
|
+
this.printContent.call(this, msg, color, otherStyle);
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* 控制台-警告输出
|
|
209
|
+
* @param msg 需要输出的内容,如果想输出多个,修改成数组,且数组内的长度最大值为4个
|
|
210
|
+
* @param color 输出的颜色
|
|
211
|
+
* @param otherStyle 其它CSS
|
|
212
|
+
*/
|
|
213
|
+
warn(
|
|
214
|
+
msg: any,
|
|
215
|
+
color = this.#details.warnColor,
|
|
216
|
+
otherStyle = "background: #FEF6D5;padding: 4px 6px 4px 0px;"
|
|
217
|
+
) {
|
|
218
|
+
if (this.#disable) return;
|
|
219
|
+
this.printContent.call(this, msg, color, otherStyle);
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* 控制台-错误输出
|
|
223
|
+
* @param msg 需要输出的内容,如果想输出多个,修改成数组,且数组内的长度最大值为4个
|
|
224
|
+
* @param color 输出的颜色
|
|
225
|
+
* @param otherStyle 其它CSS
|
|
226
|
+
*/
|
|
227
|
+
error(msg: any, color = this.#details.errorColor, otherStyle?: string) {
|
|
228
|
+
if (this.#disable) return;
|
|
229
|
+
this.printContent.call(this, msg, color, otherStyle);
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* 控制台-成功输出
|
|
233
|
+
* @param msg 需要输出的内容,如果想输出多个,修改成数组,且数组内的长度最大值为4个
|
|
234
|
+
* @param color 输出的颜色
|
|
235
|
+
* @param otherStyle 其它CSS
|
|
236
|
+
*/
|
|
237
|
+
success(msg: any, color = this.#details.successColor, otherStyle?: string) {
|
|
238
|
+
if (this.#disable) return;
|
|
239
|
+
this.printContent.call(this, msg, color, otherStyle);
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* 控制台-输出表格
|
|
243
|
+
* @param msg
|
|
244
|
+
* @param color 输出的颜色
|
|
245
|
+
* @param otherStyle 其它CSS
|
|
246
|
+
* @example
|
|
247
|
+
* log.table([{"名字":"example","值":"123"},{"名字":"example2","值":"345"}])
|
|
248
|
+
*/
|
|
249
|
+
table(msg: AnyObject[], color = this.#details.infoColor, otherStyle = "") {
|
|
250
|
+
if (this.#disable) return;
|
|
251
|
+
this.checkClearConsole();
|
|
252
|
+
let stack = new Error()!.stack!.split("\n");
|
|
253
|
+
stack.splice(0, 1);
|
|
254
|
+
let errorStackParse = this.parseErrorStack(stack);
|
|
255
|
+
let stackFunctionName = errorStackParse.name;
|
|
256
|
+
let stackFunctionNamePosition = errorStackParse.position;
|
|
257
|
+
let callerName = stackFunctionName;
|
|
258
|
+
this.#console.log(
|
|
259
|
+
`%c[${this.tag}%c-%c${callerName}%c]%c`,
|
|
260
|
+
...this.#msgColorDetails,
|
|
261
|
+
`color: ${color};${otherStyle}`
|
|
262
|
+
);
|
|
263
|
+
this.#console.table(msg);
|
|
264
|
+
if (this.#details.debug) {
|
|
265
|
+
this.#console.log(stackFunctionNamePosition);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* 配置Log对象的颜色
|
|
270
|
+
* @param paramDetails 配置信息
|
|
271
|
+
*/
|
|
272
|
+
config(paramDetails: Partial<UtilsLogOptions>) {
|
|
273
|
+
this.#details = Object.assign(this.#details, paramDetails);
|
|
274
|
+
}
|
|
275
|
+
/** 禁用输出 */
|
|
276
|
+
disable() {
|
|
277
|
+
this.#disable = true;
|
|
278
|
+
}
|
|
279
|
+
/** 恢复输出 */
|
|
280
|
+
recovery() {
|
|
281
|
+
this.#disable = false;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export { Log };
|
package/src/Progress.ts
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { Utils } from "./Utils";
|
|
2
|
+
import { UtilsCore } from "./UtilsCore";
|
|
3
|
+
|
|
4
|
+
declare interface ProgressParamConfig {
|
|
5
|
+
/** canvas元素节点 */
|
|
6
|
+
canvasNode: HTMLCanvasElement;
|
|
7
|
+
/** 绘制角度,默认:95 */
|
|
8
|
+
deg: number;
|
|
9
|
+
/** 进度,默认:0 */
|
|
10
|
+
progress: number;
|
|
11
|
+
/** 绘制的线宽度,默认:10 */
|
|
12
|
+
lineWidth: number;
|
|
13
|
+
/** 绘制的背景颜色,默认:#1e637c */
|
|
14
|
+
lineBgColor: string;
|
|
15
|
+
/** 绘制的线的颜色,默认:#25deff */
|
|
16
|
+
lineColor: string;
|
|
17
|
+
/** 绘制的字体颜色,默认:#000000 */
|
|
18
|
+
textColor: string;
|
|
19
|
+
/** 绘制的字体大小(px),默认:22px */
|
|
20
|
+
fontSize: number;
|
|
21
|
+
/** 绘制的圆的半径,默认:50 */
|
|
22
|
+
circleRadius: number;
|
|
23
|
+
}
|
|
24
|
+
class Progress {
|
|
25
|
+
#config: ProgressParamConfig = {
|
|
26
|
+
/**
|
|
27
|
+
* canvas元素节点
|
|
28
|
+
*/
|
|
29
|
+
canvasNode: null as any,
|
|
30
|
+
/**
|
|
31
|
+
* 绘制角度
|
|
32
|
+
*/
|
|
33
|
+
deg: 95,
|
|
34
|
+
/**
|
|
35
|
+
* 进度
|
|
36
|
+
*/
|
|
37
|
+
progress: 0,
|
|
38
|
+
/**
|
|
39
|
+
* 绘制的线宽度
|
|
40
|
+
*/
|
|
41
|
+
lineWidth: 10,
|
|
42
|
+
/**
|
|
43
|
+
* 绘制的背景颜色
|
|
44
|
+
*/
|
|
45
|
+
lineBgColor: "#1e637c",
|
|
46
|
+
/**
|
|
47
|
+
* 绘制的线的颜色
|
|
48
|
+
*/
|
|
49
|
+
lineColor: "#25deff",
|
|
50
|
+
/**
|
|
51
|
+
* 绘制的字体颜色
|
|
52
|
+
*/
|
|
53
|
+
textColor: "#000000",
|
|
54
|
+
/**
|
|
55
|
+
* 绘制的字体大小(px)
|
|
56
|
+
*/
|
|
57
|
+
fontSize: 22,
|
|
58
|
+
/**
|
|
59
|
+
* 绘制的圆的半径
|
|
60
|
+
*/
|
|
61
|
+
circleRadius: 50,
|
|
62
|
+
};
|
|
63
|
+
#ctx: CanvasRenderingContext2D = null as any;
|
|
64
|
+
#width: number = null as any;
|
|
65
|
+
#height: number = null as any;
|
|
66
|
+
/**
|
|
67
|
+
*
|
|
68
|
+
* @param paramConfig 配置信息
|
|
69
|
+
*/
|
|
70
|
+
constructor(paramConfig: ProgressParamConfig) {
|
|
71
|
+
this.#config = Utils.assign(this.#config, paramConfig);
|
|
72
|
+
if (!(this.#config.canvasNode instanceof HTMLCanvasElement)) {
|
|
73
|
+
throw new Error(
|
|
74
|
+
"Utils.Progress 参数 canvasNode 必须是 HTMLCanvasElement"
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
this.init();
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* 初始化
|
|
81
|
+
*/
|
|
82
|
+
private init() {
|
|
83
|
+
/* 获取画笔 */
|
|
84
|
+
let ctx = this.#config.canvasNode.getContext("2d");
|
|
85
|
+
if (ctx == null) {
|
|
86
|
+
throw new Error("Utils.Progress 获取画笔失败");
|
|
87
|
+
}
|
|
88
|
+
this.#ctx = ctx;
|
|
89
|
+
/* 元素宽度 */
|
|
90
|
+
this.#width = this.#config.canvasNode.width;
|
|
91
|
+
/* 元素高度 */
|
|
92
|
+
this.#height = this.#config.canvasNode.height;
|
|
93
|
+
/* 清除锯齿 */
|
|
94
|
+
if (UtilsCore.window.devicePixelRatio) {
|
|
95
|
+
this.#config.canvasNode.style.width = this.#width + "px";
|
|
96
|
+
this.#config.canvasNode.style.height = this.#height + "px";
|
|
97
|
+
this.#config.canvasNode.height =
|
|
98
|
+
this.#height * UtilsCore.window.devicePixelRatio;
|
|
99
|
+
this.#config.canvasNode.width =
|
|
100
|
+
this.#width * UtilsCore.window.devicePixelRatio;
|
|
101
|
+
this.#ctx.scale(
|
|
102
|
+
UtilsCore.window.devicePixelRatio,
|
|
103
|
+
UtilsCore.window.devicePixelRatio
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
/* 设置线宽 */
|
|
107
|
+
this.#ctx.lineWidth = this.#config.lineWidth;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* 绘制
|
|
111
|
+
*/
|
|
112
|
+
draw() {
|
|
113
|
+
let degActive = (this.#config.progress * 360) / 100;
|
|
114
|
+
/* 清除画布 */
|
|
115
|
+
this.#ctx.clearRect(0, 0, this.#width, this.#height);
|
|
116
|
+
/* 开始绘制底圆 */
|
|
117
|
+
this.#ctx.beginPath();
|
|
118
|
+
this.#ctx.arc(
|
|
119
|
+
this.#width / 2,
|
|
120
|
+
this.#height / 2,
|
|
121
|
+
this.#config.circleRadius,
|
|
122
|
+
1,
|
|
123
|
+
8
|
|
124
|
+
);
|
|
125
|
+
this.#ctx.strokeStyle = this.#config.lineBgColor;
|
|
126
|
+
this.#ctx.stroke();
|
|
127
|
+
/* 开始绘制动态圆 */
|
|
128
|
+
this.#ctx.beginPath();
|
|
129
|
+
this.#ctx.arc(
|
|
130
|
+
this.#width / 2,
|
|
131
|
+
this.#height / 2,
|
|
132
|
+
this.#config.circleRadius,
|
|
133
|
+
-Math.PI / 2,
|
|
134
|
+
(degActive * Math.PI) / 180 - Math.PI / 2
|
|
135
|
+
);
|
|
136
|
+
this.#ctx.strokeStyle = this.#config.lineColor;
|
|
137
|
+
this.#ctx.stroke();
|
|
138
|
+
/* 获取百分比 */
|
|
139
|
+
let txt = parseInt(this.#config.progress.toString()) + "%";
|
|
140
|
+
this.#ctx.font = this.#config.fontSize + "px SimHei";
|
|
141
|
+
/* 获取文本宽度 */
|
|
142
|
+
let w = this.#ctx.measureText(txt).width;
|
|
143
|
+
let h = this.#config.fontSize / 2;
|
|
144
|
+
this.#ctx.fillStyle = this.#config.textColor;
|
|
145
|
+
this.#ctx.fillText(txt, this.#width / 2 - w / 2, this.#height / 2 + h / 2);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export { Progress };
|
package/src/TryCatch.ts
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { Utils } from "./Utils";
|
|
2
|
+
|
|
3
|
+
export declare interface UtilsTryCatchConfig {
|
|
4
|
+
log: boolean;
|
|
5
|
+
}
|
|
6
|
+
/** tryCatch */
|
|
7
|
+
export declare interface UtilsTryCatchType {
|
|
8
|
+
run: UtilsTryCatchType;
|
|
9
|
+
config: UtilsTryCatchType;
|
|
10
|
+
error: UtilsTryCatchType;
|
|
11
|
+
}
|
|
12
|
+
const TryCatch = function (...args: any) {
|
|
13
|
+
/* 定义变量和函数 */
|
|
14
|
+
let callbackFunction = null;
|
|
15
|
+
let context = null;
|
|
16
|
+
let handleError = (error: Error) => {};
|
|
17
|
+
let defaultDetails = {
|
|
18
|
+
log: true,
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const TryCatchCore = {
|
|
22
|
+
/**
|
|
23
|
+
*
|
|
24
|
+
* @param paramDetails 配置
|
|
25
|
+
* @returns
|
|
26
|
+
*/
|
|
27
|
+
config(paramDetails: UtilsTryCatchConfig) {
|
|
28
|
+
defaultDetails = Utils.assign(defaultDetails, paramDetails);
|
|
29
|
+
return TryCatchCore;
|
|
30
|
+
},
|
|
31
|
+
/**
|
|
32
|
+
* 处理错误
|
|
33
|
+
* @param handler
|
|
34
|
+
*/
|
|
35
|
+
error(handler: ((...args: any[]) => any) | string | Function) {
|
|
36
|
+
// @ts-ignore
|
|
37
|
+
handleError = handler;
|
|
38
|
+
return TryCatchCore;
|
|
39
|
+
},
|
|
40
|
+
/**
|
|
41
|
+
* 执行传入的函数并捕获其可能抛出的错误,并通过传入的错误处理函数进行处理。
|
|
42
|
+
* @param callback 待执行函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
|
|
43
|
+
* @param __context__ 待执行函数的作用域,用于apply指定
|
|
44
|
+
* @returns 如果函数有返回值,则返回该返回值;否则返回 tryCatchObj 函数以支持链式调用。
|
|
45
|
+
* @throws {Error} 如果传入参数不符合要求,则会抛出相应类型的错误。
|
|
46
|
+
*/
|
|
47
|
+
run<A extends any[], R>(
|
|
48
|
+
callback: ((...args: A) => R) | string | Function,
|
|
49
|
+
__context__?: any
|
|
50
|
+
): UtilsTryCatchType {
|
|
51
|
+
callbackFunction = callback;
|
|
52
|
+
context = __context__ || this;
|
|
53
|
+
let result = executeTryCatch(callbackFunction, handleError, context);
|
|
54
|
+
// @ts-ignore
|
|
55
|
+
return result !== void 0 ? result : TryCatchCore;
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* 执行传入的函数并捕获其可能抛出的错误,并通过传入的错误处理函数进行处理。
|
|
61
|
+
* @param callback - 待执行函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
|
|
62
|
+
* @param handleErrorFunc - 错误处理函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
|
|
63
|
+
* @param funcThis - 待执行函数的作用域,用于apply指定
|
|
64
|
+
* @returns 如果函数有返回值,则返回该返回值;否则返回 undefined。
|
|
65
|
+
*/
|
|
66
|
+
function executeTryCatch<A extends any[], R>(
|
|
67
|
+
callback: string | ((...args: A) => R) | Function,
|
|
68
|
+
handleErrorFunc: string | ((...args: any[]) => any),
|
|
69
|
+
funcThis?: any
|
|
70
|
+
) {
|
|
71
|
+
let result = void 0;
|
|
72
|
+
try {
|
|
73
|
+
if (typeof callback === "string") {
|
|
74
|
+
(function () {
|
|
75
|
+
eval(callback as string);
|
|
76
|
+
}).apply(funcThis, args);
|
|
77
|
+
} else {
|
|
78
|
+
result = (callback as Function).apply(funcThis, args);
|
|
79
|
+
}
|
|
80
|
+
} catch (error) {
|
|
81
|
+
if (defaultDetails.log) {
|
|
82
|
+
callback = callback as Function;
|
|
83
|
+
console.log(
|
|
84
|
+
`%c ${callback?.name ? callback?.name : callback + "出现错误"} `,
|
|
85
|
+
"color: #f20000"
|
|
86
|
+
);
|
|
87
|
+
console.log(`%c 错误原因:${error}`, "color: #f20000");
|
|
88
|
+
console.trace(callback);
|
|
89
|
+
}
|
|
90
|
+
if (handleErrorFunc) {
|
|
91
|
+
if (typeof handleErrorFunc === "string") {
|
|
92
|
+
result = function () {
|
|
93
|
+
return eval(handleErrorFunc);
|
|
94
|
+
// @ts-ignore
|
|
95
|
+
}.apply(funcThis, [...args, error]);
|
|
96
|
+
} else {
|
|
97
|
+
result = handleErrorFunc.apply(funcThis, [...args, error]);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return result;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return TryCatchCore;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
export { TryCatch };
|