ziya-utils 1.1.3 → 1.1.5
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/README.md +1 -1
- package/README.zh-CN.md +1 -1
- package/dist/ziya-utils.js +1 -1
- package/es/constant/index.d.ts +3 -0
- package/es/constant/index.js +4 -0
- package/es/document/index.d.ts +20 -3
- package/es/document/index.js +78 -0
- package/es/index.d.ts +2 -0
- package/es/index.js +2 -0
- package/es/xhr/index.d.ts +6 -2
- package/es/xhr/index.js +41 -33
- package/lib/constant/index.d.ts +3 -0
- package/lib/constant/index.js +6 -0
- package/lib/document/index.d.ts +20 -3
- package/lib/document/index.js +81 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +5 -0
- package/lib/xhr/index.d.ts +6 -2
- package/lib/xhr/index.js +41 -33
- package/package.json +1 -1
- package/es/loger/index.d.ts +0 -7
- package/lib/loger/index.d.ts +0 -7
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@ A Javascript common tool library.
|
|
|
14
14
|

|
|
15
15
|

|
|
16
16
|
|
|
17
|
-
English | [简体中文](
|
|
17
|
+
English | [简体中文](README.zh-CN.md)
|
|
18
18
|
|
|
19
19
|
</div>
|
|
20
20
|
|
package/README.zh-CN.md
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|

|
|
15
15
|

|
|
16
16
|
|
|
17
|
-
[English](
|
|
17
|
+
[English](README.md) | 简体中文
|
|
18
18
|
|
|
19
19
|
</div>
|
|
20
20
|
|
package/dist/ziya-utils.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).ZiyaUtils={})}(this,function(t){"use strict";function e(t,e,n,o){return new(n||(n=Promise))(function(i,s){function r(t){try{c(o.next(t))}catch(t){s(t)}}function a(t){try{c(o.throw(t))}catch(t){s(t)}}function c(t){var e;t.done?i(t.value):(e=t.value,e instanceof n?e:new n(function(t){t(e)})).then(r,a)}c((o=o.apply(t,e||[])).next())})}"function"==typeof SuppressedError&&SuppressedError;const n={PHONE:/^1[3-9]\d{9}$/,EMAIL:/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,ID_CARD:/(^\d{15}$)|(^\d{17}(\d|X|x)$)/,URL:/^(https?:\/\/)?([\da-z.-]+)\.([a-z.]{2,6})([/\w .-]*)*\/?$/,IPV4:/^(\d{1,3}\.){3}\d{1,3}$/,STRONG_PASSWORD:/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/,CHINESE:/^[\u4e00-\u9fa5]+$/};class o{}class i extends o{static isPhone(t){return n.PHONE.test(t)}static isEmail(t){return n.EMAIL.test(t)}static isIdCard(t){return n.ID_CARD.test(t)}static isUrl(t){return n.URL.test(t)}static isIpv4(t){return n.IPV4.test(t)}static isStrongPassword(t){return n.STRONG_PASSWORD.test(t)}static isChinese(t){return n.CHINESE.test(t)}}var s={patterns:n,validator:i};const r=t=>{if(!t)return"";const e=Math.max(t.lastIndexOf("/"),t.lastIndexOf("\\"));return-1===e?t:t.slice(e+1)};function a(t){if("string"==typeof t){if(!/^\d+$/.test(t))return!1;t=Number(t)}if("number"!=typeof t||isNaN(t))return!1;const e=41024448e5;return 13===t.toString().length?t>=0&&t<=e:10===t.toString().length&&(t>=0&&t<=4102444800)}const c=t=>a(t)&&10===t.toString().length;class l{constructor(t={}){var e;this.interceptRules=[],this.isActive=!1,this.originalXMLHttpRequest=window.XMLHttpRequest,this.enableLogging=null===(e=t.enableLogging)||void 0===e||e}start(){if(this.isActive)return void this.log("拦截器已经启动");const t=this;window.XMLHttpRequest=function(){const
|
|
1
|
+
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).ZiyaUtils={})}(this,function(t){"use strict";function e(t,e,n,o){return new(n||(n=Promise))(function(i,s){function r(t){try{c(o.next(t))}catch(t){s(t)}}function a(t){try{c(o.throw(t))}catch(t){s(t)}}function c(t){var e;t.done?i(t.value):(e=t.value,e instanceof n?e:new n(function(t){t(e)})).then(r,a)}c((o=o.apply(t,e||[])).next())})}"function"==typeof SuppressedError&&SuppressedError;const n={PHONE:/^1[3-9]\d{9}$/,EMAIL:/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,ID_CARD:/(^\d{15}$)|(^\d{17}(\d|X|x)$)/,URL:/^(https?:\/\/)?([\da-z.-]+)\.([a-z.]{2,6})([/\w .-]*)*\/?$/,IPV4:/^(\d{1,3}\.){3}\d{1,3}$/,STRONG_PASSWORD:/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/,CHINESE:/^[\u4e00-\u9fa5]+$/};class o{}class i extends o{static isPhone(t){return n.PHONE.test(t)}static isEmail(t){return n.EMAIL.test(t)}static isIdCard(t){return n.ID_CARD.test(t)}static isUrl(t){return n.URL.test(t)}static isIpv4(t){return n.IPV4.test(t)}static isStrongPassword(t){return n.STRONG_PASSWORD.test(t)}static isChinese(t){return n.CHINESE.test(t)}}var s={patterns:n,validator:i};const r=t=>{if(!t)return"";const e=Math.max(t.lastIndexOf("/"),t.lastIndexOf("\\"));return-1===e?t:t.slice(e+1)};function a(t){if("string"==typeof t){if(!/^\d+$/.test(t))return!1;t=Number(t)}if("number"!=typeof t||isNaN(t))return!1;const e=41024448e5;return 13===t.toString().length?t>=0&&t<=e:10===t.toString().length&&(t>=0&&t<=4102444800)}const c=t=>a(t)&&10===t.toString().length;class l{constructor(t={}){var e;this.interceptRules=[],this.isActive=!1,this.originalXMLHttpRequest=window.XMLHttpRequest,this.enableLogging=null===(e=t.enableLogging)||void 0===e||e}start(){if(this.isActive)return void this.log("拦截器已经启动");const t=this;window.XMLHttpRequest=function(){const e=new t.originalXMLHttpRequest;let n="";const o=e.open;e.open=function(t,e,i=!0,s,r){return n="string"==typeof e?e:e.toString(),o.call(this,t,e,i,s,r)};const i=e.send;return e.send=function(o){const s=t.findMatchingRule(n);if(!s)return i.call(this,o);let r=o;if(s.requestCallback&&o)try{r=s.requestCallback(o)}catch(t){console.error("XHR请求拦截器处理失败:",t),r=o}const a=e.onreadystatechange;return e.onreadystatechange=function(o){if(4===e.readyState&&e.status>=200&&e.status<300)try{const o=JSON.parse(e.responseText),i=s.responseCallback(o);Object.defineProperty(e,"responseText",{writable:!0,configurable:!0,value:JSON.stringify(i)}),Object.defineProperty(e,"response",{writable:!0,configurable:!0,value:JSON.stringify(i)}),t.log(`已拦截并修改响应: ${n}`)}catch(e){t.log("拦截器处理失败:",e)}a&&a.call(this,o)},i.call(this,r)},e},Object.setPrototypeOf(window.XMLHttpRequest,this.originalXMLHttpRequest),window.XMLHttpRequest.prototype=this.originalXMLHttpRequest.prototype,this.isActive=!0,this.log("XHR拦截器已启动")}stop(){this.isActive?(window.XMLHttpRequest=this.originalXMLHttpRequest,this.isActive=!1,this.log("XHR拦截器已停止,已恢复原始XMLHttpRequest")):this.log("拦截器未启动")}setRules(t){this.interceptRules=[...t],this.log("已设置拦截规则:",t.map(t=>t.url))}addRule(t,e,n){this.interceptRules.push({url:t,responseCallback:e,requestCallback:n}),this.log(`已添加拦截规则: ${t}`)}removeRule(t){const e=this.interceptRules.length;this.interceptRules=this.interceptRules.filter(e=>e.url!==t);const n=this.interceptRules.length<e;return n?this.log(`已移除拦截规则: ${t}`):this.log(`未找到拦截规则: ${t}`),n}clearRules(){this.interceptRules=[],this.log("已清除所有拦截规则")}getRules(){return[...this.interceptRules]}getStatus(){return{isActive:this.isActive,rulesCount:this.interceptRules.length}}findMatchingRule(t){return this.interceptRules.find(e=>t.includes(e.url))}log(t,...e){this.enableLogging&&console.log(`[XHRInterceptor] ${t}`,...e)}}const u=new l({enableLogging:!0}),d="ziya-utils";t.Lib_Name=d,t.RegexPatterns=n,t.RegexValidator=i,t.XHRInterceptor=l,t.addStyleStr=(t="")=>{let e=document.createElement("style");return e.innerHTML=t,document.getElementsByTagName("head")[0].appendChild(e),e},t.copyToClipboard=function(t){return e(this,void 0,void 0,function*(){try{var e=JSON.stringify(t);if(navigator.clipboard&&(window.isSecureContext||"https:"===location.protocol||"localhost"===location.hostname))return yield navigator.clipboard.writeText(e),!0;var n=document.createElement("textarea");n.value=e,n.style.position="fixed",n.style.left="-9999px",n.style.top="-9999px",document.body.appendChild(n),n.select();var o=document.execCommand("copy");return document.body.removeChild(n),o}catch(t){return console.error("复制失败:",t),!1}})},t.createFormData=function(t){const e=new FormData;if("string"==typeof t){const n=new URLSearchParams(t);for(const[t,o]of n)e.append(t,o)}else"object"==typeof t&&null!==t&&Object.keys(t).forEach(n=>{const o=t[n];null!=o?Array.isArray(o)?o.forEach(t=>e.append(n,String(t))):o instanceof File||o instanceof Blob?e.append(n,o):"object"==typeof o?e.append(n,JSON.stringify(o)):e.append(n,String(o)):e.append(n,"")});return e},t.createMaskLoading=(t="加载中...",e="")=>{const n=`${d}-mask-loading-container`,o=document.getElementById(n);o&&document.body.removeChild(o);const i=document.createElement("div");i.id=n,i.innerHTML=`\n <div class="mask"></div>\n <div class="message">${t}</div>\n `;const s=document.createElement("style");return s.innerHTML=`\n #${n} {\n position: fixed;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n z-index: 9999;\n transition: opacity 0.3s;\n }\n #${n} .mask {\n position: absolute;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n background-color: rgba(0, 0, 0, 0.45);\n }\n #${n} .message {\n position: absolute;\n top: 50%;\n left: 50%;\n transform: translate(-50%, -50%);\n color: #fff;\n }\n\n ${e}\n `,document.head.appendChild(s),document.body.append(i),{close:()=>{document.body.removeChild(i)},updateMsg:t=>{i.querySelector(".message").innerHTML=t}}},t.downloadSingleFile=(t,n)=>e(void 0,void 0,void 0,function*(){if(!t||!s.validator.isUrl(t))throw new Error("无效的URL");try{const e=yield fetch(t);if(!e.ok)throw new Error(`下载失败: ${e.status} ${e.statusText}`);let o=n;if(!o){o=r(t);const e=o.indexOf("?");-1!==e&&(o=o.substring(0,e)),o||(o="downloaded_file")}const i=yield e.blob(),s=URL.createObjectURL(i),a=document.createElement("a");a.href=s,a.download=o,document.body.appendChild(a),a.click(),setTimeout(()=>{URL.revokeObjectURL(s),a.remove()},100)}catch(t){throw console.error("文件下载失败:",t),t}}),t.extractFileNameFromPath=r,t.formatDateTypeSafe=function(t,e){return function(t,e){const n=e||new Date,o=(t,e=2)=>t.toString().padStart(e,"0"),i={year:n.getFullYear(),month:n.getMonth()+1,day:n.getDate(),hours:n.getHours(),minutes:n.getMinutes(),seconds:n.getSeconds(),milliseconds:n.getMilliseconds()},s={YYYY:i.year.toString(),YY:i.year.toString().slice(-2),MM:o(i.month),M:i.month.toString(),DD:o(i.day),D:i.day.toString(),HH:o(i.hours),H:i.hours.toString(),mm:o(i.minutes),m:i.minutes.toString(),ss:o(i.seconds),s:i.seconds.toString(),SSS:o(i.milliseconds,3)};return Object.keys(s).sort((t,e)=>e.length-t.length).reduce((t,e)=>t.replace(new RegExp(e,"g"),s[e]),t)}(t,e)},t.getElapsedTimeSince=t=>{const e=Date.now();a(t)||(t=e);const n=Math.max(0,e-(c(t)?1e3*t:t));return{hours:Math.floor(n/36e5),minutes:Math.floor(n%36e5/6e4),seconds:Math.floor(n%6e4/1e3)}},t.getUuid=function(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(t){var e=16*Math.random()|0;return("x"==t?e:3&e|8).toString(16)})},t.isMillisecondTimestamp=t=>a(t)&&13===t.toString().length,t.isSecondTimestamp=c,t.isValidTimestamp=a,t.precisionFormat=function(t,e,n=!0,o=!0){var i=t<0&&o?-1:1;const s=Math.pow(10,e);return n?Math.round(t*s*i)/s*i:Math.floor(t*s*i)/s*i},t.sleep=t=>new Promise(e=>{setTimeout(()=>{e(!0)},1e3*t)}),t.xhrInterceptor=u});
|
package/es/document/index.d.ts
CHANGED
|
@@ -1,7 +1,24 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @fileoverview Document模块
|
|
2
|
+
* @fileoverview Document 模块
|
|
3
3
|
* @created 2025-04-02
|
|
4
4
|
* @author glk
|
|
5
5
|
*/
|
|
6
|
-
declare
|
|
7
|
-
|
|
6
|
+
declare const addStyleStr: (styStr?: string) => HTMLStyleElement;
|
|
7
|
+
/**
|
|
8
|
+
* 创建一个全局唯一的遮罩 Loading
|
|
9
|
+
* @param msg
|
|
10
|
+
* @param style
|
|
11
|
+
* @returns
|
|
12
|
+
*/
|
|
13
|
+
declare const createMaskLoading: (msg?: string, style?: string) => {
|
|
14
|
+
/**
|
|
15
|
+
* 关闭loading
|
|
16
|
+
*/
|
|
17
|
+
close: () => void;
|
|
18
|
+
/**
|
|
19
|
+
* 更新信息
|
|
20
|
+
* @param msg
|
|
21
|
+
*/
|
|
22
|
+
updateMsg: (msg: string) => void;
|
|
23
|
+
};
|
|
24
|
+
export { addStyleStr, createMaskLoading };
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { Lib_Name } from '../constant/index.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @fileoverview Document 模块
|
|
5
|
+
* @created 2025-04-02
|
|
6
|
+
* @author glk
|
|
7
|
+
*/
|
|
8
|
+
const addStyleStr = (styStr = "") => {
|
|
9
|
+
let _style = document.createElement("style");
|
|
10
|
+
_style.innerHTML = styStr;
|
|
11
|
+
document.getElementsByTagName("head")[0].appendChild(_style);
|
|
12
|
+
return _style;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* 创建一个全局唯一的遮罩 Loading
|
|
16
|
+
* @param msg
|
|
17
|
+
* @param style
|
|
18
|
+
* @returns
|
|
19
|
+
*/
|
|
20
|
+
const createMaskLoading = (msg = "加载中...", style = "") => {
|
|
21
|
+
const Container_Id = `${Lib_Name}-mask-loading-container`;
|
|
22
|
+
const prevLoading = document.getElementById(Container_Id);
|
|
23
|
+
!!prevLoading && document.body.removeChild(prevLoading);
|
|
24
|
+
const ele = document.createElement("div");
|
|
25
|
+
ele.id = Container_Id;
|
|
26
|
+
ele.innerHTML = `
|
|
27
|
+
<div class="mask"></div>
|
|
28
|
+
<div class="message">${msg}</div>
|
|
29
|
+
`;
|
|
30
|
+
const styleEle = document.createElement("style");
|
|
31
|
+
styleEle.innerHTML = `
|
|
32
|
+
#${Container_Id} {
|
|
33
|
+
position: fixed;
|
|
34
|
+
top: 0;
|
|
35
|
+
left: 0;
|
|
36
|
+
width: 100%;
|
|
37
|
+
height: 100%;
|
|
38
|
+
z-index: 9999;
|
|
39
|
+
transition: opacity 0.3s;
|
|
40
|
+
}
|
|
41
|
+
#${Container_Id} .mask {
|
|
42
|
+
position: absolute;
|
|
43
|
+
top: 0;
|
|
44
|
+
left: 0;
|
|
45
|
+
width: 100%;
|
|
46
|
+
height: 100%;
|
|
47
|
+
background-color: rgba(0, 0, 0, 0.45);
|
|
48
|
+
}
|
|
49
|
+
#${Container_Id} .message {
|
|
50
|
+
position: absolute;
|
|
51
|
+
top: 50%;
|
|
52
|
+
left: 50%;
|
|
53
|
+
transform: translate(-50%, -50%);
|
|
54
|
+
color: #fff;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
${style}
|
|
58
|
+
`;
|
|
59
|
+
document.head.appendChild(styleEle);
|
|
60
|
+
document.body.append(ele);
|
|
61
|
+
return {
|
|
62
|
+
/**
|
|
63
|
+
* 关闭loading
|
|
64
|
+
*/
|
|
65
|
+
close: () => {
|
|
66
|
+
document.body.removeChild(ele);
|
|
67
|
+
},
|
|
68
|
+
/**
|
|
69
|
+
* 更新信息
|
|
70
|
+
* @param msg
|
|
71
|
+
*/
|
|
72
|
+
updateMsg: (msg) => {
|
|
73
|
+
ele.querySelector(".message").innerHTML = msg;
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export { addStyleStr, createMaskLoading };
|
package/es/index.d.ts
CHANGED
package/es/index.js
CHANGED
|
@@ -6,3 +6,5 @@ export { RegexPatterns, RegexValidator } from './regexp/index.js';
|
|
|
6
6
|
export { createFormData } from './form/index.js';
|
|
7
7
|
export { precisionFormat } from './count/index.js';
|
|
8
8
|
export { XHRInterceptor, xhrInterceptor } from './xhr/index.js';
|
|
9
|
+
export { Lib_Name } from './constant/index.js';
|
|
10
|
+
export { addStyleStr, createMaskLoading } from './document/index.js';
|
package/es/xhr/index.d.ts
CHANGED
|
@@ -7,8 +7,12 @@
|
|
|
7
7
|
* 拦截规则
|
|
8
8
|
*/
|
|
9
9
|
interface InterceptRule {
|
|
10
|
+
/** 拦截的 URL,模糊匹配 */
|
|
10
11
|
url: string;
|
|
11
|
-
|
|
12
|
+
/** 拦截响应的回调函数 */
|
|
13
|
+
responseCallback: (data: any) => any;
|
|
14
|
+
/** 拦截请求的回调函数 */
|
|
15
|
+
requestCallback?: (data: any) => any;
|
|
12
16
|
}
|
|
13
17
|
/**
|
|
14
18
|
* 拦截器配置
|
|
@@ -40,7 +44,7 @@ declare class XHRInterceptor {
|
|
|
40
44
|
/**
|
|
41
45
|
* 添加单个拦截规则
|
|
42
46
|
*/
|
|
43
|
-
addRule(url: string, responseCallback: InterceptRule["responseCallback"]): void;
|
|
47
|
+
addRule(url: string, responseCallback: InterceptRule["responseCallback"], requestCallback?: InterceptRule["requestCallback"]): void;
|
|
44
48
|
/**
|
|
45
49
|
* 移除指定URL的拦截规则
|
|
46
50
|
*/
|
package/es/xhr/index.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { __awaiter } from '../node_modules/tslib/tslib.es6.js';
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
2
|
* @fileoverview XMLHttpRequest模块
|
|
5
3
|
* @created 2025-04-02
|
|
@@ -44,41 +42,51 @@ class XHRInterceptor {
|
|
|
44
42
|
// 没有匹配的规则,直接执行原始请求
|
|
45
43
|
return originalSend.call(this, body);
|
|
46
44
|
}
|
|
45
|
+
// 处理请求数据拦截
|
|
46
|
+
let processedBody = body;
|
|
47
|
+
if (matchingRule.requestCallback && body) {
|
|
48
|
+
try {
|
|
49
|
+
processedBody = matchingRule.requestCallback(body);
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
console.error('XHR请求拦截器处理失败:', error);
|
|
53
|
+
// 出错时使用原始请求体
|
|
54
|
+
processedBody = body;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
47
57
|
// 保存原始的 onreadystatechange
|
|
48
58
|
const originalOnReadyStateChange = xhr.onreadystatechange;
|
|
49
59
|
xhr.onreadystatechange = function (event) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
self.log(`已拦截并修改响应: ${requestUrl}`);
|
|
69
|
-
}
|
|
70
|
-
catch (error) {
|
|
71
|
-
self.log("拦截器处理失败:", error);
|
|
72
|
-
// 出错时保持原始响应
|
|
73
|
-
}
|
|
60
|
+
if (xhr.readyState === 4 && xhr.status >= 200 && xhr.status < 300) {
|
|
61
|
+
try {
|
|
62
|
+
// 解析原始响应
|
|
63
|
+
const originalData = JSON.parse(xhr.responseText);
|
|
64
|
+
// 使用回调函数处理响应数据
|
|
65
|
+
const modifiedData = matchingRule.responseCallback(originalData);
|
|
66
|
+
// 重写响应属性
|
|
67
|
+
Object.defineProperty(xhr, "responseText", {
|
|
68
|
+
writable: true,
|
|
69
|
+
configurable: true,
|
|
70
|
+
value: JSON.stringify(modifiedData),
|
|
71
|
+
});
|
|
72
|
+
Object.defineProperty(xhr, "response", {
|
|
73
|
+
writable: true,
|
|
74
|
+
configurable: true,
|
|
75
|
+
value: JSON.stringify(modifiedData),
|
|
76
|
+
});
|
|
77
|
+
self.log(`已拦截并修改响应: ${requestUrl}`);
|
|
74
78
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
79
|
+
catch (error) {
|
|
80
|
+
self.log("拦截器处理失败:", error);
|
|
81
|
+
// 出错时保持原始响应
|
|
78
82
|
}
|
|
79
|
-
}
|
|
83
|
+
}
|
|
84
|
+
// 调用原始的 onreadystatechange
|
|
85
|
+
if (originalOnReadyStateChange) {
|
|
86
|
+
originalOnReadyStateChange.call(this, event);
|
|
87
|
+
}
|
|
80
88
|
};
|
|
81
|
-
return originalSend.call(this,
|
|
89
|
+
return originalSend.call(this, processedBody);
|
|
82
90
|
};
|
|
83
91
|
return xhr;
|
|
84
92
|
};
|
|
@@ -110,8 +118,8 @@ class XHRInterceptor {
|
|
|
110
118
|
/**
|
|
111
119
|
* 添加单个拦截规则
|
|
112
120
|
*/
|
|
113
|
-
addRule(url, responseCallback) {
|
|
114
|
-
this.interceptRules.push({ url, responseCallback });
|
|
121
|
+
addRule(url, responseCallback, requestCallback) {
|
|
122
|
+
this.interceptRules.push({ url, responseCallback, requestCallback });
|
|
115
123
|
this.log(`已添加拦截规则: ${url}`);
|
|
116
124
|
}
|
|
117
125
|
/**
|
package/lib/document/index.d.ts
CHANGED
|
@@ -1,7 +1,24 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @fileoverview Document模块
|
|
2
|
+
* @fileoverview Document 模块
|
|
3
3
|
* @created 2025-04-02
|
|
4
4
|
* @author glk
|
|
5
5
|
*/
|
|
6
|
-
declare
|
|
7
|
-
|
|
6
|
+
declare const addStyleStr: (styStr?: string) => HTMLStyleElement;
|
|
7
|
+
/**
|
|
8
|
+
* 创建一个全局唯一的遮罩 Loading
|
|
9
|
+
* @param msg
|
|
10
|
+
* @param style
|
|
11
|
+
* @returns
|
|
12
|
+
*/
|
|
13
|
+
declare const createMaskLoading: (msg?: string, style?: string) => {
|
|
14
|
+
/**
|
|
15
|
+
* 关闭loading
|
|
16
|
+
*/
|
|
17
|
+
close: () => void;
|
|
18
|
+
/**
|
|
19
|
+
* 更新信息
|
|
20
|
+
* @param msg
|
|
21
|
+
*/
|
|
22
|
+
updateMsg: (msg: string) => void;
|
|
23
|
+
};
|
|
24
|
+
export { addStyleStr, createMaskLoading };
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var index = require('../constant/index.js');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @fileoverview Document 模块
|
|
7
|
+
* @created 2025-04-02
|
|
8
|
+
* @author glk
|
|
9
|
+
*/
|
|
10
|
+
const addStyleStr = (styStr = "") => {
|
|
11
|
+
let _style = document.createElement("style");
|
|
12
|
+
_style.innerHTML = styStr;
|
|
13
|
+
document.getElementsByTagName("head")[0].appendChild(_style);
|
|
14
|
+
return _style;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* 创建一个全局唯一的遮罩 Loading
|
|
18
|
+
* @param msg
|
|
19
|
+
* @param style
|
|
20
|
+
* @returns
|
|
21
|
+
*/
|
|
22
|
+
const createMaskLoading = (msg = "加载中...", style = "") => {
|
|
23
|
+
const Container_Id = `${index.Lib_Name}-mask-loading-container`;
|
|
24
|
+
const prevLoading = document.getElementById(Container_Id);
|
|
25
|
+
!!prevLoading && document.body.removeChild(prevLoading);
|
|
26
|
+
const ele = document.createElement("div");
|
|
27
|
+
ele.id = Container_Id;
|
|
28
|
+
ele.innerHTML = `
|
|
29
|
+
<div class="mask"></div>
|
|
30
|
+
<div class="message">${msg}</div>
|
|
31
|
+
`;
|
|
32
|
+
const styleEle = document.createElement("style");
|
|
33
|
+
styleEle.innerHTML = `
|
|
34
|
+
#${Container_Id} {
|
|
35
|
+
position: fixed;
|
|
36
|
+
top: 0;
|
|
37
|
+
left: 0;
|
|
38
|
+
width: 100%;
|
|
39
|
+
height: 100%;
|
|
40
|
+
z-index: 9999;
|
|
41
|
+
transition: opacity 0.3s;
|
|
42
|
+
}
|
|
43
|
+
#${Container_Id} .mask {
|
|
44
|
+
position: absolute;
|
|
45
|
+
top: 0;
|
|
46
|
+
left: 0;
|
|
47
|
+
width: 100%;
|
|
48
|
+
height: 100%;
|
|
49
|
+
background-color: rgba(0, 0, 0, 0.45);
|
|
50
|
+
}
|
|
51
|
+
#${Container_Id} .message {
|
|
52
|
+
position: absolute;
|
|
53
|
+
top: 50%;
|
|
54
|
+
left: 50%;
|
|
55
|
+
transform: translate(-50%, -50%);
|
|
56
|
+
color: #fff;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
${style}
|
|
60
|
+
`;
|
|
61
|
+
document.head.appendChild(styleEle);
|
|
62
|
+
document.body.append(ele);
|
|
63
|
+
return {
|
|
64
|
+
/**
|
|
65
|
+
* 关闭loading
|
|
66
|
+
*/
|
|
67
|
+
close: () => {
|
|
68
|
+
document.body.removeChild(ele);
|
|
69
|
+
},
|
|
70
|
+
/**
|
|
71
|
+
* 更新信息
|
|
72
|
+
* @param msg
|
|
73
|
+
*/
|
|
74
|
+
updateMsg: (msg) => {
|
|
75
|
+
ele.querySelector(".message").innerHTML = msg;
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
exports.addStyleStr = addStyleStr;
|
|
81
|
+
exports.createMaskLoading = createMaskLoading;
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -8,6 +8,8 @@ var index$4 = require('./regexp/index.js');
|
|
|
8
8
|
var index$5 = require('./form/index.js');
|
|
9
9
|
var index$6 = require('./count/index.js');
|
|
10
10
|
var index$7 = require('./xhr/index.js');
|
|
11
|
+
var index$8 = require('./constant/index.js');
|
|
12
|
+
var index$9 = require('./document/index.js');
|
|
11
13
|
|
|
12
14
|
|
|
13
15
|
|
|
@@ -27,3 +29,6 @@ exports.createFormData = index$5.createFormData;
|
|
|
27
29
|
exports.precisionFormat = index$6.precisionFormat;
|
|
28
30
|
exports.XHRInterceptor = index$7.XHRInterceptor;
|
|
29
31
|
exports.xhrInterceptor = index$7.xhrInterceptor;
|
|
32
|
+
exports.Lib_Name = index$8.Lib_Name;
|
|
33
|
+
exports.addStyleStr = index$9.addStyleStr;
|
|
34
|
+
exports.createMaskLoading = index$9.createMaskLoading;
|
package/lib/xhr/index.d.ts
CHANGED
|
@@ -7,8 +7,12 @@
|
|
|
7
7
|
* 拦截规则
|
|
8
8
|
*/
|
|
9
9
|
interface InterceptRule {
|
|
10
|
+
/** 拦截的 URL,模糊匹配 */
|
|
10
11
|
url: string;
|
|
11
|
-
|
|
12
|
+
/** 拦截响应的回调函数 */
|
|
13
|
+
responseCallback: (data: any) => any;
|
|
14
|
+
/** 拦截请求的回调函数 */
|
|
15
|
+
requestCallback?: (data: any) => any;
|
|
12
16
|
}
|
|
13
17
|
/**
|
|
14
18
|
* 拦截器配置
|
|
@@ -40,7 +44,7 @@ declare class XHRInterceptor {
|
|
|
40
44
|
/**
|
|
41
45
|
* 添加单个拦截规则
|
|
42
46
|
*/
|
|
43
|
-
addRule(url: string, responseCallback: InterceptRule["responseCallback"]): void;
|
|
47
|
+
addRule(url: string, responseCallback: InterceptRule["responseCallback"], requestCallback?: InterceptRule["requestCallback"]): void;
|
|
44
48
|
/**
|
|
45
49
|
* 移除指定URL的拦截规则
|
|
46
50
|
*/
|
package/lib/xhr/index.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var tslib_es6 = require('../node_modules/tslib/tslib.es6.js');
|
|
4
|
-
|
|
5
3
|
/**
|
|
6
4
|
* @fileoverview XMLHttpRequest模块
|
|
7
5
|
* @created 2025-04-02
|
|
@@ -46,41 +44,51 @@ class XHRInterceptor {
|
|
|
46
44
|
// 没有匹配的规则,直接执行原始请求
|
|
47
45
|
return originalSend.call(this, body);
|
|
48
46
|
}
|
|
47
|
+
// 处理请求数据拦截
|
|
48
|
+
let processedBody = body;
|
|
49
|
+
if (matchingRule.requestCallback && body) {
|
|
50
|
+
try {
|
|
51
|
+
processedBody = matchingRule.requestCallback(body);
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
console.error('XHR请求拦截器处理失败:', error);
|
|
55
|
+
// 出错时使用原始请求体
|
|
56
|
+
processedBody = body;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
49
59
|
// 保存原始的 onreadystatechange
|
|
50
60
|
const originalOnReadyStateChange = xhr.onreadystatechange;
|
|
51
61
|
xhr.onreadystatechange = function (event) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
self.log(`已拦截并修改响应: ${requestUrl}`);
|
|
71
|
-
}
|
|
72
|
-
catch (error) {
|
|
73
|
-
self.log("拦截器处理失败:", error);
|
|
74
|
-
// 出错时保持原始响应
|
|
75
|
-
}
|
|
62
|
+
if (xhr.readyState === 4 && xhr.status >= 200 && xhr.status < 300) {
|
|
63
|
+
try {
|
|
64
|
+
// 解析原始响应
|
|
65
|
+
const originalData = JSON.parse(xhr.responseText);
|
|
66
|
+
// 使用回调函数处理响应数据
|
|
67
|
+
const modifiedData = matchingRule.responseCallback(originalData);
|
|
68
|
+
// 重写响应属性
|
|
69
|
+
Object.defineProperty(xhr, "responseText", {
|
|
70
|
+
writable: true,
|
|
71
|
+
configurable: true,
|
|
72
|
+
value: JSON.stringify(modifiedData),
|
|
73
|
+
});
|
|
74
|
+
Object.defineProperty(xhr, "response", {
|
|
75
|
+
writable: true,
|
|
76
|
+
configurable: true,
|
|
77
|
+
value: JSON.stringify(modifiedData),
|
|
78
|
+
});
|
|
79
|
+
self.log(`已拦截并修改响应: ${requestUrl}`);
|
|
76
80
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
81
|
+
catch (error) {
|
|
82
|
+
self.log("拦截器处理失败:", error);
|
|
83
|
+
// 出错时保持原始响应
|
|
80
84
|
}
|
|
81
|
-
}
|
|
85
|
+
}
|
|
86
|
+
// 调用原始的 onreadystatechange
|
|
87
|
+
if (originalOnReadyStateChange) {
|
|
88
|
+
originalOnReadyStateChange.call(this, event);
|
|
89
|
+
}
|
|
82
90
|
};
|
|
83
|
-
return originalSend.call(this,
|
|
91
|
+
return originalSend.call(this, processedBody);
|
|
84
92
|
};
|
|
85
93
|
return xhr;
|
|
86
94
|
};
|
|
@@ -112,8 +120,8 @@ class XHRInterceptor {
|
|
|
112
120
|
/**
|
|
113
121
|
* 添加单个拦截规则
|
|
114
122
|
*/
|
|
115
|
-
addRule(url, responseCallback) {
|
|
116
|
-
this.interceptRules.push({ url, responseCallback });
|
|
123
|
+
addRule(url, responseCallback, requestCallback) {
|
|
124
|
+
this.interceptRules.push({ url, responseCallback, requestCallback });
|
|
117
125
|
this.log(`已添加拦截规则: ${url}`);
|
|
118
126
|
}
|
|
119
127
|
/**
|
package/package.json
CHANGED
package/es/loger/index.d.ts
DELETED