@zjlab-fe/util 0.2.4 → 0.2.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/README.md CHANGED
@@ -14,7 +14,7 @@
14
14
  * @param options.precision {number} 可选,精度(小数点后最多保留几位),默认是0,即没有小数点
15
15
  * @param options.showByte {boolean} 可选,是否是展示字节单位,默认是false
16
16
  */
17
- export declare function formatNumber(total: number, options?: {
17
+ function formatNumber(total: number, options?: {
18
18
  locale?: string;
19
19
  precision?: number;
20
20
  showByte?: boolean;
@@ -31,8 +31,9 @@ export declare function formatNumber(total: number, options?: {
31
31
  * @param url {string} 文件url
32
32
  * @param openWindow {bool} 可选,是否是新开标签页下载
33
33
  * @param message {object} 可选,如果传入,需要传入antd或者antdv的message
34
+ * @param headers {Record<string, string>} 可选,自定义请求头
34
35
  */
35
- function downloadFile(url: string, openWindow?: boolean, message?: any): void;
36
+ function downloadFile(url: string, openWindow?: boolean, message?: any, headers?: Record<string, string>): void;
36
37
  ```
37
38
 
38
39
  ### 提取文件类型
package/dist/index.esm.js CHANGED
@@ -1 +1 @@
1
- import e from"query-string";import n from"numeral";import o from"axios";function t(e){var n=new v,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"],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 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){var o,t,a,i,r;if(e.response&&(401===e.response.status&&n.postMessage("login"),(null===(t=null===(o=e.response)||void 0===o?void 0:o.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 a=[],i=!1;function r(e,o){for(var t=o||{},a=t.locale,i=void 0===a?"zh":a,r=t.precision,s=void 0===r?0:r,c=t.showByte,l=void 0!==c&&c,u="",d=0;d<s;d++)u+="0";if(!1===l&&"zh"===i)return e>=1e8?"".concat(n(e/1e8).format("0.[".concat(u,"]")),"亿"):e>=1e4?"".concat(n(e/1e4).format("0.[".concat(u,"]")),"万"):e;var f="";f=0===s?"0b":"0.[".concat(u,"b]");var p=n(e).format(f);return!0===l?p:(p=p.substring(0,p.length-1)).replace("K","k")}function s(e,n,o){var t;n?window.open(e):(a.push(e),i||(i=!0,null===(t=null==o?void 0:o.loading)||void 0===t||t.call(o,"文件下载中"),c(o)))}function c(e){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(),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(){c(e)}),1e3)}))}else i=!1}function l(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 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,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!==[d.HASH_CHANGE,d.PUSH_STATE,d.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!==[d.HASH_CHANGE,d.PUSH_STATE,d.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 w(){window.addEventListener("message",(function(e){var n=e.data||{},o=n.data,t=n.type;if(t===d.HASH_CHANGE||t===d.PUSH_STATE||t===d.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 h(n){var o=e.parse(window.location.search);return(null==o?void 0:o.iframeUrl)?decodeURIComponent(o.iframeUrl):n}function g(e,n){var o=new v;if(window.parent&&window.parent!==window){window.onhashchange=function(){o.postMessage(d.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(d.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(d.REPLACE_STATE,{href:-1!==t.indexOf("//")?t:window.location.origin+t})},window.addEventListener("popstate",(function(e){o.postMessage(d.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"}(p||(p={}));export{v as IfmChannel,s as downloadFile,r as formatNumber,t as getAxiosInstanceWithAuth,l as getFileExt,h as getIframeUrl,w as mainAppInit,g as subAppInit,u as trimObj};
1
+ import e from"query-string";import n from"numeral";import o from"axios";function t(e){var n=new v,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"],r=null==t?void 0:t.token;return r&&(n.headers.Authorization="Bearer ".concat(r)),(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 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){var o,t,r,a,i;if(e.response&&(401===e.response.status&&n.postMessage("login"),(null===(t=null===(o=e.response)||void 0===o?void 0:o.data)||void 0===t?void 0:t.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)})),t}var r=[],a=!1;function i(e,o){for(var t=o||{},r=t.locale,a=void 0===r?"zh":r,i=t.precision,s=void 0===i?0:i,c=t.showByte,l=void 0!==c&&c,u="",d=0;d<s;d++)u+="0";if(!1===l&&"zh"===a)return e>=1e8?"".concat(n(e/1e8).format("0.[".concat(u,"]")),"亿"):e>=1e4?"".concat(n(e/1e4).format("0.[".concat(u,"]")),"万"):e;var f="";f=0===s?"0b":"0.[".concat(u,"b]");var p=n(e).format(f);return!0===l?p:(p=p.substring(0,p.length-1)).replace("K","k")}function s(e,n,o,t){var i;n?window.open(e):(r.push({url:e,headers:t}),a||(a=!0,null===(i=null==o?void 0:o.loading)||void 0===i||i.call(o,"文件下载中"),c(o)))}function c(e){if(r.length>0){var n=r.shift(),o="string"==typeof n?n:n.url,t="string"==typeof n?{}:n.headers||{};fetch(o,{headers:t}).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""}(o);return e.blob().then((function(e){var o=URL.createObjectURL(e),t=document.createElement("a");t.style.display="none",document.body.appendChild(t),t.href=o,n&&(t.download=n),t.click(),t.remove(),URL.revokeObjectURL(o)}))}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,"文件下载异常:".concat(o))})).finally((function(){setTimeout((function(){c(e)}),1e3)}))}else a=!1}function l(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 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,o,t){var r;if(e){var a=void 0;"function"==typeof o?a=o:t&&(a=t);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 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 r="";r=-1!==[d.HASH_CHANGE,d.PUSH_STATE,d.REPLACE_STATE].indexOf(e)?e:"".concat(f,"_").concat(e),t&&this.onMessage(e,t,!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,o){var t=function(r){o&&window.removeEventListener("message",t);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(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 h(){window.addEventListener("message",(function(e){var n=e.data||{},o=n.data,t=n.type;if(t===d.HASH_CHANGE||t===d.PUSH_STATE||t===d.REPLACE_STATE){var r="".concat(window.location.pathname,"?iframeUrl=").concat(encodeURIComponent(o.href));console.log("+++ iframe url data",t,o),window.history.replaceState(null,"",r)}}))}function w(n){var o=e.parse(window.location.search);return(null==o?void 0:o.iframeUrl)?decodeURIComponent(o.iframeUrl):n}function g(e,n){var o=new v;if(window.parent&&window.parent!==window){window.onhashchange=function(){o.postMessage(d.HASH_CHANGE,{href:window.location.href})};var t=window.history.pushState;window.history.pushState=function(e,n,r){t.call(window.history,e,n,r),r&&o.postMessage(d.PUSH_STATE,{href:-1!==r.indexOf("//")?r:window.location.origin+r})};var r=window.history.replaceState;window.history.replaceState=function(e,n,t){r.call(window.history,e,n,t),t&&o.postMessage(d.REPLACE_STATE,{href:-1!==t.indexOf("//")?t:window.location.origin+t})},window.addEventListener("popstate",(function(e){o.postMessage(d.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"}(p||(p={}));export{v as IfmChannel,s as downloadFile,i as formatNumber,t as getAxiosInstanceWithAuth,l as getFileExt,w as getIframeUrl,h as mainAppInit,g as subAppInit,u as trimObj};
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var e=require("query-string"),n=require("numeral"),o=require("axios");var t=[],a=!1;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}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,l=function(){function e(){this.onMessageCallback={}}return e.prototype.postMessageToIfm=function(e,n,o,t){var a;if(e){var r=void 0;"function"==typeof o?r=o:t&&(r=t);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 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(s,"_").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 r=a.data||{},c=r.data,l=r.type;-1!==[i.HASH_CHANGE,i.PUSH_STATE,i.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(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"}(c||(c={})),exports.IfmChannel=l,exports.downloadFile=function(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)))},exports.formatNumber=function(e,o){for(var t=o||{},a=t.locale,r=void 0===a?"zh":a,i=t.precision,s=void 0===i?0:i,c=t.showByte,l=void 0!==c&&c,u="",d=0;d<s;d++)u+="0";if(!1===l&&"zh"===r)return e>=1e8?"".concat(n(e/1e8).format("0.[".concat(u,"]")),"亿"):e>=1e4?"".concat(n(e/1e4).format("0.[".concat(u,"]")),"万"):e;var f="";f=0===s?"0b":"0.[".concat(u,"b]");var p=n(e).format(f);return!0===l?p:(p=p.substring(0,p.length-1)).replace("K","k")},exports.getAxiosInstanceWithAuth=function(e){var n=new l,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"],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 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){var o,t,a,r,i;if(e.response&&(401===e.response.status&&n.postMessage("login"),(null===(t=null===(o=e.response)||void 0===o?void 0:o.data)||void 0===t?void 0:t.msg)||(null===(r=null===(a=e.response)||void 0===a?void 0:a.data)||void 0===r?void 0:r.message)))return Promise.reject(null===(i=e.response)||void 0===i?void 0:i.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};
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||{};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,"文件下载异常:".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,l=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,l=a.type;-1!==[i.HASH_CHANGE,i.PUSH_STATE,i.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}();!function(e){e.zh="zh",e.en="en"}(c||(c={})),exports.IfmChannel=l,exports.downloadFile=function(e,n,t,i){var s;n?window.open(e):(o.push({url:e,headers:i}),r||(r=!0,null===(s=null==t?void 0:t.loading)||void 0===s||s.call(t,"文件下载中"),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,l=void 0!==c&&c,u="",d=0;d<s;d++)u+="0";if(!1===l&&"zh"===a)return e>=1e8?"".concat(n(e/1e8).format("0.[".concat(u,"]")),"亿"):e>=1e4?"".concat(n(e/1e4).format("0.[".concat(u,"]")),"万"):e;var f="";f=0===s?"0b":"0.[".concat(u,"b]");var p=n(e).format(f);return!0===l?p:(p=p.substring(0,p.length-1)).replace("K","k")},exports.getAxiosInstanceWithAuth=function(e){var n=new l,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 l;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};
@@ -19,8 +19,9 @@ export declare function formatNumber(total: number, options?: {
19
19
  * @param url {string} 文件url
20
20
  * @param openWindow {bool} 可选,是否是新开标签页下载
21
21
  * @param message {object} 可选,如果传入,需要传入antd或者antdv的message
22
+ * @param headers {Record<string, string>} 可选,自定义请求头
22
23
  */
23
- export declare function downloadFile(url: string, openWindow?: boolean, message?: any): void;
24
+ export declare function downloadFile(url: string, openWindow?: boolean, message?: any, headers?: Record<string, string>): void;
24
25
  /**
25
26
  * 提取文件类型
26
27
  * @param filePath {string} 文件路径 or 文件url
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zjlab-fe/util",
3
- "version": "0.2.4",
3
+ "version": "0.2.6",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.esm.js",