123456789101112131415161718192021222324252627282930313233343536 |
- 'use strict';
- const { CrudService } = require('naf-framework-mongoose-free/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const _ = require('lodash');
- const assert = require('assert');
- //
- class AdminService extends CrudService {
- constructor(ctx) {
- super(ctx, 'admin');
- this.model = this.ctx.model.User.Admin;
- }
- /**
- * 登陆
- * @param {Object} body 登陆参数
- * @param body.account 账户
- * @param body.password 密码
- */
- async login({ account, password }) {
- const { populate } = this.getRefMods();
- const user = await this.model.findOne({ account }, '+password').populate(populate);
- if (!user) throw new BusinessError(ErrorCode.USER_NOT_EXIST);
- const { password: upwd, status } = user;
- if (status !== '0') throw new BusinessError(ErrorCode.USER_NOT_BIND, '该账号处于禁止使用状态');
- if (password !== upwd.secret) throw new BusinessError(ErrorCode.BAD_PASSWORD);
- // // 使用redis存储,后续的任何操作进行token的校验
- // await this.setUserInRedis(user);
- delete user.password;
- delete user.meta;
- delete user.__v;
- const token = this.ctx.service.util.jwt.encrypt(user);
- return token;
- }
- }
- module.exports = AdminService;
|