'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; } async login({ openid }) { const num = await this.model.count({ openid }); if (num <= 0) return null; 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;