123456789101112131415161718192021222324252627282930313233 |
- 'use strict';
- const Controller = require('egg').Controller;
- const { CrudController } = require('naf-framework-mongoose-free/lib/controller');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- // 项目测试及管理员登陆
- class HomeController extends Controller {
- async index() {
- const { ctx } = this;
- ctx.body = 'hi, egg';
- }
- /**
- * 系统管理员登陆
- * 太简单了,就不写service了,直接在这处理完完事了
- */
- async login() {
- let admin = await this.ctx.model.Admin.findOne({}, '+password').exec();
- if (!admin) throw new BusinessError(ErrorCode.FILE_FAULT, '未初始化管理员,拒绝请求!');
- const { account, password } = this.ctx.request.body;
- if (!account) throw new BusinessError(ErrorCode.BADPARAM, '未找到要登陆用户账号');
- if (!password) throw new BusinessError(ErrorCode.BADPARAM, '未找到要登陆用户密码');
- if (admin.account !== account) throw new BusinessError(ErrorCode.USER_NOT_EXIST, '未找到要登录的用户');
- 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;
- delete admin._id;
- const token = this.ctx.service.util.jwt.encrypt(admin);
- this.ctx.ok({ data: token });
- }
- }
- module.exports = CrudController(HomeController, {});
|