wanke-bff-share v1.1.28
wank-bff-share(万克中间层公用部分)
约定
- route层书写的graphQlQuery外层包装属性为results
- models层书写函数时,函数命名按照请求方式+资源名的形式,例:getUsers,postUser,patchUser,putUser,deleteUser,getUserById
- 参数中Array类型的参数后缀用List代表,例:idList,typeList等
- models中的参数类型命名规范,基础资源返回I + 资源名(IUser),函数参数按I + 方法名定义(IPostUsers)
- 编写models中的graphTypeDefine文件时,如果类型为数组格式,区分数组格式还是对象格式,则命名中应加List,例:AlarmEventType, AlarmEventListType
- route层引入模块命名按模块文件名+模块功能+s(usersModels,baseHandlers)
- 常量命名全部大写,多单词按下划线间隔
- 查询某类资源详情,修改,删除,路由命名按:id的形式,例(/users/:id),查询该资源的某类关系也一样,例(users/:id/stations)
- 请求方式规范尽量参照RESTful风格
- routes文件命名采用脊柱风格
- 其他文件采用驼峰命名
configs(通用配置项部分)
调用方式:share.configs.XXX
configResource.json(通用枚举查询配置项)
使用示例
{
"负荷单元": "loadUnit",
"匹配参数": {
"stationId": "电站Id"
}
},
以上配置代表可以查询loadUnit的枚举值,可过滤的参数为stationId 增加通用枚举只需在models里增加对应的方法和在该配置项里写出,让前端通过/enums?resource=XXX的格式即可
configTranslationEN(国际化-英文翻译配置项)
configTranslationZH(国际化-中文翻译配置项)
handlers(通用基础方法)
调用方式:share.handlers.XXX
baseHandler(基础方法)
cloneObj
描述: cloneObj()方法克隆传入的对象,并返回该对象的新的引用
方法 demo:
const origin = { a: 1 };
const new1 = origin;
const new2 = cloneObj(origin);
origin.a = 2;
console.log(new1);
// { a: 2 }
console.log(new2);
// { a: 1 }
语法:
const newReference = baseHandler.cloneObj(origin)
参数: | property | type | require | default | |----------|------| ------- | ------- | | origin | any | true | 无 |
返回值: 入参对象的新引用
flatTreeToArray
描述: flatTreeToArray()方法将传入的树型结构数据扁平化,并返回扁平化后的数组结构
方法 demo:
const children = {
a: 1,
children: [{
b: 2,
c: 3,
children: []
}, {
d: 4,
children: []
}],
};
const child = {
a: 1,
child: [{
b: 2,
c: 3,
child: []
}, {
d: 4,
child: []
}],
};
const childrenArr = flatTreeToArray(children);
console.log(childrenArr);
// [ { a: 1 }, { b: 2, c: 3 }, { d: 4 } ]
const childrenArr1 = flatTreeToArray(children, 'child');
console.log(childrenArr1);
// 返回children对象本身
const childArr = flatTreeToArray(child, 'child');
console.log(childArr);
// [ { a: 1 }, { b: 2, c: 3 }, { d: 4 } ]
语法:
const flatArr = baseHandler.flatTreeToArray(treeData, flatProperty)
参数: | property | type | require | default | |----------|------| ------- | ------- | | treeData | object | true | 无 | | flatProperty | string | false | children | 返回值: 树形结构数据扁平化后的数组,每一项的值为树形结构每一层的对象
fixDigits
描述: fixDigits()方法将传入的数据四舍五入后保留一定位数,并返回处理后的结果
方法 demo:
console.log(fixDigits(100.124135));
// 100.12
console.log(fixDigits(0.1235425));
// 0.12
console.log(fixDigits(100.124135, 4));
// 100.1241
console.log(fixDigits(0));
// 0
console.log(fixDigits(0, 2, true));
// 0.00
语法:
const valueStr = baseHandler.fixDigits(target, digits, isMandatoryDigits)
参数: | property | type | require | default | |----------|------| ------- | ------- | | target | Number | true | 无 | | digits | Number | true | 2 | | isMandatoryDigits | Boolean | true | false | 返回值: 经过处理后的字符串
filterTree
描述: filterTree()方法根据传入的filter方法过滤根节点下的子树,并返回新树
Tips:根节点暂未进行过滤
方法 demo:
const tree = {
name: 'john',
age: 50,
children: [{
name: 'john-son1',
age: 18
}, {
name: 'john-son2',
age: 15
}, {
name: 'john-daughter1',
age: 18
}]
};
const filter = node => node.age === 15;
const newTree = filterTree(tree.children, filter);
console.log(newTree);
// [{
// name: 'john-son1',
// age: 18
// }, {
// name: 'john-daughter1',
// age: 18
// }]
语法:
const newChildrenTree = baseHandler.filterTree(treeChildren, filter)
参数: | property | type | require | default | |----------|------| ------- | ------- | | treeChildren | any[] | true | 无 | | filter | function | true | 无 | 返回值: 过滤后的新树
streamToBuffer
描述: streamToBuffer()方法将可读流对象转换,并返回一个Buffer对象
方法 demo:
这个比较抽象,输出也看不出含义,大家按照功能描述使用即可
语法:
const buffer = baseHandler.streamToBuffer(stream)
参数: | property | type | require | default | |----------|------| ------- | ------- | | stream | any | true | 无 | 返回值: 转换后的buffer对象
judgeKeyIsExist
描述: judgeKeyIsExist()方法将遍历传入的对象,判断是否含有必要的键值,只要非undefined都认为存在,并返回一个缺失的属性数组,如果必要属性都存在,返回一个空数组
方法 demo:
const origin = {
a: 1,
b: 2,
c: 3
};
console.log(judgeKeyIsExist(origin, ['a', 'b']));
// []
console.log(judgeKeyIsExist(origin, ['a', 'b', 'd']));
// [ 'd' ]
语法:
const losts = baseHandler.judgeKeyIsExist(origin, requestKey)
参数: | property | type | require | default | |----------|------| ------- | ------- | | origin | object | true | 无 | | requestKey | string[] | true | 无 | 返回值: 缺失键值的数组
getUniqueId
描述: getUniqueId()方法获取uid,返回一个uid
方法 demo:
const uid = getUniqueId();
const uid2 = getUniqueId();
console.log(uid);
// js/4ed1_9d51
console.log(uid2);
// js/4c07_82a1
语法:
const uid = baseHandler.getUniqueId()
参数: | property | type | require | default | |----------|------| ------- | ------- | 返回值: uid
judgeEmpty
描述: judgeEmpty()方法判断字段是否为空,并返回一个布尔值 空值包括undefined,null,空串,null的字符串
方法 demo:
const origin = {
b: null,
c: '',
d: 'null',
e: 0,
};
console.log(judgeEmpty(origin.a));
// true
console.log(judgeEmpty(origin.b));
// true
console.log(judgeEmpty(origin.c));
// true
console.log(judgeEmpty(origin.d));
// true
console.log(judgeEmpty(origin.e));
// false
语法:
const isEmpty = baseHandler.judgeEmpty(property)
参数: | property | type | require | default | |----------|------| ------- | ------- | | property | any | true | 无 | 返回值: 根据是否为给定的假值返回一个布尔值
jsonToCsv
描述: jsonToCsv()方法是对第三方库json2csv的parseAsync函数的包装,将json文件转为csv格式数据
方法 demo:
具体使用可查询第三方库json2csv,参数与第三方库一致
Github链接:https://github.com/zemirco/json2csv
语法:
const csvData = baseHandler.jsonToCsv(data, opts)
参数: | property | type | require | default | |----------|------| ------- | ------- | | data | array | true | 无 | | opts | object | true | 无 |
options参数:查看具体第三方库链接 返回值: 转换后的csv格式数据
getPropertyFromOrigin
描述: getPropertyFromOrigin()方法从传入的方法中获取对应的属性,如没有对应属性则只为null
方法 demo:
const objOrigin = {
id: 1,
name: 'hans',
title: 'HANS',
};
const arrOrigin = [{
id: 1,
name: 'hans',
title: 'HANS',
}, {
id: 2,
name: 'john',
title: 'JOHN'
}];
const objData = getPropertyFromOrigin(objOrigin);
console.log(objData);
// { name: 'HANS', value: 1 }
const objData2 = getPropertyFromOrigin(objOrigin, { name: 'name', title: 'title', age: 'age' });
console.log(objData2);
// { name: 'hans', title: 'HANS', age: null }
const arrData = getPropertyFromOrigin(arrOrigin);
console.log(arrData);
// [ { name: 'HANS', value: 1 }, { name: 'JOHN', value: 2 } ]
语法:
const data = baseHandler.getPropertyFromOrigin(origin, propertyMap)
参数: | property | type | require | default | |----------|------| ------- | ------- | | origin | any | true | 无 | | propertyMap | object | true | { name: 'title', value: 'id' } |
返回值: 根据propertyMap中的对应关系转换后的数据
__cutoutProperty
描述: __cutoutProperty()方法是对GraphQL的裁剪的封装,返回裁剪后的数据
方法 demo:
具体见使用端
语法:
const results = baseHandler.__cutoutProperty(type, resolve, graphqlQuery, catchGraphQlError)
参数: | property | type | require | default | |----------|------| ------- | ------- | | type | GraphQLObjectType | true | 无 | | resolve | any | true | 无 | | graphqlQuery | string | true | 无 | | catchGraphQlError | function | true | (error: ReadonlyArray) => console.log(error) |
返回值: 截取后的数据
frontHandler(前端处理方法)
setPageToTarget
描述: setPageToTarget()方法将后端返回的分页属性赋值给中间层返回对象上,主要使用为中间层返回分页参数的层级和后台不一致,进行统一处理,会修改传入的target对象
方法 demo:
const origin = {
page: 1,
size: 20,
totalCount: 2,
totalPages: 1
};
const target = {};
setPageToTarget(origin, target);
console.log(target);
// { page: 1, size: 20, totalCount: 2, totalPages: 1 }
语法:
baseHandler.setPageToTarget(origin, target)
参数: | property | type | require | default | |----------|------| ------- | ------- | | origin | object | true | 无 | | target | object | true | 无 |
返回值: 无返回值
runMonthToStr
描述: runMonthToStr()方法将后端返回的适用月份字符串改为前端适合的字符串显示,如果有连续数字则转换为start~end格式,具体见方法demo,并返回对象本身
方法 demo:
console.log(runMonthToStr({ runMonth: '1,2,4,6' }));
// { runMonth: '1,2,4,6', runMonthTitle: '1~2,4,6' }
console.log(runMonthToStr({ runMonth: '1,2,3,4,5,6,7,8,9,10' }));
// { runMonth: '1,2,3,4,5,6,7,8,9,10', runMonthTitle: '1~10' }
语法:
frontHandler.runMonthToStr(origin)
参数: | property | type | require | default | |----------|------| ------- | ------- | | origin | object | true | 无 |
返回值: 传入的对象本身,增加了runMonthTitle字段
setUniqueId
描述: setUniqueId()方法为后端返回树结构数据添加唯一标识,并返回添加后的树形数据,会修改原对象
方法 demo:
const origin = {
a: 1,
children: [{
b: 1,
children: [{
d: 1
}]
}, {
c: 1
}]
};
setUniqueId(origin);
console.log(origin);
// {
// a: 1,
// key: '0',
// children: [{
// b: 1,
// key: '0-0'
// children: [{
// d: 1
// key: '0-0-0'
// }]
// }, {
// c: 1,
// key: '0-1'
// }]
// }
setUniqueId(origin, '1');
console.log(origin);
// {
// a: 1,
// key: '1',
// children: [{
// b: 1,
// key: '1-0'
// children: [{
// d: 1
// key: '1-0-0'
// }]
// }, {
// c: 1,
// key: '1-1'
// }]
// }
语法:
frontHandler.setUniqueId(tree, key)
参数: | property | type | require | default | |----------|------| ------- | ------- | | tree | object | true | 无 | | key | string | true | 空串 |
返回值: 增加标识后的树形结构
echartDataGenerator
描述: echartDataGenerator()方法为了前端展示需要,将按传入的参数生成固定间隔的时间轴数据,后端数据传入时,相同时间的数据将对应到具体时间,并返回xData和yData
方法 demo:
const baseData = [{
dtime: '2020-06-29 00:00:00',
val: 1
}, {
dtime: '2020-06-29 00:15:00',
val: 2
}, {
dtime: '2020-06-29 00:30:00',
val: 3
}];
// 无数据生成x轴,默认参数
const echarts = echartDataGenerator([], '2020-06-29 00:00:00', '2020-06-29 00:30:00');
console.log(echarts);
// {
// xData:
// [ '2020-06-29 00:00:00',
// '2020-06-29 00:15:00',
// '2020-06-29 00:30:00' ],
// yData:
// [ null, null, null ]
// }
// 无数据生成x轴,按指定参数生成
const echarts2 = echartDataGenerator([], '2020-06-29 00:00:00', '2020-06-29 00:10:00', 'minute', 1);
console.log(echarts2);
// { xData:
// [ '2020-06-29 00:00:00',
// '2020-06-29 00:01:00',
// '2020-06-29 00:02:00',
// '2020-06-29 00:03:00',
// '2020-06-29 00:04:00',
// '2020-06-29 00:05:00',
// '2020-06-29 00:06:00',
// '2020-06-29 00:07:00',
// '2020-06-29 00:08:00',
// '2020-06-29 00:09:00',
// '2020-06-29 00:10:00' ],
// yData:
// [ null, null, null, null, null, null, null, null, null, null, null ]
// }
// 数据赋值
const echarts3 = echartDataGenerator(baseData, '2020-06-29 00:00:00', '2020-06-29 00:30:00');
console.log(echarts3);
// {
// xData:
// ['2020-06-29 00:00:00',
// '2020-06-29 00:15:00',
// '2020-06-29 00:30:00'],
// yData:
// ['1', '2', '3']
// }
语法:
const echarts = frontHandler.echartDataGenerator(results, startTime, endTime, timeType, timeInterval, fix, withTime)
参数: | property | type | require | default | |----------|------| ------- | ------- | | results | { dtime: string, val: number }[] | true | 无 | | startDate | string | true | 无 | | endDate | string | true | 无 | | timeType | string | true | 'minute' | | timeInterval | number | true | 15 | | fix | number | true | 2 | | withTime | boolean | true | true |
返回值: 返回一个包含xData和yData属性的对象
addNum
描述: addNum()方法为了前端展示需要,列表展示需要序号,统一添加序号,支持分页参数,并返回添加后的数据,会修改原数组,如果数据为空,则返回空数组
方法 demo:
let arr = [{
name: 'hans'
}, {
name: 'john'
}];
arr = addNum(arr);
console.log(arr);
// [ { name: 'hans', num: 1 }, { name: 'john', num: 2 } ]
arr = addNum(arr, 2, 10);
console.log(arr);
// [ { name: 'hans', num: 11 }, { name: 'john', num: 12 } ]
语法:
frontHandler.addNum(target, page, size)
参数: | property | type | require | default | |----------|------| ------- | ------- | | target | any[] | true | 无 | | page | number | false | 无 | | size | number | false | 无 |
返回值: 返回添加后的对象本身或空数组
mixArrToObj
描述: mixArrToObj()方法为了将后端返回的多个数据数组拼接到一个对象数组中,dataArr,typeArr,unitArr数组的长度应保持一致,存在对应关系,startIndex为返回对象中num属性的起始数字,digits为dataArr中数据保留位数
方法 demo:
const dataArr = [
[{ dtime: '2020-06-30 00:00:00', val: 1.1345 }, { dtime: '2020-06-30 00:15:00', val: 4.1345 }],
[{ dtime: '2020-06-30 00:00:00', val: 2.1345 }, { dtime: '2020-06-30 00:15:00', val: 5.1345 }],
[{ dtime: '2020-06-30 00:00:00', val: 3.1345 }, { dtime: '2020-06-30 00:15:00', val: 6.1345 }],
];
const typeArr = ['a', 'b', 'c'];
const unitArr = ['V', 'V', 'V'];
const fixArr = mixArrToObj(dataArr, typeArr, unitArr, 1, 3);
console.log(fixArr);
// [
// {
// num: 1,
// dtime: '2020-06-30 00:00:00',
// a: '1.135V',
// b: '2.135V',
// c: '3.135V'
// },
// {
// num: 2,
// dtime: '2020-06-30 00:15:00',
// a: '4.135V',
// b: '5.135V',
// c: '6.135V'
// }
// ]
语法:
const fixArr = frontHandler.mixArrToObj(dataArr, typeArr, unitArr, startIndex, digits)
参数: | property | type | require | default | |----------|------| ------- | ------- | | dataArr | any[] | true | 无 | | typeArr | string[] | true | 无 | | unitArr | string[] | true | 无 | | startIndex | number | true | 1 | | digits | number | true | 2 |
返回值: dataArr数据混合后的对象数组
getParamLost
描述: getParamLost()方法获取参数缺失,与baseHandler中的judgeKeyIsExist配套使用,用于检验请求参数的校验,并返回一个参数缺失对象,如果不存在缺失返回null
方法 demo:
const isNull = getParamLost([]);
console.log(isNull);
// null
const isLost = getParamLost(['a']);
console.log(isLost);
// { errorCode: 52, errorMsg: '参数缺失a' }
语法:
const isLost = frontHandler.getParamLost(losts)
参数: | property | type | require | default | |----------|------| ------- | ------- | | losts | string[] | true | 无 |
返回值: 参数缺失对象或者null
qrCodeGener
描述: qrCodeGener()方法生成二维码,供APP扫码使用。 采用第三方库qr-image生成,并返回一个buffer对象, 使用参数语法可参考第三方库
方法 demo:
这个比较抽象,输出也看不出含义,大家按照功能描述使用即可
语法:
const buffer = await frontHandler.qrCodeGener(content, type)
参数: | property | type | require | default | |----------|------| ------- | ------- | | content | string | true | 无 | | type | string | true | png |
返回值: 二维码buffer对象
getCategoryType
描述: qrCodeGener()方法生成二维码,供APP扫码使用。 采用第三方库qr-image生成,并返回一个buffer对象, 使用参数语法可参考第三方库
方法 demo:
这个比较抽象,输出也看不出含义,大家按照功能描述使用即可
语法:
const buffer = await frontHandler.qrCodeGener(content, type)
参数: | property | type | require | default | |----------|------| ------- | ------- | | content | string | true | 无 | | type | string | true | png |
返回值: 二维码buffer对象
conversionUnit
描述: conversionUnit()方法用于单位转换,并返回单位转换后的对象。该方法中英文方法通用,转换逻辑不同。 后台传回的单位都是基础单位 如果是电量数据,达到10000才进行转换,改为万kWh
中文情况下:1000进行转换,单位最高到G到就不再进行转换
英文情况下:不转换单位
方法 demo:
const opts = {
target: 1000,
unit: 'kWh'
}
const valueObj = conversionUnit(opts);
console.log(valueObj);
// { value: '1', unit: 'MWh' }
const opts2 = {
target: 1000,
unit: 'kWh',
threshold: 10000
}
const valueObj2 = conversionUnit(opts2);
console.log(valueObj2);
// { value: '1000', unit: 'kWh' }
const opts3 = {
target: 1000.145,
unit: 'kWh'
}
const valueObj3 = conversionUnit(opts3);
console.log(valueObj3);
// { value: '1', unit: 'MWh' }
const opts4 = {
target: 1000.145,
unit: 'kWh',
isEnglish: true
}
const valueObj4 = conversionUnit(opts4);
console.log(valueObj4);
// { value: '1000.15', unit: 'kWh' }
const opts5 = {
target: 10000.145,
unit: 'kWh',
isElectricity: true
}
const valueObj5 = conversionUnit(opts5);
console.log(valueObj5);
// { value: '1万', unit: 'kWh' }
语法:
const valueObj = frontHandler.conversionUnit(content, type)
参数: | property | type | require | default | |----------|------| ------- | ------- | | options | object | true | 无 |
options参数: | property | type | require | default | |----------|------| ------- | ------- | | target | number/null | true | 0 | | unit | string | true | 无 | | threshold | number | false | 1000 | | isEnglish | boolean | false | false | | digits | number | false | 2 | | isElectricity | boolean | false | false |
返回值: 单位转换后的对象,含有属性value,unit
getBaseResp
描述: getBaseResp()方法根据传入的基础数据类型,并返回基础的响应对象
方法 demo:
console.log(getBaseResp())
// { results: [], errorCode: 0, errorMsg: '' }
console.log(getBaseResp({}));
// { results: {}, errorCode: 0, errorMsg: '' }
console.log(getBaseResp(''));
// { results: '', errorCode: 0, errorMsg: '' }
语法:
const baseResp = frontHandler.getBaseResp(baseDataType)
参数: | property | type | require | default | |----------|------| ------- | ------- | | baseDataType | any | true | [] | 返回值: 基础的响应对象
getScaleUnitAndPowerUnit
描述: getScaleUnitAndPowerUnit()方法传入后台获取的属性配置数组,如果存在scale和ratedPower配置则获取对应的单位,如果未找到,默认返回kW
方法 demo:
const propertyArr = [{
name: 'scale',
unit: {
name: 'kWh'
}
}, {
name: 'ratedPower',
unit: {
name: 'kW'
}
}];
const unitObj = getScaleUnitAndPowerUnit(propertyArr);
console.log(unitObj);
// { scaleUnit: 'kWh', powerUnit: 'kW' }
console.log(getScaleUnitAndPowerUnit([]));
// { scaleUnit: 'kW', powerUnit: 'kW' }
语法:
const unitObj = frontHandler.getScaleUnitAndPowerUnit(propertyArr)
参数: | property | type | require | default | |----------|------| ------- | ------- | | propertyArr | array | true | 无 |
返回值: 单位对象,含有属性scaleUnit,powerUnit
requestHandler(请求处理方法)
getApiBaseUrl
描述: getApiBaseUrl()方法获取后台基础IP端口,返回值已封装到req._apiBaseUrl,需要的时候直接获取即可
方法 demo:
无
语法:
const baseUrl = await requestHandler.getApiBaseUrl()
参数: | property | type | require | default | |----------|------| ------- | ------- |
返回值: 获取后台接口的基础IP端口
joinUrl
描述: joinUrl()方法根据传入的参数拼接URL,query中含有数组时转为字符串,逗号间隔
方法 demo:
const baseUrl = 'http://localhost:3000/';
const pathArr = ['api', 'login'];
const query = {
name: 'hans',
password: 'password'
};
const url = joinUrl(baseUrl, pathArr, query);
console.log(url);
// http://localhost:3000/api/login?name=hans&password=password
语法:
const baseUrl = await requestHandler.joinUrl(baseUrl, path, query)
参数: | property | type | require | default | |----------|------| ------- | ------- | | baseUrl | string | true | 无 | | path | string[] | true | 无 | | query | object | true | {} |
返回值: 拼接后的url
requestPromise
描述: requestPromise()方法是第三方请求库request的封装
方法 demo:
具体使用
语法:
const baseUrl = await requestHandler.requestPromise(req, httpMethod, url, postBody)
参数: | property | type | require | default | |----------|------| ------- | ------- | | req | any | true | 无 | | httpMethod | string | true | 无 | | url | string | true | 无 | | postBody | object | true | {} |
返回值: url请求返回的Promise
responseHandle
描述: responseHandle()方法是对返回对象的处理,主要为logger打印返回内容服务,express原生的res对象里没有返回的具体数据内容,所以自己封装处理,如果未传具体返回,则默认为frontHandler.getBaseResp({})的默认返回
方法 demo:
具体使用
语法: requestHandler.responseHandle(res, results)`
参数: | property | type | require | default | |----------|------| ------- | ------- | | res | any | true | 无 | | results | any | false | 无 |
返回值: url请求返回的Promise
judgeAbnormalResp
描述: judgeAbnormalResp()方法判断后台返回是否出现跟约定数据格式不同,如不同则认为异常返回
方法 demo:
const objResp = {};
const arrayResp = [];
console.log(judgeAbnormalResp({ origin: objResp, promiseDataType: 'array' }));
// true
console.log(judgeAbnormalResp({ origin: objResp, promiseDataType: 'object' }));
// false
console.log(judgeAbnormalResp({ origin: arrayResp, promiseDataType: 'array' }));
// false
console.log(judgeAbnormalResp({ origin: arrayResp, promiseDataType: 'object' }));
// true
语法:
const isAbnormal = requestHandler.judgeAbnormalResp(options)
参数: | property | type | require | default | |----------|------| ------- | ------- | | options | object | true | 无 |
options参数: | property | type | require | default | |----------|------| ------- | ------- | | origin | any | true | 无 | | promiseDataType | string | true | 无 | 返回值: 判断与约定的数据类型是否一致返回一个布尔值,判断是否出现异常返回
timeHandler(时间处理方法)
checkDateFormat
描述: checkDateFormat()方法判断传入的日期格式是否符合需要的日期格式,并返回布尔值区分是否符合
方法 demo:
const dateStr = '2020-07-01';
console.log(checkDateFormat(dateStr));
// false
console.log(checkDateFormat(dateStr, 'YYYY-MM-DD'));
// true
语法:
const isTrue = timeHandler.checkDateFormat(dateStr, format)
参数: | property | type | require | default | |----------|------| ------- | ------- | | dateStr | string | true | 无 | | format | string | true | 'YYYY-MM-DD HH:mm:ss' |
返回值: 如格式符合返回true,格式不符返回false
__existDate
描述: __existDate()方法判断传入的日期是否存在,并返回一个布尔值表示是否存在该日期
Tips: 只是配合其他基础方法使用,外部不会调取到该方法,所以方法名前加了__表示私有
方法 demo:
console.log(__existDate('2020-02-29'));
// true
console.log(__existDate('2020-07-01'));
// true
console.log(__existDate('2029-02-29'));
// false
语法:
const isExist = timeHandler.__existDate(dateStr)
参数: | property | type | require | default | |----------|------| ------- | ------- | | dateStr | string | true | 无 |
返回值: 如存在返回true,不存在false
getFormatTimeObj
描述: getFormatTimeObj()方法获取当前时间的格式化对象,小于10的数字会进行补零,并返回一个对象
方法 demo:
const timeObj = getFormatTimeObj();
console.log(timeObj);
// {
// year: '2020',
// month: '07',
// day: '01',
// hour: '11',
// minute: '47',
// second: '14',
// curTime: '2020-07-01 11:47:14',
// curMonth: '2020-07',
// curDate: '2020-07-01'
// }
语法:
const timeObj = timeHandler.getFormatTimeObj()
参数: | property | type | require | default | |----------|------| ------- | ------- | | date | string | true | 当前时间 |
返回值: 年月日时分秒和常用当前月份,当前日期,当前时间的对象
getDateAfterCalculate
描述: getDateAfterCalculate()方法对于moment.add()方法的封装,并返回一个时间间隔后的日期
方法 demo:
const timeObj = getFormatTimeObj();
console.log(timeObj);
// {
// year: '2020',
// month: '07',
// day: '01',
// hour: '11',
// minute: '47',
// second: '14',
// curTime: '2020-07-01 11:47:14',
// curMonth: '2020-07',
// curDate: '2020-07-01'
// }
语法:
const timeObj = timeHandler.getDateAfterCalculate()
参数: | property | type | require | default | |----------|------| ------- | ------- |
返回值: 年月日时分秒和常用当前月份,当前日期,当前时间的对象
getCertainMonthDays
描述: getCertainMonthDays()方法获得传入月份的日期数组,长度为当月时间长度
方法 demo:
const certainMonth = '2020-02';
const monthDays = getCertainMonthDays(certainMonth);
console.log(monthDays);
// ['2020-02-01', '2020-02-02', ... '2020-02-29'];
语法:
const monthDays = timeHandler.getCertainMonthDays(certainMonth)
参数: | property | type | require | default | |----------|------| ------- | ------- | | certainMonth | string | true | 无 |
返回值: 返回一个数组,包含该月的日期
getDaysByArgs
描述: getDaysByArgs()方法根据传入的起止时间,获取该时间段内的日期,包含传入的日期
方法 demo:
const days = getDaysByArgs('2020-01-29', '2020-02-05');
console.log(days);
// ['2020-01-29', '2020-02-02', ... '2020-02-05'];
语法:
const days = timeHandler.getDaysByArgs(startDate, endDate)
参数: | property | type | require | default | |----------|------| ------- | ------- | | startDate | string | true | 无 | | endDate | string | true | 无 |
返回值: 返回一个数组,包含起止时间段内的日期
getTimePoint
描述: getTimePoint()方法根据传入的起止时间和时间间隔,返回该时间段内的年月日时分秒的数组
方法 demo:
const times = getTimePoint('2020-02-05', '2020-02-05', 60);
console.log(times);
// [
// '2020-02-05 00:00:00',
// '2020-02-05 01:00:00',
// '2020-02-05 02:00:00',
// '2020-02-05 03:00:00',
// '2020-02-05 04:00:00',
// '2020-02-05 05:00:00',
// '2020-02-05 06:00:00',
// '2020-02-05 07:00:00',
// '2020-02-05 08:00:00',
// '2020-02-05 09:00:00',
// '2020-02-05 10:00:00',
// '2020-02-05 11:00:00',
// '2020-02-05 12:00:00',
// '2020-02-05 13:00:00',
// '2020-02-05 14:00:00',
// '2020-02-05 15:00:00',
// '2020-02-05 16:00:00',
// '2020-02-05 17:00:00',
// '2020-02-05 18:00:00',
// '2020-02-05 19:00:00',
// '2020-02-05 20:00:00',
// '2020-02-05 21:00:00',
// '2020-02-05 22:00:00',
// '2020-02-05 23:00:00'
// ]
语法:
const times = timeHandler.getTimePoint(startDate, endDate, timeInterval)
参数: | property | type | require | default | |----------|------| ------- | ------- | | startDate | string | true | 无 | | endDate | string | true | 无 | | timeInterval | number | true | 15 |
返回值: 返回该时间段内的年月日时分秒的数组
getMonthBetween
描述: getMonthBetween()方法根据传入的起止时间和时间间隔,返回该时间段内的年月日时分秒的数组
方法 demo:
const months = getMonthBetween('2019-12-05', '2020-03-05');
console.log(months);
// [ '2019-12', '2020-01', '2020-02', '2020-03' ]
语法:
const months = timeHandler.getMonthBetween(startDate, endDate)
参数: | property | type | require | default | |----------|------| ------- | ------- | | startDate | string | true | 无 | | endDate | string | true | 无 |
返回值: 返回该时间段内的月份数组
getTimePointInterval
描述: getTimePointInterval()方法根据参数返回两个时间点的差值,差值的纬度可选minute/second
方法 demo:
const time1 = '2020-07-01 00:00:00';
const time2 = '2020-07-01 00:01:30';
console.log(getTimePointInterval(time1, time2));
// 1.5
console.log(getTimePointInterval(time1, time2, 'second'));
// 90
console.log(getTimePointInterval('00:00:00', '00:30:00'));
// 30
语法:
const timeInterval = timeHandler.getTimePointInterval(time1, time2, resultTimeType)
参数: | property | type | require | default | |----------|------| ------- | ------- | | time1 | string | true | 无 | | time2 | string | true | 无 | | resultTimeType | string | true | 'minute' |
返回值: 返回该时间段内的月份数组
msToDate
描述: msToDate()方法将输入的ms值转化为前端页面所需的显示格式,最长时间为30天,超过30天则只按秒为单位显示,如果不传则按0s处理
方法 demo:
const oneSecond = 1000;
console.log(msToDate(oneSecond));
// 1s
console.log(msToDate(oneSecond * 37));
// 37s
console.log(msToDate(oneSecond * 3760))
// 1h2min40s
console.log(msToDate(oneSecond * 3600 * 25))
// 1d1h
console.log(msToDate(oneSecond * 3600 * 24 * 31))
// 2678400s
console.log(msToDate());
// 0s
语法:
const display = timeHandler.msToDate(msd)
参数: | property | type | require | default | |----------|------| ------- | ------- | | msd | string/number | true | 无 |
返回值: ms转换后的显示值
splicingDate
描述: splicingDate()方法将把传入的参数拼接成年月日时分秒的字符串,具体见下面示例
方法 demo:
console.log(splicingDate('2020-07-01', '2020-07-02'));
// 2020-07-01 00:00:00,2020-07-02 23:59:59
console.log(splicingDate('2020-07', '2020-08'));
// 2020-07-01 00:00:00,2020-08-31 23:59:59
console.log(splicingDate('2019', '2020'));
// 2019-01-01 00:00:00,2020-01-01 00:00:00
语法:
const times = timeHandler.splicingDate(startDate, endDate)
参数: | property | type | require | default | |----------|------| ------- | ------- | | startDate | string | true | 无 | | endDate | string | true | 无 |
返回值: 拼接后的字符串
__timeToMoment
描述: __timeToMoment()方法获取传入的时间内最近的时刻点(0,15,30,45),并返回时间对象,包含属性hour,minute
Tips: 传入的格式为HH:mm
方法 demo:
console.log(__timeToMoment('12:14'));
// { hour: 12, minute: 15 }
console.log(__timeToMoment('12:01'));
// { hour: 12, minute: 0 }
console.log(__timeToMoment('12:59'));
// { hour: 13, minute: 0 }
语法:
const timeObj = timeHandler.__timeToMoment(time)
参数: | property | type | require | default | |----------|------| ------- | ------- | | time | string | true | 无 |
返回值: 时间对象,包含属性hour,minute
transfromTimeFormat
描述: transfromTimeFormat()方法是moment().format()方法的封装,用来转化时间格式
方法 demo:
console.log(transfromTimeFormat('2020-01-01'));
// 2020-01-01 00:00
console.log(transfromTimeFormat('2020-01-01 00:00:00', ETimeFormatType.DAY));
// 2020-01-01
语法:
const timeObj = timeHandler.transfromTimeFormat(time, format)
参数: | property | type | require | default | |----------|------| ------- | ------- | | time | string | true | 无 | | format | string | true | 'YYYY-MM-DD HH:mm' |
返回值: 转换后的时间字符串
judgeIsBetween
描述: judgeIsBetween()方法判断一个时间是否在两个时间点之间,并返回一个字符串表示关系,返回结果before/between/after
方法 demo:
console.log(judgeIsBetween('2020-07-01', '2020-07-01', '2020-07-01'));
// between
console.log(judgeIsBetween('2020-07-01', '2020-06-01', '2020-06-01'));
// after
console.log(judgeIsBetween('2020-07-01', '2020-08-01', '2020-08-01'));
// before
语法:
const relation = timeHandler.judgeIsBetween(cur, start, end)
参数: | property | type | require | default | |----------|------| ------- | ------- | | cur | string | true | 无 | | start | string | true | 无 | | end | string | true | 无 |
返回值: 转换后的时间字符串
getTimeIntervalByMoment
描述: getTimeIntervalByMoment()方法是moment.diff()方法的封装,获取两个时间点的具体时间间隔
方法 demo:
console.log(ms);
// 180000
console.log(day);
// 1
语法:
const relation = timeHandler.getTimeIntervalByMoment(originTime, targetTime, timeType)
参数: | property | type | require | default | |----------|------| ------- | ------- | | originTime | string | true | 无 | | targetTime | string | true | 无 | | timeType | string | true | days |
返回值: 两个时间点的具体时间间隔
msToDisplay
描述: msToDisplay()方法是针对前端页面显示需求,将ms值转换为所需格式
方法 demo:
console.log(msToDisplay(1000 * 60 * 15, 'HH:mm'));
// { H: '0', m: '15' }
console.log(msToDisplay(1000 * 60 * 75, 'HH:mm'));
// { H: '1', m: '15' }
console.log(msToDisplay(1000 * 60 * 70, 'HH'));
// { H: '1.17' }
语法:
const display = timeHandler.msToDisplay(ms, format, digits)
参数: | property | type | require | default | |----------|------| ------- | ------- | | ms | number | true | 无 | | format | string | true | 'HH' | | digits | number | true | 2 |
返回值: 转换后的时间对象
models(后台路由封装)
调用方式:share.models.XXX
参数: | property | type | require | default | |----------|------| ------- | ------- | | req | object | true | 无 | | params | object | true | 无 | | graphqlQuery | string | false | 无 |
Tips:这里的参数为model的通用参数,以下具体方法中的参数都为params中的参数
abnormalAlarmConfigs(告警配置)
总览: abnormalAlarmConfigs模块对应后台告警配置模块,存在以下函数
function | remark |
---|---|
getAbnormalAlarmConfigs | 获取告警配置列表 |
postAbnormalAlarmConfigs | 新增告警配置 |
patchAbnormalAlarmConfigs | 变量修改告警配置 |
deleteAbnormalAlarmConfigs | 删除告警配置 |
getAbnormalAlarmConfigs
描述: 后台对应URL:GET /ems-abnormal/alarmStatuses 根据传入的参数获取告警配置列表
参数: | property | type | require | default | |----------|------| ------- | ------- | | stationId | number | false | 无 | | page | number | false | 无 | | size | number | false | 无 | | queryStr | string | false | 无 |
接口开发流程
接口测试流程暂时跳过,等待hq-test包二期开发支持接口测试后实行
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago