123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- 'use strict';
- const Service = require('../service/baseService');
- const request = require('request');
- const fs = require('fs');
- const path = require('path');
- 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.defaultUrl + data.photoPath; // 采集文件路径;
- const visitPicUrl = this.app.config.defaultUrl + data.visitPhotoPath; // 采集文件路径;
- const jsonData = [
- {
- // image: 'https://bucketnj1-1254259530.cos.ap-nanjing.myqcloud.com/face/tmp_0c7c2dcde7374488f97228d2134d48950d150d9f6d5d492c.jpg',
- image: infoPicUrl,
- image_type: 'URL',
- },
- {
- // image: 'https://bucketnj1-1254259530.cos.ap-nanjing.myqcloud.com/face/tmp_0c7c2dcde7374488f97228d2134d48950d150d9f6d5d492c.jpg',
- image: visitPicUrl,
- image_type: 'URL',
- },
- ];
- 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);
- }
- console.log('error');
- }
- 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);
- }
- console.log('error');
- }
- 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 };
- }
- 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;
- if (pop) {
- const total = await this.tag().find(data).countDocuments();
- const userTotal = await this.tag().aggregate([
- { $match: data },
- { $group: { _id: { userid: '$userid' }, count: { $sum: 1 } } },
- ]);
- const oldPersonTotal = await this.tag().aggregate([
- { $match: data },
- { $group: { _id: { oldInfo: '$oldIdNumber' }, count: { $sum: 1 } } }]);
- 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));
- return {
- count: total,
- // 这俩有问题,带地区显示为0
- userCount: userTotal.length,
- oldPersonCount: oldPersonTotal.length,
- list: result,
- };
- }
- }
- }
- module.exports = VisitService;
|