appletsService.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. const md5 = require('md5');
  4. class AppletsService extends Service {
  5. async bing(query) {
  6. const { service, ctx } = this;
  7. const { name, pwd, appletsId } = query;
  8. const user = await ctx.service.sysUserService.oneData({ appletsId }, ctx.getUserPop());
  9. const result = await service.sysUserService.oneData({ loginName: name, loginPwd: pwd });
  10. if (result) {
  11. if ('' + result.role._id === this.app.config.defaultUserRoleId) { // 采集员
  12. if (user && user.loginName === name) { // 本人登录
  13. return null;
  14. }
  15. if (!result.appletsId || result.appletsId === '') {
  16. if (result.file && result.file !== '') {
  17. if (result.userName && result.sex
  18. && result.company && result.job
  19. && result.politicalOutlook && result.phone) {
  20. const res = await service.sysUserService.update(result._id, { appletsId });
  21. ctx.logger.info('bing========' + JSON.stringify(res));
  22. return null;
  23. }
  24. return '请管理员到后台完善资料方可登录!';
  25. }
  26. return '请管理员到后台上传认证文件方可登录!';
  27. }
  28. return '该账号已被其他用户绑定!';
  29. }
  30. return '请使用采集员账号!';
  31. }
  32. return '用户名或者密码错误,请联系管理员!';
  33. }
  34. async updatePwdWithAppletsId(query) {
  35. const { ctx, service } = this;
  36. const { oldPwd, pwd } = query;
  37. const users = await service.sysUserService.list({ appletsId: ctx.appletsId, loginPwd: md5(oldPwd) });
  38. if (users.length === 1) {
  39. return await service.sysUserService.update(users[0]._id, { loginPwd: md5(pwd) });
  40. }
  41. return null;
  42. }
  43. async deptStatistics(query) {
  44. const { ctx } = this;
  45. const { model } = ctx;
  46. const { appletsId, openId } = query;
  47. const result = {};
  48. const where = {};
  49. let user;
  50. if (openId) {
  51. user = await ctx.service.sysUserService.oneData({ openId }, ctx.getUserPop());
  52. }
  53. if (appletsId) {
  54. user = await ctx.service.sysUserService.oneData({ appletsId }, ctx.getUserPop());
  55. }
  56. // ctx.logger.info('登录人信息================' + user)
  57. // ctx.logger.info('openId================' + openId)
  58. // ctx.logger.info('appletsId================' + appletsId)
  59. if (appletsId || openId) {
  60. // const user = await ctx.service.sysUserService.oneData({ appletsId }, ctx.getUserPop());
  61. if (user) {
  62. result.dept = user.dept2.name + user.dept3.name;
  63. where.dept3 = user.dept3._id;
  64. } else {
  65. result.dept = '吉林省';
  66. where.dept1 = '5d4289205ffc6694f7e42082';
  67. }
  68. } else {
  69. result.dept = '吉林省';
  70. where.dept1 = '5d4289205ffc6694f7e42082';
  71. }
  72. const visitCount = await model.VisitModel.find(where).countDocuments();
  73. const infoTotal = await model.InfoModel.find(where).countDocuments();
  74. where.role = this.app.config.defaultUserRoleId;
  75. const userTotal = await model.SysUserModel.find(where).countDocuments();
  76. result.visitCount = visitCount;
  77. result.infoTotal = infoTotal;
  78. result.userTotal = userTotal;
  79. return result;
  80. }
  81. async userValue() {
  82. const { ctx } = this;
  83. const { model } = ctx;
  84. const user = ctx.user;
  85. // const pop = [
  86. // {
  87. // path: 'infoId',
  88. // select: 'name',
  89. // },
  90. // ];
  91. // const valueInfoList = await model.ValueModel.find({ userid: user._id, type: '0' }).populate(pop);
  92. // const valueVisitList = await model.ValueModel.find({ userid: user._id, type: '1' }).populate(pop);
  93. const valueInfoList = await model.ValueModel.aggregate([
  94. { $match: { userid: user._id, type: '0' } },
  95. { $lookup: { from: 'info', localField: 'infoId', foreignField: '_id', as: 'infos' } },
  96. { $unwind: { path: '$infos', preserveNullAndEmptyArrays: true } },
  97. { $project: {
  98. _id: 0,
  99. oldName: '$infos.name',
  100. time: '$infos.time',
  101. } },
  102. { $sort: { time: -1 } },
  103. ]).allowDiskUse(true);// 允许使用磁盘缓存
  104. const valueVisitList = await model.ValueModel.aggregate([
  105. { $match: { userid: user._id, type: '1' } },
  106. { $lookup: { from: 'info', localField: 'infoId', foreignField: '_id', as: 'infos' } },
  107. { $unwind: { path: '$infos', preserveNullAndEmptyArrays: true } },
  108. { $project: {
  109. _id: 0,
  110. oldName: '$infos.name',
  111. time: '$infos.time',
  112. } },
  113. { $sort: { time: -1 } },
  114. ]).allowDiskUse(true);// 允许使用磁盘缓存
  115. const valueInfoCount = await model.ValueModel.find({ userid: user._id, type: '0' }).countDocuments();
  116. const valueVisitCount = await model.ValueModel.find({ userid: user._id, type: '1' }).countDocuments();
  117. const userTotalValue = 5 * valueInfoCount + valueVisitCount;
  118. let order = 1;
  119. const visitOrder = await model.ValueModel.aggregate([
  120. { $match: { dept1: user.dept1._id } },
  121. { $group:
  122. {
  123. _id: '$userid',
  124. type0: { $sum: {
  125. $cond: [{ $eq: [ '$type', '0' ] }, 1, 0 ], // 采集审核通过数
  126. } },
  127. type1: { $sum: {
  128. $cond: [{ $eq: [ '$type', '1' ] }, 1, 0 ], // 探访数
  129. } },
  130. },
  131. },
  132. { $project: {
  133. id: 1,
  134. infoValue: { $multiply: [ '$type0', 5 ] },
  135. visitValue: { $multiply: [ '$type1', 1 ] },
  136. totalValue: { $add: [{ $multiply: [ '$type0', 5 ] }, { $multiply: [ '$type1', 1 ] }] },
  137. } },
  138. { $sort: { totalValue: -1 } },
  139. { $project: {
  140. infoValue: 1,
  141. visitValue: 1,
  142. totalValue: 1,
  143. } },
  144. { $match: { totalValue: { $gt: userTotalValue } } }, // 排名
  145. ]).allowDiskUse(true);// 允许使用磁盘缓存
  146. if (visitOrder.length > 0) {
  147. order = visitOrder.length + 1;
  148. }
  149. return {
  150. _id: user._id, userName: user.userName, loginName: user.loginName,
  151. userTotalValue, order, valueInfoList, valueVisitList,
  152. };
  153. }
  154. async getToken(query) {
  155. const { ctx } = this;
  156. const { key, secret } = query;
  157. const tokenUrl = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + key + '&client_secret=' + secret;
  158. const res = await this.ctx.curl(tokenUrl, {
  159. dataType: 'json',
  160. });
  161. if (res.status == '200') {
  162. this.ctx.logger.info('accessToken=============>' + res.data.access_token);
  163. return { status: 200, token: res.data.access_token };
  164. }
  165. return { status: 500, msg: res.data.error_description };
  166. }
  167. }
  168. module.exports = AppletsService;