Browse Source

新增私人聊天记录表、聊天房间表

reloaded 5 years ago
parent
commit
3b81a9c0d6

+ 43 - 0
app/controller/.personchat.js

@@ -0,0 +1,43 @@
+module.exports = {
+  create: {
+    requestBody: [
+      "!sender_id",
+      "!receiver_id",
+      "!content",
+      "!personroom_id",
+      "status",
+    ],
+  },
+  destroy: {
+    params: ["!id"],
+    service: "delete",
+  },
+  update: {
+    params: ["!id"],
+    requestBody: [
+      "sender_id",
+      "receiver_id",
+      "content",
+      "personroom_id",
+      "status",
+    ],
+  },
+  show: {
+    parameters: {
+      params: ["!id"],
+    },
+    service: "fetch",
+  },
+  index: {
+    parameters: {
+      query: ["sender_id", "receiver_id", "content", "personroom_id", "status"],
+    },
+    service: "query",
+    options: {
+      query: ["skip", "limit"],
+      sort: ["meta.createdAt"],
+      desc: true,
+      count: true,
+    },
+  },
+};

+ 32 - 0
app/controller/.personroom.js

@@ -0,0 +1,32 @@
+//弃用
+module.exports = {
+  create: {
+    requestBody: ["!buyer_id", "!seller_id", "!buyer_name", "!seller_name"],
+  },
+  destroy: {
+    params: ["!id"],
+    service: "delete",
+  },
+  update: {
+    params: ["!id"],
+    requestBody: ["buyer_id", "seller_id", "buyer_name", "seller_name"],
+  },
+  show: {
+    parameters: {
+      params: ["!id"],
+    },
+    service: "fetch",
+  },
+  index: {
+    parameters: {
+      query: ["buyer_id", "seller_id", "buyer_name", "seller_name"],
+    },
+    service: "query",
+    options: {
+      query: ["skip", "limit"],
+      sort: ["meta.createdAt"],
+      desc: true,
+      count: true,
+    },
+  },
+};

+ 16 - 0
app/controller/personchat.js

@@ -0,0 +1,16 @@
+'use strict';
+
+// const _ = require('lodash');
+const meta = require("./.personchat.js");
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose/lib/controller');
+
+// 私人聊天表管理
+class PersonchatController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.personchat;
+  }
+}
+
+module.exports = CrudController(PersonchatController, meta);

+ 16 - 0
app/controller/personroom.js

@@ -0,0 +1,16 @@
+'use strict';
+// 弃用
+// const _ = require('lodash');
+const meta = require('./.personroom.js');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose/lib/controller');
+
+// 房间表管理
+class PersonroomController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.room;
+  }
+}
+
+module.exports = CrudController(PersonroomController, meta);

+ 19 - 0
app/model/personchat.js

@@ -0,0 +1,19 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
+
+const Personchat = {
+  sender_id: { type: String, required: true, maxLength: 200 }, // 发送人id
+  receiver_id: { type: String, required: true, maxLength: 200 }, // 接收人id
+  personroom_id: { type: String, required: true, maxLength: 200 }, //聊天房间id
+  content: { type: String, required: true, maxLength: 1000 }, // 发言内容
+  status: { type: String, required: false, maxLength: 100, default: "0" }, // 状态,0-未读,1-已读
+};
+const schema = new Schema(Personchat, { toJSON: { virtuals: true } });
+schema.index({ id: 1 });
+schema.plugin(metaPlugin);
+
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model("Personchat", schema, "person_chat");
+};

+ 19 - 0
app/model/personroom.js

@@ -0,0 +1,19 @@
+'use strict';
+// 弃用
+const Schema = require('mongoose').Schema;
+const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
+
+const Personroom = {
+  buyer_id: { type: String, required: true, maxLength: 200 }, // 买家id
+  seller_id: { type: String, required: true, maxLength: 200 }, // 卖家id
+  buyer_name: { type: String, required: true, maxLength: 200 }, // 买家名称
+  seller_name: { type: String, required: true, maxLength: 200 }, // 卖家名称
+};
+const schema = new Schema(Personroom, { toJSON: { virtuals: true } });
+schema.index({ id: 1 });
+schema.plugin(metaPlugin);
+
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model("personroom", schema, "person_room");
+};

+ 8 - 0
app/router.js

@@ -58,4 +58,12 @@ module.exports = app => {
   // 信息表设置路由
   router.resources('news', '/api/live/news', controller.news); // index、create、show、destroy
   router.post('news', '/api/live/news/update/:id', controller.news.update);
+
+   // 私人聊天室表设置路由
+  router.resources('personroom', '/api/live/personroom', controller.personroom); // index、create、show、destroy
+  router.post('personroom', '/api/live/personroom/update/:id', controller.personroom.update);
+
+     // 私人聊天记录表设置路由
+  router.resources('personchat', '/api/live/personchat', controller.personchat); // index、create、show、destroy
+  router.post('personchat', '/api/live/personchat/update/:id', controller.personchat.update);
 };

+ 47 - 0
app/service/personchat.js

@@ -0,0 +1,47 @@
+'use strict';
+
+const assert = require('assert');
+// const _ = require('lodash');
+// const { ObjectId } = require('mongoose').Types;
+const { CrudService } = require('naf-framework-mongoose/lib/service');
+// const { BusinessError, ErrorCode } = require('naf-core').Error;
+const moment = require('moment');
+class PersonchatService extends CrudService {
+  constructor(ctx) {
+    super(ctx, "chat");
+    this.model = this.ctx.model.Chat;
+  }
+  async create(query, { sender_id, receiver_id, content, personroom_id }) {
+    assert(sender_id, "缺少发送人信息");
+    assert(content, "缺少发言内容");
+    assert(receiver_id, "缺少接收人信息");
+    assert(personroom_id, "缺少聊天房间信息");
+    const res = await this.model.create({
+      sender_id,
+      receiver_id,
+      content,
+      personroom_id,
+    });
+    // TODO MQ
+    const { mq } = this.ctx;
+    if (mq) {
+      const exchange = "person_chat";
+      const parm = {
+        durable: true,
+        headers: {
+          sender_id: sender_id,
+          receiver_id: receiver_id,
+        },
+      };
+      await mq.topic(
+        exchange,
+        personroom_id + "_" + receiver_id,
+        JSON.stringify(res),
+        parm
+      );
+    }
+    return res;
+  }
+}
+
+module.exports = PersonchatService;

+ 16 - 0
app/service/personroom.js

@@ -0,0 +1,16 @@
+'use strict';
+
+// const assert = require('assert');
+// const _ = require('lodash');
+// const { ObjectId } = require('mongoose').Types;
+const { CrudService } = require('naf-framework-mongoose/lib/service');
+// const { BusinessError, ErrorCode } = require('naf-core').Error;
+
+class PersonroomService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'personroom');
+    this.model = this.ctx.model.Personroom;
+  }
+}
+
+module.exports = PersonroomService;