|
@@ -1,87 +1,87 @@
|
|
|
-'use strict';
|
|
|
-const { CrudService } = require('naf-framework-mongoose/lib/service');
|
|
|
-const { BusinessError, ErrorCode } = require('naf-core').Error;
|
|
|
-const { ObjectId } = require('mongoose').Types;
|
|
|
-const _ = require('lodash');
|
|
|
-const jwt = require('jsonwebtoken');
|
|
|
-const assert = require('assert');
|
|
|
-
|
|
|
-// 个人用户
|
|
|
-class PersonalService extends CrudService {
|
|
|
- constructor(ctx) {
|
|
|
- super(ctx, 'personal');
|
|
|
- this.redis = this.app.redis;
|
|
|
- this.model = this.ctx.model.Personal;
|
|
|
- }
|
|
|
- /**
|
|
|
- * 创建用户
|
|
|
- * @param {Object} params 用户信息
|
|
|
- */
|
|
|
- async create({ password, ...data }) {
|
|
|
- data.password = { secret: password };
|
|
|
- const { phone } = data;
|
|
|
- // 检查手机号
|
|
|
- const num = await this.model.count({ phone, isdel: '0' });
|
|
|
- if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '已有个人用户使用该手机号');
|
|
|
- return await this.model.create(data);
|
|
|
- }
|
|
|
- /**
|
|
|
- * 修改密码
|
|
|
- * @param {Object} {id,password} 用户id和密码
|
|
|
- */
|
|
|
- async password({ id, password }) {
|
|
|
- const object = await this.model.findById(id);
|
|
|
- if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
|
|
|
- object.password = { secret: password };
|
|
|
- await object.save();
|
|
|
- }
|
|
|
- /**
|
|
|
- * 登陆
|
|
|
- * @param {Object} params 登陆信息
|
|
|
- * @property phone 手机号
|
|
|
- * @property password 密码
|
|
|
- */
|
|
|
- async login({ phone, password }) {
|
|
|
- const object = await this.model.findOne({ phone, isdel: '0' }, '+password');
|
|
|
- if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
|
|
|
- const { password: op, status } = object;
|
|
|
- const { secret } = op;
|
|
|
- if (status !== '1') throw new BusinessError(ErrorCode.ACCESS_DENIED, '拒绝访问!');
|
|
|
- if (secret !== password) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误');
|
|
|
- const data = _.omit(JSON.parse(JSON.stringify(object)), [ 'meta', 'password', '__v' ]);
|
|
|
- const { secret: secrets } = this.config.jwt;
|
|
|
- const token = jwt.sign(data, secrets);
|
|
|
- // 记录登陆
|
|
|
- let number = await this.redis.get('login_number') || 0;
|
|
|
- number++;
|
|
|
- await this.redis.set('login_number', number);
|
|
|
- return token;
|
|
|
- }
|
|
|
-
|
|
|
- async delete({ id }) {
|
|
|
- const object = await this.model.findById(id);
|
|
|
- if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
|
|
|
- object.isdel = '1';
|
|
|
- await object.save();
|
|
|
- }
|
|
|
- /**
|
|
|
- * 个人升变专家
|
|
|
- * @param {Object} body 升变数据
|
|
|
- * 升级的时候要把所属的信息(产品,需求等)换到升级后的数据上,一种数据迁移
|
|
|
- */
|
|
|
- async upgrade({ id, ...data }) {
|
|
|
- assert(id, '缺少个人用户ID');
|
|
|
- const user = await this.model.findOne({ _id: ObjectId(id) }, '+password');
|
|
|
- if (!user) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
|
|
|
- data = _.omit(data, [ 'meta', '__v' ]);
|
|
|
- data.user_id = id;
|
|
|
- // 创建专家
|
|
|
- const is_expert = await this.ctx.model.Expert.count({ user_id: ObjectId(id) });
|
|
|
- if (is_expert > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '已升级为专家,无需再次升级');
|
|
|
- await this.ctx.model.Expert.create(data);
|
|
|
- user.is_expert = true;
|
|
|
- await user.save();
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-module.exports = PersonalService;
|
|
|
+'use strict';
|
|
|
+const { CrudService } = require('naf-framework-mongoose/lib/service');
|
|
|
+const { BusinessError, ErrorCode } = require('naf-core').Error;
|
|
|
+const { ObjectId } = require('mongoose').Types;
|
|
|
+const _ = require('lodash');
|
|
|
+const jwt = require('jsonwebtoken');
|
|
|
+const assert = require('assert');
|
|
|
+
|
|
|
+// 个人用户
|
|
|
+class PersonalService extends CrudService {
|
|
|
+ constructor(ctx) {
|
|
|
+ super(ctx, 'personal');
|
|
|
+ this.redis = this.app.redis;
|
|
|
+ this.model = this.ctx.model.Personal;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 创建用户
|
|
|
+ * @param {Object} params 用户信息
|
|
|
+ */
|
|
|
+ async create({ password, ...data }) {
|
|
|
+ data.password = { secret: password };
|
|
|
+ const { phone } = data;
|
|
|
+ // 检查手机号
|
|
|
+ const num = await this.model.count({ phone, isdel: '0' });
|
|
|
+ if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '已有个人用户使用该手机号');
|
|
|
+ return await this.model.create(data);
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 修改密码
|
|
|
+ * @param {Object} {id,password} 用户id和密码
|
|
|
+ */
|
|
|
+ async password({ id, password }) {
|
|
|
+ const object = await this.model.findById(id);
|
|
|
+ if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
|
|
|
+ object.password = { secret: password };
|
|
|
+ await object.save();
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 登陆
|
|
|
+ * @param {Object} params 登陆信息
|
|
|
+ * @property phone 手机号
|
|
|
+ * @property password 密码
|
|
|
+ */
|
|
|
+ async login({ phone, password }) {
|
|
|
+ const object = await this.model.findOne({ phone, isdel: '0' }, '+password');
|
|
|
+ if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
|
|
|
+ const { password: op, status } = object;
|
|
|
+ const { secret } = op;
|
|
|
+ if (status !== '1') throw new BusinessError(ErrorCode.ACCESS_DENIED, '拒绝访问!');
|
|
|
+ if (secret !== password) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误');
|
|
|
+ const data = _.omit(JSON.parse(JSON.stringify(object)), [ 'meta', 'password', '__v' ]);
|
|
|
+ const { secret: secrets } = this.config.jwt;
|
|
|
+ const token = jwt.sign(data, secrets);
|
|
|
+ // 记录登陆
|
|
|
+ // let number = await this.redis.get('login_number') || 0;
|
|
|
+ // number++;
|
|
|
+ // await this.redis.set('login_number', number);
|
|
|
+ return token;
|
|
|
+ }
|
|
|
+
|
|
|
+ async delete({ id }) {
|
|
|
+ const object = await this.model.findById(id);
|
|
|
+ if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
|
|
|
+ object.isdel = '1';
|
|
|
+ await object.save();
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 个人升变专家
|
|
|
+ * @param {Object} body 升变数据
|
|
|
+ * 升级的时候要把所属的信息(产品,需求等)换到升级后的数据上,一种数据迁移
|
|
|
+ */
|
|
|
+ async upgrade({ id, ...data }) {
|
|
|
+ assert(id, '缺少个人用户ID');
|
|
|
+ const user = await this.model.findOne({ _id: ObjectId(id) }, '+password');
|
|
|
+ if (!user) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
|
|
|
+ data = _.omit(data, [ 'meta', '__v' ]);
|
|
|
+ data.user_id = id;
|
|
|
+ // 创建专家
|
|
|
+ const is_expert = await this.ctx.model.Expert.count({ user_id: ObjectId(id) });
|
|
|
+ if (is_expert > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '已升级为专家,无需再次升级');
|
|
|
+ await this.ctx.model.Expert.create(data);
|
|
|
+ user.is_expert = true;
|
|
|
+ await user.save();
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+module.exports = PersonalService;
|