@zjlab-fe/util 0.0.2 → 0.0.3

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.
@@ -0,0 +1,15 @@
1
+ module.exports = {
2
+ printWidth: 140,
3
+ tabWidth: 2,
4
+ semi: true,
5
+ singleQuote: true,
6
+ trailingComma: 'all',
7
+ bracketSpacing: true,
8
+ jsxBracketSameLine: false,
9
+ jsxSingleQuote: false,
10
+ arrowParens: 'always',
11
+ insertPragma: false,
12
+ requirePragma: false,
13
+ useTabs: false,
14
+ endOfLine: 'auto',
15
+ };
package/README.md CHANGED
@@ -1,6 +1,5 @@
1
1
  ## API
2
2
 
3
- ### 下载文件
4
3
 
5
4
  ```javascript
6
5
  /**
@@ -9,30 +8,107 @@
9
8
  * @param openWindow {bool} 是否是新开标签页下载
10
9
  */
11
10
  export declare function downloadFile(url: string, openWindow?: boolean): void;
12
- ```
13
-
14
- ### 提取文件类型
15
-
16
- ```javascript
17
11
  /**
18
12
  * 提取文件类型
19
13
  * @param filePath {string} 文件路径 or 文件url
20
14
  * @returns ext {string} 文件类型
21
15
  */
22
16
  export declare function getFileExt(filePath: string): string;
23
- ```
24
-
25
-
26
- ### 深度遍历对字符串进行trim操作
27
-
28
- ```javascript
29
17
  /**
30
18
  * 深度遍历,对于类型是字符串的value进行trim操作
31
19
  * @param obj 原始的数据
32
20
  * @returns newObj trim操作后的数据
33
21
  */
34
22
  export declare function trimObj<T>(obj: T): T;
35
- ```
36
-
23
+ /**
24
+ * iframe通信类
25
+ */
26
+ export declare class IfmChannel {
27
+ onMessageCallback: Record<string, any>;
28
+ /**
29
+ * 向iframe发送消息
30
+ * @param ifmDom
31
+ * @param type
32
+ * @param data
33
+ * @param callback
34
+ */
35
+ postMessageToIfm(ifmDom: HTMLIFrameElement, type: string, data?: any, callback?: (data: any) => void): void;
36
+ /**
37
+ * 向父窗口发送消息
38
+ * @param type {string} 消息类型
39
+ * @param data {any} 可选,发送的数据
40
+ * @param callback {function} 可选,如果传入,表示等待消息回应,等同于onMessage(type, callback, true)
41
+ */
42
+ postMessage(type: string, data?: any, callback?: (data: any) => void): void;
43
+ /**
44
+ * 监听来自父窗口的消息 or 父窗口监听来自iframe的消息
45
+ * @param type {string} 消息类型
46
+ * @param callback {function} 消息处理函数
47
+ * @param isOnce {boolean} 可选,表示消息处理函数是否是一次性的
48
+ */
49
+ onMessage(type: string, callback: (data: any) => void, isOnce?: boolean): void;
50
+ /**
51
+ * 销毁事件监听
52
+ */
53
+ destroy(): void;
54
+ }
55
+ interface IUserInfo {
56
+ /** 用户id */
57
+ userId: string;
58
+ /** 用户名 */
59
+ userName: string;
60
+ /** 邮箱 */
61
+ email: string;
62
+ /** 昵称 */
63
+ nickName: string;
64
+ /** 头像 */
65
+ avatar: string;
66
+ /** 所在组织名 */
67
+ orgName: string;
68
+ /** 所在组织code */
69
+ orgCode: string;
70
+ /** 所在组织id */
71
+ orgId: string;
72
+ /** 账号状态 */
73
+ status: number;
74
+ /** 用于组织内部的用户名称 */
75
+ realName?: string;
76
+ /** 根组织id,只有当前登录用户的用户信息才包含 */
77
+ rootOrgId?: string;
78
+ }
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';
37
112
 
113
+ ```
38
114
 
package/dist/index.esm.js CHANGED
@@ -1 +1 @@
1
- import{message as t}from"antd";var n=[],e=!1;function r(r,i){i?window.open(r):(n.push(r),e||(e=!0,t.loading("文件下载中"),o()))}function o(){if(n.length>0){var r=n.shift();fetch(r).then((function(t){if(200!==(null==t?void 0:t.status))return Promise.reject({message:null==t?void 0:t.statusText});var n=document.createElement("a");n.style.display="none",document.body.appendChild(n),n.href=r,n.click(),document.body.removeChild(n)})).catch((function(){t.error("文件下载异常:".concat(r))})).finally((function(){setTimeout((function(){o()}),1e3)}))}else e=!1}function i(t){var n=t,e=t.indexOf("?");-1!==e&&(n=t.substring(0,e));var r=n.lastIndexOf(".");return n.substring(r+1)}function c(t){if(Array.isArray(t)){var n=[];return t.forEach((function(t){n.push(c(t))})),n}if("[object Object]"===Object.prototype.toString.call(t)){var e={};return Object.keys(t).forEach((function(n){e[n]=c(t[n])})),e}return"string"==typeof t?t.trim():t}export{r as downloadFile,i as getFileExt,c as trimObj};
1
+ import{message as e}from"antd";import n from"query-string";import o from"axios";function t(e){var n=new d,t=o.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 o;return 401===(null===(o=null==e?void 0:e.data)||void 0===o?void 0:o.code)?(n.postMessage("login"),Promise.reject({code:401,msg:"用户未登录"})):e}),(function(e){e.response&&(401===e.response.status&&n.postMessage("login"));return Promise.reject(e)})),t}var a=[],i=!1;function r(n,o){o?window.open(n):(a.push(n),i||(i=!0,e.loading("文件下载中"),s()))}function s(){if(a.length>0){var n=a.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(){e.error("文件下载异常:".concat(n))})).finally((function(){setTimeout((function(){s()}),1e3)}))}else i=!1}function c(e){var n=e,o=e.indexOf("?");-1!==o&&(n=e.substring(0,o));var t=n.lastIndexOf(".");return n.substring(t+1)}function u(e){if(Array.isArray(e)){var n=[];return e.forEach((function(e){n.push(u(e))})),n}if("[object Object]"===Object.prototype.toString.call(e)){var o={};return Object.keys(e).forEach((function(n){o[n]=u(e[n])})),o}return"string"==typeof e?e.trim():e}var l,f="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 d=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(f,"_").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(f,"_").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(f,"_").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 p(){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 w(e){var o=n.parse(window.location.search);return(null==o?void 0:o.iframeUrl)?decodeURIComponent(o.iframeUrl):e}function h(e,n){var o=new d;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&&o.postMessage(l.PUSH_STATE,{href:-1!==a.indexOf("//")?a:window.location.origin+a})};var a=window.history.replaceState;window.history.replaceState=function(e,n,t){a.call(window.history,e,n,t),t&&o.postMessage(l.REPLACE_STATE,{href:-1!==t.indexOf("//")?t:window.location.origin+t})},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{d as IfmChannel,r as downloadFile,t as getAxiosInstanceWithAuth,c as getFileExt,w as getIframeUrl,p as mainAppInit,h as subAppInit,u as trimObj};
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var t=require("antd"),e=[],n=!1;function r(){if(e.length>0){var i=e.shift();fetch(i).then((function(t){if(200!==(null==t?void 0:t.status))return Promise.reject({message:null==t?void 0:t.statusText});var e=document.createElement("a");e.style.display="none",document.body.appendChild(e),e.href=i,e.click(),document.body.removeChild(e)})).catch((function(){t.message.error("文件下载异常:".concat(i))})).finally((function(){setTimeout((function(){r()}),1e3)}))}else n=!1}exports.downloadFile=function(i,o){o?window.open(i):(e.push(i),n||(n=!0,t.message.loading("文件下载中"),r()))},exports.getFileExt=function(t){var e=t,n=t.indexOf("?");-1!==n&&(e=t.substring(0,n));var r=e.lastIndexOf(".");return e.substring(r+1)},exports.trimObj=function t(e){if(Array.isArray(e)){var n=[];return e.forEach((function(e){n.push(t(e))})),n}if("[object Object]"===Object.prototype.toString.call(e)){var r={};return Object.keys(e).forEach((function(n){r[n]=t(e[n])})),r}return"string"==typeof e?e.trim():e};
1
+ "use strict";var e=require("antd"),n=require("query-string"),t=require("axios");var o=[],a=!1;function r(){if(o.length>0){var n=o.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(){e.message.error("文件下载异常:".concat(n))})).finally((function(){setTimeout((function(){r()}),1e3)}))}else a=!1}var i,s="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 c=function(){function e(){this.onMessageCallback={}}return e.prototype.postMessageToIfm=function(e,n,t,o){var a;if(e){var r=void 0;"function"==typeof t?r=t:o&&(r=o);var i="".concat(s,"_").concat(n);r&&this.onMessage(n,r,!0),null===(a=e.contentWindow)||void 0===a||a.postMessage({type:i,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(s,"_").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 r=a.data||{},c=r.data,u=r.type;-1!==[i.HASH_CHANGE,i.PUSH_STATE,i.REPLACE_STATE].indexOf(e)?u===e&&n(c):u==="".concat(s,"_").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=c,exports.downloadFile=function(n,t){t?window.open(n):(o.push(n),a||(a=!0,e.message.loading("文件下载中"),r()))},exports.getAxiosInstanceWithAuth=function(e){var n=new c,o=t.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 t;return 401===(null===(t=null==e?void 0:e.data)||void 0===t?void 0:t.code)?(n.postMessage("login"),Promise.reject({code:401,msg:"用户未登录"})):e}),(function(e){e.response&&(401===e.response.status&&n.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(e){var t=n.parse(window.location.search);return(null==t?void 0:t.iframeUrl)?decodeURIComponent(t.iframeUrl):e},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 c;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&&t.postMessage(i.PUSH_STATE,{href:-1!==a.indexOf("//")?a:window.location.origin+a})};var a=window.history.replaceState;window.history.replaceState=function(e,n,o){a.call(window.history,e,n,o),o&&t.postMessage(i.REPLACE_STATE,{href:-1!==o.indexOf("//")?o:window.location.origin+o})},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};
@@ -0,0 +1,11 @@
1
+ interface IConfig {
2
+ /** 账号关联的token存放在了window[authTokenNamespace]下,authTokenNamespace的默认值是_subApp,同subAppInit中的namespace */
3
+ authTokenNamespace: string;
4
+ }
5
+ /**
6
+ * 获取经过配置的axios实例
7
+ * @param instanceConfig {IConfig} 实例化配置
8
+ * @returns axios实例
9
+ */
10
+ export declare function getAxiosInstanceWithAuth(instanceConfig?: IConfig): import("axios").AxiosInstance;
11
+ export {};
@@ -16,3 +16,93 @@ export declare function getFileExt(filePath: string): string;
16
16
  * @returns newObj trim操作后的数据
17
17
  */
18
18
  export declare function trimObj<T>(obj: T): T;
19
+ /**
20
+ * iframe通信类
21
+ */
22
+ export declare class IfmChannel {
23
+ onMessageCallback: Record<string, any>;
24
+ /**
25
+ * 向iframe发送消息
26
+ * @param ifmDom
27
+ * @param type
28
+ * @param data
29
+ * @param callback
30
+ */
31
+ postMessageToIfm(ifmDom: HTMLIFrameElement, type: string, data?: any, callback?: (data: any) => void): void;
32
+ /**
33
+ * 向父窗口发送消息
34
+ * @param type {string} 消息类型
35
+ * @param data {any} 可选,发送的数据
36
+ * @param callback {function} 可选,如果传入,表示等待消息回应,等同于onMessage(type, callback, true)
37
+ */
38
+ postMessage(type: string, data?: any, callback?: (data: any) => void): void;
39
+ /**
40
+ * 监听来自父窗口的消息 or 父窗口监听来自iframe的消息
41
+ * @param type {string} 消息类型
42
+ * @param callback {function} 消息处理函数
43
+ * @param isOnce {boolean} 可选,表示消息处理函数是否是一次性的
44
+ */
45
+ onMessage(type: string, callback: (data: any) => void, isOnce?: boolean): void;
46
+ /**
47
+ * 销毁事件监听
48
+ */
49
+ destroy(): void;
50
+ }
51
+ interface IUserInfo {
52
+ /** 用户id */
53
+ userId: string;
54
+ /** 用户名 */
55
+ userName: string;
56
+ /** 邮箱 */
57
+ email: string;
58
+ /** 昵称 */
59
+ nickName: string;
60
+ /** 头像 */
61
+ avatar: string;
62
+ /** 所在组织名 */
63
+ orgName: string;
64
+ /** 所在组织code */
65
+ orgCode: string;
66
+ /** 所在组织id */
67
+ orgId: string;
68
+ /** 账号状态 */
69
+ status: number;
70
+ /** 用于组织内部的用户名称 */
71
+ realName?: string;
72
+ /** 根组织id,只有当前登录用户的用户信息才包含 */
73
+ rootOrgId?: string;
74
+ }
75
+ interface SubAppInitData {
76
+ token: string | undefined;
77
+ userInfo: IUserInfo | null;
78
+ }
79
+ /**
80
+ * 主应用初始化,监听路由
81
+ */
82
+ export declare function mainAppInit(): void;
83
+ /**
84
+ * 获取iframe的最终url
85
+ * @param initUrl {string} iframe初始url
86
+ * @returns
87
+ */
88
+ export declare function getIframeUrl(initUrl: string): string;
89
+ /**
90
+ * 子应用初始化配置对象类型
91
+ */
92
+ interface ISubAppInitConfig {
93
+ /** 账号信息数据会存放在window[namespace]下,默认值是_subApp,默认会将账号信息数据同步到window._subApp下 */
94
+ namespace: string;
95
+ }
96
+ /**
97
+ * 子应用初始化
98
+ * @param callback (data: SubAppInitData) => void) 可选,回调函数中会返回token和用户信息
99
+ */
100
+ /**
101
+ * 子应用初始化
102
+ * @param callback 可选,回调函数中会传回来自父窗口的账号信息
103
+ * @param config {ISubAppInitConfig} 可选
104
+ *
105
+ */
106
+ export declare function subAppInit(callback?: (data: SubAppInitData) => void, config?: ISubAppInitConfig): void;
107
+ /** 获取经过配置的axios实例 */
108
+ export { getAxiosInstanceWithAuth } from './axios';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zjlab-fe/util",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.esm.js",
@@ -19,7 +19,9 @@
19
19
  "typescript": "^5.5.4"
20
20
  },
21
21
  "dependencies": {
22
- "antd": "^5.20.0"
22
+ "antd": "^5.20.0",
23
+ "axios": "^1.7.4",
24
+ "query-string": "^9.1.0"
23
25
  },
24
26
  "publishConfig": {
25
27
  "registry": "https://registry.npmjs.org/"