---添加数据转换工具

This commit is contained in:
2026-01-29 18:39:46 +08:00
parent 22fdadc17d
commit ce48951f37
5 changed files with 694 additions and 67 deletions

View File

@ -63,7 +63,7 @@
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
"editor.defaultFormatter": "vscode.typescript-language-features"
},
"[typescriptreact]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"

View File

@ -12,6 +12,7 @@
<!-- <div id="GLOB_API_URL" style="display: none">%VITE_GLOB_API_URL%</div>-->
<script src="/iconfont.js"></script>
<script src="/desktopIconfont.js"></script>
<script src="/native.extends.js"></script>
<script type="module">
import {getAppEnvConfig} from "./src/utils/env";

481
public/native.extends.js Normal file
View File

@ -0,0 +1,481 @@
/***
* 扩展String对象
*/
;(function(){
Object.is = function(obj){
return Object.prototype.toString.call(obj) == "[object Object]";
}
String.is = function(obj){
return Object.prototype.toString.call(obj) == "[object String]";
}
String.prototype.getBytes = function(charset){
charset = charset||"UTF-8";
return new TextEncoder(charset).encode(this);
}
String.ofBytes = function(arr,charset){
charset = charset||"UTF-8";
var uint = arr instanceof Uint8Array?arr:new Uint8Array(arr);
return new TextDecoder(charset).decode(uint);
}
String.prototype.startWith = function(str){
if(str==undefined) return false;
if(str.length > this.length) return false;
for (let i = 0; i < str.length; i++) {
if(str.charCodeAt(i)!=this.charCodeAt(i)){
return false;
}
}
return true;
}
String.prototype.endWith = function(str){
if(str==undefined) return false;
if(str.length > this.length) return false;
var j = this.length - str.length;
for (let i = 0; i < str.length; i++) {
if(str.charCodeAt(i)!=this.charCodeAt(i+j)){
return false;
}
}
return true;
}
String.prototype.eq = function(str){
return (this+'')===str;
}
})();
/**
* 扩展Number对象
*/
;(function(){
Number.is = function(obj){
return Object.prototype.toString.call(obj) == "[object Number]";
}
Number.format = function(num,fmt){
var fmts = fmt.split(".");
var format = {
int:fmts[0].split("").reverse(),
scale:fmts[1]!=undefined?fmts[1].split(""):undefined
}
var numsp = (num+"").split(".");
var numinfo = {
int:numsp[0].split("").reverse(),
scale:numsp[1]!=undefined?numsp[1].split(""):undefined
}
var res = {
int:[],
scale:undefined
}
var ni = 0;
for (var i = 0; i < format.int.length; i++) {
var item = format.int[i];
if(item=="#" && ni < numinfo.int.length){
res.int.push(numinfo.int[ni]);
ni++;
}else if(item=="0"){
if(ni < numinfo.int.length){
res.int.push(numinfo.int[ni]);
ni++;
}else{
res.int.push(item);
}
}else if(item==","){
if(ni < numinfo.int.length){
res.int.push(",");
}else{
continue;
}
}else if(item!="#"){
res.int.push(item);
}
}
if(format.scale!=undefined){
res.scale = [];
for (var j = 0; j < format.scale.length; j++) {
var item = format.scale[j];
if((item=="0" || item=="#") && numinfo.scale!=undefined && j < numinfo.scale.length){
res.scale.push(numinfo.scale[j]);
}else if(item!="#"){
res.scale.push(item);
}
}
var numL = 0;
for (var k = 0; k < res.scale.length; k++) {
var item = res.scale[k];
if(/\b/.test(item)){
numL++;
}
}
if(numinfo.scale!=undefined && numinfo.scale.length > numL){
var nz = res.scale[numL-1];
res.scale[numL-1] = parseInt(numinfo.scale[numL])>=5?parseInt(nz)+1:nz;
}
}
var reStr = "";
reStr += res.int.reverse().join("");
if(res.scale!=undefined){
reStr+= ".";
reStr+= res.scale.join("");
}
return reStr.replace("-,", "-");
}
Number.prototype.format = function(fmt){
if(fmt==undefined) throw new Error("参数格式化表达式不能为空!");
return Number.format(this,fmt);
}
Number.parse = function(str){
if(str==undefined) return str;
str += "";
if(/^-?\d+[\.\d]*$/.test(str)){
return new Number(str);
}
var sps = str.split("");
var res = [];
for (var i = 0; i < sps.length; i++) {
var item = sps[i];
if(item=="-" || item=="." || /[0-9]/.test(item)){
res.push(item);
}
}
return new Number(res.join(""));
}
})();
/**
* 扩展日期对象
*/
;(function(){
Date.SECOND = {
MINUTES:60000,
HOURS:3600000,
DAY:86400000,
WEEK:604800000,
}
Date.Dictionary = {
Holiday:{
"0101":"元旦",
"0214":"情人节",
"0308":"妇女节",
"0312":"植树节",
"0315":"消费者权益日",
"0401":"愚人节",
"0501":"劳动节",
"0504":"青年节",
"0512":"护士节",
"0601":"儿童节",
"0701":"建党节",
"0801":"建军节",
"0910":"教师节",
"0928":"孔子诞辰",
"1001":"国庆节",
"1006":"老人节",
"1024":"联合国日",
"1224":"平安夜",
"1225":"圣诞节",
},
LunarHoliday:{
"0101":"春节",
"0115":"元宵节",
"0505":"端午节",
"0707":"七夕情人节",
"0715":"中元节",
"0815":"中秋节",
"0909":"重阳节",
"1208":"腊八节",
"1224":"小年"
},
Week:{
"0" : "日",
"1" : "一",
"2" : "二",
"3" : "三",
"4" : "四",
"5" : "五",
"6" : "六"
},
timeDistance:{
yesterday:"昨天",
daysAgo:"{0}天前",
hoursAgo:"{0}小时前",
minutesAgo:"{0}分钟前",
justNow:"刚刚"
}
}
Date.is = function(obj){
return Object.prototype.toString.call(obj) == "[object Date]";
}
Date.valueOf = function(val,fmt){
if(val == undefined) return;
if(Date.is(val)){
return val;
}
if(String.is(val)){
if (/^\d+$/.test(val)) {
return new Date(parseInt(val));
}else if(fmt!=undefined){
var fmts = fmt.split("");
var conf = {
y:{start:undefined,end:undefined},
M:{start:undefined,end:undefined},
d:{start:undefined,end:undefined},
H:{start:undefined,end:undefined},
h:{start:undefined,end:undefined},
m:{start:undefined,end:undefined},
s:{start:undefined,end:undefined},
};
for (var i = 0; i < fmts.length; i++) {
var item = conf[fmts[i]];
if(item!=undefined){
if(item.start==undefined){
item.start = i;
}else{
item.end = i;
}
}
}
var keys = Object.keys(conf);
var parse = {};
for (var j = 0; j < keys.length; j++) {
var key = keys[j];
if(conf[key].start!=undefined && conf[key].end!=undefined){
parse[key] = parseInt(val.substr(conf[key].start,conf[key].end+1));
}
}
return new Date(parse.y||1975,(parse.M||1)-1,parse.d||1,parse.H||parse.h||0,parse.m||0,parse.s||0);
}else{
return new Date(val.replace(/-/g, "/"));
}
}else if(Number.is(val)){
return new Date(val);
}
return undefined;
}
Date.prototype.format = function(fmt){
fmt = fmt || "yyyy-MM-dd HH:mm:ss";
var o = {
"M+" : this.getMonth()+1, //月份
"d+" : this.getDate(), //日
"h+" : this.getHours()%12 == 0 ? 12 : this.getHours()%12, //小时
"H+" : this.getHours(), //小时
"m+" : this.getMinutes(), //分
"s+" : this.getSeconds(), //秒
"q+" : Math.floor((this.getMonth()+3)/3), //季度
"S" : this.getMilliseconds() //毫秒
};
if(/(y+)/.test(fmt)){
fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
}
if(/(E+)/.test(fmt)){
fmt=fmt.replace(RegExp.$1,((RegExp.$1.length>1)?(RegExp.$1.length==3?"星期":"周"):"")+this.i18n("Date.Week."+this.getDay()));
}
for(var k in o){
if(new RegExp("("+ k +")").test(fmt)){
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
}
}
return fmt;
}
Date.prototype.toString = function(){
return this.format();
}
Date.prototype.getMonthInfo = function(){
return {
first:new Date(this.getFullYear(),this.getMonth(),1),
last:new Date(this.getFullYear(),this.getMonth()+1,0),
month:this.getMonth(),
name:this.format("yyyy-MM"),
monthIndex:this.getMonthIndex()
}
}
Date.prototype.getMonthIndex = function(){
return parseInt(this.format("yyyyMM"));
}
Date.prototype.getYearInfo = function(){
return {
first:new Date(this.getFullYear(),0,1),
last:new Date(this.getFullYear(),12,0),
name:this.format("yyyy")
}
}
Date.prototype.getWeekOpt = function(){
var yearFirst = new Date(this.getFullYear(),0,1);
var d = Math.round((this.valueOf() - yearFirst.valueOf()) / Date.SECOND.DAY);
return {
first:new Date(this.getTime() - this.getDay()* Date.SECOND.DAY),
last:new Date(this.getTime() + (7 - this.getDay())* Date.SECOND.DAY),
monthIndex:Math.ceil((this.getDate() + 6 - this.getDay()) / 7),
yearIndex:Math.ceil((d + ((yearFirst.getDay() + 1) - 1)) / 7),
name:this.format("EE"),
fullName:this.format("yyyy-MM EE")
}
}
Date.prototype.copy = function(){
return new Date(this.getFullYear(),this.getMonth(),this.getDate(),this.getHours(),this.getMinutes(),this.getSeconds());
}
Date.prototype.getDayIndex = function(){
var yearFirst = new Date(this.getFullYear(),0,1);
return parseInt((this.valueOf() - yearFirst.valueOf())/Date.SECOND.DAY)+1
}
Date.prototype.getDayName = function(){
return this.format("yyyy-MM-dd");
}
Date.prototype.next = function(){
if(arguments.length==0){
return new Date(this.getTime()+Date.SECOND.DAY);
}
if(arguments.length > 0){
var type = arguments[0];
if(Object.prototype.toString.call(type) == "[object Number]" ||
(Object.prototype.toString.call(type) == "[object String]" && /^-?\d+$/.test(type))){
return new Date(this.getTime()+Date.SECOND.DAY*parseInt(type));
}
var step = parseInt(arguments[1]) || 1;
var res = undefined;
switch (type) {
case "y":
res = new Date(this.getTime());
res.setFullYear(this.getFullYear()+step);
break;
case "M":
res = new Date(this.getTime());
res.setDate(1);
var y = step%12;
res.setFullYear(this.getFullYear()+parseInt(step/12));
var month = res.getMonth()+y;
if(month>11){
res.setFullYear(res.getFullYear()+1);
res.setMonth(month-12);
}else if(month < 0){
res.setFullYear(res.getFullYear()-1);
res.setMonth(month+12);
}else{
res.setMonth(month);
}
var conf = res.getMonthInfo();
if(this.getDate() < conf.last.getDate()){
res.setDate(this.getDate());
}else{
res.setDate(conf.last.getDate());
}
break;
case "w": res = new Date(this.getTime()+Date.SECOND.WEEK*step); break;
case "d": res = new Date(this.getTime()+Date.SECOND.DAY*step); break;
case "H": res = new Date(this.getTime()+Date.SECOND.HOURS*step); break;
case "m": res = new Date(this.getTime()+Date.SECOND.MINUTES*step); break;
case "s": res = new Date(this.getTime()+1000*step); break;
default:
res = new Date(this.getTime()+Date.SECOND.DAY*step);
break;
}
return res;
}
}
Date.prototype.i18n = function(fmt,args){
var str = undefined;
if($i18n){
var val = $i18n.t(fmt);
str =val==fmt?undefined:val;
}
if(str==undefined){
var obj = Date.Dictionary;
var fmts = fmt.split('.');
for (let i = 1; i < fmts.length; i++) {
const item = fmts[i];
obj = obj[item];
if(obj==undefined){
break;
}
}
str = obj;
}
if(str==undefined) return str;
if(args!=undefined){
for (var i = 0; i < args.length; i++) {
args[i] = args[i] || "";
var reg = new RegExp("({)" + i + "(})", "g");
str = str.replace(reg, args[i]);
}
}
str = str.replace(/^\{\d+\}$/g, "");
return str;
}
Date.prototype.getHolidayName = function(){
return this.i18n("Date.Holiday."+this.format("MMdd"));
}
Date.prototype.getLunarHolidayName = function(fmt){
return this.i18n("Date.LunarHoliday."+fmt);
}
Date.prototype.getTimeDistance = function(){
var date_now = new Date();
var time = this.getTime();
var distance = date_now.getTime() - time;
var days = parseInt(distance / (1000 * 60 * 60 * 24));
if (days == 1) {
return this.i18n('Date.timeDistance.yesterday');
} else if (days > 1 && days < 4) {
return this.i18n('Date.timeDistance.daysAgo',[days]);//days ago
} else if (days > 3) {
// 超过3天的返回日期如 2018-12-05
// 如果是今年的,就省去年份,返回 12-05
var year = this.getFullYear();
var month = this.getMonth() + 1;
if (month < 10) {
month = "0" + month;
}
var day = this.getDate();
if (day < 10) {
day = "0" + day;
}
if (date_now.getFullYear() == year) {
return month + "-" + day;
} else {
return year + "-" + month + "-" + day;
}
}
var hours = parseInt((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
if (hours > 0) {
return this.i18n('Date.timeDistance.hoursAgo',[hours]);
}
var minutes = parseInt((distance % (1000 * 60 * 60)) / (1000 * 60));
if (minutes > 0) {
return this.i18n('Date.timeDistance.minutesAgo',[minutes]);
};
return this.i18n('Date.timeDistance.justNow');
}
})();

109
src/utils/dataFormat.ts Normal file
View File

@ -0,0 +1,109 @@
import TypeTools, { TYPES } from './type-tools';
const FormatType = {
DATE: 'date',
TIME: 'time',
DATETIME: 'datetime',
QTY: 'qty',
AMT: 'amt',
}
const DATE_FORMAT = {
DEFAULT: 'yyyy-MM-dd HH:mm:ss',
FULL_DATE: 'yyyy-MM-dd HH:mm:ss.S',
DATE: 'yyyy-MM-dd',
YEAR_MONTH: 'yyyy-MM',
TIME: 'HH:mm:ss'
}
const DataFormat = {
/**
* 格式化日期
* @param date 日期
* @param formatString 格式化字符串
* @returns 格式化后的日期
*/
formatDate: (date: Date | string | number, fmt: string = 'yyyy-MM-dd HH:mm:ss') => {
return Date.valueOf(date).format(fmt);
},
formatQty: (val: string | number, digits:number) => {
return format(new Date(date), digits);
},
formatAmt: (val: string | number, formatString: number) => {
return format(new Date(date), digits);
},
parseDate: (date: string | Date, formatString: string = 'yyyy-MM-dd HH:mm:ss') => {
return new Date(date);
},
format:(data:any,option:any) => {
},
};
export class FormatOption {
key:string;
type: string;
formatString: string|number;
constructor(key:string,type: string, formatString: string|number) {
this.key = key;
this.type = type;
this.formatString = formatString;
}
static createDate(key:string,type: string, formatString: string){
return new FormatOption(key,type,formatString);
}
static createQty(key:string,decimal?:number){
return new FormatOption(key,FormatType.QTY,decimal!=undefined?decimal:3);
}
static createAmt(key:string){
return new FormatOption(key,FormatType.AMT,2);
}
format(data:any):any{
var keys = this.key.split(".");
let valueKey = keys[keys.length-1];
let valueData;
if(keys.length<=1){
valueData = data;
}else{
valueData = TypeTools.getNestedValue(data,keys.splice(0,keys.length-1));
}
if(valueData!=undefined && TypeTools.isArray(valueData)){
for (let i = 0; i < valueData.length; i++) {
let item = valueData[i];
let val = item[valueKey];
item[valueKey] = this.format(val);
}
}
if(this.type==FormatType.DATE){
}else{
}
return data;
}
formatValue(value:any):any{
if(value==undefined || value==null) return value;
try {
if(this.type==FormatType.DATE || this.type==FormatType.TIME || this.type==FormatType.DATETIME){
return DataFormat.formatDate(value,this.formatString as string);
}else if(this.type==FormatType.QTY){
return DataFormat.formatQty(value,this.formatString as number);
}else if(this.type==FormatType.AMT){
return DataFormat.formatAmt(value,this.formatString as number);
}
} catch (error) {
return value;
}
}
}

View File

@ -1,77 +1,113 @@
const OBJ_TYPE = {
OBJ: "[object Object]",
NUMBER: "[object Number]",
STRING: "[object String]",
UNDEFINED: "[object Undefined]",
NULL: "[object Null]",
BOOLEAN: "[object Boolean]",
ARRAY: "[object Array]",
FUNCTION: "[object Function]",
DATE: "[object Date]",
REGEXP: "[object RegExp]",
JSON: "[object JSON]",
OBJ: "[object Object]",
NUMBER: "[object Number]",
STRING: "[object String]",
UNDEFINED: "[object Undefined]",
NULL: "[object Null]",
BOOLEAN: "[object Boolean]",
ARRAY: "[object Array]",
FUNCTION: "[object Function]",
DATE: "[object Date]",
REGEXP: "[object RegExp]",
JSON: "[object JSON]",
}
const TYPES = {
BOOLEAN: "BOOLEAN",
NUMBER: "NUMBER",
STRING: "STRING",
DATE: "DATE",
REGEXP: "REGEXP",
NULL: "NULL",
OBJ: "OBJ",
ARRAY: "ARRAY",
JSON: "JSON",
FUNCTION: "FUNCTION",
UNKNOWN: "UNKNOWN",
BOOLEAN: "BOOLEAN",
NUMBER: "NUMBER",
STRING: "STRING",
DATE: "DATE",
REGEXP: "REGEXP",
NULL: "NULL",
OBJ: "OBJ",
ARRAY: "ARRAY",
JSON: "JSON",
FUNCTION: "FUNCTION",
UNKNOWN: "UNKNOWN",
}
const TypeTools = {
parseType: function (obj: any) {
if (obj === null) return TYPES.NULL;
if(this.isNull(obj)) return TYPES.NULL;
if(this.isString(obj)) return TYPES.STRING;
if(this.isNumber(obj)) return TYPES.NUMBER;
if(this.isBoolean(obj)) return TYPES.BOOLEAN;
if(this.isDate(obj)) return TYPES.DATE;
if(this.isRegExp(obj)) return TYPES.REGEXP;
if(this.isArray(obj)) return TYPES.ARRAY;
if(this.isFunction(obj)) return TYPES.FUNCTION;
if(this.isObject(obj)) return TYPES.OBJ;
if(this.isJSON(obj)) return TYPES.JSON;
return TYPES.UNKNOWN;
},
isString: function (obj: any) {
return OBJ_TYPE.STRING === Object.prototype.toString.call(obj);
},
isBoolean: function (obj: any) {
return OBJ_TYPE.BOOLEAN === Object.prototype.toString.call(obj);
},
isNumber: function (obj: any) {
return OBJ_TYPE.NUMBER === Object.prototype.toString.call(obj);
},
isDate: function (obj: any) {
return OBJ_TYPE.DATE === Object.prototype.toString.call(obj);
},
isRegExp: function (obj: any) {
return OBJ_TYPE.REGEXP === Object.prototype.toString.call(obj);
},
isNull: function (obj: any) {
const type = Object.prototype.toString.call(obj);
return OBJ_TYPE.NULL === type || OBJ_TYPE.UNDEFINED === type;
},
isJSON: function (obj: any) {
return OBJ_TYPE.JSON === Object.prototype.toString.call(obj);
},
isArray: function (obj: any) {
return OBJ_TYPE.ARRAY === Object.prototype.toString.call(obj);
},
isFunction: function (obj: any) {
return OBJ_TYPE.FUNCTION === Object.prototype.toString.call(obj);
},
isObject: function (obj: any) {
return OBJ_TYPE.OBJ === Object.prototype.toString.call(obj);
parseType: function (obj: any) {
if (obj === null) return TYPES.NULL;
if (this.isNull(obj)) return TYPES.NULL;
if (this.isString(obj)) return TYPES.STRING;
if (this.isNumber(obj)) return TYPES.NUMBER;
if (this.isBoolean(obj)) return TYPES.BOOLEAN;
if (this.isDate(obj)) return TYPES.DATE;
if (this.isRegExp(obj)) return TYPES.REGEXP;
if (this.isArray(obj)) return TYPES.ARRAY;
if (this.isFunction(obj)) return TYPES.FUNCTION;
if (this.isObject(obj)) return TYPES.OBJ;
if (this.isJSON(obj)) return TYPES.JSON;
return TYPES.UNKNOWN;
},
isString: function (obj: any) {
return OBJ_TYPE.STRING === Object.prototype.toString.call(obj);
},
isBoolean: function (obj: any) {
return OBJ_TYPE.BOOLEAN === Object.prototype.toString.call(obj);
},
isNumber: function (obj: any) {
return OBJ_TYPE.NUMBER === Object.prototype.toString.call(obj);
},
isDate: function (obj: any) {
return OBJ_TYPE.DATE === Object.prototype.toString.call(obj);
},
isRegExp: function (obj: any) {
return OBJ_TYPE.REGEXP === Object.prototype.toString.call(obj);
},
isNull: function (obj: any) {
const type = Object.prototype.toString.call(obj);
return OBJ_TYPE.NULL === type || OBJ_TYPE.UNDEFINED === type;
},
isJSON: function (obj: any) {
return OBJ_TYPE.JSON === Object.prototype.toString.call(obj);
},
isArray: function (obj: any) {
return OBJ_TYPE.ARRAY === Object.prototype.toString.call(obj);
},
isFunction: function (obj: any) {
return OBJ_TYPE.FUNCTION === Object.prototype.toString.call(obj);
},
isObject: function (obj: any) {
return OBJ_TYPE.OBJ === Object.prototype.toString.call(obj);
},
getNestedValue: function (obj: any, paths: string[]) {
let i = 0;
let o = obj;
do {
let key = paths[i];
if (o != undefined && Array.isArray(o)) {
o = this.getArrayValue(o, key);
} else {
o = o != undefined ? o[key] : undefined;
}
i++;
if (o == undefined) return undefined;
} while (i < paths.length);
return o;
},
getArrayValue: function (o: any[], key: string) {
if (o.length > 0) {
let next:any = [];
for (let i = 0; i < o.length; i++) {
const it = o[i];
if (it != undefined && Array.isArray(it)) {
let vals = this.getArrayValue(it, key);
if (vals != undefined && vals.length > 0) {
for (let j = 0; j < vals.length; j++) {
const val = vals[j];
next.push(val);
}
}
} else {
let val = it[key];
if (val != undefined && val != null) next.push(val);
}
}
return next;
}
},
};
export default TypeTools;