接口开发2

parent 8eb38c38
const path = require('path');
import path from "path";
import * as fs from "fs";
import { BizError } from "../util/bizError";
import { analysisXml } from "../util/myXML";
......@@ -16,7 +16,7 @@ export async function initConfig() {
if (!configInfo || !configInfo.config) throw new BizError('xml中无配置');
let { port, sign, img, mysqldb } = configInfo.config;
let { port, sign, img, mysqldb, feiyi, diqin } = configInfo.config;
// 基本配置
systemConfig.port = parseInt(port[0]);
......@@ -37,6 +37,24 @@ export async function initConfig() {
}
}
// 飞奕云平台配置
if (feiyi) {
let feiyiConfig = feiyi[0];
systemConfig.feiyi = { appKey: '', secretKey: '', baseUrl: '' };
if (feiyiConfig.feiyiAppKey) systemConfig.feiyi.appKey = feiyiConfig.feiyiAppKey[0];
if (feiyiConfig.feiyiSecretKey) systemConfig.feiyi.secretKey = feiyiConfig.feiyiSecretKey[0];
if (feiyiConfig.feiyiBaseUrl) systemConfig.feiyi.baseUrl = feiyiConfig.feiyiBaseUrl[0];
}
// 迪勤云平台配置
if (diqin) {
let diqinConfig = diqin[0];
systemConfig.diqin = { appKey: '', secretKey: '', baseUrl: '' };
if (diqinConfig.diqinAppKey) systemConfig.diqin.appKey = diqinConfig.diqinAppKey[0];
if (diqinConfig.diqinSecretKey) systemConfig.diqin.secretKey = diqinConfig.diqinSecretKey[0];
if (diqinConfig.diqinBaseUrl) systemConfig.diqin.baseUrl = diqinConfig.diqinBaseUrl[0];
}
} catch(err) {
console.log('ERROR => 服务器配置解析错误 请检查根目录下 serverConfig.xml 文件是否正确');
console.log(err);
......
......@@ -15,4 +15,16 @@ export class ServerConfig {
pwd:string,
dataBase:string,
}
/** 飞奕云平台配置 */
feiyi: {
appKey: string;
secretKey: string;
baseUrl: string;
}
/** 迪勤云平台配置 */
diqin: {
appKey: string;
secretKey: string;
baseUrl: string;
}
}
\ No newline at end of file
......@@ -6,20 +6,106 @@ import * as diqinClientBiz from '../biz/diqinClient';
import { eccReqParamater } from '../util/verificationParam';
export function setRouter(httpServer) {
/** 获取环境监测数据 */
/** 获取环境设备数据(调试入口,综合接口) */
httpServer.post('/api/dq/diqin/getProcessDevices', asyncHandler(getProcessDevices));
/** 场所列表 */
httpServer.post('/api/dq/diqin/location/list', asyncHandler(getLocationList));
/** 场所明细 */
httpServer.post('/api/dq/diqin/location/detail', asyncHandler(getLocationDetail));
/** 监测点明细 */
httpServer.post('/api/dq/diqin/station/detail', asyncHandler(getStationDetail));
/** 设备列表 */
httpServer.post('/api/dq/diqin/dataSource/list', asyncHandler(getDataSourceList));
/** 设备明细 */
httpServer.post('/api/dq/diqin/dataSource/detail', asyncHandler(getDataSourceDetail));
/** 历史数据 */
httpServer.post('/api/dq/diqin/reading/list', asyncHandler(getReadingList));
/** 图表数据 */
httpServer.post('/api/dq/diqin/graph/data', asyncHandler(getGraphData));
}
/**
* 获取环境监测数据
* 获取环境设备数据(调试入口,综合接口)
* @param page 当前页码,默认 1
* @param limit 每页条数,默认 10
* @param limit 每页条数,默认 10
*/
async function getProcessDevices(req, res) {
let reqConf = {page:'Number', limit:'Number', roomId: 'String', indoorUnitAddressFull: 'String'};
const NotMustHaveKeys = [ "page", "limit", "roomId", "indoorUnitAddressFull" ];
let reqConf = { page: 'Number', limit: 'Number' };
const NotMustHaveKeys = ["page", "limit"];
let { page, limit } = eccReqParamater(reqConf, req.body, NotMustHaveKeys);
const result = await diqinClientBiz.processDeviceData(page, limit);
res.success(result);
}
/**
* 场所列表
*/
async function getLocationList(req, res) {
const result = await diqinClientBiz.getLocations();
res.success(result);
}
/**
* 场所明细
* @param locationId 场所 ID
*/
async function getLocationDetail(req, res) {
let reqConf = { locationId: 'String' };
let { locationId } = eccReqParamater(reqConf, req.body, []);
const result = await diqinClientBiz.getLocationDetail(locationId);
res.success(result);
}
/**
* 监测点明细
* @param stationId 监测点 ID
*/
async function getStationDetail(req, res) {
let reqConf = { stationId: 'String' };
let { stationId } = eccReqParamater(reqConf, req.body, []);
const result = await diqinClientBiz.getStationDetail(stationId);
res.success(result);
}
/**
* 设备列表
*/
async function getDataSourceList(req, res) {
const result = await diqinClientBiz.getDataSources();
res.success(result);
}
/**
* 设备明细
* @param dataSourceId 设备 ID
*/
async function getDataSourceDetail(req, res) {
let reqConf = { dataSourceId: 'String' };
let { dataSourceId } = eccReqParamater(reqConf, req.body, []);
const result = await diqinClientBiz.getDataSourceDetail(dataSourceId);
res.success(result);
}
/**
* 历史数据
* @param params 查询参数(m, id, type, start, end 等)
*/
async function getReadingList(req, res) {
let reqConf = { m: 'String', id: 'String' };
const NotMustHaveKeys = ["m", "id", "type", "start", "end"];
let params = eccReqParamater(reqConf, req.body, NotMustHaveKeys);
const result = await diqinClientBiz.processDeviceData(params);
const result = await diqinClientBiz.getReadings(params);
res.success(result);
}
\ No newline at end of file
}
/**
* 图表数据
* @param params 查询参数(m, id, indicator, start, end 等)
*/
async function getGraphData(req, res) {
let reqConf = { m: 'String', id: 'String' };
const NotMustHaveKeys = ["m", "id", "indicator", "start", "end"];
let params = eccReqParamater(reqConf, req.body, NotMustHaveKeys);
const result = await diqinClientBiz.getGraphs(params);
res.success(result);
}
......@@ -38,8 +38,9 @@ export function setRouter(httpServer) {
* 运行分析弹窗
*/
async function getAnalysisPopup(req, res) {
const { } = req.body;
const result = await runningBiz.getAnalysisPopup();
let reqConf = { regionKey: 'String' };
let { regionKey } = eccReqParamater(reqConf, req.body);
const result = await runningBiz.getAnalysisPopup(regionKey);
res.success(result);
}
......@@ -65,8 +66,9 @@ async function getRunMonitoring(req, res) {
* 智能监控弹窗
*/
async function getMonitorPopup(req, res) {
const { } = req.body;
const result = await runningBiz.getMonitorPopup();
let reqConf = { regionKey: 'String' };
let { regionKey } = eccReqParamater(reqConf, req.body);
const result = await runningBiz.getMonitorPopup(regionKey);
res.success(result);
}
......
/**
* 日期时间工具函数
* 统一项目中所有日期格式化、时间范围计算逻辑
*/
/** 格式化为本地时间:yyyy-MM-dd HH:mm:ss */
export function formatTime(date: Date | string): string {
if (!date) return '';
const d = new Date(date);
const y = d.getFullYear();
const mo = String(d.getMonth() + 1).padStart(2, '0');
const day = String(d.getDate()).padStart(2, '0');
const h = String(d.getHours()).padStart(2, '0');
const mi = String(d.getMinutes()).padStart(2, '0');
const s = String(d.getSeconds()).padStart(2, '0');
return `${y}-${mo}-${day} ${h}:${mi}:${s}`;
}
/** 获取今天 00:00:00 */
export function getTodayStart(): string {
const now = new Date();
now.setHours(0, 0, 0, 0);
return formatTime(now);
}
/** 获取今天 23:59:59 */
export function getTodayEnd(): string {
const now = new Date();
now.setHours(23, 59, 59, 999);
return formatTime(now);
}
/** 获取昨天 00:00:00 */
export function getYesterdayStart(): string {
const now = new Date();
now.setDate(now.getDate() - 1);
now.setHours(0, 0, 0, 0);
return formatTime(now);
}
/** 获取昨天 23:59:59 */
export function getYesterdayEnd(): string {
const now = new Date();
now.setDate(now.getDate() - 1);
now.setHours(23, 59, 59, 999);
return formatTime(now);
}
/** 获取本月 1 号 00:00:00 */
export function getMonthStart(): string {
const now = new Date();
now.setDate(1);
now.setHours(0, 0, 0, 0);
return formatTime(now);
}
/** 获取上月 1 号 00:00:00 */
export function getLastMonthStart(): string {
const now = new Date();
now.setMonth(now.getMonth() - 1, 1);
now.setHours(0, 0, 0, 0);
return formatTime(now);
}
/** 获取上月最后一天 23:59:59 */
export function getLastMonthEnd(): string {
const now = new Date();
now.setDate(1);
now.setHours(0, 0, 0, 0);
now.setMilliseconds(now.getMilliseconds() - 1);
return formatTime(now);
}
/** 获取 N 小时前的时间字符串 */
export function getHoursAgo(n: number): string {
const now = new Date();
now.setHours(now.getHours() - n);
return formatTime(now);
}
/** 获取 N 天前的 start-of-day */
export function getDaysAgoStart(n: number): string {
const now = new Date();
now.setDate(now.getDate() - n);
now.setHours(0, 0, 0, 0);
return formatTime(now);
}
/** 获取 N 个月前的首日 00:00:00 */
export function getMonthsAgoStart(n: number): string {
const now = new Date();
now.setMonth(now.getMonth() - n, 1);
now.setHours(0, 0, 0, 0);
return formatTime(now);
}
/** 计算百分比同比/环比 */
export function calcCompareRate(current: number, previous: number): string {
if (previous === 0) return current > 0 ? '100' : '0';
return ((current - previous) / previous * 100).toFixed(2);
}
// ==================== 时间分组 key 格式化 ====================
export function formatHourKey(date: Date): string {
const y = date.getFullYear();
const mo = String(date.getMonth() + 1).padStart(2, '0');
const d = String(date.getDate()).padStart(2, '0');
const h = String(date.getHours()).padStart(2, '0');
return `${y}-${mo}-${d} ${h}时`;
}
export function formatDayKey(date: Date): string {
const y = date.getFullYear();
const mo = String(date.getMonth() + 1).padStart(2, '0');
const d = String(date.getDate()).padStart(2, '0');
return `${y}-${mo}-${d}`;
}
export function formatMonthKey(date: Date): string {
const y = date.getFullYear();
const mo = String(date.getMonth() + 1).padStart(2, '0');
return `${y}-${mo}`;
}
......@@ -13,7 +13,7 @@ import { BizError } from './bizError';
* @returns
*/
export function get(url:string, query?, headers?) {
if (!url || (url.search(/http:/) && url.search(/https:/)) ) throw new BizError(!url ? "请求地址为空" : "请求地址错误");
if (!url || !(url.startsWith('http://') || url.startsWith('https://'))) throw new BizError(!url ? "请求地址为空" : "请求地址错误");
return new Promise((resolve, reject)=>{
let paramater:any = { url, json:true };
if (query) paramater.qs = query;
......@@ -42,7 +42,7 @@ export function post(url, body, headers) {
resolve(body);
}
else {
// reject(error)
reject(error || new Error(`HTTP ${response?.statusCode || 'error'}`));
}
});
})
......
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