@zjlab-fe/util 0.0.4 → 0.0.6
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.esm.js +1 -1
- package/dist/index.js +1 -1
- package/package.json +28 -28
package/dist/index.esm.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import e from"query-string";import n from"axios";function o(e){var o=new
|
|
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=[],i=!1;function a(e,n,o){var a;n?window.open(e):(t.push(e),i||(i=!0,null===(a=null==o?void 0:o.loading)||void 0===a||a.call(o,"文件下载中"),s(o)))}function r(e){if(e){var n=e.split("/").pop();if(n&&n.indexOf(".")>-1)return decodeURIComponent(n.split("?")[0])}return""}function s(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});if(r(n))return e.blob();var o=document.createElement("a");o.style.display="none",document.body.appendChild(o),o.href=n,o.click(),o.remove()})).then((function(e){var o=window.URL.createObjectURL(e),t=document.createElement("a");t.style.display="none",t.href=o,t.download=r(n),document.body.appendChild(t),t.click(),t.remove(),window.URL.revokeObjectURL(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(){s(e)}),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 l(e){if(Array.isArray(e)){var n=[];return e.forEach((function(e){n.push(l(e))})),n}if("[object Object]"===Object.prototype.toString.call(e)){var o={};return Object.keys(e).forEach((function(n){o[n]=l(e[n])})),o}return"string"==typeof e?e.trim():e}var u,d="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"}(u||(u={}));var f=function(){function e(){this.onMessageCallback={}}return e.prototype.postMessageToIfm=function(e,n,o,t){var i;if(e){var a=void 0;"function"==typeof o?a=o:t&&(a=t);var r="".concat(d,"_").concat(n);a&&this.onMessage(n,a,!0),null===(i=e.contentWindow)||void 0===i||i.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 i="";i=-1!==[u.HASH_CHANGE,u.PUSH_STATE,u.REPLACE_STATE].indexOf(e)?e:"".concat(d,"_").concat(e),t&&this.onMessage(e,t,!0),window.parent.postMessage({type:i,data:"function"==typeof n?null:n},"*")}else console.error("postMessage error, no parent")},e.prototype.onMessage=function(e,n,o){var t=function(i){o&&window.removeEventListener("message",t);var a=i.data||{},r=a.data,s=a.type;-1!==[u.HASH_CHANGE,u.PUSH_STATE,u.REPLACE_STATE].indexOf(e)?s===e&&n(r):s==="".concat(d,"_").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===u.HASH_CHANGE||t===u.PUSH_STATE||t===u.REPLACE_STATE){var i="".concat(window.location.pathname,"?iframeUrl=").concat(encodeURIComponent(o.href));console.log("+++ iframe url data",t,o),window.history.replaceState(null,"",i)}}))}function w(n){var o=e.parse(window.location.search);return(null==o?void 0:o.iframeUrl)?decodeURIComponent(o.iframeUrl):n}function v(e,n){var o=new f;if(window.parent&&window.parent!==window){window.onhashchange=function(){o.postMessage(u.HASH_CHANGE,{href:window.location.href})};var t=window.history.pushState;window.history.pushState=function(e,n,i){t.call(window.history,e,n,i),i&&o.postMessage(u.PUSH_STATE,{href:-1!==i.indexOf("//")?i:window.location.origin+i})};var i=window.history.replaceState;window.history.replaceState=function(e,n,t){i.call(window.history,e,n,t),t&&o.postMessage(u.REPLACE_STATE,{href:-1!==t.indexOf("//")?t:window.location.origin+t})},window.addEventListener("popstate",(function(e){o.postMessage(u.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,a as downloadFile,o as getAxiosInstanceWithAuth,c as getFileExt,w as getIframeUrl,p as mainAppInit,v as subAppInit,l as trimObj};
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e=require("query-string"),n=require("axios");var t=[],o=!1;function i(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.
|
|
1
|
+
"use strict";var e=require("query-string"),n=require("axios");var t=[],o=!1;function i(e){if(e){var n=e.split("/").pop();if(n&&n.indexOf(".")>-1)return decodeURIComponent(n.split("?")[0])}return""}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});if(i(n))return e.blob();var t=document.createElement("a");t.style.display="none",document.body.appendChild(t),t.href=n,t.click(),t.remove()})).then((function(e){var t=window.URL.createObjectURL(e),o=document.createElement("a");o.style.display="none",o.href=t,o.download=i(n),document.body.appendChild(o),o.click(),o.remove(),window.URL.revokeObjectURL(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(){r(e)}),1e3)}))}else o=!1}var a,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"}(a||(a={}));var c=function(){function e(){this.onMessageCallback={}}return e.prototype.postMessageToIfm=function(e,n,t,o){var i;if(e){var r=void 0;"function"==typeof t?r=t:o&&(r=o);var a="".concat(s,"_").concat(n);r&&this.onMessage(n,r,!0),null===(i=e.contentWindow)||void 0===i||i.postMessage({type:a,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 i="";i=-1!==[a.HASH_CHANGE,a.PUSH_STATE,a.REPLACE_STATE].indexOf(e)?e:"".concat(s,"_").concat(e),o&&this.onMessage(e,o,!0),window.parent.postMessage({type:i,data:"function"==typeof n?null:n},"*")}else console.error("postMessage error, no parent")},e.prototype.onMessage=function(e,n,t){var o=function(i){t&&window.removeEventListener("message",o);var r=i.data||{},c=r.data,l=r.type;-1!==[a.HASH_CHANGE,a.PUSH_STATE,a.REPLACE_STATE].indexOf(e)?l===e&&n(c):l==="".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(e,n,i){var a;n?window.open(e):(t.push(e),o||(o=!0,null===(a=null==i?void 0:i.loading)||void 0===a||a.call(i,"文件下载中"),r(i)))},exports.getAxiosInstanceWithAuth=function(e){var t=new c,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===a.HASH_CHANGE||o===a.PUSH_STATE||o===a.REPLACE_STATE){var i="".concat(window.location.pathname,"?iframeUrl=").concat(encodeURIComponent(t.href));console.log("+++ iframe url data",o,t),window.history.replaceState(null,"",i)}}))},exports.subAppInit=function(e,n){var t=new c;if(window.parent&&window.parent!==window){window.onhashchange=function(){t.postMessage(a.HASH_CHANGE,{href:window.location.href})};var o=window.history.pushState;window.history.pushState=function(e,n,i){o.call(window.history,e,n,i),i&&t.postMessage(a.PUSH_STATE,{href:-1!==i.indexOf("//")?i:window.location.origin+i})};var i=window.history.replaceState;window.history.replaceState=function(e,n,o){i.call(window.history,e,n,o),o&&t.postMessage(a.REPLACE_STATE,{href:-1!==o.indexOf("//")?o:window.location.origin+o})},window.addEventListener("popstate",(function(e){t.postMessage(a.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/package.json
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@zjlab-fe/util",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"type": "module",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"module": "dist/index.esm.js",
|
|
7
|
-
"types": "dist/types/index.d.ts",
|
|
8
|
-
"scripts": {
|
|
9
|
-
"build": "rollup -c"
|
|
10
|
-
},
|
|
11
|
-
"license": "ISC",
|
|
12
|
-
"devDependencies": {
|
|
13
|
-
"@rollup/plugin-commonjs": "^26.0.1",
|
|
14
|
-
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
15
|
-
"@rollup/plugin-terser": "^0.4.4",
|
|
16
|
-
"@rollup/plugin-typescript": "^11.1.6",
|
|
17
|
-
"rollup": "^4.20.0",
|
|
18
|
-
"tslib": "^2.6.3",
|
|
19
|
-
"typescript": "^5.5.4"
|
|
20
|
-
},
|
|
21
|
-
"dependencies": {
|
|
22
|
-
"axios": "^1.7.4",
|
|
23
|
-
"query-string": "^9.1.0"
|
|
24
|
-
},
|
|
25
|
-
"publishConfig": {
|
|
26
|
-
"registry": "https://registry.npmjs.org/"
|
|
27
|
-
}
|
|
28
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@zjlab-fe/util",
|
|
3
|
+
"version": "0.0.6",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.esm.js",
|
|
7
|
+
"types": "dist/types/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "rollup -c"
|
|
10
|
+
},
|
|
11
|
+
"license": "ISC",
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"@rollup/plugin-commonjs": "^26.0.1",
|
|
14
|
+
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
15
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
16
|
+
"@rollup/plugin-typescript": "^11.1.6",
|
|
17
|
+
"rollup": "^4.20.0",
|
|
18
|
+
"tslib": "^2.6.3",
|
|
19
|
+
"typescript": "^5.5.4"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"axios": "^1.7.4",
|
|
23
|
+
"query-string": "^9.1.0"
|
|
24
|
+
},
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"registry": "https://registry.npmjs.org/"
|
|
27
|
+
}
|
|
28
|
+
}
|