Browse Source

用户,及绑定裁判

lrf 2 years ago
parent
commit
adff9ae6bf
6 changed files with 143 additions and 0 deletions
  1. 44 0
      app/controller/config/.user.js
  2. 13 0
      app/controller/user.js
  3. 33 0
      app/model/race/user.js
  4. 1 0
      app/router.js
  5. 32 0
      app/service/user.js
  6. 20 0
      app/z_router/user.js

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

@@ -0,0 +1,44 @@
+module.exports = {
+  create: {
+    requestBody: ['openid', 'user_id', 'type', 'parent_id'],
+  },
+  destroy: {
+    params: ['!id'],
+    service: 'delete',
+  },
+  update: {
+    params: ['!id'],
+    requestBody: ['openid', 'user_id', 'type', 'parent_id'],
+  },
+  show: {
+    parameters: {
+      params: ['!id'],
+    },
+    service: 'fetch',
+  },
+  index: {
+    parameters: {
+      query: {
+        'meta.createdAt@start': 'meta.createdAt@start',
+        'meta.createdAt@end': 'meta.createdAt@end',
+        openid: 'openid',
+        user_id: 'user_id',
+        type: 'type',
+        parent_id: 'parent_id',
+      },
+      // options: {
+      //   "meta.state": 0 // 默认条件
+      // },
+    },
+    service: 'query',
+    options: {
+      query: ['skip', 'limit'],
+      sort: ['meta.createdAt'],
+      desc: true,
+      count: true,
+    },
+  },
+  bindJudge: {
+    requestBody: ['openid', 'user_id', 'type', 'parent_id'],
+  },
+};

+ 13 - 0
app/controller/user.js

@@ -0,0 +1,13 @@
+'use strict';
+const meta = require('./config/.user.js');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose-free/lib/controller');
+
+// 
+class UserController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.user;
+  }
+}
+module.exports = CrudController(UserController, meta);

+ 33 - 0
app/model/race/user.js

@@ -0,0 +1,33 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
+
+// 用户表
+const user = {
+  openid: { type: String, required: false, zh: '微信id' }, //
+  user_id: { type: String, required: false, zh: '用户id', ref: 'base.User' }, //
+  type: { type: String, required: false, default: '0', zh: '用户类别' }, // 0:普通用户;-1:超级管理员;1比赛管理员;2裁判
+  parent_id: { type: String, required: false, zh: '所属id' }, // 裁判需要填写管理员的数据id
+};
+const schema = new Schema(user, { toJSON: { getters: true, virtuals: true } });
+schema.index({ id: 1 });
+schema.index({ 'meta.createdAt': 1 });
+schema.index({ openid: 1 });
+schema.index({ user_id: 1 });
+schema.index({ type: 1 });
+schema.index({ parent_id: 1 });
+
+schema.plugin(metaPlugin);
+const source = 'race';
+module.exports = app => {
+  const is_multiple = app.mongooseDB.clients;
+  let model;
+  if (is_multiple) {
+    const conn = app.mongooseDB.get(source);
+    model = conn.model('User', schema, 'user');
+  } else {
+    const { mongoose } = app;
+    model = mongoose.model('User', schema, 'user');
+  }
+  return model;
+};

+ 1 - 0
app/router.js

@@ -23,6 +23,7 @@ module.exports = app => {
   const ipAddress = getIPAdress();
   console.log(`前缀:http://${ipAddress}:${cluster.listen.port}${routePrefix}`);
   router.get('/', controller.home.index);
+  require('./z_router/user')(app); // 比赛模块用户
   require('./z_router/match')(app); // 比赛信息
   require('./z_router/matchGroup')(app); // 比赛组别
   require('./z_router/matchProject')(app); // 组别项目

+ 32 - 0
app/service/user.js

@@ -0,0 +1,32 @@
+'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 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;

+ 20 - 0
app/z_router/user.js

@@ -0,0 +1,20 @@
+'use strict';
+// 路由配置
+const path = require('path');
+const regPath = path.resolve('app', 'public', 'routerRegister');
+const routerRegister = require(regPath);
+const rkey = 'user';
+const ckey = 'user';
+const keyZh = '比赛用户';
+const routes = [
+  { 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}列表查询` },
+  { method: 'get', path: `${rkey}/:id`, controller: `${ckey}.show`, name: `${ckey}Show`, zh: `${keyZh}查询` },
+  { method: 'post', path: `${rkey}`, controller: `${ckey}.create`, name: `${ckey}Create`, zh: `创建${keyZh}` },
+  { method: 'post', path: `${rkey}/:id`, controller: `${ckey}.update`, name: `${ckey}Update`, zh: `修改${keyZh}` },
+  { method: 'delete', path: `${rkey}/:id`, controller: `${ckey}.destroy`, name: `${ckey}Delete`, zh: `删除${keyZh}` },
+];
+
+module.exports = app => {
+  routerRegister(app, routes, keyZh, rkey, ckey);
+};