1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- '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');
- const Transaction = require('mongoose-transactions');
- //
- class AdminService extends CrudService {
- constructor(ctx) {
- super(ctx, 'admin');
- this.model = this.ctx.model.User.Admin;
- this.tran = new Transaction();
- }
- async beforeCreate(data) {
- const { account } = data;
- const num = await this.model.count({ account });
- if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '该账号名已存在');
- return data;
- }
- async beforeUpdate(filter, update) {
- const { account } = update;
- const id = _.get(filter, '_id', _.get(filter, 'id'));
- const num = await this.model.count({ _id: { $ne: id }, account });
- if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '该账号名已存在');
- return { filter, update };
- }
- /**
- * 登陆
- * @param {Object} body 登陆参数
- * @param body.account 账户
- * @param body.password 密码
- */
- async login({ account, password }) {
- const { populate } = this.getRefMods();
- let user = await this.model.findOne({ account }, '+password').populate(populate);
- if (!user) throw new BusinessError(ErrorCode.USER_NOT_EXIST);
- const { password: upwd } = user;
- if (password !== upwd.secret) throw new BusinessError(ErrorCode.BAD_PASSWORD);
- // // 使用redis存储,后续的任何操作进行token的校验
- // await this.setUserInRedis(user);
- user = JSON.parse(JSON.stringify(user));
- delete user.password;
- delete user.meta;
- delete user.__v;
- const token = this.ctx.service.util.jwt.encrypt(user);
- return token;
- }
- 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 要重置的账号
- */
- async emailResetPwd({ account }) {
- const admin = await this.model.findOne({ account });
- if (!admin) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户');
- const { email } = admin;
- if (!email) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到账户的邮箱');
- // 重置密码+发邮件
- const secret = this.ctx.service.util.trade.createNonceStr();
- try {
- this.tran.update('Admin', admin._id, { password: { secret } });
- await this.ctx.service.util.email.resetPwd(email, secret);
- await this.tran.run();
- } catch (error) {
- this.tran.rollback();
- throw new Error(error);
- } finally {
- this.tran.clean();
- }
- }
- /**
- * 检验当前管理员密码,生成操作key
- * @param {Object} body 参数体
- * @param {String} body.password 管理员密码
- * @param {String} body.target 目标key
- */
- async toMakeKey({ password, target }) {
- const admin = _.get(this.ctx, 'admin');
- const _id = _.get(admin, '_id');
- const taData = await this.model.findById(_id, '+password');
- if (!taData) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到当前操作管理人');
- const { password: upwd } = taData;
- if (password !== upwd.secret) throw new BusinessError(ErrorCode.BAD_PASSWORD);
- const key = this.ctx.service.util.user.getDeleteUserKey(_id, target);
- return key;
- }
- }
- module.exports = AdminService;
|