@zjlab-fe/util 0.0.3 → 0.0.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 +102 -38
- package/dist/index.esm.js +1 -1
- package/dist/index.js +1 -1
- package/dist/types/index.d.ts +4 -7
- package/package.json +1 -2
package/README.md
CHANGED
|
@@ -1,29 +1,52 @@
|
|
|
1
1
|
## API
|
|
2
2
|
|
|
3
|
+
### 下载文件
|
|
4
|
+
|
|
5
|
+
支持多个文件同时下载
|
|
3
6
|
|
|
4
7
|
```javascript
|
|
5
8
|
/**
|
|
6
9
|
* 下载文件
|
|
7
10
|
* @param url {string} 文件url
|
|
8
|
-
* @param openWindow {bool}
|
|
11
|
+
* @param openWindow {bool} 可选,是否是新开标签页下载
|
|
12
|
+
* @param message {object} 可选,如果传入,需要传入antd或者antdv的message
|
|
9
13
|
*/
|
|
10
|
-
|
|
14
|
+
function downloadFile(url: string, openWindow?: boolean, message?: any): void;
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
### 提取文件类型
|
|
18
|
+
|
|
19
|
+
```javascript
|
|
11
20
|
/**
|
|
12
21
|
* 提取文件类型
|
|
13
22
|
* @param filePath {string} 文件路径 or 文件url
|
|
14
23
|
* @returns ext {string} 文件类型
|
|
15
24
|
*/
|
|
16
|
-
|
|
25
|
+
function getFileExt(filePath: string): string;
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
### 对object或者array进行trim操作
|
|
30
|
+
|
|
31
|
+
```javascript
|
|
17
32
|
/**
|
|
18
33
|
* 深度遍历,对于类型是字符串的value进行trim操作
|
|
19
34
|
* @param obj 原始的数据
|
|
20
35
|
* @returns newObj trim操作后的数据
|
|
21
36
|
*/
|
|
22
|
-
|
|
37
|
+
function trimObj<T>(obj: T): T;
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
### iframe通信相关
|
|
42
|
+
|
|
43
|
+
#### 通信类
|
|
44
|
+
|
|
45
|
+
```javascript
|
|
23
46
|
/**
|
|
24
47
|
* iframe通信类
|
|
25
48
|
*/
|
|
26
|
-
|
|
49
|
+
class IfmChannel {
|
|
27
50
|
onMessageCallback: Record<string, any>;
|
|
28
51
|
/**
|
|
29
52
|
* 向iframe发送消息
|
|
@@ -52,6 +75,80 @@ export declare class IfmChannel {
|
|
|
52
75
|
*/
|
|
53
76
|
destroy(): void;
|
|
54
77
|
}
|
|
78
|
+
|
|
79
|
+
// 使用
|
|
80
|
+
const ifmChannel = new IfmChannel();
|
|
81
|
+
// 给父窗口发送消息
|
|
82
|
+
ifmChannel.postMessage('messageType', {
|
|
83
|
+
name: 'xxx',
|
|
84
|
+
projectId: 'xxx'
|
|
85
|
+
});
|
|
86
|
+
// 监听来自父窗口的消息
|
|
87
|
+
ifmChannel.onMessage('messageType', (data: any) => {
|
|
88
|
+
// ...
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
#### iframe嵌入,主应用相关api
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
```javascript
|
|
97
|
+
/**
|
|
98
|
+
* 主应用初始化,监听路由
|
|
99
|
+
*/
|
|
100
|
+
function mainAppInit(): void;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* 获取iframe的最终url
|
|
104
|
+
* @param initUrl {string} iframe初始url
|
|
105
|
+
* @returns
|
|
106
|
+
*/
|
|
107
|
+
function getIframeUrl(initUrl: string): string;
|
|
108
|
+
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
#### iframe嵌入,子应用相关api
|
|
113
|
+
|
|
114
|
+
```javascript
|
|
115
|
+
interface SubAppInitData {
|
|
116
|
+
token: string | undefined;
|
|
117
|
+
userInfo: IUserInfo | null;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* 子应用初始化配置对象类型
|
|
123
|
+
*/
|
|
124
|
+
interface ISubAppInitConfig {
|
|
125
|
+
/** 账号信息数据会存放在window[namespace]下,默认值是_subApp,默认会将账号信息数据同步到window._subApp下 */
|
|
126
|
+
namespace: string;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* 子应用初始化
|
|
130
|
+
* @param callback (data: SubAppInitData) => void) 可选,回调函数中会传回来自父窗口的账号信息
|
|
131
|
+
* @param config {ISubAppInitConfig} 可选
|
|
132
|
+
*
|
|
133
|
+
*/
|
|
134
|
+
function subAppInit(callback?: (data: SubAppInitData) => void, config?: ISubAppInitConfig): void;
|
|
135
|
+
|
|
136
|
+
interface IConfig {
|
|
137
|
+
/** 账号关联的token存放在了window[authTokenNamespace]下,authTokenNamespace的默认值是_subApp,同subAppInit中的namespace */
|
|
138
|
+
authTokenNamespace: string;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* 获取经过配置的axios实例
|
|
142
|
+
* @param instanceConfig {IConfig} 实例化配置
|
|
143
|
+
* @returns axios实例
|
|
144
|
+
*/
|
|
145
|
+
function getAxiosInstanceWithAuth(instanceConfig?: IConfig): import("axios").AxiosInstance;
|
|
146
|
+
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### 账号信息类型定义
|
|
150
|
+
|
|
151
|
+
```javascript
|
|
55
152
|
interface IUserInfo {
|
|
56
153
|
/** 用户id */
|
|
57
154
|
userId: string;
|
|
@@ -76,39 +173,6 @@ interface IUserInfo {
|
|
|
76
173
|
/** 根组织id,只有当前登录用户的用户信息才包含 */
|
|
77
174
|
rootOrgId?: string;
|
|
78
175
|
}
|
|
79
|
-
interface SubAppInitData {
|
|
80
|
-
token: string | undefined;
|
|
81
|
-
userInfo: IUserInfo | null;
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* 主应用初始化,监听路由
|
|
85
|
-
*/
|
|
86
|
-
export declare function mainAppInit(): void;
|
|
87
|
-
/**
|
|
88
|
-
* 获取iframe的最终url
|
|
89
|
-
* @param initUrl {string} iframe初始url
|
|
90
|
-
* @returns
|
|
91
|
-
*/
|
|
92
|
-
export declare function getIframeUrl(initUrl: string): string;
|
|
93
|
-
/**
|
|
94
|
-
* 子应用初始化配置对象类型
|
|
95
|
-
*/
|
|
96
|
-
interface ISubAppInitConfig {
|
|
97
|
-
/** 账号信息数据会存放在window[namespace]下,默认值是_subApp,默认会将账号信息数据同步到window._subApp下 */
|
|
98
|
-
namespace: string;
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* 子应用初始化
|
|
102
|
-
* @param callback (data: SubAppInitData) => void) 可选,回调函数中会返回token和用户信息
|
|
103
|
-
*/
|
|
104
|
-
/**
|
|
105
|
-
* 子应用初始化
|
|
106
|
-
* @param callback 可选,回调函数中会传回来自父窗口的账号信息
|
|
107
|
-
* @param config {ISubAppInitConfig} 可选
|
|
108
|
-
*
|
|
109
|
-
*/
|
|
110
|
-
export declare function subAppInit(callback?: (data: SubAppInitData) => void, config?: ISubAppInitConfig): void;
|
|
111
|
-
export { getAxiosInstanceWithAuth } from './axios';
|
|
112
176
|
|
|
113
177
|
```
|
|
114
178
|
|
package/dist/index.esm.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import
|
|
1
|
+
import e from"query-string";import n from"axios";function o(e){var o=new f,t=n.create({});return t.interceptors.request.use((function(n){var o,t=window[null!==(o=null==e?void 0:e.authTokenNamespace)&&void 0!==o?o:"_subApp"].token;return t&&(n.headers.Authorization="Bearer ".concat(t)),n}),(function(e){return Promise.reject(e)})),t.interceptors.response.use((function(e){var n;return 401===(null===(n=null==e?void 0:e.data)||void 0===n?void 0:n.code)?(o.postMessage("login"),Promise.reject({code:401,msg:"用户未登录"})):e}),(function(e){e.response&&(401===e.response.status&&o.postMessage("login"));return Promise.reject(e)})),t}var t=[],a=!1;function i(e,n,o){var i;n?window.open(e):(t.push(e),a||(a=!0,null===(i=null==o?void 0:o.loading)||void 0===i||i.call(o,"文件下载中"),r(o)))}function r(e){if(t.length>0){var n=t.shift();fetch(n).then((function(e){if(200!==(null==e?void 0:e.status))return Promise.reject({message:null==e?void 0:e.statusText});var o=document.createElement("a");o.style.display="none",document.body.appendChild(o),o.href=n,o.click(),document.body.removeChild(o)})).catch((function(){var o;null===(o=null==e?void 0:e.error)||void 0===o||o.call(e,"文件下载异常:".concat(n))})).finally((function(){setTimeout((function(){r(e)}),1e3)}))}else a=!1}function s(e){var n=e,o=e.indexOf("?");-1!==o&&(n=e.substring(0,o));var t=n.lastIndexOf(".");return n.substring(t+1)}function c(e){if(Array.isArray(e)){var n=[];return e.forEach((function(e){n.push(c(e))})),n}if("[object Object]"===Object.prototype.toString.call(e)){var o={};return Object.keys(e).forEach((function(n){o[n]=c(e[n])})),o}return"string"==typeof e?e.trim():e}var l,u="zjlab_util_ifm";!function(e){e.HASH_CHANGE="zjlab_util_ifm_hashChange",e.PUSH_STATE="zjlab_util_ifm_pushState",e.REPLACE_STATE="zjlab_util_ifm_replaceState"}(l||(l={}));var f=function(){function e(){this.onMessageCallback={}}return e.prototype.postMessageToIfm=function(e,n,o,t){var a;if(e){var i=void 0;"function"==typeof o?i=o:t&&(i=t);var r="".concat(u,"_").concat(n);i&&this.onMessage(n,i,!0),null===(a=e.contentWindow)||void 0===a||a.postMessage({type:r,data:"function"==typeof o?null:o},"*")}else console.error("postMessage error, no parent")},e.prototype.postMessage=function(e,n,o){if(window.parent&&window.parent!==window){var t=void 0;"function"==typeof n?t=n:o&&(t=o);var a="";a=-1!==[l.HASH_CHANGE,l.PUSH_STATE,l.REPLACE_STATE].indexOf(e)?e:"".concat(u,"_").concat(e),t&&this.onMessage(e,t,!0),window.parent.postMessage({type:a,data:"function"==typeof n?null:n},"*")}else console.error("postMessage error, no parent")},e.prototype.onMessage=function(e,n,o){var t=function(a){o&&window.removeEventListener("message",t);var i=a.data||{},r=i.data,s=i.type;-1!==[l.HASH_CHANGE,l.PUSH_STATE,l.REPLACE_STATE].indexOf(e)?s===e&&n(r):s==="".concat(u,"_").concat(e)&&n(r)};this.onMessageCallback[e]||(this.onMessageCallback[e]=[]),this.onMessageCallback[e].push(t),window.addEventListener("message",t)},e.prototype.destroy=function(){var e=this,n=Object.keys(this.onMessageCallback);n.length>0&&n.forEach((function(n){var o;null===(o=e.onMessageCallback[n])||void 0===o||o.forEach((function(e){window.removeEventListener("message",e)}))}))},e}();function d(){window.addEventListener("message",(function(e){var n=e.data||{},o=n.data,t=n.type;if(t===l.HASH_CHANGE||t===l.PUSH_STATE||t===l.REPLACE_STATE){var a="".concat(window.location.pathname,"?iframeUrl=").concat(encodeURIComponent(o.href));console.log("+++ iframe url data",t,o),window.history.replaceState(null,"",a)}}))}function p(n){var o=e.parse(window.location.search);return(null==o?void 0:o.iframeUrl)?decodeURIComponent(o.iframeUrl):n}function w(e,n){var o=new f;if(window.parent&&window.parent!==window){window.onhashchange=function(){o.postMessage(l.HASH_CHANGE,{href:window.location.href})};var t=window.history.pushState;window.history.pushState=function(e,n,a){t.call(window.history,e,n,a),a&&setTimeout((function(){o.postMessage(l.PUSH_STATE,{href:window.location.href})}),500)};var a=window.history.replaceState;window.history.replaceState=function(e,n,t){a.call(window.history,e,n,t),t&&setTimeout((function(){o.postMessage(l.REPLACE_STATE,{href:window.location.href})}),500)},window.addEventListener("popstate",(function(e){o.postMessage(l.REPLACE_STATE,{href:window.location.href})})),o.postMessage("getUserInfo",(function(o){var t;window[null!==(t=null==n?void 0:n.namespace)&&void 0!==t?t:"_subApp"]=o,e&&e(o)}))}}export{f as IfmChannel,i as downloadFile,o as getAxiosInstanceWithAuth,s as getFileExt,p as getIframeUrl,d as mainAppInit,w as subAppInit,c as trimObj};
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e=require("
|
|
1
|
+
"use strict";var e=require("query-string"),n=require("axios");var t=[],o=!1;function a(e){if(t.length>0){var n=t.shift();fetch(n).then((function(e){if(200!==(null==e?void 0:e.status))return Promise.reject({message:null==e?void 0:e.statusText});var t=document.createElement("a");t.style.display="none",document.body.appendChild(t),t.href=n,t.click(),document.body.removeChild(t)})).catch((function(){var t;null===(t=null==e?void 0:e.error)||void 0===t||t.call(e,"文件下载异常:".concat(n))})).finally((function(){setTimeout((function(){a(e)}),1e3)}))}else o=!1}var i,r="zjlab_util_ifm";!function(e){e.HASH_CHANGE="zjlab_util_ifm_hashChange",e.PUSH_STATE="zjlab_util_ifm_pushState",e.REPLACE_STATE="zjlab_util_ifm_replaceState"}(i||(i={}));var s=function(){function e(){this.onMessageCallback={}}return e.prototype.postMessageToIfm=function(e,n,t,o){var a;if(e){var i=void 0;"function"==typeof t?i=t:o&&(i=o);var s="".concat(r,"_").concat(n);i&&this.onMessage(n,i,!0),null===(a=e.contentWindow)||void 0===a||a.postMessage({type:s,data:"function"==typeof t?null:t},"*")}else console.error("postMessage error, no parent")},e.prototype.postMessage=function(e,n,t){if(window.parent&&window.parent!==window){var o=void 0;"function"==typeof n?o=n:t&&(o=t);var a="";a=-1!==[i.HASH_CHANGE,i.PUSH_STATE,i.REPLACE_STATE].indexOf(e)?e:"".concat(r,"_").concat(e),o&&this.onMessage(e,o,!0),window.parent.postMessage({type:a,data:"function"==typeof n?null:n},"*")}else console.error("postMessage error, no parent")},e.prototype.onMessage=function(e,n,t){var o=function(a){t&&window.removeEventListener("message",o);var s=a.data||{},c=s.data,l=s.type;-1!==[i.HASH_CHANGE,i.PUSH_STATE,i.REPLACE_STATE].indexOf(e)?l===e&&n(c):l==="".concat(r,"_").concat(e)&&n(c)};this.onMessageCallback[e]||(this.onMessageCallback[e]=[]),this.onMessageCallback[e].push(o),window.addEventListener("message",o)},e.prototype.destroy=function(){var e=this,n=Object.keys(this.onMessageCallback);n.length>0&&n.forEach((function(n){var t;null===(t=e.onMessageCallback[n])||void 0===t||t.forEach((function(e){window.removeEventListener("message",e)}))}))},e}();exports.IfmChannel=s,exports.downloadFile=function(e,n,i){var r;n?window.open(e):(t.push(e),o||(o=!0,null===(r=null==i?void 0:i.loading)||void 0===r||r.call(i,"文件下载中"),a(i)))},exports.getAxiosInstanceWithAuth=function(e){var t=new s,o=n.create({});return o.interceptors.request.use((function(n){var t,o=window[null!==(t=null==e?void 0:e.authTokenNamespace)&&void 0!==t?t:"_subApp"].token;return o&&(n.headers.Authorization="Bearer ".concat(o)),n}),(function(e){return Promise.reject(e)})),o.interceptors.response.use((function(e){var n;return 401===(null===(n=null==e?void 0:e.data)||void 0===n?void 0:n.code)?(t.postMessage("login"),Promise.reject({code:401,msg:"用户未登录"})):e}),(function(e){e.response&&(401===e.response.status&&t.postMessage("login"));return Promise.reject(e)})),o},exports.getFileExt=function(e){var n=e,t=e.indexOf("?");-1!==t&&(n=e.substring(0,t));var o=n.lastIndexOf(".");return n.substring(o+1)},exports.getIframeUrl=function(n){var t=e.parse(window.location.search);return(null==t?void 0:t.iframeUrl)?decodeURIComponent(t.iframeUrl):n},exports.mainAppInit=function(){window.addEventListener("message",(function(e){var n=e.data||{},t=n.data,o=n.type;if(o===i.HASH_CHANGE||o===i.PUSH_STATE||o===i.REPLACE_STATE){var a="".concat(window.location.pathname,"?iframeUrl=").concat(encodeURIComponent(t.href));console.log("+++ iframe url data",o,t),window.history.replaceState(null,"",a)}}))},exports.subAppInit=function(e,n){var t=new s;if(window.parent&&window.parent!==window){window.onhashchange=function(){t.postMessage(i.HASH_CHANGE,{href:window.location.href})};var o=window.history.pushState;window.history.pushState=function(e,n,a){o.call(window.history,e,n,a),a&&setTimeout((function(){t.postMessage(i.PUSH_STATE,{href:window.location.href})}),500)};var a=window.history.replaceState;window.history.replaceState=function(e,n,o){a.call(window.history,e,n,o),o&&setTimeout((function(){t.postMessage(i.REPLACE_STATE,{href:window.location.href})}),500)},window.addEventListener("popstate",(function(e){t.postMessage(i.REPLACE_STATE,{href:window.location.href})})),t.postMessage("getUserInfo",(function(t){var o;window[null!==(o=null==n?void 0:n.namespace)&&void 0!==o?o:"_subApp"]=t,e&&e(t)}))}},exports.trimObj=function e(n){if(Array.isArray(n)){var t=[];return n.forEach((function(n){t.push(e(n))})),t}if("[object Object]"===Object.prototype.toString.call(n)){var o={};return Object.keys(n).forEach((function(t){o[t]=e(n[t])})),o}return"string"==typeof n?n.trim():n};
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 下载文件
|
|
3
3
|
* @param url {string} 文件url
|
|
4
|
-
* @param openWindow {bool}
|
|
4
|
+
* @param openWindow {bool} 可选,是否是新开标签页下载
|
|
5
|
+
* @param message {object} 可选,如果传入,需要传入antd或者antdv的message
|
|
5
6
|
*/
|
|
6
|
-
export declare function downloadFile(url: string, openWindow?: boolean): void;
|
|
7
|
+
export declare function downloadFile(url: string, openWindow?: boolean, message?: any): void;
|
|
7
8
|
/**
|
|
8
9
|
* 提取文件类型
|
|
9
10
|
* @param filePath {string} 文件路径 or 文件url
|
|
@@ -95,11 +96,7 @@ interface ISubAppInitConfig {
|
|
|
95
96
|
}
|
|
96
97
|
/**
|
|
97
98
|
* 子应用初始化
|
|
98
|
-
* @param callback (data: SubAppInitData) => void)
|
|
99
|
-
*/
|
|
100
|
-
/**
|
|
101
|
-
* 子应用初始化
|
|
102
|
-
* @param callback 可选,回调函数中会传回来自父窗口的账号信息
|
|
99
|
+
* @param callback (data: SubAppInitData) => void) 可选,回调函数中会传回来自父窗口的账号信息
|
|
103
100
|
* @param config {ISubAppInitConfig} 可选
|
|
104
101
|
*
|
|
105
102
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zjlab-fe/util",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.esm.js",
|
|
@@ -19,7 +19,6 @@
|
|
|
19
19
|
"typescript": "^5.5.4"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"antd": "^5.20.0",
|
|
23
22
|
"axios": "^1.7.4",
|
|
24
23
|
"query-string": "^9.1.0"
|
|
25
24
|
},
|