1234567891011121314151617181920212223242526272829303132333435363738394041 |
- '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.Admin;
- }
- async resetPwd({ id }, { password }) {
- const data = await this.model.findById(id);
- if (!data) throw new BusinessError(ErrorCode.USER_NOT_EXIST);
- data.password = { secret: password };
- await data.save();
- }
- /**
- * 登陆
- * @param {Object} body 登陆参数
- * @param body.account
- * @param body.password
- */
- async login({ account, password }) {
- let user = await this.model.findOne({ account }, '+password');
- if (!user) throw new BusinessError(ErrorCode.USER_NOT_EXIST);
- const { password: upwd } = user;
- // if (status !== '1') throw new BusinessError(ErrorCode.USER_NOT_BIND, '该账号处于禁止使用状态');
- if (password !== upwd.secret) { throw new BusinessError(ErrorCode.BAD_PASSWORD); }
- user = JSON.parse(JSON.stringify(user));
- delete user.meta;
- delete user.__v;
- delete user.password;
- const token = this.ctx.service.util.jwt.encrypt(user);
- return token;
- }
- }
- module.exports = AdminService;
|