Commit c7313daa authored by 涂茜's avatar 涂茜

feat: add basic tools

parent f847d9ce
Pipeline #25460 passed with stages
in 4 minutes 13 seconds
...@@ -71,7 +71,7 @@ export default { ...@@ -71,7 +71,7 @@ export default {
}, },
{ {
title: '通用', title: '通用',
children: ['ImageSelect', 'QuotaSelect', 'TimeRangePicker', 'MqttView'], children: ['BasicTools', 'ImageSelect', 'QuotaSelect', 'TimeRangePicker', 'MqttView'],
}, },
{ {
title: '数据录入', title: '数据录入',
......
# `@wisdom-components/BasicTools`
> TODO: description
## Usage
```
const basicTools = require('@wisdom-components/BasicTools');
// TODO: DEMONSTRATE API
```
{
"name": "@wisdom-components/basictools",
"version": "1.1.0",
"description": "> TODO: description",
"author": "tuqian <webtuqian@163.com>",
"homepage": "",
"license": "ISC",
"main": "lib/index.js",
"directories": {
"lib": "lib",
"test": "__tests__"
},
"files": [
"lib"
],
"publishConfig": {
"registry": "https://g.civnet.cn:4873/"
},
"repository": {
"type": "git",
"url": "https://g.civnet.cn:8443/ReactWeb5/wisdom-components.git"
},
"scripts": {
"test": "echo \"Error: run tests from root\" && exit 1"
},
"dependencies": {
"moment": "^2.27.0"
}
}
---
title: BasicTools - 通用工具库
nav:
title: 组件
path: /components
group:
path: /
---
# BasicTools 通用工具库
## 代码演示
<code src="./demos/Basic.tsx">
## 使用说明
```javascript
import { getTimeRange, getRange } from '@wisdom-components/BasicTools';
let result1 = getTimeRange('lastYear');
let result2 = getTimeRange('nextWeek');
let result3 = getRange(new Date(), 'day');
let result4 = getRange('', 'month');
```
import React from 'react';
import { getTimeRange, getRange } from '../index';
const Demo = () => {
return (
<div>
<h2>getTimeRange(type):</h2>
<div>
<b>全部:</b> {JSON.stringify(getTimeRange())}
</div>
<div>
<b>今年-thisYear:</b> {JSON.stringify(getTimeRange('thisYear'))}
</div>
<div>
<b>去年-lastYear:</b> {JSON.stringify(getTimeRange('lastYear'))}
</div>
<div>
<b>明年-nextYear:</b> {JSON.stringify(getTimeRange('nextYear'))}
</div>
<div>
<b>上季度-lastQuarter:</b> {JSON.stringify(getTimeRange('lastQuarter'))}
</div>
<div>
<b>本季度-thisQuarter:</b> {JSON.stringify(getTimeRange('thisQuarter'))}
</div>
<div>
<b>下季度-nextQuarter:</b> {JSON.stringify(getTimeRange('nextQuarter'))}
</div>
<div>
<b>上月-lastMonth:</b> {JSON.stringify(getTimeRange('lastMonth'))}
</div>
<div>
<b>本月-thisMonth:</b> {JSON.stringify(getTimeRange('thisMonth'))}
</div>
<div>
<b>下月-nextMonth:</b> {JSON.stringify(getTimeRange('nextMonth'))}
</div>
<div>
<b>上周-lastWeek:</b> {JSON.stringify(getTimeRange('lastWeek'))}
</div>
<div>
<b>本周-thisWeek:</b> {JSON.stringify(getTimeRange('thisWeek'))}
</div>
<div>
<b>下周-nextWeek:</b> {JSON.stringify(getTimeRange('nextWeek'))}
</div>
<div>
<b>昨天-lastDay:</b> {JSON.stringify(getTimeRange('lastDay'))}
</div>
<div>
<b>今天-thisDay:</b> {JSON.stringify(getTimeRange('thisDay'))}
</div>
<div>
<b>明天-nextDay:</b> {JSON.stringify(getTimeRange('nextDay'))}
</div>
<br />
<h2>getRange(date, type):</h2>
<div>
<b>type:</b> {JSON.stringify(getRange())}
</div>
<div>
<b>type-day:</b> {JSON.stringify(getRange(new Date(), 'day'))}
</div>
<div>
<b>type-week:</b> {JSON.stringify(getRange(new Date(), 'week'))}
</div>
<div>
<b>type-month:</b> {JSON.stringify(getRange(new Date(), 'month'))}
</div>
<div>
<b>type-year:</b> {JSON.stringify(getRange(new Date(), 'year'))}
</div>
</div>
);
};
export default Demo;
import moment from 'moment';
// 根据设定查询条件 返回时间范围
const getTimeRange = (type) => {
let start = '',
end = '';
switch (type) {
case 'thisYear': // 今年
start = moment().startOf('year');
end = moment().endOf('year');
break;
case 'lastYear': // 去年
start = moment()
.year(moment().year() - 1)
.startOf('year');
end = moment()
.year(moment().year() - 1)
.endOf('year');
break;
case 'nextYear': // 明年
start = moment()
.year(moment().year() + 1)
.startOf('year');
end = moment()
.year(moment().year() + 1)
.endOf('year');
break;
case 'lastQuarter': // 上季度
start = moment()
.quarter(moment().quarter() - 1)
.startOf('quarter');
end = moment()
.quarter(moment().quarter() - 1)
.endOf('quarter');
break;
case 'thisQuarter': // 本季度
start = moment().startOf('quarter');
end = moment().endOf('quarter');
break;
case 'nextQuarter': // 下季度
start = moment()
.quarter(moment().quarter() + 1)
.startOf('quarter');
end = moment()
.quarter(moment().quarter() + 1)
.endOf('quarter');
break;
case 'lastMonth': // 上月
start = moment()
.month(moment().month() - 1)
.startOf('month');
end = moment()
.month(moment().month() - 1)
.endOf('month');
break;
case 'thisMonth': // 本月
start = moment().startOf('month');
end = moment().endOf('month');
break;
case 'nextMonth': // 下月
start = moment()
.month(moment().month() + 1)
.startOf('month');
end = moment()
.month(moment().month() + 1)
.endOf('month');
break;
case 'lastWeek': // 上周
start = moment().subtract(14, 'days');
end = moment().subtract(7, 'days');
break;
case 'thisWeek': // 本周
start = moment().subtract(6, 'days');
end = moment();
break;
case 'nextWeek': // 下周
start = moment().subtract(-1, 'days');
end = moment().subtract(-7, 'days');
break;
case 'lastDay': // 昨天
start = moment().subtract(1, 'days');
end = moment().subtract(1, 'days');
break;
case 'thisDay': // 今天
start = moment();
end = moment();
break;
case 'nextDay': // 明天
start = moment().subtract(-1, 'days');
end = moment().subtract(-1, 'days');
break;
default:
start = moment();
end = moment();
break;
}
if (!!start && !!end) {
start = start.format(startFormat);
end = end.format(endFormat);
}
return { startTime: start, endTime: end };
};
const getRange = (date, type) => {
let start = '',
end = '';
switch (type) {
case 'day':
start = moment(date);
end = moment(date);
break;
case 'week':
start = moment(date).startOf('week');
end = moment(date).endOf('week');
break;
case 'month':
start = moment(date).startOf('month');
end = moment(date).endOf('month');
break;
case 'year':
start = moment(date).startOf('year');
end = moment(date).endOf('year');
break;
default:
start = moment(date);
end = moment(date);
break;
}
if (!!start && !!end) {
start = start.format(startFormat);
end = end.format(endFormat);
}
return { startTime: start, endTime: end };
};
export { getTimeRange, getRange };
const startFormat = 'YYYY-MM-DD 00:00:00';
const endFormat = 'YYYY-MM-DD 23:59:59';
...@@ -22,5 +22,9 @@ ...@@ -22,5 +22,9 @@
}, },
"scripts": { "scripts": {
"test": "echo \"Error: run tests from root\" && exit 1" "test": "echo \"Error: run tests from root\" && exit 1"
},
"dependencies": {
"classnames": "^2.2.6",
"@wisdom-components/Empty": "^1.2.0"
} }
} }
...@@ -23,5 +23,8 @@ ...@@ -23,5 +23,8 @@
}, },
"scripts": { "scripts": {
"test": "echo \"Error: run tests from root\" && exit 1" "test": "echo \"Error: run tests from root\" && exit 1"
},
"dependencies": {
"classnames": "^2.2.6"
} }
} }
...@@ -22,5 +22,13 @@ ...@@ -22,5 +22,13 @@
}, },
"scripts": { "scripts": {
"test": "echo \"Error: run tests from root\" && exit 1" "test": "echo \"Error: run tests from root\" && exit 1"
},
"dependencies": {
"moment": "^2.27.0",
"classnames": "^2.2.6",
"@wisdom-components/Empty": "^1.2.0",
"@wisdom-components/timerangepicker": "^1.2.0",
"highcharts": "^9.0.1",
"highcharts-react-official": "^3.0.0"
} }
} }
...@@ -22,5 +22,9 @@ ...@@ -22,5 +22,9 @@
}, },
"scripts": { "scripts": {
"test": "echo \"Error: run tests from root\" && exit 1" "test": "echo \"Error: run tests from root\" && exit 1"
},
"dependencies": {
"classnames": "^2.2.6",
"@wisdom-components/Empty": "^1.2.0"
} }
} }
...@@ -22,5 +22,9 @@ ...@@ -22,5 +22,9 @@
}, },
"scripts": { "scripts": {
"test": "echo \"Error: run tests from root\" && exit 1" "test": "echo \"Error: run tests from root\" && exit 1"
},
"dependencies": {
"classnames": "^2.2.6",
"@wisdom-components/Empty": "^1.2.0"
} }
} }
...@@ -22,5 +22,8 @@ ...@@ -22,5 +22,8 @@
}, },
"scripts": { "scripts": {
"test": "echo \"Error: run tests from root\" && exit 1" "test": "echo \"Error: run tests from root\" && exit 1"
},
"dependencies": {
"classnames": "^2.2.6"
} }
} }
...@@ -22,5 +22,9 @@ ...@@ -22,5 +22,9 @@
}, },
"scripts": { "scripts": {
"test": "echo \"Error: run tests from root\" && exit 1" "test": "echo \"Error: run tests from root\" && exit 1"
},
"dependencies": {
"classnames": "^2.2.6",
"@wisdom-components/Empty": "^1.2.0"
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment