'use strict'; const Service = require('../service/baseService'); const request = require('request'); const fs = require('fs'); const path = require('path'); const sm4 = require('../util/sm4').sma4; class VisitService extends Service { tag() { return this.ctx.model.VisitModel; } // baidu-base64编码 async getResult(data) { if (data.photoPath && data.visitPhotoPath && data.accessToken) { // 读取待识别图像并base64编码 const infoPicName = path.basename('' + data.photoPath);// 该用户的采集图片的文件名 xxx.jpg const visitPicName = path.basename('' + data.visitPhotoPath);// 该用户的探访图片的文件名 xxx.jpg // const infoPicPath = this.app.config.defaultUploadPath + infoPicName; // 采集文件路径; const infoPicPath = infoPicName; // 采集文件路径; // const visitPicPath = this.app.config.defaultUploadPath + visitPicName; // 探访文件路径 const visitPicPath = visitPicName; // 探访文件路径 const infoData = fs.readFileSync(infoPicPath); // 例:fileUrl="D:\\test\\test.bmp" const visitData = fs.readFileSync(visitPicPath); // 例:fileUrl="D:\\test\\test.bmp" const infoBase64 = infoData.toString('base64'); const visitBase64 = visitData.toString('base64'); const jsonData = [ { image: infoBase64, image_type: 'BASE64', }, { image: visitBase64, image_type: 'BASE64', }, ]; const url = 'https://aip.baidubce.com/rest/2.0/face/v3/match?access_token=' + data.accessToken; // 正式 const res = await this.ctx.curl(url, { method: 'POST', headers: { accept: 'application/json', 'Content-Type': 'application/x-www-form-urlencoded' }, contentType: 'json', data: JSON.stringify(jsonData), dataType: 'json', }); this.ctx.logger.info('对比结果==========>', res); if (res.status == '200') { return res.data; } return ''; } } // baidu-url async getResult2(data) { // this.ctx.logger.info('Service==data=======================>' + JSON.stringify(data)); if (data.photoPath && data.visitPhotoPath && data.accessToken) { const infoPicUrl = this.app.config.defaultUrlDoFace + data.photoPath.replaceAll('/', '$'); // 采集文件路径; const visitPicUrl = this.app.config.defaultUrlDoFace + data.visitPhotoPath.replaceAll('/', '$'); // 采集文件路径; const jsonData = [ { // image: 'https://bucketnj1-1254259530.cos.ap-nanjing.myqcloud.com/face/tmp_0c7c2dcde7374488f97228d2134d48950d150d9f6d5d492c.jpg', image: infoPicUrl, image_type: 'URL', liveness_control: 'NORMAL', }, { // image: 'https://bucketnj1-1254259530.cos.ap-nanjing.myqcloud.com/face/tmp_0c7c2dcde7374488f97228d2134d48950d150d9f6d5d492c.jpg', image: visitPicUrl, image_type: 'URL', liveness_control: 'NORMAL', }, ]; const options1 = { url: 'https://aip.baidubce.com/rest/2.0/face/v3/match?access_token=' + data.accessToken, method: 'POST', body: JSON.stringify(jsonData), headers: { accept: 'application/json', 'content-type': 'application/x-www-form-urlencoded', }, }; const result = await this.getFace(options1); return JSON.parse(result); } } async getPeopleFace(data) { // this.ctx.logger.info('Service==data=======================>' + JSON.stringify(data)); if (data.photoPath && data.accessToken) { const photoPath = this.app.config.defaultUrl + data.photoPath; // 采集文件路径; const jsonData = { // image: 'https://bucketnj1-1254259530.cos.ap-nanjing.myqcloud.com/face/tmp_0c7c2dcde7374488f97228d2134d48950d150d9f6d5d492c.jpg', image: photoPath, image_type: 'URL', // liveness_control: 'HIGH', liveness_control: 'NORMAL', // liveness_control: 'LOW', }; const options1 = { url: 'https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token=' + data.accessToken, method: 'POST', body: JSON.stringify(jsonData), headers: { accept: 'application/json', 'content-type': 'application/x-www-form-urlencoded', }, }; const result = await this.getFace(options1); return JSON.parse(result); } } async getPeopleFaceByBase64(data) { if (data.photoBase64 && data.accessToken) { // const photoBase64 = this.app.config.defaultUrl + data.photoPath; // 采集文件路径; // const infoPicUrl = this.app.config.defaultUrlDoFace + data.photoPath.replaceAll('/', '$'); // 采集文件路径; const jsonData = { image: data.photoBase64, image_type: 'BASE64', liveness_control: 'NORMAL', }; const options1 = { url: 'https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token=' + data.accessToken, method: 'POST', body: JSON.stringify(jsonData), headers: { accept: 'application/json', 'content-type': 'application/x-www-form-urlencoded', }, }; const result = await this.getFace(options1); return JSON.parse(result); } } async getFace(option) { return new Promise((resolve, reject) => { request(option, function(error, response, body) { if (!error) { if (response.statusCode == 200) { resolve(body); } else { resolve(body); } } else { reject(error); } }); }); } async bing(query) { const { ctx, service } = this; const { fid, openId } = query; const result = await service.familyService.oneData({ _id: this.app.mongoose.Types.ObjectId(fid) }); // ctx.logger.error('service==========', result); if (result) { let openIdArray = []; if (result.openId) { openIdArray = result.openId; // openId已绑定 if (openIdArray.indexOf(openId) == -1) { openIdArray.push(openId); } else { ctx.error('openId已绑定'); } } else { openIdArray.push(openId); } await service.familyService.update(result._id, { openId: openIdArray }); return null; } return '该户不存在,请联系管理员!'; } // 根据身份证查询 async visitByNumber(data) { const { model } = this.ctx; return await model.VisitModel.find(data).sort({ visitTime: -1 }).limit(15); } // 导出探访数据 async visitExportData(data) { const { model } = this.ctx; const where = {}; if (data.dept1) { where.dept1 = data.dept1;// 省 } if (data.dept2) { where.dept2 = data.dept2; // 市 } if (data.dept3) { where.dept3 = data.dept3; // 区 } if (data.dept4) { where.dept4 = data.dept4; // 乡 } if (data.dept5) { where.dept5 = data.dept5; // 社区 } const queryName = data.queryName; delete data.queryName; if (queryName) { const userInfo = await this.ctx.model.SysUserModel.find({ loginName: { $regex: queryName } }); if (userInfo.length > 0) { where.userid = userInfo[0]._id; } } // 老人姓名 if (data.oldName) { // where.oldInfo = { $regex: data.oldName }; where.oldInfo = sm4.encrypt_ECB(data.oldName); // TODO 数据加密处理-CH } // 时间 if (data.startTime && data.endTime) { where.visitTime = { $gte: new Date(data.startTime + ' 00:00:00'), $lt: new Date(data.endTime + ' 23:59:59') }; } if (data.eightType && data.eightType != '') { if (data.eightType === '1') { where.$or = [ { infoOldType: '空巢老年人', }, { infoOldType: '重残老人', }, { infoOldType: '独居老年人', }, { infoOldType: '计划生育特殊家庭老人', }, { infoOldType: '重残老年人', }, { infoOldType: '失能', }, { infoOldType: '农村留守老年人', }, { infoOldType: '分散供养特困老年人', }, { infoOldType: '高龄老人', }, { infoOldAbility: '失能', }, ]; } else if (data.eightType === '0') { where.$and = [{ infoOldType: '居家老年人' }, { infoOldType: { $size: 1 } }]; where.$or = [ { infoOldAbility: '完全自理', }, { infoOldAbility: '半失能(部分自理)', }, { infoOldAbility: '半失能(部分自理)', }, ]; } } this.ctx.logger.info('条件', where); const pop = [ { path: 'userid', select: 'loginName', }, ]; const result = await model.VisitModel.find(where, { dept1: 1, dept2: 1, dept3: 1, dept4: 1, dept5: 1, visitTime: 1, visitLocation: 1, oldInfo: 1, oldIdNumber: 1, health: 1, mind: 1, security: 1, hygiene: 1, live: 1, urgency: 1, demand: 1, visitMessage: 1, userid: 1 }).populate(pop); return result; } // pc端首页近七天探访数据统计 async sevendayNum(data, user) { const { model } = this.ctx; const level = user.dept.level; const match = {}; match.time = { $gte: data.startday }; // admin的dept 存在冲突,所以它不需要结合 if (user.role._id != this.app.config.defaultAdminRoleId) { match['dept' + level] = user.dept._id; } const res = await model.VisitModel.aggregate([ { $project: { time: { $dateToString: { format: '%Y-%m-%d', date: '$visitTime' } }, _id: 1, dept1: 1, dept2: 1, dept3: 1, dept4: 1, dept5: 1 } }, { $match: match }, { $group: { _id: '$time', value: { $sum: 1 } } }, { $project: { label: '$_id', value: 1, _id: 0 } }, { $sort: { label: 1 } }, ]); this.ctx.logger.info(data); const morenArr = []; for (let i = 0; i < 7; i++) { const yestday = new Date(); yestday.setTime(yestday.getTime() - 24 * 60 * 60 * 1000 * (6 - i)); let month = yestday.getMonth() + 1; if (month < 10) month = '0' + month; let day = yestday.getDate(); if (day < 10) day = '0' + day; const ysday = yestday.getFullYear() + '-' + month + '-' + day; const vote = {}; vote.label = ysday; vote.value = 0; morenArr.push(vote); } if (res.length > 0) { for (let j = 0; j < morenArr.length; j++) { const mlabel = morenArr[j].label; for (let k = 0; k < res.length; k++) { const clabel = res[k].label; if (mlabel == clabel) { morenArr[j].value = res[k].value; } } } } return morenArr; } async listForPageWithDept(data, pop) { const page = data.page || 1; const rows = Number.parseInt(data.rows) || this.app.config.defaultPageSize; const sort = data.sort; delete data.page; delete data.rows; delete data.sort; const eightType = data.eightType; if (eightType !== '') { if (eightType === '1') { data.$or = [ { infoOldType: '空巢老年人', }, { infoOldType: '重残老人', }, { infoOldType: '独居老年人', }, { infoOldType: '计划生育特殊家庭老人', }, { infoOldType: '重残老年人', }, { infoOldType: '失能', }, { infoOldType: '农村留守老年人', }, { infoOldType: '分散供养特困老年人', }, { infoOldType: '高龄老人', }, { infoOldAbility: '失能', }, ]; } else if (eightType === '0') { data.$and = [{ infoOldType: '居家老年人' }, { infoOldType: { $size: 1 } }]; data.$or = [ { infoOldAbility: '完全自理', }, { infoOldAbility: '半失能(部分自理)', }, { infoOldAbility: '半失能(部分自理)', }, ]; } } delete data.eightType; // 老人类别 if (data.infoOldType) { const ts = data.infoOldType.split(','); data.infoOldType = { $in: ts }; } else { delete data.infoOldType; } if (pop) { const total = await this.tag().find(data).countDocuments(); let userTotal = await this.tag().aggregate([ { $match: data }, { $group: { _id: { userid: '$userid' } } }, { $count: 'total' }, ]); let oldPersonTotal = await this.tag().aggregate([ { $match: data }, { $group: { _id: { infoId: '$infoId' } } }, { $count: 'total' }]); const result = (sort ? await this.tag().find(data).populate(pop) .sort(sort) .skip((page - 1) * rows) .limit(rows) : await this.tag().find(data).populate(pop) .skip((page - 1) * rows) .limit(rows)); result.forEach(item => { let str = ''; if (item.fwxq_rzylyxq === '1') str += '入住养老院需求 '; if (item.fwxq_zyfw === '1') str += '助医服务 '; if (item.fwxq_zxfw === '1') str += '助行服务 '; if (item.fwxq_zjfw === '1') str += '助洁服务 '; if (item.fwxq_zcfw === '1') str += '助餐服务 '; if (item.fwxq_xzfw === '1') str += '洗澡服务 '; if (item.fwxq_kffw === '1') str += '康复服务 '; if (item.fwxq_jtcw === '1') str += '家庭床位 '; if (item.fwxq_rzsqyljgxq === '1') str += '入住社区养老院需求 '; if (item.fwxq_jkjcxq === '1') str += '健康检查需求 '; if (item.fwxq_qtfwxq === '1') str += '其他服务需求 '; if (item.demand == null) { if (str.length === 0) { str = '无'; } item.demand = str; } }); if (userTotal[0] !== undefined) { userTotal = userTotal[0].total; } else { userTotal = 0; } if (oldPersonTotal[0] !== undefined) { oldPersonTotal = oldPersonTotal[0].total; } else { oldPersonTotal = 0; } return { count: total, // 共计X次 // 这俩有问题,带地区显示为0 userCount: userTotal, // 位巡访员 oldPersonCount: oldPersonTotal, // 巡访X位老年人 list: result, // 数据列表 }; } } } module.exports = VisitService;