home.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. 'use strict';
  2. const Controller = require('egg').Controller;
  3. const { CrudController } = require('naf-framework-mongoose-free/lib/controller');
  4. const { BusinessError, ErrorCode } = require('naf-core').Error;
  5. const { ObjectId } = require('mongoose').Types;
  6. // 项目测试及管理员登陆
  7. class HomeController extends Controller {
  8. async index() {
  9. const { ctx } = this;
  10. const enrollModel = this.ctx.model.Enroll;
  11. const teamApply = this.ctx.model.TeamApply;
  12. const enrollQuery = { project_id: '62e5fff82dc7343e137ffef5', grouping_id: '62e601c72dc7343e137fff6f', status: { $ne: '0' } };
  13. const teamApplyQuery = { project_id: '62e5fff82dc7343e137ffef5', grouping_id: '62e601c72dc7343e137fff6f' };
  14. // 报名数据
  15. const eData = await enrollModel.find(enrollQuery);
  16. // 组队申请数据
  17. const taData = await teamApply.find(teamApplyQuery);
  18. // 查看每个申请组队的人,是否有报名数据
  19. for (const ta of taData) {
  20. const { applyuser_id, teammate_id } = ta;
  21. const ar = eData.find(f => f.openid === applyuser_id);
  22. const tr = eData.find(f => f.openid === teammate_id);
  23. if (!ar) {
  24. console.log('没有申请人');
  25. console.log(ta);
  26. } else if (!tr) {
  27. console.log('没有队友');
  28. console.log(ta);
  29. } else if (!ar && !tr) {
  30. console.log('都没有');
  31. console.log(ta);
  32. }
  33. }
  34. ctx.body = { eData, taData };
  35. }
  36. /**
  37. * 系统管理员登陆
  38. * 太简单了,就不写service了,直接在这处理完完事了
  39. */
  40. async login() {
  41. const { account, password } = this.ctx.request.body;
  42. let admin = await this.ctx.model.Admin.findOne({ account }, '+password').exec();
  43. if (!account) throw new BusinessError(ErrorCode.BADPARAM, '未找到要登陆用户账号');
  44. if (!password) throw new BusinessError(ErrorCode.BADPARAM, '未找到要登陆用户密码');
  45. if (admin.password.secret !== password) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误');
  46. admin = JSON.parse(JSON.stringify(admin));
  47. delete admin.password;
  48. delete admin.meta;
  49. delete admin.__v;
  50. const { is_super } = admin;
  51. if (!is_super) {
  52. // 获取角色
  53. const roleRes = await this.ctx.model.Role.findOne({ _id: admin.role });
  54. if (roleRes) {
  55. admin.role_id = roleRes._id;
  56. }
  57. }
  58. const token = this.ctx.service.util.jwt.encrypt(admin);
  59. this.ctx.ok({ data: token });
  60. }
  61. }
  62. module.exports = CrudController(HomeController, {});