|
@@ -0,0 +1,91 @@
|
|
|
+'use strict';
|
|
|
+
|
|
|
+
|
|
|
+const assert = require('assert');
|
|
|
+const _ = require('lodash');
|
|
|
+const { ObjectId } = require('mongoose').Types;
|
|
|
+const { CrudService } = require('naf-framework-mongoose/lib/service');
|
|
|
+const { BusinessError, ErrorCode } = require('naf-core').Error;
|
|
|
+const jwt = require('jsonwebtoken');
|
|
|
+
|
|
|
+class UserService extends CrudService {
|
|
|
+ constructor(ctx) {
|
|
|
+ super(ctx, 'user');
|
|
|
+ this.model = this.ctx.model.User;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 重写创建方法
|
|
|
+ async create(data) {
|
|
|
+ const { uid, openid, passwd, role_id, name, phone } = data;
|
|
|
+ // console.log(data);
|
|
|
+ assert(name && phone && passwd, '缺少部分信息项');
|
|
|
+ assert(/^\d{11}$/i.test(phone), '无效的手机号');
|
|
|
+ const user = await this.model.findOne({ phone });
|
|
|
+ if (user) {
|
|
|
+ throw new BusinessError(ErrorCode.DATA_EXISTED);
|
|
|
+ }
|
|
|
+ const newdata = data;
|
|
|
+ const pas = await this.createJwtPwd(passwd);
|
|
|
+ newdata.passwd = pas;
|
|
|
+ const res = await this.model.create(newdata);
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 用户修改密码
|
|
|
+ async uppasswd(data) {
|
|
|
+ const { userid, oldpasswd, newpasswd } = data;
|
|
|
+ assert(userid && oldpasswd && newpasswd, '缺少部分信息项');
|
|
|
+ // 根据用户id查询其他用户表中是否存在相应数据
|
|
|
+ const user = await this.model.findById(userid, '+passwd');
|
|
|
+ // 如果用户不存在抛出异常
|
|
|
+ if (!user) {
|
|
|
+ throw new BusinessError(ErrorCode.USER_NOT_EXIST);
|
|
|
+ }
|
|
|
+ // 将用户输入的密码进行加密并与查询到的用户数据密码相比对
|
|
|
+ const _oldpasswd = await this.createJwtPwd(oldpasswd);
|
|
|
+ // 如果两个密码不一致抛出异常
|
|
|
+ if (_oldpasswd !== user.passwd) {
|
|
|
+ throw new BusinessError(ErrorCode.BAD_PASSWORD);
|
|
|
+ }
|
|
|
+ const _newpasswd = await this.createJwtPwd(newpasswd);
|
|
|
+ user.passwd = _newpasswd;
|
|
|
+ await user.save();
|
|
|
+ }
|
|
|
+
|
|
|
+ async login(data) {
|
|
|
+ const { phone, passwd } = data;
|
|
|
+ // 根据用户输入的手机号查询其他用户表中是否存在相应数据
|
|
|
+ const user = await this.model.findOne({ phone });
|
|
|
+ // 如果用户不存在抛出异常
|
|
|
+ if (!user) {
|
|
|
+ throw new BusinessError(ErrorCode.USER_NOT_EXIST);
|
|
|
+ }
|
|
|
+ const _user = await this.model.findOne({ phone }, '+passwd');
|
|
|
+ // 将用户输入的密码进行加密并与查询到的用户数据密码相比对
|
|
|
+ const pas = await this.createJwtPwd(passwd);
|
|
|
+ // 如果两个密码不一致抛出异常
|
|
|
+ if (pas !== _user.passwd) {
|
|
|
+ throw new BusinessError(ErrorCode.BAD_PASSWORD);
|
|
|
+ }
|
|
|
+ // 取出用户的类型,根据用户类型返回相应信息
|
|
|
+ return await this.createJwt(user);
|
|
|
+ }
|
|
|
+
|
|
|
+ async createJwtPwd(password) {
|
|
|
+ const { secret, expiresIn, issuer } = this.config.jwt;
|
|
|
+ const token = await jwt.sign(password, secret);
|
|
|
+ return token;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建登录Token
|
|
|
+ async createJwt({ id, uid, openid, role_id, name, phone }) {
|
|
|
+ const { secret, expiresIn = '1d', issuer } = this.config.jwt;
|
|
|
+ const subject = phone;
|
|
|
+ const _userid = id;
|
|
|
+ const res = { userid: _userid, uid, openid, role_id, name, phone };
|
|
|
+ const token = await jwt.sign(res, secret, { expiresIn, issuer, subject });
|
|
|
+ return token;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+module.exports = UserService;
|