12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- 'use strict';
- const Controller = require('egg').Controller;
- const { CrudController } = require('naf-framework-mongoose-free/lib/controller');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const { ObjectId } = require('mongoose').Types;
- // 项目测试及管理员登陆
- class HomeController extends Controller {
- async index() {
- const { ctx } = this;
- const enrollModel = this.ctx.model.Enroll;
- const teamApply = this.ctx.model.TeamApply;
- const enrollQuery = { project_id: '62e5fff82dc7343e137ffef5', grouping_id: '62e601c72dc7343e137fff6f', status: { $ne: '0' } };
- const teamApplyQuery = { project_id: '62e5fff82dc7343e137ffef5', grouping_id: '62e601c72dc7343e137fff6f' };
- // 报名数据
- const eData = await enrollModel.find(enrollQuery);
- // 组队申请数据
- const taData = await teamApply.find(teamApplyQuery);
- // 查看每个申请组队的人,是否有报名数据
- for (const ta of taData) {
- const { applyuser_id, teammate_id } = ta;
- const ar = eData.find(f => f.openid === applyuser_id);
- const tr = eData.find(f => f.openid === teammate_id);
- if (!ar) {
- console.log('没有申请人');
- console.log(ta);
- } else if (!tr) {
- console.log('没有队友');
- console.log(ta);
- } else if (!ar && !tr) {
- console.log('都没有');
- console.log(ta);
- }
- }
- ctx.body = { eData, taData };
- }
- /**
- * 系统管理员登陆
- * 太简单了,就不写service了,直接在这处理完完事了
- */
- async login() {
- const { account, password } = this.ctx.request.body;
- let admin = await this.ctx.model.Admin.findOne({ account }, '+password').exec();
- if (!account) throw new BusinessError(ErrorCode.BADPARAM, '未找到要登陆用户账号');
- if (!password) throw new BusinessError(ErrorCode.BADPARAM, '未找到要登陆用户密码');
- if (admin.password.secret !== password) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误');
- admin = JSON.parse(JSON.stringify(admin));
- delete admin.password;
- delete admin.meta;
- delete admin.__v;
- const { is_super } = admin;
- if (!is_super) {
- // 获取角色
- const roleRes = await this.ctx.model.Role.findOne({ _id: admin.role });
- if (roleRes) {
- admin.role_id = roleRes._id;
- }
- }
- const token = this.ctx.service.util.jwt.encrypt(admin);
- this.ctx.ok({ data: token });
- }
- }
- module.exports = CrudController(HomeController, {});
|