ruifeng_liu 3 年之前
父节点
当前提交
8567eb46a2
共有 3 个文件被更改,包括 34 次插入3 次删除
  1. 8 0
      app/controller/patent/.patentchat.js
  2. 1 0
      app/router/patent/patentchat.js
  3. 25 3
      app/service/patent/patentchat.js

+ 8 - 0
app/controller/patent/.patentchat.js

@@ -51,4 +51,12 @@ module.exports = {
       count: true,
     },
   },
+  chatPerson: {
+    parameters: {
+      query: {
+        id: "id",
+      },
+    },
+    service: "getChatList",
+  },
 };

+ 1 - 0
app/router/patent/patentchat.js

@@ -7,6 +7,7 @@ module.exports = app => {
   const vision = 'v0';
   const index = 'patent';
   const target = 'patentchat';
+  router.get(target, `${profix}${vision}/${index}/${target}/chatPerson`, controller[index][target].chatPerson);
   router.resources(target, `${profix}${vision}/${index}/${target}`, controller[index][target]); // index、create、show、destroy
   router.post(target, `${profix}${vision}/${index}/${target}/update/:id`, controller[index][target].update);
 };

+ 25 - 3
app/service/patent/patentchat.js

@@ -13,9 +13,10 @@ class PatentchatService extends CrudService {
     this.model = this.ctx.model.Patent.Patentchat;
   }
 
-  async query({ id }) {
+  async query({ id }, { skip = 0, limit = 0 } = {}) {
     assert(id, '缺少用户信息');
-    const data = await this.model.find({ $or: [{ sender_id: id }, { receiver_id: id }] }).sort({ 'meta.createdAt': 1 });
+    const data = await this.model.find({ $or: [{ sender_id: id }, { receiver_id: id }] }).sort({ 'meta.createdAt': 1 }).skip(parseInt(skip))
+      .limit(parseInt(limit));
     return data;
   }
   async count({ id }) {
@@ -25,7 +26,28 @@ class PatentchatService extends CrudService {
   }
 
   async getChatList({ id }) {
-
+    const data = await this.model.aggregate([
+      { $match: { $or: [{ sender_id: ObjectId(id) }, { receiver_id: ObjectId(id) }] } },
+    ]);
+    const obj = {};
+    for (const i of data) {
+      const { sender_id, receiver_id } = i;
+      let getArrId = '';
+      if (ObjectId(sender_id).equals(id)) getArrId = receiver_id;
+      else getArrId = sender_id;
+      let midArr = _.get(obj, getArrId, []);
+      console.log(getArrId, midArr);
+      midArr = [ ...midArr, i ];
+      obj[getArrId] = midArr;
+    }
+    const rData = [];
+    for (const key in obj) {
+      let arr = obj[key];
+      arr = _.orderBy(arr, [ 'send_time' ], [ 'desc' ]);
+      const head = _.head(arr);
+      rData.push(head);
+    }
+    return rData;
   }
 }