appletsService.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 valueInfoCount = await model.ValueModel.find({ userid: user._id, type: '0' }).countDocuments();
  94. const valueVisitCount = await model.ValueModel.find({ userid: user._id, type: '1' }).countDocuments();
  95. const userTotalValue = 5 * valueInfoCount + valueVisitCount;
  96. let order = 1;
  97. const visitOrder = await model.ValueModel.aggregate([
  98. { $match: { dept1: user.dept1._id } },
  99. { $group:
  100. {
  101. _id: '$userid',
  102. type0: { $sum: {
  103. $cond: [{ $eq: [ '$type', '0' ] }, 1, 0 ], // 采集审核通过数
  104. } },
  105. type1: { $sum: {
  106. $cond: [{ $eq: [ '$type', '1' ] }, 1, 0 ], // 探访数
  107. } },
  108. },
  109. },
  110. { $project: {
  111. id: 1,
  112. infoValue: { $multiply: [ '$type0', 5 ] },
  113. visitValue: { $multiply: [ '$type1', 1 ] },
  114. totalValue: { $add: [{ $multiply: [ '$type0', 5 ] }, { $multiply: [ '$type1', 1 ] }] },
  115. } },
  116. { $sort: { totalValue: -1 } },
  117. { $project: {
  118. infoValue: 1,
  119. visitValue: 1,
  120. totalValue: 1,
  121. } },
  122. { $match: { totalValue: { $gt: userTotalValue } } }, // 排名
  123. ]).allowDiskUse(true);// 允许使用磁盘缓存
  124. if (visitOrder.length > 0) {
  125. order = visitOrder.length + 1;
  126. }
  127. return {
  128. _id: user._id, userName: user.userName, loginName: user.loginName,
  129. userTotalValue, order, valueInfoList, valueVisitList,
  130. };
  131. }
  132. async getToken(query) {
  133. const { ctx } = this;
  134. const { key, secret } = query;
  135. const tokenUrl = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + key + '&client_secret=' + secret;
  136. const res = await this.ctx.curl(tokenUrl, {
  137. dataType: 'json',
  138. });
  139. if (res.status == '200') {
  140. this.ctx.logger.info('accessToken=============>' + res.data.access_token);
  141. return { status: 200, token: res.data.access_token };
  142. }
  143. return { status: 500, msg: res.data.error_description };
  144. }
  145. }
  146. module.exports = AppletsService;