123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- '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');
- //
- class UserService extends CrudService {
- constructor(ctx) {
- super(ctx, 'user');
- this.model = this.ctx.model.Race.User;
- this.baseUserModel = this.ctx.model.Base.User;
- }
- async login({ openid }) {
- const num = await this.model.count({ openid });
- if (num <= 0) {
- // 去找基础库找用户,然后将信息拿来
- const user = await this.baseUserModel.findOne({ openid });
- if (!user) throw new BusinessError(ErrorCode.USER_NOT_EXIST, '用户不存在,无法进入比赛系统');
- const { _id: user_id, type } = user;
- const obj = { openid, user_id };
- if (type === '0' || type === '3' || type === '2') obj.type = '0';
- else obj.type = type;
- await this.create(obj);
- }
- const list = await this.query({ openid });
- return _.head(list);
- }
- async beforeCreate(body) {
- const { openid, user_id } = body;
- const num = await this.model.count({ openid, user_id });
- if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '比赛模块已有该用户信息');
- return body;
- }
- /**
- * 根据传来的数据,以openid为唯一条件进行查询,如果有用户,则覆盖.没有用户则创建
- * @param {Object} body 参数体
- */
- async bindJudge(body) {
- const { openid } = body;
- const num = await this.model.count({ openid });
- if (num > 0) await this.model.update({ openid }, body);
- else await this.model.create(body);
- return await this.model.findOne({ openid });
- }
- }
- module.exports = UserService;
|