123456789101112131415161718192021222324252627282930313233343536373839404142 |
- '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 crypto = require('crypto');
- const { ObjectId } = require('mongoose').Types;
- // 用户管理
- class UserService extends CrudService {
- constructor(ctx) {
- super(ctx, 'user');
- this.model = this.ctx.model.User;
- }
- /**
- * 登陆
- * @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 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 = UserService;
|