lrf402788946 4 years ago
parent
commit
28895d9ade
5 changed files with 265 additions and 0 deletions
  1. 93 0
      app/controller/.trainlive.js
  2. 13 0
      app/controller/trainlive.js
  3. 47 0
      app/model/trainlive.js
  4. 9 0
      app/router.js
  5. 103 0
      app/service/trainlive.js

+ 93 - 0
app/controller/.trainlive.js

@@ -0,0 +1,93 @@
+module.exports = {
+  create: {
+    requestBody: [
+      "title",
+      "province",
+      "city",
+      "place",
+      "sponsor",
+      "brief",
+      "user",
+      "phone",
+      "video_data",
+      "user_data",
+      "create_date",
+    ],
+  },
+  destory: {
+    params: ["!id"],
+    service: "delete",
+  },
+  update: {
+    params: ["!id"],
+    requestBody: [
+      "title",
+      "province",
+      "city",
+      "place",
+      "sponsor",
+      "brief",
+      "user",
+      "phone",
+      "video_data",
+      "user_data",
+      "create_date",
+    ],
+  },
+  show: {
+    parameters: {
+      params: ["!id"],
+    },
+    service: "fetch",
+  },
+  index: {
+    parameters: {
+      query: {
+        title: "title",
+        province: "province",
+        city: "city",
+        place: "place",
+        sponsor: "sponsor",
+        brief: "brief",
+        user: "user",
+        phone: "phone",
+        "create_time@start": "create_time@start",
+        "create_time@end": "create_time@end",
+      },
+      // options: {
+      //   "meta.status": 0 // 默认条件
+      // },
+    },
+    service: "query",
+    options: {
+      query: ["skip", "limit"],
+      sort: ["meta.createdAt"],
+      desc: true,
+      count: true,
+    },
+  },
+  addUser: {
+    params: ["!id"],
+    requestBody: ["users"],
+    service: "addUser",
+  },
+  updateUser: {
+    params: ["!id"],
+    requestBody: ["users"],
+    service: "updateUser",
+  },
+  deleteUser: {
+    params: ["!id"],
+    requestBody: ["users"],
+    service: "deleteUser",
+  },
+  login: {
+    params: ["!id"],
+    requestBody: ["user_phone", "user_password"],
+    service: "login",
+  },
+  logout: {
+    params: ["!id"],
+    service: "logout",
+  },
+};

+ 13 - 0
app/controller/trainlive.js

@@ -0,0 +1,13 @@
+'use strict';
+const meta = require('./.trainlive.js');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose/lib/controller');
+
+// 培训问诊
+class TrainliveController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.trainlive;
+  }
+}
+module.exports = CrudController(TrainliveController, meta);

+ 47 - 0
app/model/trainlive.js

@@ -0,0 +1,47 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const moment = require('moment');
+const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
+
+const video = new Schema({
+  video_title: { type: String, maxLength: 500 }, // 标题
+  video_date: { type: String, maxLength: 500 }, // 时间
+  video_url: { type: String, required: true, maxLength: 500 }, // 视频路径
+});
+const user = new Schema({
+  user_title: { type: String, maxLength: 500 }, // 用户名
+  user_phone: { type: String, maxLength: 500, unique: true }, // 账号
+  user_password: { type: String, required: true, maxLength: 500 }, // 密码
+});
+// 培训问诊表
+const trainlive = {
+  room_id: { type: String, required: true, unique: true }, // 房间号
+  password: { type: String }, // 密码
+  title: { type: String }, // 标题
+  province: { type: String }, // 省份
+  city: { type: String }, // 城市
+  place: { type: String }, // 市区
+  sponsor: { type: String }, // 主办方
+  brief: { type: String }, // 简介
+  user: { type: String }, // 负责人
+  phone: { type: String }, // 负责人手机号
+  video_data: { type: [ video ] }, // 视频
+  user_data: { type: [ user ] }, // 参加用户
+  create_date: { type: String }, // 开始时间
+
+  remark: { type: String, maxLength: 200 },
+  create_time: { type: String, default: moment().format('YYYY-MM-DD HH:mm:ss') },
+};
+const schema = new Schema(trainlive, { toJSON: { virtuals: true } });
+schema.index({ id: 1 });
+schema.index({ room_id: 1 });
+schema.index({ title: 1 });
+schema.index({ province: 1 });
+schema.index({ city: 1 });
+schema.index({ user: 1 });
+schema.index({ phone: 1 });
+schema.plugin(metaPlugin);
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('Train_live', schema, 'train_live');
+};

+ 9 - 0
app/router.js

