对接飞奕API、用户注册和登录

parent 316d5da8
...@@ -11,20 +11,22 @@ import { controlAcByEnvironment } from "./running"; ...@@ -11,20 +11,22 @@ import { controlAcByEnvironment } from "./running";
/** /**
* 注册或更新设备信息 * 注册或更新设备信息
* @param deviceId 设备唯一标识(必填) * @param deviceId 设备唯一标识(必填)
* @param regionKey 区域编号(必填),对应 region 表的 id * @param roomId 房间编号(必填),对应 region 表的 room_id
* @param deviceType 设备类型(必填),如“灯光”“空调” * @param deviceType 设备类型(必填),如“灯光”“空调”
* @param deviceName 设备名称(可选),如“主照明灯” * @param deviceName 设备名称(可选),如“主照明灯”
* @param controlParams 设备支持的参数定义(可选),JSON对象 * @param controlParams 设备支持的参数定义(可选),JSON对象
* @returns {Promise<{success: boolean}>} * @returns {Promise<{success: boolean}>}
* @throws 如果 regionKey 对应的区域不存在,则抛出错误 * @throws 如果 regionKey 对应的区域不存在,则抛出错误
*/ */
export async function registerOrUpdateDevice( deviceId: string, regionKey: number, deviceType: string, deviceName?: string | null, controlParams?: any ) { export async function registerOrUpdateDevice( deviceId: string, roomId: string, deviceType: string, deviceName?: string | null, controlParams?: any ) {
// 校验区域是否存在 // 校验区域是否存在
let region = await selectOneDataByParam(TABLENAME.区域表, { id: regionKey }); let region = await selectOneDataByParam(TABLENAME.区域表, { room_id: roomId });
if (!region.data) { if (!region.data) {
throw new BizError(ERRORENUM.参数错误, `区域编号 ${regionKey} 不存在`); throw new BizError(ERRORENUM.参数错误, `区域编号 ${roomId} 不存在`);
} }
let regionKey = region.data.id;
let exist = await selectOneDataByParam(TABLENAME.设备表, { device_id: deviceId }); let exist = await selectOneDataByParam(TABLENAME.设备表, { device_id: deviceId });
let now = new Date().toISOString().slice(0, 19).replace('T', ' '); let now = new Date().toISOString().slice(0, 19).replace('T', ' ');
......
...@@ -2,7 +2,7 @@ import { selectDataListByParam } from "../data/findData"; ...@@ -2,7 +2,7 @@ import { selectDataListByParam } from "../data/findData";
import { TABLENAME } from "../config/dbEnum"; import { TABLENAME } from "../config/dbEnum";
/** /**
* 获取所有区域列表(供前端下拉选择) * 获取楼栋区域列表(供前端下拉选择)
* @returns 区域列表,按 sort_order 排序 * @returns 区域列表,按 sort_order 排序
*/ */
export async function getRegionList( regionGroup: string ) { export async function getRegionList( regionGroup: string ) {
...@@ -35,3 +35,38 @@ export async function getRegionList( regionGroup: string ) { ...@@ -35,3 +35,38 @@ export async function getRegionList( regionGroup: string ) {
list: regionMap list: regionMap
}; };
} }
/**
* 获取所有区域列表(供前端下拉选择)
* @returns 区域列表,按 sort_order 排序
*/
export async function getRegionAllList( regionGroup: string ) {
let where: any = { "%orderAsc%": "sort_order" };
if (regionGroup) {
where.groups = regionGroup;
}
let regionList = await selectDataListByParam(
TABLENAME.区域表,
where,
["id", "room_id", "name", "type", "groups"],
);
let regionGroupTypeArr: any = [];
let regionMap: any = {};
regionList.data.forEach((r: any) => {
let regionArr = [];
if (regionMap[r.groups]) {
regionArr = regionMap[r.groups];
}
if (!regionGroupTypeArr.includes(r.groups + "-" + r.type)) {
regionGroupTypeArr.push(r.groups + "-" + r.type);
regionArr.push({regionKey: r.id, roomId: r.room_id, regionName: r.name, regionType: r.type, regionGroup: r.groups});
regionMap[r.groups] = regionArr;
}
});
console.log("区域列表:", regionMap);
return {
list: regionMap
};
}
\ No newline at end of file
...@@ -32,26 +32,26 @@ export async function registerUser(loginId:string, pwd:string, name:string) { ...@@ -32,26 +32,26 @@ export async function registerUser(loginId:string, pwd:string, name:string) {
* @returns * @returns
*/ */
export async function adminLogin(loginId:string, pwd:string) { export async function adminLogin(loginId:string, pwd:string) {
let filesList = ["name", "id", "pwd", "pwd"]; let filesList = ["name", "id", "loginId", "pwd"];
let adminUserInfo : any = await selectOneDataByParam(TABLENAME.用户信息表, {loginId}, filesList); let adminUserInfo : any = await selectOneDataByParam(TABLENAME.用户信息表, {loginId}, filesList);
console.log(adminUserInfo.pwd); console.log(adminUserInfo.data);
if (!adminUserInfo || !adminUserInfo.id) { if (!adminUserInfo.data || !adminUserInfo.data.id) {
throw new BizError(ERRORENUM.账号或密码错误); throw new BizError(ERRORENUM.账号或密码错误);
} }
const encryptedPwd = getPwdMd5(adminUserInfo.id, pwd); const encryptedPwd = getPwdMd5(adminUserInfo.data.loginId, pwd);
if (adminUserInfo.pwd != encryptedPwd) { if (adminUserInfo.data.pwd != encryptedPwd) {
throw new BizError(ERRORENUM.账号或密码错误); throw new BizError(ERRORENUM.账号或密码错误);
} }
let updateUserInfo = { let updateUserInfo = {
token : getToken(adminUserInfo.loginId), token : getToken(adminUserInfo.data.loginId),
tokenMs : getMySqlMs() tokenMs : getMySqlMs()
}; };
await updateManyData(TABLENAME.用户信息表, updateUserInfo, {id:adminUserInfo.id}); await updateManyData(TABLENAME.用户信息表, updateUserInfo, {id:adminUserInfo.data.id});
let userInfo = { let userInfo = {
userId:adminUserInfo.id, userId:adminUserInfo.data.id,
userName:adminUserInfo.name, userName:adminUserInfo.data.name,
token:updateUserInfo.token, token:updateUserInfo.token,
}; };
......
...@@ -8,7 +8,6 @@ import * as regionBiz from '../biz/region'; ...@@ -8,7 +8,6 @@ import * as regionBiz from '../biz/region';
import * as runningBiz from '../biz/running'; import * as runningBiz from '../biz/running';
import { eccReqParamater } from '../util/verificationParam'; import { eccReqParamater } from '../util/verificationParam';
import { checkUser } from '../middleware/user'; import { checkUser } from '../middleware/user';
import { worker } from 'cluster';
export function setRouter(httpServer) { export function setRouter(httpServer) {
/** 注册/更新设备信息 */ /** 注册/更新设备信息 */
...@@ -40,10 +39,10 @@ export function setRouter(httpServer) { ...@@ -40,10 +39,10 @@ export function setRouter(httpServer) {
* 注册或更新设备信息 * 注册或更新设备信息
*/ */
async function registerDevice(req, res) { async function registerDevice(req, res) {
let reqConf = {deviceId:'String', regionKey:'Number', deviceType:'String', deviceName:'String', controlParams:'Object'}; let reqConf = {deviceId:'String', roomId:'String', deviceType:'String', deviceName:'String', controlParams:'Object'};
const NotMustHaveKeys = []; const NotMustHaveKeys = [];
let { deviceId, regionKey, deviceType, deviceName, controlParams } = eccReqParamater(reqConf, req.body, NotMustHaveKeys); let { deviceId, roomId, deviceType, deviceName, controlParams } = eccReqParamater(reqConf, req.body, NotMustHaveKeys);
const result = await deviceBiz.registerOrUpdateDevice(deviceId, regionKey, deviceType, deviceName, controlParams); const result = await deviceBiz.registerOrUpdateDevice(deviceId, roomId, deviceType, deviceName, controlParams);
res.success(result); res.success(result);
} }
...@@ -80,7 +79,7 @@ async function getRegionList(req, res) { ...@@ -80,7 +79,7 @@ async function getRegionList(req, res) {
let reqConf = {regionGroup:'String'}; let reqConf = {regionGroup:'String'};
const NotMustHaveKeys = []; const NotMustHaveKeys = [];
let { regionGroup } = eccReqParamater(reqConf, req.body, NotMustHaveKeys); let { regionGroup } = eccReqParamater(reqConf, req.body, NotMustHaveKeys);
const result = await regionBiz.getRegionList(regionGroup); const result = await regionBiz.getRegionAllList(regionGroup);
res.success(result); res.success(result);
} }
......
...@@ -2,10 +2,12 @@ ...@@ -2,10 +2,12 @@
* 总路由入口 * 总路由入口
*/ */
import * as usersRouter from './users';
import * as deviceRouter from './device'; import * as deviceRouter from './device';
import * as feiyiClientRouter from './feiyiClient'; import * as feiyiClientRouter from './feiyiClient';
export function setRouter(httpServer) { export function setRouter(httpServer) {
usersRouter.setRouter(httpServer);
deviceRouter.setRouter(httpServer); deviceRouter.setRouter(httpServer);
feiyiClientRouter.setRouter(httpServer); feiyiClientRouter.setRouter(httpServer);
} }
......
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