'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; } /** * 登陆 * @param {Object} body 登陆参数 */ async login({ phone, password }) { let user = await this.model.findOne({ phone }, '+password'); if (!user) throw new BusinessError(ErrorCode.USER_NOT_EXIST); const { password: upwd, status } = 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; // 用户信息toekn解密。小程序不适用 const token = this.ctx.service.util.jwt.encrypt(user); return token; // return user; } // 修改密码 async updatePwd({ 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(); } } module.exports = AdminService;