From e17bf87f28bf67fae83358f4208b983a5ddd39a4 Mon Sep 17 00:00:00 2001 From: zsmj Date: Fri, 5 May 2023 17:03:03 +0800 Subject: [PATCH] =?UTF-8?q?REPORT-95153=20fix:=20BI.encodeURIComponent=20?= =?UTF-8?q?=E7=BC=96=E7=A0=81=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/fineui/src/core/func/alias.js | 96 +++++++++++++++----------- 1 file changed, 55 insertions(+), 41 deletions(-) diff --git a/packages/fineui/src/core/func/alias.js b/packages/fineui/src/core/func/alias.js index 049797832..c334caf00 100644 --- a/packages/fineui/src/core/func/alias.js +++ b/packages/fineui/src/core/func/alias.js @@ -1,9 +1,21 @@ -import { each, isFunction, isNull, isObject, isPlainObject, keys, leftPad, parseDateTime, values, isArray } from "../2.base"; +import { + each, + get, + isFunction, + isNull, + isObject, + isPlainObject, + keys, + leftPad, + parseDateTime, + values, + isArray +} from "../2.base"; import { replaceAll } from "./string"; import { getFullDayName, getMonthName, getTimezone } from "./date"; import { _global } from "../0.foundation"; -export const specialCharsMap = {}; +const getSpecialCharsMap = () => (get(_global, ["BI", "specialCharsMap"]) || {}); function isEmpty(value) { // 判断是否为空值 @@ -142,7 +154,7 @@ function _eFormat(text, fmt) { let roundNum = Math.round(num); num = (`${num}`).split(".")[0]; roundNum = (`${roundNum}`).split(".")[0]; - + return num.length !== roundNum.length; } } @@ -161,7 +173,7 @@ function _dealNumberPrecision(text, fright) { precision++; } } - + return Number(text).toFixed(precision); } @@ -185,7 +197,7 @@ function _numberFormat(text, format) { if (text >= 0) { return _numberFormat(`${text}`, format.substring(0, numMod)); } - + return _numberFormat(`${-text}`, format.substring(numMod + 1)); } else { // 兼容格式处理负数的情况(copy:fr-jquery.format.js) @@ -275,7 +287,7 @@ function _dealWithRight(tright, fright) { } } result.num = right; - + return result; } @@ -346,11 +358,11 @@ function _dealWithLeft(tleft, fleft) { } left = left.replace(/[0-9]+,/, newstr); } - + return left; } -export const cjkEncode = function (text) { +export const cjkEncode = function(text) { // alex:如果非字符串,返回其本身(cjkEncode(234) 返回 ""是不对的) if (typeof text !== "string") { return text; @@ -376,7 +388,7 @@ export const cjkEncode = function (text) { * @param text 需要做解码的字符串 * @return {String} 解码后的字符串 */ -export const cjkDecode = function (text) { +export const cjkDecode = function(text) { if (text == null) { return ""; } @@ -416,11 +428,11 @@ const SPECIAL_TAGS = { "\x20": " ", "\n": " ", }; -export const htmlEncode = function (text) { +export const htmlEncode = function(text) { return isNull(text) ? "" : replaceAll(`${text}`, keys(SPECIAL_TAGS).join("|"), v => SPECIAL_TAGS[v] ? SPECIAL_TAGS[v] : v); }; // html decode -export const htmlDecode = function (text) { +export const htmlDecode = function(text) { return isNull(text) ? "" : replaceAll(`${text}`, values(SPECIAL_TAGS).join("|"), v => { switch (v) { case "&": @@ -441,7 +453,7 @@ export const htmlDecode = function (text) { }); }; -export const cjkEncodeDO = function (o) { +export const cjkEncodeDO = function(o) { if (isPlainObject(o)) { const result = {}; each(o, (v, k) => { @@ -452,14 +464,14 @@ export const cjkEncodeDO = function (o) { k = cjkEncode(k); result[k] = cjkEncode(v); }); - + return result; } - + return o; }; -export const jsonEncode = function (o) { +export const jsonEncode = function(o) { // james:这个Encode是抄的EXT的 const useHasOwn = !!{}.hasOwnProperty; @@ -476,7 +488,7 @@ export const jsonEncode = function (o) { "\\": "\\\\", }; - const encodeString = function (s) { + const encodeString = function(s) { if (/["\\\x00-\x1f]/.test(s)) { return `"${s.replace(/([\x00-\x1f\\"])/g, (a, b) => { let c = m[b]; @@ -484,17 +496,17 @@ export const jsonEncode = function (o) { return c; } c = b.charCodeAt(); - + return `\\u00${ Math.floor(c / 16).toString(16) }${(c % 16).toString(16)}`; })}"`; } - + return `"${s}"`; }; - const encodeArray = function (o) { + const encodeArray = function(o) { let a = ["["], b, i, l = o.length, v; for (i = 0; i < l; i += 1) { v = o[i]; @@ -512,7 +524,7 @@ export const jsonEncode = function (o) { } } a.push("]"); - + return a.join(""); }; @@ -556,11 +568,11 @@ export const jsonEncode = function (o) { } } a.push("}"); - + return a.join(""); }; -export const jsonDecode = function (text) { +export const jsonDecode = function(text) { let jo; try { jo = JSON.parse(text); @@ -589,7 +601,7 @@ export const jsonDecode = function (text) { if (!json || typeof json !== "string") { return false; } - + return json.indexOf("__time__") !== -1; } @@ -619,11 +631,12 @@ export const jsonDecode = function (text) { * @example * BI.getEncodeURL("design/{tableName}/{fieldName}",{tableName: "A", fieldName: "a"}) // design/A/a */ -export const getEncodeURL = function (urlTemplate, param) { +export const getEncodeURL = function(urlTemplate, param) { return replaceAll(urlTemplate, "\\{(.*?)\\}", (ori, str) => encodeURIComponent(isObject(param) ? param[str] : param)); }; -export const encodeURIComponent = function (url) { +export const encodeURIComponent = function(url) { + const specialCharsMap = getSpecialCharsMap(); url = url || ""; url = replaceAll(`${url}`, keys(specialCharsMap || []).join("|"), str => { switch (str) { @@ -633,22 +646,23 @@ export const encodeURIComponent = function (url) { return specialCharsMap[str] || str; } }); - + return _global.encodeURIComponent(url); }; -export const decodeURIComponent = function (url) { +export const decodeURIComponent = function(url) { + const specialCharsMap = getSpecialCharsMap(); const reserveSpecialCharsMap = {}; each(specialCharsMap, (initialChar, encodeChar) => { reserveSpecialCharsMap[encodeChar] = initialChar === "\\\\" ? "\\" : initialChar; }); url = url || ""; url = replaceAll(`${url}`, keys(reserveSpecialCharsMap || []).join("|"), str => reserveSpecialCharsMap[str] || str); - + return _global.decodeURIComponent(url); }; -export const contentFormat = function (cv, fmt) { +export const contentFormat = function(cv, fmt) { if (isEmpty(cv)) { // 原值为空,返回空字符 return ""; @@ -685,7 +699,7 @@ export const contentFormat = function (cv, fmt) { } // ¤ - 货币格式 text = text.replace(/¤/g, "¥"); - + return text; }; @@ -694,7 +708,7 @@ export const contentFormat = function (cv, fmt) { * @param fmt 日期格式 * @returns {string} */ -export const parseFmt = function (fmt) { +export const parseFmt = function(fmt) { if (!fmt) { return ""; } @@ -742,12 +756,12 @@ export const parseFmt = function (fmt) { * @param format 日期格式 * @returns {Date} */ -export const str2Date = function (str, format) { +export const str2Date = function(str, format) { if (typeof str != "string" || typeof format != "string") { return null; } const fmt = parseFmt(format); - + return parseDateTime(str, fmt); }; @@ -760,7 +774,7 @@ export const str2Date = function (str, format) { * @param format * @returns {string} */ -export const date2Str = function (date, format) { +export const date2Str = function(date, format) { if (!date) { return ""; } @@ -789,7 +803,7 @@ export const date2Str = function (date, format) { len: len - start, }, date); } - + return result; function compileJFmt(jfmt, date) { @@ -863,12 +877,12 @@ export const date2Str = function (date, format) { str = jfmt.str; break; } - + return str; } }; -export const object2Number = function (value) { +export const object2Number = function(value) { if (value == null) { return 0; } @@ -879,11 +893,11 @@ export const object2Number = function (value) { if (str.indexOf(".") === -1) { return parseInt(str); } - + return parseFloat(str); }; -export const object2Date = function (obj) { +export const object2Date = function(obj) { if (obj == null) { return new Date(); } @@ -902,7 +916,7 @@ export const object2Date = function (obj) { return new Date(); }; -export const object2Time = function (obj) { +export const object2Time = function(obj) { if (obj == null) { return new Date(); } @@ -925,6 +939,6 @@ export const object2Time = function (obj) { if (!isInvalidDate(dt)) { return dt; } - + return new Date(); };