@zjlab-fe/util 0.1.0 → 0.2.0
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 +1 -1
package/dist/index.esm.js
CHANGED
|
@@ -1 +1 @@
|
|
|
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"],a=null==t?void 0:t.token;return a&&(n.headers.Authorization="Bearer ".concat(a)),(null==t?void 0:t.language)&&(n.headers.language=t.language),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(),o.remove()})).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 d,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 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
|
|
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"],a=null==t?void 0:t.token;return a&&(n.headers.Authorization="Bearer ".concat(a)),(null==t?void 0:t.language)&&(n.headers.language=t.language),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){var n,t,a,i,r;if(e.response&&(401===e.response.status&&o.postMessage("login"),(null===(t=null===(n=e.response)||void 0===n?void 0:n.data)||void 0===t?void 0:t.msg)||(null===(i=null===(a=e.response)||void 0===a?void 0:a.data)||void 0===i?void 0:i.message)))return Promise.reject(null===(r=e.response)||void 0===r?void 0:r.data);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(),o.remove()})).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 d,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 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 v(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&&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)}))}}!function(e){e.zh="zh",e.en="en"}(d||(d={}));export{f as IfmChannel,i as downloadFile,o as getAxiosInstanceWithAuth,s as getFileExt,v as getIframeUrl,p as mainAppInit,w as subAppInit,c as trimObj};
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e=require("query-string"),n=require("axios");var
|
|
1
|
+
"use strict";var e=require("query-string"),n=require("axios");var o=[],t=!1;function a(e){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 o=document.createElement("a");o.style.display="none",document.body.appendChild(o),o.href=n,o.click(),o.remove()})).catch((function(){var o;null===(o=null==e?void 0:e.error)||void 0===o||o.call(e,"文件下载异常:".concat(n))})).finally((function(){setTimeout((function(){a(e)}),1e3)}))}else t=!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,l=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 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 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!==[i.HASH_CHANGE,i.PUSH_STATE,i.REPLACE_STATE].indexOf(e)?e:"".concat(r,"_").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 s=a.data||{},l=s.data,c=s.type;-1!==[i.HASH_CHANGE,i.PUSH_STATE,i.REPLACE_STATE].indexOf(e)?c===e&&n(l):c==="".concat(r,"_").concat(e)&&n(l)};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(e){e.zh="zh",e.en="en"}(s||(s={})),exports.IfmChannel=l,exports.downloadFile=function(e,n,i){var r;n?window.open(e):(o.push(e),t||(t=!0,null===(r=null==i?void 0:i.loading)||void 0===r||r.call(i,"文件下载中"),a(i)))},exports.getAxiosInstanceWithAuth=function(e){var o=new l,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"],a=null==t?void 0:t.token;return a&&(n.headers.Authorization="Bearer ".concat(a)),(null==t?void 0:t.language)&&(n.headers.language=t.language),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){var n,t,a,i,r;if(e.response&&(401===e.response.status&&o.postMessage("login"),(null===(t=null===(n=e.response)||void 0===n?void 0:n.data)||void 0===t?void 0:t.msg)||(null===(i=null===(a=e.response)||void 0===a?void 0:a.data)||void 0===i?void 0:i.message)))return Promise.reject(null===(r=e.response)||void 0===r?void 0:r.data);return Promise.reject(e)})),t},exports.getFileExt=function(e){var n=e,o=e.indexOf("?");-1!==o&&(n=e.substring(0,o));var t=n.lastIndexOf(".");return n.substring(t+1)},exports.getIframeUrl=function(n){var o=e.parse(window.location.search);return(null==o?void 0:o.iframeUrl)?decodeURIComponent(o.iframeUrl):n},exports.mainAppInit=function(){window.addEventListener("message",(function(e){var n=e.data||{},o=n.data,t=n.type;if(t===i.HASH_CHANGE||t===i.PUSH_STATE||t===i.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)}}))},exports.subAppInit=function(e,n){var o=new l;if(window.parent&&window.parent!==window){window.onhashchange=function(){o.postMessage(i.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(i.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(i.REPLACE_STATE,{href:-1!==t.indexOf("//")?t:window.location.origin+t})},window.addEventListener("popstate",(function(e){o.postMessage(i.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)}))}},exports.trimObj=function e(n){if(Array.isArray(n)){var o=[];return n.forEach((function(n){o.push(e(n))})),o}if("[object Object]"===Object.prototype.toString.call(n)){var t={};return Object.keys(n).forEach((function(o){t[o]=e(n[o])})),t}return"string"==typeof n?n.trim():n};
|