lrf 2 yıl önce
ebeveyn
işleme
17cd50b411

+ 6 - 0
app/controller/config/.user.js

@@ -44,4 +44,10 @@ module.exports = {
   login: {
     requestBody: ['!openid'],
   },
+  coachToBeJudge: {
+    requestBody: ['!coach_id', '!parent_id'],
+  },
+  userToBeJudge: {
+    requestBody: ['!user_id', '!parent_id'],
+  },
 };

+ 33 - 0
app/model/base/coach.js

@@ -0,0 +1,33 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
+
+
+// 教练
+const coach = {
+  name: { type: String, required: false, zh: '姓名' }, //
+  icon: { type: Array, required: false, zh: '头像' }, //
+  card: { type: String, required: false, zh: '身份证号' }, //
+  gender: { type: String, required: false, zh: '性别' }, //
+  age: { type: Number, required: false, zh: '年龄' }, //
+  phone: { type: String, required: false, zh: '联系电话' }, //
+  major: { type: String, required: false, zh: '专业' }, //
+  level: { type: String, required: false, zh: '等级' }, //
+  brief: { type: String, required: false, zh: '简介' }, // 500字以内
+  honor: { type: String, required: false, zh: '荣誉' }, //
+  exp: { type: String, required: false, zh: '教学经历' }, //
+  user_id: { type: String, required: false, zh: '用户id', ref: 'User', getProp: [ 'name' ] }, //
+};
+const schema = new Schema(coach, { toJSON: { virtuals: true } });
+schema.index({ id: 1 });
+schema.index({ 'meta.createdAt': 1 });
+schema.index({ name: 1 });
+schema.index({ card: 1 });
+schema.index({ level: 1 });
+schema.index({ user_id: 1 });
+
+schema.plugin(metaPlugin);
+module.exports = app => {
+  const conn = app.mongooseDB.get('base');
+  return conn.model('Coach', schema, 'coach');
+};

+ 20 - 1
app/service/user.js

@@ -10,6 +10,7 @@ class UserService extends CrudService {
     super(ctx, 'user');
     this.model = this.ctx.model.Race.User;
     this.baseUserModel = this.ctx.model.Base.User;
+    this.baseCoachModel = this.ctx.model.Base.Coach;
   }
 
   async login({ openid }) {
@@ -28,7 +29,6 @@ class UserService extends CrudService {
     return _.head(list);
   }
 
-
   async beforeCreate(body) {
     const { openid, user_id } = body;
     const num = await this.model.count({ openid, user_id });
@@ -46,6 +46,25 @@ class UserService extends CrudService {
     else await this.model.create(body);
     return await this.model.findOne({ openid });
   }
+
+  // 教练成为裁判
+  async coachToBeJudge({ coach_id, parent_id }) {
+    const coach = await this.baseCoachModel.findById(coach_id);
+    if (!coach) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到教练信息');
+    const { user_id } = coach;
+    return await this.userToBeJudge({ user_id, parent_id });
+  }
+
+  // 用户成为裁判
+  async userToBeJudge({ user_id, parent_id }) {
+    assert(user_id, '缺少裁判用户信息');
+    assert(parent_id, '缺少赛事创建方信息');
+    const user = await this.baseUserModel.findById(user_id);
+    if (!user) throw new BusinessError(ErrorCode.USER_NOT_EXIST, '未找到比赛模块用户信息');
+    const { openid } = user;
+    const obj = { user_id, parent_id, type: '2', openid };
+    return this.bindJudge(obj);
+  }
 }
 
 module.exports = UserService;

+ 2 - 0
app/z_router/user.js

@@ -7,6 +7,8 @@ const rkey = 'user';
 const ckey = 'user';
 const keyZh = '比赛用户';
 const routes = [
+  { method: 'post', path: `${rkey}/ctbj`, controller: `${ckey}.coachToBeJudge`, name: `${ckey}coachToBeJudge`, zh: `${keyZh}-教练成为裁判` },
+  { method: 'post', path: `${rkey}/utbj`, controller: `${ckey}.userToBeJudge`, name: `${ckey}userToBeJudge`, zh: `${keyZh}-用户成为裁判` },
   { method: 'post', path: `${rkey}/login`, controller: `${ckey}.login`, name: `${ckey}login`, zh: `${keyZh}-登陆` },
   { method: 'post', path: `${rkey}/bindJudge`, controller: `${ckey}.bindJudge`, name: `${ckey}bindJudge`, zh: `${keyZh}-绑定裁判` },
   { method: 'get', path: `${rkey}`, controller: `${ckey}.index`, name: `${ckey}Query`, zh: `${keyZh}列表查询` },