@zjlab-fe/util 0.2.13 → 0.3.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/README.md CHANGED
@@ -1,5 +1,15 @@
1
1
  ## API
2
2
 
3
+ ### 检查当前域名是否是线上环境
4
+
5
+ ```javascript
6
+ /**
7
+ * 检查当前域名是否是线上环境,返回true表示是线上环境,返回false表示是非线上环境
8
+ * @returns {boolean}
9
+ */
10
+ export declare function isOnline(): boolean;
11
+ ```
12
+
3
13
  ### 格式化展示数字
4
14
 
5
15
  ```javascript
package/dist/index.esm.js CHANGED
@@ -1 +1 @@
1
- import e from"query-string";import n from"numeral";import t from"axios";function o(e){var n=new v,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"],r=null==o?void 0:o.token;return r&&(n.headers.Authorization="Bearer ".concat(r)),(null==o?void 0:o.language)&&(n.headers.language=o.language),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){var t,o,r,a,i;if(e.response&&(401===e.response.status&&n.postMessage("login"),(null===(o=null===(t=e.response)||void 0===t?void 0:t.data)||void 0===o?void 0:o.msg)||(null===(a=null===(r=e.response)||void 0===r?void 0:r.data)||void 0===a?void 0:a.message)))return Promise.reject(null===(i=e.response)||void 0===i?void 0:i.data);return Promise.reject(e)})),o}var r=[],a=!1;function i(e,t){for(var o=t||{},r=o.locale,a=void 0===r?"zh":r,i=o.precision,s=void 0===i?0:i,c=o.showByte,l=void 0!==c&&c,u=o.splitUnit,d=void 0!==u&&u,f="",p=0;p<s;p++)f+="0";if(!1===l&&"zh"===a)return e>=1e8?d?{num:n(e/1e8).format("0.[".concat(f,"]")),unit:"亿"}:"".concat(n(e/1e8).format("0.[".concat(f,"]")),"亿"):e>=1e4?d?{num:n(e/1e4).format("0.[".concat(f,"]")),unit:"万"}:"".concat(n(e/1e4).format("0.[".concat(f,"]")),"万"):d?{num:"".concat(e),unit:""}:"".concat(e);var v="";v=0===s?"0b":"0.[".concat(f,"b]");var h=n(e).format(v),w=function(e){var n={size:0,unit:"B"};if(!e)return n;var t=e.match(/^([\d.]+)\s*([KMGTPEZY]?B?)?$/i);if(!t)return n;var o=parseFloat(t[1]),r=(t[2]||"B").toUpperCase();return 1===r.length&&"B"!==r&&(r+="B"),{size:o,unit:r}}(h),g=w.size,m=w.unit;if(!0===l)return d?{num:"".concat(g),unit:m}:h;if(d){var E=m.replace("K","k");return E&&(E=E.substring(0,E.length-1)),{num:"".concat(g),unit:E}}return(h=h.substring(0,h.length-1)).replace("K","k")}function s(e,n,t,o,i){var s;if(n)window.open(e);else{var l=i||"zh";r.push({url:e,headers:o,locale:l}),a||(a=!0,null===(s=null==t?void 0:t.loading)||void 0===s||s.call(t,"zh"===l?"下载中":"Downloading"),c(t))}}function c(e){if(r.length>0){var n=r.shift(),t="string"==typeof n?n:n.url,o="string"==typeof n?{}:n.headers||{},i="string"==typeof n?"zh":n.locale||"zh";fetch(t,{headers:o}).then((function(e){if(200===(null==e?void 0:e.status)){var n=function(e){if(!e)return"";var n=/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec(e);return n&&n[1]?n[1].replace(/['"]/g,""):""}(e.headers.get("Content-Disposition"))||function(e){if(e){var n=e.split("/").pop();if(n&&n.indexOf(".")>-1)return decodeURIComponent(n.split("?")[0])}return""}(t);return e.blob().then((function(e){var t=URL.createObjectURL(e),o=document.createElement("a");o.style.display="none",document.body.appendChild(o),o.href=t,n&&(o.download=n),o.click(),o.remove(),URL.revokeObjectURL(t)}))}return Promise.reject({message:null==e?void 0:e.statusText})})).catch((function(){var n;null===(n=null==e?void 0:e.error)||void 0===n||n.call(e,"zh"===i?"文件下载异常:".concat(t):"File download error: ".concat(t))})).finally((function(){setTimeout((function(){c(e)}),1e3)}))}else a=!1}function l(e){var n=e,t=e.indexOf("?");-1!==t&&(n=e.substring(0,t));var o=n.lastIndexOf(".");return n.substring(o+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 t={};return Object.keys(e).forEach((function(n){t[n]=u(e[n])})),t}return"string"==typeof e?e.trim():e}var d,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"}(d||(d={}));var p,v=function(){function e(){this.onMessageCallback={}}return e.prototype.postMessageToIfm=function(e,n,t,o){var r;if(e){var a=void 0;"function"==typeof t?a=t:o&&(a=o);var i="".concat(f,"_").concat(n);a&&this.onMessage(n,a,!0),null===(r=e.contentWindow)||void 0===r||r.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 r="";r=-1!==[d.HASH_CHANGE,d.PUSH_STATE,d.REPLACE_STATE].indexOf(e)?e:"".concat(f,"_").concat(e),o&&this.onMessage(e,o,!0),window.parent.postMessage({type:r,data:"function"==typeof n?null:n},"*")}else console.error("postMessage error, no parent")},e.prototype.onMessage=function(e,n,t){var o=function(r){t&&window.removeEventListener("message",o);var a=r.data||{},i=a.data,s=a.type;-1!==[d.HASH_CHANGE,d.PUSH_STATE,d.REPLACE_STATE].indexOf(e)?s===e&&n(i):s==="".concat(f,"_").concat(e)&&n(i)};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}();function h(){window.addEventListener("message",(function(e){var n=e.data||{},t=n.data,o=n.type;if(o===d.HASH_CHANGE||o===d.PUSH_STATE||o===d.REPLACE_STATE){var r="".concat(window.location.pathname,"?iframeUrl=").concat(encodeURIComponent(t.href));console.log("+++ iframe url data",o,t),window.history.replaceState(null,"",r)}}))}function w(n){var t=e.parse(window.location.search);return(null==t?void 0:t.iframeUrl)?decodeURIComponent(t.iframeUrl):n}function g(e,n){var t=new v;if(window.parent&&window.parent!==window){window.onhashchange=function(){t.postMessage(d.HASH_CHANGE,{href:window.location.href})};var o=window.history.pushState;window.history.pushState=function(e,n,r){o.call(window.history,e,n,r),r&&t.postMessage(d.PUSH_STATE,{href:-1!==r.indexOf("//")?r:window.location.origin+r})};var r=window.history.replaceState;window.history.replaceState=function(e,n,o){r.call(window.history,e,n,o),o&&t.postMessage(d.REPLACE_STATE,{href:-1!==o.indexOf("//")?o:window.location.origin+o})},window.addEventListener("popstate",(function(e){t.postMessage(d.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)}))}}!function(e){e.zh="zh",e.en="en"}(p||(p={}));export{v as IfmChannel,s as downloadFile,i as formatNumber,o as getAxiosInstanceWithAuth,l as getFileExt,w as getIframeUrl,h as mainAppInit,g as subAppInit,u as trimObj};
1
+ import e from"query-string";import n from"numeral";import t from"axios";function o(e){var n=new m,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"],r=null==o?void 0:o.token;return r&&(n.headers.Authorization="Bearer ".concat(r)),(null==o?void 0:o.language)&&(n.headers.language=o.language),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){var t,o,r,i,a;if(e.response&&(401===e.response.status&&n.postMessage("login"),(null===(o=null===(t=e.response)||void 0===t?void 0:t.data)||void 0===o?void 0:o.msg)||(null===(i=null===(r=e.response)||void 0===r?void 0:r.data)||void 0===i?void 0:i.message)))return Promise.reject(null===(a=e.response)||void 0===a?void 0:a.data);return Promise.reject(e)})),o}"function"==typeof SuppressedError&&SuppressedError;var r=[],i=!1;function a(){var e=window.location.hostname;return/^(?!test-).*zero2x\.org$/i.test(e)}function s(e,t){for(var o=t||{},r=o.locale,i=void 0===r?"zh":r,a=o.precision,s=void 0===a?0:a,c=o.showByte,l=void 0!==c&&c,u=o.splitUnit,d=void 0!==u&&u,f="",p=0;p<s;p++)f+="0";if(!1===l&&"zh"===i)return e>=1e8?d?{num:n(e/1e8).format("0.[".concat(f,"]")),unit:"亿"}:"".concat(n(e/1e8).format("0.[".concat(f,"]")),"亿"):e>=1e4?d?{num:n(e/1e4).format("0.[".concat(f,"]")),unit:"万"}:"".concat(n(e/1e4).format("0.[".concat(f,"]")),"万"):d?{num:"".concat(e),unit:""}:"".concat(e);var v="";v=0===s?"0b":"0.[".concat(f,"b]");var h=n(e).format(v),m=function(e){var n={size:0,unit:"B"};if(!e)return n;var t=e.match(/^([\d.]+)\s*([KMGTPEZY]?B?)?$/i);if(!t)return n;var o=parseFloat(t[1]),r=(t[2]||"B").toUpperCase();return 1===r.length&&"B"!==r&&(r+="B"),{size:o,unit:r}}(h),g=m.size,w=m.unit;if(!0===l)return d?{num:"".concat(g),unit:w}:h;if(d){var y=w.replace("K","k");return y&&(y=y.substring(0,y.length-1)),{num:"".concat(g),unit:y}}return(h=h.substring(0,h.length-1)).replace("K","k")}function c(e,n,t,o,a){var s;if(n)window.open(e);else{var c=a||"zh";r.push({url:e,headers:o,locale:c}),i||(i=!0,null===(s=null==t?void 0:t.loading)||void 0===s||s.call(t,"zh"===c?"下载中":"Downloading"),l(t))}}function l(e){if(r.length>0){var n=r.shift(),t="string"==typeof n?n:n.url,o="string"==typeof n?{}:n.headers||{},a="string"==typeof n?"zh":n.locale||"zh";fetch(t,{headers:o}).then((function(e){if(200===(null==e?void 0:e.status)){var n=function(e){if(!e)return"";var n=/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec(e);return n&&n[1]?n[1].replace(/['"]/g,""):""}(e.headers.get("Content-Disposition"))||function(e){if(e){var n=e.split("/").pop();if(n&&n.indexOf(".")>-1)return decodeURIComponent(n.split("?")[0])}return""}(t);return e.blob().then((function(e){var t=URL.createObjectURL(e),o=document.createElement("a");o.style.display="none",document.body.appendChild(o),o.href=t,n&&(o.download=n),o.click(),o.remove(),URL.revokeObjectURL(t)}))}return Promise.reject({message:null==e?void 0:e.statusText})})).catch((function(){var n;null===(n=null==e?void 0:e.error)||void 0===n||n.call(e,"zh"===a?"文件下载异常:".concat(t):"File download error: ".concat(t))})).finally((function(){setTimeout((function(){l(e)}),1e3)}))}else i=!1}function u(e){var n=e,t=e.indexOf("?");-1!==t&&(n=e.substring(0,t));var o=n.lastIndexOf(".");return n.substring(o+1)}function d(e){return new Promise((function(n){if(e&&e.length>0){var t=function(e,n,t){if(t||2===arguments.length)for(var o,r=0,i=n.length;r<i;r++)!o&&r in n||(o||(o=Array.prototype.slice.call(n,0,r)),o[r]=n[r]);return e.concat(o||Array.prototype.slice.call(n))}([],e,!0),o=function(){t.length>0?setTimeout(r):setTimeout((function(){n(null)}))},r=function(){var e=t.shift(),n=u(e);if("js"===n||"mjs"===n){if(!document.querySelector('script[src="'.concat(e,'"]'))){var r=document.createElement("script");return r.src=e,"mjs"===n&&(r.type="module"),document.head.appendChild(r),r.onload=o,void(r.onerror=o)}}else if("css"===n&&!document.querySelector('link[href="'.concat(e,'"]'))){var i=document.createElement("link");return i.rel="stylesheet",i.href=e,document.head.appendChild(i),i.onload=o,void(i.onerror=o)}o()};r()}else n(null)}))}function f(e){if(Array.isArray(e)){var n=[];return e.forEach((function(e){n.push(f(e))})),n}if("[object Object]"===Object.prototype.toString.call(e)){var t={};return Object.keys(e).forEach((function(n){t[n]=f(e[n])})),t}return"string"==typeof e?e.trim():e}var p,v="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"}(p||(p={}));var h,m=function(){function e(){this.onMessageCallback={}}return e.prototype.postMessageToIfm=function(e,n,t,o){var r;if(e){var i=void 0;"function"==typeof t?i=t:o&&(i=o);var a="".concat(v,"_").concat(n);i&&this.onMessage(n,i,!0),null===(r=e.contentWindow)||void 0===r||r.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 r="";r=-1!==[p.HASH_CHANGE,p.PUSH_STATE,p.REPLACE_STATE].indexOf(e)?e:"".concat(v,"_").concat(e),o&&this.onMessage(e,o,!0),window.parent.postMessage({type:r,data:"function"==typeof n?null:n},"*")}else console.error("postMessage error, no parent")},e.prototype.onMessage=function(e,n,t){var o=function(r){t&&window.removeEventListener("message",o);var i=r.data||{},a=i.data,s=i.type;-1!==[p.HASH_CHANGE,p.PUSH_STATE,p.REPLACE_STATE].indexOf(e)?s===e&&n(a):s==="".concat(v,"_").concat(e)&&n(a)};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}();function g(){window.addEventListener("message",(function(e){var n=e.data||{},t=n.data,o=n.type;if(o===p.HASH_CHANGE||o===p.PUSH_STATE||o===p.REPLACE_STATE){var r="".concat(window.location.pathname,"?iframeUrl=").concat(encodeURIComponent(t.href));console.log("+++ iframe url data",o,t),window.history.replaceState(null,"",r)}}))}function w(n){var t=e.parse(window.location.search);return(null==t?void 0:t.iframeUrl)?decodeURIComponent(t.iframeUrl):n}function y(e,n){var t=new m;if(window.parent&&window.parent!==window){window.onhashchange=function(){t.postMessage(p.HASH_CHANGE,{href:window.location.href})};var o=window.history.pushState;window.history.pushState=function(e,n,r){o.call(window.history,e,n,r),r&&t.postMessage(p.PUSH_STATE,{href:-1!==r.indexOf("//")?r:window.location.origin+r})};var r=window.history.replaceState;window.history.replaceState=function(e,n,o){r.call(window.history,e,n,o),o&&t.postMessage(p.REPLACE_STATE,{href:-1!==o.indexOf("//")?o:window.location.origin+o})},window.addEventListener("popstate",(function(e){t.postMessage(p.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)}))}}!function(e){e.zh="zh",e.en="en"}(h||(h={}));export{m as IfmChannel,d as appendAsset,c as downloadFile,s as formatNumber,o as getAxiosInstanceWithAuth,u as getFileExt,w as getIframeUrl,a as isOnline,g as mainAppInit,y as subAppInit,f as trimObj};
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var e=require("query-string"),n=require("numeral"),t=require("axios");var o=[],r=!1;function a(e){if(o.length>0){var n=o.shift(),t="string"==typeof n?n:n.url,i="string"==typeof n?{}:n.headers||{},s="string"==typeof n?"zh":n.locale||"zh";fetch(t,{headers:i}).then((function(e){if(200===(null==e?void 0:e.status)){var n=function(e){if(!e)return"";var n=/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec(e);return n&&n[1]?n[1].replace(/['"]/g,""):""}(e.headers.get("Content-Disposition"))||function(e){if(e){var n=e.split("/").pop();if(n&&n.indexOf(".")>-1)return decodeURIComponent(n.split("?")[0])}return""}(t);return e.blob().then((function(e){var t=URL.createObjectURL(e),o=document.createElement("a");o.style.display="none",document.body.appendChild(o),o.href=t,n&&(o.download=n),o.click(),o.remove(),URL.revokeObjectURL(t)}))}return Promise.reject({message:null==e?void 0:e.statusText})})).catch((function(){var n;null===(n=null==e?void 0:e.error)||void 0===n||n.call(e,"zh"===s?"文件下载异常:".concat(t):"File download error: ".concat(t))})).finally((function(){setTimeout((function(){a(e)}),1e3)}))}else r=!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,u=function(){function e(){this.onMessageCallback={}}return e.prototype.postMessageToIfm=function(e,n,t,o){var r;if(e){var a=void 0;"function"==typeof t?a=t:o&&(a=o);var i="".concat(s,"_").concat(n);a&&this.onMessage(n,a,!0),null===(r=e.contentWindow)||void 0===r||r.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 r="";r=-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:r,data:"function"==typeof n?null:n},"*")}else console.error("postMessage error, no parent")},e.prototype.onMessage=function(e,n,t){var o=function(r){t&&window.removeEventListener("message",o);var a=r.data||{},c=a.data,u=a.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}();!function(e){e.zh="zh",e.en="en"}(c||(c={})),exports.IfmChannel=u,exports.downloadFile=function(e,n,t,i,s){var c;if(n)window.open(e);else{var u=s||"zh";o.push({url:e,headers:i,locale:u}),r||(r=!0,null===(c=null==t?void 0:t.loading)||void 0===c||c.call(t,"zh"===u?"下载中":"Downloading"),a(t))}},exports.formatNumber=function(e,t){for(var o=t||{},r=o.locale,a=void 0===r?"zh":r,i=o.precision,s=void 0===i?0:i,c=o.showByte,u=void 0!==c&&c,l=o.splitUnit,d=void 0!==l&&l,f="",p=0;p<s;p++)f+="0";if(!1===u&&"zh"===a)return e>=1e8?d?{num:n(e/1e8).format("0.[".concat(f,"]")),unit:"亿"}:"".concat(n(e/1e8).format("0.[".concat(f,"]")),"亿"):e>=1e4?d?{num:n(e/1e4).format("0.[".concat(f,"]")),unit:"万"}:"".concat(n(e/1e4).format("0.[".concat(f,"]")),"万"):d?{num:"".concat(e),unit:""}:"".concat(e);var v="";v=0===s?"0b":"0.[".concat(f,"b]");var h=n(e).format(v),g=function(e){var n={size:0,unit:"B"};if(!e)return n;var t=e.match(/^([\d.]+)\s*([KMGTPEZY]?B?)?$/i);if(!t)return n;var o=parseFloat(t[1]),r=(t[2]||"B").toUpperCase();return 1===r.length&&"B"!==r&&(r+="B"),{size:o,unit:r}}(h),w=g.size,m=g.unit;if(!0===u)return d?{num:"".concat(w),unit:m}:h;if(d){var E=m.replace("K","k");return E&&(E=E.substring(0,E.length-1)),{num:"".concat(w),unit:E}}return(h=h.substring(0,h.length-1)).replace("K","k")},exports.getAxiosInstanceWithAuth=function(e){var n=new u,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"],r=null==o?void 0:o.token;return r&&(n.headers.Authorization="Bearer ".concat(r)),(null==o?void 0:o.language)&&(n.headers.language=o.language),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){var t,o,r,a,i;if(e.response&&(401===e.response.status&&n.postMessage("login"),(null===(o=null===(t=e.response)||void 0===t?void 0:t.data)||void 0===o?void 0:o.msg)||(null===(a=null===(r=e.response)||void 0===r?void 0:r.data)||void 0===a?void 0:a.message)))return Promise.reject(null===(i=e.response)||void 0===i?void 0:i.data);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 r="".concat(window.location.pathname,"?iframeUrl=").concat(encodeURIComponent(t.href));console.log("+++ iframe url data",o,t),window.history.replaceState(null,"",r)}}))},exports.subAppInit=function(e,n){var t=new u;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,r){o.call(window.history,e,n,r),r&&t.postMessage(i.PUSH_STATE,{href:-1!==r.indexOf("//")?r:window.location.origin+r})};var r=window.history.replaceState;window.history.replaceState=function(e,n,o){r.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};
1
+ "use strict";var e=require("query-string"),n=require("numeral"),t=require("axios");"function"==typeof SuppressedError&&SuppressedError;var o=[],r=!1;function i(e){if(o.length>0){var n=o.shift(),t="string"==typeof n?n:n.url,a="string"==typeof n?{}:n.headers||{},s="string"==typeof n?"zh":n.locale||"zh";fetch(t,{headers:a}).then((function(e){if(200===(null==e?void 0:e.status)){var n=function(e){if(!e)return"";var n=/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec(e);return n&&n[1]?n[1].replace(/['"]/g,""):""}(e.headers.get("Content-Disposition"))||function(e){if(e){var n=e.split("/").pop();if(n&&n.indexOf(".")>-1)return decodeURIComponent(n.split("?")[0])}return""}(t);return e.blob().then((function(e){var t=URL.createObjectURL(e),o=document.createElement("a");o.style.display="none",document.body.appendChild(o),o.href=t,n&&(o.download=n),o.click(),o.remove(),URL.revokeObjectURL(t)}))}return Promise.reject({message:null==e?void 0:e.statusText})})).catch((function(){var n;null===(n=null==e?void 0:e.error)||void 0===n||n.call(e,"zh"===s?"文件下载异常:".concat(t):"File download error: ".concat(t))})).finally((function(){setTimeout((function(){i(e)}),1e3)}))}else r=!1}function a(e){var n=e,t=e.indexOf("?");-1!==t&&(n=e.substring(0,t));var o=n.lastIndexOf(".");return n.substring(o+1)}var s,c="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"}(s||(s={}));var l,u=function(){function e(){this.onMessageCallback={}}return e.prototype.postMessageToIfm=function(e,n,t,o){var r;if(e){var i=void 0;"function"==typeof t?i=t:o&&(i=o);var a="".concat(c,"_").concat(n);i&&this.onMessage(n,i,!0),null===(r=e.contentWindow)||void 0===r||r.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 r="";r=-1!==[s.HASH_CHANGE,s.PUSH_STATE,s.REPLACE_STATE].indexOf(e)?e:"".concat(c,"_").concat(e),o&&this.onMessage(e,o,!0),window.parent.postMessage({type:r,data:"function"==typeof n?null:n},"*")}else console.error("postMessage error, no parent")},e.prototype.onMessage=function(e,n,t){var o=function(r){t&&window.removeEventListener("message",o);var i=r.data||{},a=i.data,l=i.type;-1!==[s.HASH_CHANGE,s.PUSH_STATE,s.REPLACE_STATE].indexOf(e)?l===e&&n(a):l==="".concat(c,"_").concat(e)&&n(a)};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}();!function(e){e.zh="zh",e.en="en"}(l||(l={})),exports.IfmChannel=u,exports.appendAsset=function(e){return new Promise((function(n){if(e&&e.length>0){var t=function(e,n,t){if(t||2===arguments.length)for(var o,r=0,i=n.length;r<i;r++)!o&&r in n||(o||(o=Array.prototype.slice.call(n,0,r)),o[r]=n[r]);return e.concat(o||Array.prototype.slice.call(n))}([],e,!0),o=function(){t.length>0?setTimeout(r):setTimeout((function(){n(null)}))},r=function(){var e=t.shift(),n=a(e);if("js"===n||"mjs"===n){if(!document.querySelector('script[src="'.concat(e,'"]'))){var r=document.createElement("script");return r.src=e,"mjs"===n&&(r.type="module"),document.head.appendChild(r),r.onload=o,void(r.onerror=o)}}else if("css"===n&&!document.querySelector('link[href="'.concat(e,'"]'))){var i=document.createElement("link");return i.rel="stylesheet",i.href=e,document.head.appendChild(i),i.onload=o,void(i.onerror=o)}o()};r()}else n(null)}))},exports.downloadFile=function(e,n,t,a,s){var c;if(n)window.open(e);else{var l=s||"zh";o.push({url:e,headers:a,locale:l}),r||(r=!0,null===(c=null==t?void 0:t.loading)||void 0===c||c.call(t,"zh"===l?"下载中":"Downloading"),i(t))}},exports.formatNumber=function(e,t){for(var o=t||{},r=o.locale,i=void 0===r?"zh":r,a=o.precision,s=void 0===a?0:a,c=o.showByte,l=void 0!==c&&c,u=o.splitUnit,d=void 0!==u&&u,f="",p=0;p<s;p++)f+="0";if(!1===l&&"zh"===i)return e>=1e8?d?{num:n(e/1e8).format("0.[".concat(f,"]")),unit:"亿"}:"".concat(n(e/1e8).format("0.[".concat(f,"]")),"亿"):e>=1e4?d?{num:n(e/1e4).format("0.[".concat(f,"]")),unit:"万"}:"".concat(n(e/1e4).format("0.[".concat(f,"]")),"万"):d?{num:"".concat(e),unit:""}:"".concat(e);var v="";v=0===s?"0b":"0.[".concat(f,"b]");var h=n(e).format(v),g=function(e){var n={size:0,unit:"B"};if(!e)return n;var t=e.match(/^([\d.]+)\s*([KMGTPEZY]?B?)?$/i);if(!t)return n;var o=parseFloat(t[1]),r=(t[2]||"B").toUpperCase();return 1===r.length&&"B"!==r&&(r+="B"),{size:o,unit:r}}(h),m=g.size,w=g.unit;if(!0===l)return d?{num:"".concat(m),unit:w}:h;if(d){var y=w.replace("K","k");return y&&(y=y.substring(0,y.length-1)),{num:"".concat(m),unit:y}}return(h=h.substring(0,h.length-1)).replace("K","k")},exports.getAxiosInstanceWithAuth=function(e){var n=new u,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"],r=null==o?void 0:o.token;return r&&(n.headers.Authorization="Bearer ".concat(r)),(null==o?void 0:o.language)&&(n.headers.language=o.language),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){var t,o,r,i,a;if(e.response&&(401===e.response.status&&n.postMessage("login"),(null===(o=null===(t=e.response)||void 0===t?void 0:t.data)||void 0===o?void 0:o.msg)||(null===(i=null===(r=e.response)||void 0===r?void 0:r.data)||void 0===i?void 0:i.message)))return Promise.reject(null===(a=e.response)||void 0===a?void 0:a.data);return Promise.reject(e)})),o},exports.getFileExt=a,exports.getIframeUrl=function(n){var t=e.parse(window.location.search);return(null==t?void 0:t.iframeUrl)?decodeURIComponent(t.iframeUrl):n},exports.isOnline=function(){var e=window.location.hostname;return/^(?!test-).*zero2x\.org$/i.test(e)},exports.mainAppInit=function(){window.addEventListener("message",(function(e){var n=e.data||{},t=n.data,o=n.type;if(o===s.HASH_CHANGE||o===s.PUSH_STATE||o===s.REPLACE_STATE){var r="".concat(window.location.pathname,"?iframeUrl=").concat(encodeURIComponent(t.href));console.log("+++ iframe url data",o,t),window.history.replaceState(null,"",r)}}))},exports.subAppInit=function(e,n){var t=new u;if(window.parent&&window.parent!==window){window.onhashchange=function(){t.postMessage(s.HASH_CHANGE,{href:window.location.href})};var o=window.history.pushState;window.history.pushState=function(e,n,r){o.call(window.history,e,n,r),r&&t.postMessage(s.PUSH_STATE,{href:-1!==r.indexOf("//")?r:window.location.origin+r})};var r=window.history.replaceState;window.history.replaceState=function(e,n,o){r.call(window.history,e,n,o),o&&t.postMessage(s.REPLACE_STATE,{href:-1!==o.indexOf("//")?o:window.location.origin+o})},window.addEventListener("popstate",(function(e){t.postMessage(s.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};
@@ -1,3 +1,8 @@
1
+ /**
2
+ * 检查当前域名是否是线上环境,返回true表示是线上环境,返回false表示是非线上环境
3
+ * @returns {boolean}
4
+ */
5
+ export declare function isOnline(): boolean;
1
6
  type formatNumberReturnType<T> = T extends {
2
7
  locale?: string;
3
8
  precision?: number;
@@ -40,6 +45,12 @@ export declare function downloadFile(url: string, openWindow?: boolean, message?
40
45
  * @returns ext {string} 文件类型
41
46
  */
42
47
  export declare function getFileExt(filePath: string): string;
48
+ /**
49
+ * 添加前端资源,比如js、css、mjs等,如果资源已经加载过,则不会重复加载
50
+ * @param assetUrlArr {string[]} 资源url数组
51
+ * @returns {Promise<null>} 返回一个Promise,当所有资源都加载完成时,返回null
52
+ */
53
+ export declare function appendAsset(assetUrlArr: string[]): Promise<null>;
43
54
  /**
44
55
  * 深度遍历,对于类型是字符串的value进行trim操作
45
56
  * @param obj 原始的数据
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zjlab-fe/util",
3
- "version": "0.2.13",
3
+ "version": "0.3.0",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.esm.js",