'use strict'; const Service = require('egg').Service; const md5 = require('md5'); class AppletsService extends Service { async bing(query) { const { service, ctx } = this; const { name, pwd, appletsId } = query; const user = await ctx.service.sysUserService.oneData({ appletsId }, ctx.getUserPop()); const result = await service.sysUserService.oneData({ loginName: name, loginPwd: pwd }); if (result) { if ('' + result.role._id === this.app.config.defaultUserRoleId) { // 采集员 if (user && user.loginName === name) { // 本人登录 return null; } if (!result.appletsId || result.appletsId === '') { if (result.file && result.file !== '') { if (result.userName && result.sex && result.company && result.job && result.politicalOutlook && result.phone) { const res = await service.sysUserService.update(result._id, { appletsId }); ctx.logger.info('bing========' + JSON.stringify(res)); return null; } return '请管理员到后台完善资料方可登录!'; } return '请管理员到后台上传认证文件方可登录!'; } return '该账号已被其他用户绑定!'; } return '请使用采集员账号!'; } return '用户名或者密码错误,请联系管理员!'; } async updatePwdWithAppletsId(query) { const { ctx, service } = this; const { oldPwd, pwd } = query; const users = await service.sysUserService.list({ appletsId: ctx.appletsId, loginPwd: md5(oldPwd) }); if (users.length === 1) { return await service.sysUserService.update(users[0]._id, { loginPwd: md5(pwd) }); } return null; } async deptStatistics(query) { const { ctx } = this; const { model } = ctx; const { appletsId, openId } = query; const result = {}; const where = {}; let user; if (openId) { user = await ctx.service.sysUserService.oneData({ openId }, ctx.getUserPop()); } if (appletsId) { user = await ctx.service.sysUserService.oneData({ appletsId }, ctx.getUserPop()); } // ctx.logger.info('登录人信息================' + user) // ctx.logger.info('openId================' + openId) // ctx.logger.info('appletsId================' + appletsId) if (appletsId || openId) { // const user = await ctx.service.sysUserService.oneData({ appletsId }, ctx.getUserPop()); if (user) { result.dept = user.dept2.name + user.dept3.name; where.dept3 = user.dept3._id; } else { result.dept = '吉林省'; where.dept1 = '5d4289205ffc6694f7e42082'; } } else { result.dept = '吉林省'; where.dept1 = '5d4289205ffc6694f7e42082'; } const visitCount = await model.VisitModel.find(where).countDocuments(); const infoTotal = await model.InfoModel.find(where).countDocuments(); where.role = this.app.config.defaultUserRoleId; const userTotal = await model.SysUserModel.find(where).countDocuments(); result.visitCount = visitCount; result.infoTotal = infoTotal; result.userTotal = userTotal; return result; } async userValue() { const { ctx } = this; const { model } = ctx; const user = ctx.user; // const pop = [ // { // path: 'infoId', // select: 'name', // }, // ]; // const valueInfoList = await model.ValueModel.find({ userid: user._id, type: '0' }).populate(pop); // const valueVisitList = await model.ValueModel.find({ userid: user._id, type: '1' }).populate(pop); const valueInfoList = await model.ValueModel.aggregate([ { $match: { userid: user._id, type: '0' } }, { $lookup: { from: 'info', localField: 'infoId', foreignField: '_id', as: 'infos' } }, { $unwind: { path: '$infos', preserveNullAndEmptyArrays: true } }, { $project: { _id: 0, oldName: '$infos.name', time: '$infos.time', } }, { $sort: { time: -1 } }, ]).allowDiskUse(true);// 允许使用磁盘缓存 const valueVisitList = await model.ValueModel.aggregate([ { $match: { userid: user._id, type: '1' } }, { $lookup: { from: 'info', localField: 'infoId', foreignField: '_id', as: 'infos' } }, { $unwind: { path: '$infos', preserveNullAndEmptyArrays: true } }, { $project: { _id: 0, oldName: '$infos.name', time: '$infos.time', } }, { $sort: { time: -1 } }, ]).allowDiskUse(true);// 允许使用磁盘缓存 const valueInfoCount = await model.ValueModel.find({ userid: user._id, type: '0' }).countDocuments(); const valueVisitCount = await model.ValueModel.find({ userid: user._id, type: '1' }).countDocuments(); const userTotalValue = 5 * valueInfoCount + valueVisitCount; let order = 1; const visitOrder = await model.ValueModel.aggregate([ { $match: { dept1: user.dept1._id } }, { $group: { _id: '$userid', type0: { $sum: { $cond: [{ $eq: [ '$type', '0' ] }, 1, 0 ], // 采集审核通过数 } }, type1: { $sum: { $cond: [{ $eq: [ '$type', '1' ] }, 1, 0 ], // 探访数 } }, }, }, { $project: { id: 1, infoValue: { $multiply: [ '$type0', 5 ] }, visitValue: { $multiply: [ '$type1', 1 ] }, totalValue: { $add: [{ $multiply: [ '$type0', 5 ] }, { $multiply: [ '$type1', 1 ] }] }, } }, { $sort: { totalValue: -1 } }, { $project: { infoValue: 1, visitValue: 1, totalValue: 1, } }, { $match: { totalValue: { $gt: userTotalValue } } }, // 排名 ]).allowDiskUse(true);// 允许使用磁盘缓存 if (visitOrder.length > 0) { order = visitOrder.length + 1; } return { _id: user._id, userName: user.userName, loginName: user.loginName, userTotalValue, order, valueInfoList, valueVisitList, }; } async getToken(query) { const { ctx } = this; const { key, secret } = query; const tokenUrl = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + key + '&client_secret=' + secret; const res = await this.ctx.curl(tokenUrl, { dataType: 'json', }); if (res.status == '200') { this.ctx.logger.info('accessToken=============>' + res.data.access_token); return { status: 200, token: res.data.access_token }; } return { status: 500, msg: res.data.error_description }; } } module.exports = AppletsService;