@@ -114,4 +114,13 @@ module.exports = app => {
   router.resources('imgtxtdock', '/api/live/imgtxtdock', controller.imgtxtdock); // index、create、show、destroy
   router.post('imgtxtdock', '/api/live/imgtxtdock/update/:id', controller.imgtxtdock.update);
 
+  // 培训问诊表
+  router.post('trainlive', '/api/live/trainlive/login/:id', controller.trainlive.login);
+  router.post('trainlive', '/api/live/trainlive/logout/:id', controller.trainlive.logout);
+  router.post('trainlive', '/api/live/trainlive/user/:id', controller.trainlive.addUser);
+  router.delete('trainlive', '/api/live/trainlive/user/:id', controller.trainlive.deleteUser);
+  router.post('trainlive', '/api/live/trainlive/user/update/:id', controller.trainlive.updateUser);
+  router.post('trainlive', '/api/live/trainlive/update/:id', controller.trainlive.update);
+  router.resources('trainlive', '/api/live/trainlive', controller.trainlive); // index、create、show、destroy
+
 };

+ 103 - 0
app/service/trainlive.js

@@ -0,0 +1,103 @@
+'use strict';
+const { CrudService } = require('naf-framework-mongoose/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+const { ObjectId } = require('mongoose').Types;
+const assert = require('assert');
+const _ = require('lodash');
+// 培训问诊表
+class TrainliveService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'trainlive');
+    this.model = this.ctx.model.Trainlive;
+  }
+  /**
+   * 重写创建,room_id自动生成
+   * @param {Object} body 数据
+   */
+  async create(body) {
+    const last = await this.model.findOne().sort({ room_id: -1 });
+    let room_id = '3001',
+      password = '3001';
+    if (last) {
+      room_id = parseInt(_.get(last, 'room_id', '3000')) + 1;
+      password = room_id;
+    }
+    body.room_id = room_id;
+    body.password = password;
+    return await this.model.create(body);
+  }
+
+  /**
+   * 为培训问诊添加参加用户
+   * @param {Object} {id} 培训问诊的数据id
+   * @param {Array} {users} 培训问诊的参加用户
+   */
+  async addUser({ id }, { users }) {
+    const object = await this.model.findById(id);
+    if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定的培训问诊信息!');
+    object.user_data.push(...users);
+    await object.save();
+  }
+
+  /**
+   * 更改培训问诊下指定用户的数据
+   * @param {Object} {id} 培训问诊的数据id
+   * @param {Array} {users} 用户的数据
+   */
+  async updateUser({ id }, { users }) {
+    const object = await this.model.findById(id);
+    if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定的培训问诊信息!');
+    for (const user of users) {
+      const { _id, ...info } = user;
+      if (_id) {
+        // 存在_id,修改
+        const oldData = object.user_data.id(ObjectId(_id));
+        if (!oldData) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到培训问诊下指定的参会人员信息!');
+        const keys = Object.keys(info);
+        for (const key of keys) {
+          oldData[key] = info[key];
+        }
+      }
+    }
+    await object.save();
+  }
+
+  /**
+   *移除培训问诊下指定用户
+   * @param {Object} {id} 培训问诊的数据id
+   * @param {Object} {users} 用户的数据id集合
+   */
+  async deleteUser({ id }, { users }) {
+    const object = await this.model.findById(id);
+    if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定的培训问诊信息!');
+    object.user_data = object.user_data.filter(f => !users.find(uf => ObjectId(uf).equals(f._id)));
+    await object.save();
+  }
+  /**
+   * 参会人员登陆
+   * @param {Object} {id} 培训问诊的数据id
+   * @param {Object} user 参会人员的信息,手机号和密码
+   */
+  async login({ id }, { user_phone, user_password }) {
+    assert(user_phone, '缺少登陆的参会人员 手机号');
+    assert(user_password, '缺少登陆的参会人员 密码');
+    const object = await this.model.findById(id);
+    if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定的培训问诊信息!');
+    const user = object.user_data.find(f => f.user_phone === user_phone);
+    if (!user) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到培训问诊下该手机号的用户信息!');
+    const is_login = await this.app.redis.get(`trainlive/${user._id}`);
+    if (is_login) throw new BusinessError(ErrorCode.BUSINESS, '用户已登录');
+    if (user.user_password !== user_password) throw new BusinessError(ErrorCode.BAD_PASSWORD, '用户密码错误!');
+    await this.app.redis.set(`trainlive/${user._id}`, user);
+  }
+
+  /**
+   * 参会人员注销
+   * @param {String} {id} 参会人员id
+   */
+  async logout({ id }) {
+    await this.app.redis.del(`trainlive/${id}`);
+  }
+}
+
+module.exports = TrainliveService;