card.js 848 B

123456789101112131415161718192021222324252627
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const { ObjectId } = require('mongoose').Types;
  5. class CardService extends CrudService {
  6. constructor(ctx) {
  7. super(ctx, 'card');
  8. this.model = this.ctx.model.Card;
  9. }
  10. async create(data) {
  11. const { password, mobile } = data;
  12. const is_exists = await this.model.count({ mobile });
  13. if (is_exists) throw new BusinessError(ErrorCode.DATA_EXISTED, '手机号已存在');
  14. data.password = { secret: password };
  15. const res = await this.model.create(data);
  16. return res;
  17. }
  18. async passwd({ id, password }) {
  19. password = { secret: password };
  20. const res = await this.model.update({ _id: ObjectId(id) }, { password });
  21. return res;
  22. }
  23. }
  24. module.exports = CardService;