12345678910111213141516171819202122232425262728293031323334353637 |
- 'use strict';
- const assert = require('assert');
- const _ = require('lodash');
- const { ObjectId } = require('mongoose').Types;
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const { UserError, ErrorCode } = require('naf-core').Error;
- const jwt = require('jsonwebtoken');
- class UserService extends CrudService {
- constructor(ctx) {
- super(ctx, 'user');
- this.model = this.ctx.model.User;
- }
- // 创建登录Token
- async createJwtPwd(password) {
- const { secret } = this.config.jwt;
- const token = await jwt.sign(password, secret);
- return token;
- }
- async create(data) {
- const { name, mobile, password } = data;
- assert(name, '用户名不能为空');
- assert(password, '密码不能为空');
- const has_phone = await this.model.findOne({ mobile });
- if (has_phone) {
- throw new UserError('此身份手机号已被注册,请更换手机号');
- } else {
- // const pas = await this.createJwtPwd(password);
- data.password = { secret: password };
- const res = await this.model.create(data);
- return res;
- }
- }
- }
- module.exports = UserService;
|