visitService.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. 'use strict';
  2. const Service = require('../service/baseService');
  3. const request = require('request');
  4. const fs = require('fs');
  5. const path = require('path');
  6. class VisitService extends Service {
  7. tag() {
  8. return this.ctx.model.VisitModel;
  9. }
  10. // baidu-base64编码
  11. async getResult(data) {
  12. if (data.photoPath && data.visitPhotoPath && data.accessToken) {
  13. // 读取待识别图像并base64编码
  14. const infoPicName = path.basename('' + data.photoPath);// 该用户的采集图片的文件名 xxx.jpg
  15. const visitPicName = path.basename('' + data.visitPhotoPath);// 该用户的探访图片的文件名 xxx.jpg
  16. // const infoPicPath = this.app.config.defaultUploadPath + infoPicName; // 采集文件路径;
  17. const infoPicPath = infoPicName; // 采集文件路径;
  18. // const visitPicPath = this.app.config.defaultUploadPath + visitPicName; // 探访文件路径
  19. const visitPicPath = visitPicName; // 探访文件路径
  20. const infoData = fs.readFileSync(infoPicPath); // 例:fileUrl="D:\\test\\test.bmp"
  21. const visitData = fs.readFileSync(visitPicPath); // 例:fileUrl="D:\\test\\test.bmp"
  22. const infoBase64 = infoData.toString('base64');
  23. const visitBase64 = visitData.toString('base64');
  24. const jsonData = [
  25. {
  26. image: infoBase64,
  27. image_type: 'BASE64',
  28. },
  29. {
  30. image: visitBase64,
  31. image_type: 'BASE64',
  32. },
  33. ];
  34. const url = 'https://aip.baidubce.com/rest/2.0/face/v3/match?access_token=' + data.accessToken; // 正式
  35. const res = await this.ctx.curl(url, {
  36. method: 'POST',
  37. headers: {
  38. accept: 'application/json',
  39. 'Content-Type': 'application/x-www-form-urlencoded' },
  40. contentType: 'json',
  41. data: JSON.stringify(jsonData),
  42. dataType: 'json',
  43. });
  44. this.ctx.logger.info('对比结果==========>', res);
  45. if (res.status == '200') {
  46. return res.data;
  47. }
  48. return '';
  49. }
  50. }
  51. // baidu-url
  52. async getResult2(data) {
  53. // this.ctx.logger.info('Service==data=======================>' + JSON.stringify(data));
  54. if (data.photoPath && data.visitPhotoPath && data.accessToken) {
  55. const infoPicUrl = this.app.config.defaultUrl + data.photoPath; // 采集文件路径;
  56. const visitPicUrl = this.app.config.defaultUrl + data.visitPhotoPath; // 采集文件路径;
  57. const jsonData = [
  58. {
  59. // image: 'https://bucketnj1-1254259530.cos.ap-nanjing.myqcloud.com/face/tmp_0c7c2dcde7374488f97228d2134d48950d150d9f6d5d492c.jpg',
  60. image: infoPicUrl,
  61. image_type: 'URL',
  62. },
  63. {
  64. // image: 'https://bucketnj1-1254259530.cos.ap-nanjing.myqcloud.com/face/tmp_0c7c2dcde7374488f97228d2134d48950d150d9f6d5d492c.jpg',
  65. image: visitPicUrl,
  66. image_type: 'URL',
  67. },
  68. ];
  69. const options1 = {
  70. url: 'https://aip.baidubce.com/rest/2.0/face/v3/match?access_token=' + data.accessToken,
  71. method: 'POST',
  72. body: JSON.stringify(jsonData),
  73. headers: {
  74. accept: 'application/json',
  75. 'content-type': 'application/x-www-form-urlencoded',
  76. },
  77. };
  78. const result = await this.getFace(options1);
  79. return JSON.parse(result);
  80. }
  81. console.log('error');
  82. }
  83. async getPeopleFace(data) {
  84. // this.ctx.logger.info('Service==data=======================>' + JSON.stringify(data));
  85. if (data.photoPath && data.accessToken) {
  86. const photoPath = this.app.config.defaultUrl + data.photoPath; // 采集文件路径;
  87. const jsonData =
  88. {
  89. // image: 'https://bucketnj1-1254259530.cos.ap-nanjing.myqcloud.com/face/tmp_0c7c2dcde7374488f97228d2134d48950d150d9f6d5d492c.jpg',
  90. image: photoPath,
  91. image_type: 'URL',
  92. // liveness_control: 'HIGH',
  93. liveness_control: 'NORMAL',
  94. // liveness_control: 'LOW',
  95. };
  96. const options1 = {
  97. url: 'https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token=' + data.accessToken,
  98. method: 'POST',
  99. body: JSON.stringify(jsonData),
  100. headers: {
  101. accept: 'application/json',
  102. 'content-type': 'application/x-www-form-urlencoded',
  103. },
  104. };
  105. const result = await this.getFace(options1);
  106. return JSON.parse(result);
  107. }
  108. console.log('error');
  109. }
  110. async getFace(option) {
  111. return new Promise((resolve, reject) => {
  112. request(option, function(error, response, body) {
  113. if (!error) {
  114. if (response.statusCode == 200) {
  115. resolve(body);
  116. } else {
  117. resolve(body);
  118. }
  119. } else {
  120. reject(error);
  121. }
  122. });
  123. });
  124. }
  125. async bing(query) {
  126. const { ctx, service } = this;
  127. const { fid, openId } = query;
  128. const result = await service.familyService.oneData({ _id: this.app.mongoose.Types.ObjectId(fid) });
  129. // ctx.logger.error('service==========', result);
  130. if (result) {
  131. let openIdArray = [];
  132. if (result.openId) {
  133. openIdArray = result.openId;
  134. // openId已绑定
  135. if (openIdArray.indexOf(openId) == -1) {
  136. openIdArray.push(openId);
  137. } else {
  138. ctx.error('openId已绑定');
  139. }
  140. } else {
  141. openIdArray.push(openId);
  142. }
  143. await service.familyService.update(result._id, { openId: openIdArray });
  144. return null;
  145. }
  146. return '该户不存在,请联系管理员!';
  147. }
  148. // 根据身份证查询
  149. async visitByNumber(data) {
  150. const { model } = this.ctx;
  151. return await model.VisitModel.find(data).sort({ visitTime: -1 }).limit(15);
  152. }
  153. // 导出探访数据
  154. async visitExportData(data) {
  155. const { model } = this.ctx;
  156. const where = {};
  157. if (data.dept1) {
  158. where.dept1 = data.dept1;// 省
  159. }
  160. if (data.dept2) {
  161. where.dept2 = data.dept2; // 市
  162. }
  163. if (data.dept3) {
  164. where.dept3 = data.dept3; // 区
  165. }
  166. if (data.dept4) {
  167. where.dept4 = data.dept4; // 乡
  168. }
  169. if (data.dept5) {
  170. where.dept5 = data.dept5; // 社区
  171. }
  172. const queryName = data.queryName;
  173. delete data.queryName;
  174. if (queryName) {
  175. const userInfo = await this.ctx.model.SysUserModel.find({ loginName: { $regex: queryName } });
  176. if (userInfo.length > 0) {
  177. where.userid = userInfo[0]._id;
  178. }
  179. }
  180. // 老人姓名
  181. if (data.oldName) {
  182. where.oldInfo = { $regex: data.oldName };
  183. }
  184. this.ctx.logger.info('条件', where);
  185. const pop = [
  186. {
  187. path: 'userid',
  188. select: 'loginName',
  189. },
  190. ];
  191. 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,
  192. health: 1, mind: 1, security: 1, hygiene: 1, live: 1, urgency: 1,
  193. demand: 1, visitMessage: 1, userid: 1 }).populate(pop);
  194. return result;
  195. }
  196. // pc端首页近七天探访数据统计
  197. async sevendayNum(data, user) {
  198. const { model } = this.ctx;
  199. const level = user.dept.level;
  200. const match = {};
  201. match.time = { $gte: data.startday };
  202. // admin的dept 存在冲突,所以它不需要结合
  203. if (user.role._id != this.app.config.defaultAdminRoleId) {
  204. match['dept' + level] = user.dept._id;
  205. }
  206. const res = await model.VisitModel.aggregate([
  207. { $project: { time: { $dateToString: { format: '%Y-%m-%d', date: '$visitTime' } }, _id: 1, dept1: 1, dept2: 1, dept3: 1, dept4: 1, dept5: 1 } },
  208. { $match: match },
  209. { $group: { _id: '$time', value: { $sum: 1 } } },
  210. { $project: { label: '$_id', value: 1, _id: 0 } },
  211. { $sort: { label: 1 } },
  212. ]);
  213. this.ctx.logger.info(data);
  214. const morenArr = [];
  215. for (let i = 0; i < 7; i++) {
  216. const yestday = new Date();
  217. yestday.setTime(yestday.getTime() - 24 * 60 * 60 * 1000 * (6 - i));
  218. let month = yestday.getMonth() + 1;
  219. if (month < 10) month = '0' + month;
  220. let day = yestday.getDate();
  221. if (day < 10) day = '0' + day;
  222. const ysday = yestday.getFullYear() + '-' + month + '-' + day;
  223. const vote = {};
  224. vote.label = ysday;
  225. vote.value = 0;
  226. morenArr.push(vote);
  227. }
  228. if (res.length > 0) {
  229. for (let j = 0; j < morenArr.length; j++) {
  230. const mlabel = morenArr[j].label;
  231. for (let k = 0; k < res.length; k++) {
  232. const clabel = res[k].label;
  233. if (mlabel == clabel) {
  234. morenArr[j].value = res[k].value;
  235. }
  236. }
  237. }
  238. }
  239. return morenArr;
  240. }
  241. async listForPageWithDept(data, pop) {
  242. const page = data.page || 1;
  243. const rows = Number.parseInt(data.rows)
  244. || this.app.config.defaultPageSize;
  245. const sort = data.sort;
  246. delete data.page;
  247. delete data.rows;
  248. delete data.sort;
  249. if (pop) {
  250. const total = await this.tag().find(data).countDocuments();
  251. const userTotal = await this.tag().aggregate([
  252. { $match: data },
  253. { $group: { _id: { userid: '$userid' }, count: { $sum: 1 } } },
  254. ]);
  255. const oldPersonTotal = await this.tag().aggregate([
  256. { $match: data },
  257. { $group: { _id: { oldInfo: '$oldIdNumber' }, count: { $sum: 1 } } }]);
  258. const result = (sort ? await this.tag().find(data).populate(pop)
  259. .sort(sort)
  260. .skip((page - 1) * rows)
  261. .limit(rows)
  262. : await this.tag().find(data).populate(pop)
  263. .skip((page - 1) * rows)
  264. .limit(rows));
  265. return {
  266. count: total,
  267. // 这俩有问题,带地区显示为0
  268. userCount: userTotal.length,
  269. oldPersonCount: oldPersonTotal.length,
  270. list: result,
  271. };
  272. }
  273. }
  274. }
  275. module.exports = VisitService;