'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; ctx.body = 'hello'; } /** * 系统管理员登陆 * 太简单了,就不写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, {});