Browse Source

增加查询接口

liuyu 5 năm trước cách đây
mục cha
commit
0c8ae6a115

+ 24 - 0
app/controller/personroomtalk.js

@@ -0,0 +1,24 @@
+'use strict';
+// 弃用
+// const _ = require('lodash');
+const Controller = require('egg').Controller;
+
+// 房间表管理
+class PersonroomtalkController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.personroomtalk;
+  }
+
+  async delete() {
+    const res = await this.service.delete(this.ctx.params);
+    this.ctx.ok({ res });
+  }
+
+  async countroom() {
+    const res = await this.service.countroom(this.ctx.query);
+    this.ctx.ok({ total: res });
+  }
+}
+
+module.exports = PersonroomtalkController;

+ 1 - 1
app/model/personchat.js

@@ -10,7 +10,7 @@ const Personchat = {
   personroom_id: { type: String, required: true, maxLength: 200 }, // 聊天房间id
   content: { type: String, required: true, maxLength: 1000 }, // 发言内容
   send_time: { type: String, required: true, maxLength: 100 }, // 发言时间:年月日时分秒
-  status: { type: String, required: false, maxLength: 100, default: "0" }, // 状态,0-未读,1-已读
+  status: { type: String, required: false, maxLength: 100, default: '0' }, // 状态,0-未读,1-已读
 };
 const schema = new Schema(Personchat, { toJSON: { virtuals: true } });
 schema.index({ id: 1 });

+ 17 - 0
app/model/personroomtalk.js

@@ -0,0 +1,17 @@
+'use strict';
+// 弃用
+const Schema = require('mongoose').Schema;
+const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
+
+const Personroomtalk = {
+  pr_id: { type: String, required: true, maxLength: 200 }, // 房间id
+  type: { type: String, required: false, maxLength: 200 }, // 类型
+};
+const schema = new Schema(Personroomtalk, { toJSON: { virtuals: true } });
+schema.index({ id: 1 });
+schema.plugin(metaPlugin);
+
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('Personroomtalk', schema, 'person_room_talk');
+};

+ 3 - 0
app/router.js

@@ -67,4 +67,7 @@ module.exports = app => {
   router.resources('personchat', '/api/live/personchat', controller.personchat); // index、create、show、destroy
   router.post('personchat', '/api/live/personchat/update/:id', controller.personchat.update);
   router.post('personchat', '/api/live/personchat/received', controller.personchat.received); // 将消息设置成已读
+
+  router.delete('/api/live/personroomtalk/:id', controller.personroomtalk.delete);
+  router.get('/api/live/personroomtalk/countroom', controller.personroomtalk.countroom);
 };

+ 7 - 1
app/service/personroom.js

@@ -10,6 +10,7 @@ class PersonroomService extends CrudService {
   constructor(ctx) {
     super(ctx, 'personroom');
     this.model = this.ctx.model.Personroom;
+    this.tmodel = this.ctx.model.Personroomtalk;
   }
 
   async create(query, { buyer_id, seller_id, buyer_name, seller_name }) {
@@ -21,7 +22,12 @@ class PersonroomService extends CrudService {
     let res = await this.model.findOne({ buyer_id, seller_id });
     if (!res) res = await this.model.create({ buyer_id, seller_id, buyer_name, seller_name });
     // TODO MQ
-    //这个地方不应该有MQ;买家在请求完房间号后订阅MQ;卖家应该在自己进入中台管理时订阅MQ,且订阅的应该是这个人是否有未读消息
+    const talk = await this.tmodel.findOne({ pr_id: res.id });
+    if (!talk) {
+      const newdata = { pr_id: res.id, type: '0' };
+      await this.tmodel.create(newdata);
+    }
+    // 这个地方不应该有MQ;买家在请求完房间号后订阅MQ;卖家应该在自己进入中台管理时订阅MQ,且订阅的应该是这个人是否有未读消息
     const { mq } = this.ctx;
     if (mq) {
       const exchange = 'person_room';

+ 26 - 0
app/service/personroomtalk.js

@@ -0,0 +1,26 @@
+'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, 'person_room_talk');
+    this.model = this.ctx.model.Personroomtalk;
+  }
+
+  async delete(params) {
+    const talk = await this.model.findOne({ pr_id: params.id });
+    return talk.delete();
+  }
+
+  async countroom(query) {
+    const total = await this.model.count({ pr_id: query.qr_id });
+    return total;
+  }
+}
+
+module.exports = PersonroomService;