@zjlab-fe/util 0.2.1 → 0.2.2
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/dist/types/index.d.ts +13 -0
- package/package.json +30 -28
package/dist/index.esm.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import e from"query-string";import n from"axios";function
|
|
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="",l=0;l<s;l++)c+="0";return"zh"===i?e>=1e8?"".concat(n(e/1e8).format("0.[".concat(c,"]")),"亿"):e>=1e4?"".concat(n(e/1e4).format("0.[".concat(c,"]")),"万"):e:e>=1e9?"".concat(n(e/1e9).format("0,0.[".concat(c,"]")),"billion"):e>=1e6?"".concat(n(e/1e6).format("0,0.[".concat(c,"]")),"million"):"".concat(n(e).format("0,0.[".concat(c,"]")))}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};
|
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("numeral"),o=require("axios");var t=[],a=!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 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(){i(e)}),1e3)}))}else a=!1}var r,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"}(r||(r={}));var c,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 r="".concat(s,"_").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!==[r.HASH_CHANGE,r.PUSH_STATE,r.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 i=a.data||{},c=i.data,l=i.type;-1!==[r.HASH_CHANGE,r.PUSH_STATE,r.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 r;n?window.open(e):(t.push(e),a||(a=!0,null===(r=null==o?void 0:o.loading)||void 0===r||r.call(o,"文件下载中"),i(o)))},exports.formatNumber=function(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="",l=0;l<s;l++)c+="0";return"zh"===i?e>=1e8?"".concat(n(e/1e8).format("0.[".concat(c,"]")),"亿"):e>=1e4?"".concat(n(e/1e4).format("0.[".concat(c,"]")),"万"):e:e>=1e9?"".concat(n(e/1e9).format("0,0.[".concat(c,"]")),"billion"):e>=1e6?"".concat(n(e/1e6).format("0,0.[".concat(c,"]")),"million"):"".concat(n(e).format("0,0.[".concat(c,"]")))},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,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},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===r.HASH_CHANGE||t===r.PUSH_STATE||t===r.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(r.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(r.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(r.REPLACE_STATE,{href:-1!==t.indexOf("//")?t:window.location.origin+t})},window.addEventListener("popstate",(function(e){o.postMessage(r.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};
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 格式化规则如下:
|
|
3
|
+
* 当语言是中文时,1. >=1亿时,展示成xx亿; 2. >=1万 && <1亿时,展示成xx万; 3. <1万时,展示成xx
|
|
4
|
+
* 当语言是英文时,1. >=10亿时,展示成xx billion; 2. >=100万 && <10亿时,展示成xx million 3. 其它直接返回xx,其中xx是千分位格式化后的数字
|
|
5
|
+
* @param total {number}
|
|
6
|
+
* @param options {object} 可选,配置项
|
|
7
|
+
* @param options.locale {string} 可选,语言,默认是zh,可选值有zh和en
|
|
8
|
+
* @param options.precision {number} 可选,精度(小数点后最多保留几位),默认是0,即没有小数点
|
|
9
|
+
*/
|
|
10
|
+
export declare function formatNumber(total: number, options?: {
|
|
11
|
+
locale?: string;
|
|
12
|
+
precision?: number;
|
|
13
|
+
}): string | number;
|
|
1
14
|
/**
|
|
2
15
|
* 下载文件
|
|
3
16
|
* @param url {string} 文件url
|
package/package.json
CHANGED
|
@@ -1,28 +1,30 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@zjlab-fe/util",
|
|
3
|
-
"version": "0.2.
|
|
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
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
"
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@zjlab-fe/util",
|
|
3
|
+
"version": "0.2.2",
|
|
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
|
+
"@types/numeral": "^2.0.5",
|
|
18
|
+
"rollup": "^4.20.0",
|
|
19
|
+
"tslib": "^2.6.3",
|
|
20
|
+
"typescript": "^5.5.4"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"axios": "^1.7.4",
|
|
24
|
+
"numeral": "^2.0.6",
|
|
25
|
+
"query-string": "^9.1.0"
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"registry": "https://registry.npmjs.org/"
|
|
29
|
+
}
|
|
30
|
+
}
|