UNPKG

mm_os

Version:

这是超级美眉服务端框架,用于快速构建应用程序。

604 lines (564 loc) 14.3 kB
/** * 查询个人记录 * @param {String} clientid 客户端ID * @param {Object} param */ exports.get_log = async function(clientid, param) { var res = await this.push("GetRecordsByPicOrID", clientid, param); return res; } /** * 考勤打卡 * @param {String} clientid 客户端ID * @param {Object} uuid 用户唯一标识 * @param {String} avatar base64头像 * @param {Number} longtime 超时回馈 */ exports.sign_in = async function(clientid, uuid, avatar) { var res = await this.push("PullRecordOnlyPic", clientid, { "personID": uuid, "picinfo": avatar }); if (!res || res.result == "fail") { return "打卡失败!"; } } /** * 下发二维码(没用到) * @param {String} clientid * @param {Object} param * @param {Number} longtime 超时回馈 */ exports.push_qrcode = function(clientid, param) { return this.push("ShowQRCode", clientid, param); } /** * 以图搜人 * @param {String} clientid 客户端ID * @param {Object} avatar 人脸照片 * @param {Number} longtime 超时回馈 */ exports.get_member_pic = async function(clientid, avatar) { var res = await this.push("GetPictureSearch", clientid, { "facesluiceId": clientid, "MaxSimilarity": 80, "MaxNum": 1, "picinfo": avatar }); var user; if (res) { if (res.result == "fail") { } else { var u = res.SearchInfo[0]; var name = u.name || u.persionName; var customId = u.customId.trim(); var phone = (u.telnum || "").trim(); var idcard = (u.idCard || "").trim(); user = { name, customId, phone, idcard } } } return user } /** * 远程开门 * @param {String} clientid 客户端ID * @param {String} uuid 用户唯一标识 * @param {Boolean} pass 是否可通行 true为是 * @param {String} tip 提示 */ exports.unlock = async function(clientid, uuid = '0', pass = false, tip = '没有访问权限') { var res = await this.push("Unlock", clientid, { "uid": uuid, // 0不开门,1开门 "openDoor": pass ? "1" : "0", "showInfo": tip }); if (!res || res.result == "fail") { return "非内部人员!"; } } /** * 远程锁门(没用到) * @param {String} clientid 客户端ID * @param {String} uuid 用户唯一标识 * @param {Boolean} pass 是否可通行 true为是 * @param {String} tip 提示 */ exports.lock = async function(clientid, uuid = '0', pass = false, tip = '没有访问权限') { var res = await this.push("Lock", clientid, { "uid": uuid, "showInfo": tip }); if (!res || res.result == "fail") { return "已锁门!"; } } /** * 删除成员 * @param {String} clientid 客户端ID * @param {String} uuid 用户唯一标识 * @param {Number} longtime 超时回馈 */ exports.delete_member = async function(clientid, uuid) { var res = await this.push("DeletePersons", clientid, { "customId": uuid }); return } /** * 批量删除成员 * @param {String} clientid 客户端ID * @param {Object} arr 成员列表 * @param {Number} longtime 超时回馈 */ exports.delete_member_batch = async function(clientid, arr, longtime) { var res = await this.push("DeletePersons", clientid, { DataBegin: "BeginFlag", DataEnd: "EndFlag", PersonNum: arr.length.toString(), customId: arr }, longtime); var result; var error = 0; var success = 0; var error_info = []; var success_info = []; if (res) { if (res.result == 'ok') { var now = new Date().toStr("yyyy-MM-dd hh:mm:ss"); error = Number(res.DelErrNum); success = Number(res.DelSucNum); error_info = res.DelErrInfo.map((o) => { o.time_create = now; return o }); success_info = res.DelSucInfo.map((o) => { o.time_create = now; return o }); result = res.result; } else if (res.detail && res.detail.indexOf('busy') === -1) { result = 'fail'; } } return { error, success, error_info, success_info, result }; return } /** * 重启设备 * @param {String} clientid 设备sn */ exports.reboot_device = async function(clientid) { var res = await this.push("RebootDevice", clientid, {}); if (!res || res.result == "ok") { return } return "重启设备失败" } /** * 设置设备时间 * @param {String} clientid 设备sn * @param {String} datetime 日期时间 */ exports.set_device_time = async function(clientid, datetime) { var res = await this.push("SetSysTime", clientid, { "SysTime": datetime.replace(" ", "T") }); if (!res || res.result == "ok") { return } return "重置时间失败" } /** * 恢复出厂设置 * @param {String} clientid 设备sn * @param {Boolean} record 是否恢复控制记录,true为恢复 * @param {Boolean} member 是否恢复成员,true为恢复 * @param {Boolean} log 是否恢复请求日志,true为恢复 */ exports.reset_device = async function(clientid, record = true, member = false, log = true) { var res = await this.push("SetFactoryDefault", clientid, { DefaltRecord: record ? "1" : "0", DefaltPerson: member ? "1" : "0", DefaltLog: log ? "1" : "0" }); if (!res || res.result == "ok") { return } return "恢复出厂设置失败" } /** * 查询下发进度 * @param {String} clientid 设备sn * @param {String} mode 查询方式 默认为更新 */ exports.get_progress = async function(clientid, mode = 'update') { var type = ""; if (mode == 'update') { type = "EditPersonsNew"; } else { type = "DeletePersons"; } var res = await this.push("QueryProgress", clientid, { "QueryType": type }); if (res) { if (res.Status !== "0") { return 1; } else { return 0; } } return -1; } /** * 考勤补卡 * @param {String} clientid 设备sn * @param {Object} param 其他信息 * @param {Number} longtime 超时回馈 */ exports.replacement = async function(clientid, uuid, time, avatar) { var res = await this.push("PullRecordOnlyPic", clientid, { "personID": uuid, "time": time, "picinfo": avatar }); if (!res || res.result == "fail") { return "补卡失败!"; } return } /** * 查询人员名单 * @param {String} clientid 客户端ID * @param {Number} longtime 超时回馈 */ exports.get_member = function(clientid, longtime = 0) { return this.push("QueryPerson", clientid, longtime); } /** * 查询人员名单 * @param {String} clientid 客户端ID * @param {String} uuid 用户唯一标识 * @param {Number} longtime 超时回馈 */ exports.get_member_one = function(clientid, uuid, longtime = 0) { return this.push("QueryPerson", clientid, { "personId": uuid }, longtime); } /** * 成员模型 * @param {Object} m 原始的成员 */ exports.member_model_out = function(m) { var nfc = m.nfc || ''; var obj = { "customId": m.customId, "name": m.name, "native": m.place || '', "nation": m.nation || '', "address": m.address || '', "idCard": m.idcard || '', "gender": m.sex - 1, "phone": m.phone, "birthday": m.birthday, "cardType2": 2, "cardNum2": nfc, "RFCardMode": 0, "RFIDCard": nfc, "notes": "", "personType": m.state == 4 ? 1 : 0, "cardType": 0, "picURI": this.fullUrl(m.avatar) } if (m.time_valid_end && m.time_valid_end.toTime() > "2021-01-01 00:00:00".toTime() && m.time_valid_end .toTime() <= "2038-01-01 00:00:00".toTime()) { if (typeof(m.time_valid_start) == "object") { obj.cardValidBegin = m.time_valid_start.toStr("yyyy-MM-dd hh:mm:ss"); } else { obj.cardValidBegin = m.time_valid_start.toTime().toStr("yyyy-MM-dd hh:mm:ss"); } if (typeof(m.time_valid_end) == "object") { obj.cardValidEnd = m.time_valid_end.toStr("yyyy-MM-dd hh:mm:ss"); } else { obj.cardValidEnd = m.time_valid_end.toTime().toStr("yyyy-MM-dd hh:mm:ss"); } obj.tempCardType = 1; } else { obj.tempCardType = 0; } return obj } /** * 批量下发人员名单 * @param {String} clientid 客户端ID * @param {Array} list 人员名单 * @param {Number} longtime 超时回馈 */ exports.update_member_batch = async function(clientid, list, longtime = 180) { var res = await this.pushS(clientid, { "messageId": this.get_msgid('EditPersonsNew'), "DataBegin": "BeginFlag", "DataEnd": "EndFlag", "operator": "EditPersonsNew", "PersonNum": list.length, "info": list.map((o) => { return this.member_model_out(o); }) }, longtime); var result; var error = 0; var success = 0; var error_info = []; var success_info = []; if (res) { if (res.result == 'ok') { var now = new Date().toStr("yyyy-MM-dd hh:mm:ss"); error = Number(res.AddErrNum); success = Number(res.AddSucNum); error_info = res.AddErrInfo.map((o) => { o.time_create = now; return o }); success_info = res.AddSucInfo.map((o) => { o.time_create = now; return o }); result = res.result; } else if (res.detail && res.detail.indexOf('busy') === -1) { result = 'fail'; } } return { error, success, error_info, success_info, result }; } /** * 下发人员名单 * @param {String} clientid 设备ID * @param {Object} user 人员信息 * @param {Number} longtime 超时回馈 */ exports.update_member = function(clientid, user, longtime = 0) { return this.pushS(clientid, { "messageId": this.get_msgid('EditPerson'), "DataBegin": "BeginFlag", "DataEnd": "EndFlag", "method": "EditPerson", "info": this.member_model_out(user) }, longtime); } /** * 初始化函数, 用于定义开放给前端的函数 */ exports.init = function() { var m = this.methods; var _this = this; m["HeartBeat"] = function(clientid, params) { if (!_this.drives[clientid]) { _this.drives[clientid] = {}; } _this.drives[clientid].online = 1; _this.drives[clientid].time_last = new Date().getTime(); // console.log("心跳", clientid, _this.drives[clientid]); } /** * 离线通知 * @param {String} clientid 设备sn * @param {Object} param 其他信息 */ m["Offline"] = async function(clientid, params) { _this.update_online(clientid, 0); // console.log("离线", clientid, _this.drives[clientid]); }; /** * 在线通知 * @param {String} clientid 设备sn * @param {Object} param 其他信息 */ m["Online"] = async function(clientid, params) { await _this.update_online(clientid, 1, params.ip); return { "facesluiceId": clientid, "result": "ok", "detail": "" }; console.log("上线", clientid, _this.drives[clientid]); }; /** * 更新人体生命体征,如温度、湿度、位置、 * @param {String} clientid 设备sn * @param {Object} param 其他信息 */ m["life"] = async function(clientid, params) { _this.update_life(clientid, { // 心率 heart_rate: 0, // 睡眠状态 是深度睡眠还是浅睡眠 sleep: 0, // 血氧饱和度 oxygen: 0, // 行走步数 step: 0, // 压力 pressure: 0, // 时间戳 timestamp: 0, // GPS横坐标,保留生命体脱下设备时的最后位置 gps_x: 0, // GPS纵坐标,保留生命体脱下设备时的最后位置 gps_y: 0 }); // console.log("离线", clientid, _this.drives[clientid]); }; /** * 更新设备信息,如温度、湿度、位置 * @param {String} clientid 设备sn * @param {Object} param 其他信息 */ m["info"] = async function(clientid, params) { _this.update_device_info(clientid, { // 温度 temperature: 0, // 湿度 humidity: 0, // 氧气 oxygen: 0, // 二氧化碳 carbon_dioxide: 0, // 光照强度 light: 0, // 烟雾浓度 smoke: 0, // 压力 pressure: 0,, // 光敏 photosensitive: 0, // 声波 acoustic_wave: 0, // 磁力 magnetism: 0, // 火焰 flame: 0, // 震动 vibrate: 0, // 时间戳 timestamp: 0, // GPS横坐标 gps_x: 0, // GPS纵坐标 gps_y: 0 }); // console.log("离线", clientid, _this.drives[clientid]); }; /** * 上报 * @param {String} clientid 设备sn * @param {Object} param 其他信息 */ m["RecPush"] = async function(clientid, params, msg, id) { _this.send(`mqtt/face/${clientid}`, { "messageId": _this.get_msgid('PushAck'), "operator": "PushAck", "info": { "PushAckType": "2", "SnapOrRecordID": params.RecordID } }); var type = 1; switch (params.otype) { // (1刷脸验证|2远程开门|3智码开门|4刷卡验证) case "7": case "27": type = 2; break; case "47": case "48": case "55": case "56": case "57": type = 3; break; case "21": case "22": case "24": case "25": type = 4; break; default: break; } if (type == 2) { return; } // 1白名单|2黑名单 var person_type = params.PersonType == "1" ? 2 : 1; var record_type = params.RecordType ? Number(params.RecordType) + 1 : 1; // 拿到打卡人的姓名 var name = params.persionName || params.name; if (!name) { return; } var customId = params.customId.trim(); var phone = (params.telnum || "").trim(); var idcard = (params.idCard || "").trim(); var nfc = (params.cardNum2 || params.RFIDCard || "").trim(); if (nfc == "0") { ncf = "" } var time = params.time; var avatar = ""; if (params.pic) { avatar = params.pic; } if (typeof(time) == "object") { time = time.toStr("yyyy-MM-dd hh:mm:ss"); } else { time = time.toTime().toStr("yyyy-MM-dd hh:mm:ss"); } var log = { customId, name, phone, idcard, nfc, clientid, type, person_type, record_type, action: "刷脸验证", time, avatar } $.server.exec_log(clientid, log); } m["QRCodePush"] = async function(clientid, params, msg, id) { var qrcode = params.QRCodeInfo; var { uuid, pass, tip, device } = await $.server.exec_qrcode(clientid, qrcode); if (device && device.online) { await _this.unlock(clientid, uuid, pass, tip); } } };