From ad8477d3ee82a3fffd5de4cd60a237c9ee6b1fb7 Mon Sep 17 00:00:00 2001
From: quanwei <419654421@qq.com>
Date: Wed, 04 Mar 2026 14:27:05 +0800
Subject: [PATCH] 后台添加供需求发布

---
 admin/app/api/model/plus/release/Chat.php |   34 ++++++++++++++++++++++++++--------
 1 files changed, 26 insertions(+), 8 deletions(-)

diff --git a/admin/app/api/model/plus/release/Chat.php b/admin/app/api/model/plus/release/Chat.php
index 50bbca1..ea799fa 100644
--- a/admin/app/api/model/plus/release/Chat.php
+++ b/admin/app/api/model/plus/release/Chat.php
@@ -24,25 +24,43 @@
     public function myList($user)
     {
         $ChatRelation = new ChatRelation();
-        $list = $ChatRelation->with(['user'])
+        $list = $ChatRelation
             ->where(['user_id' => $user['user_id']])
             ->whereOr(['other_user_id' => $user['user_id']])
             ->order('update_time desc')
             ->select();
-        foreach ($list as $key => &$value) {
-            $where['relation_id'] = $value['relation_id'];
-            if($value['user_id'] == $user['user_id']){
-                $where['user_id']= $other_user_id = $value['other_user_id'];
-            }else{
-                $where['user_id']= $other_user_id  = $value['user_id'];
+
+        // 过滤掉无效的关系记录(即user_id和other_user_id相同的记录)
+        $validList = [];
+        foreach ($list as $value) {
+            if ($value['user_id'] != $value['other_user_id']) {
+                $validList[] = $value;
             }
+        }
+
+        foreach ($validList as $key => &$value) {
+            $where['relation_id'] = $value['relation_id'];
+            // 确定显示的用户ID:应该是与自己不同的另一个用户
+            if($value['user_id'] == $user['user_id']){
+                // 当前记录是从当前用户角度创建的
+                $where['user_id'] = $other_user_id = $value['other_user_id'];
+            } else {
+                // 当前记录是从对方角度创建的,对方是user_id
+                $where['user_id'] = $other_user_id = $value['user_id'];
+            }
+
+            // 获取未读消息数量
             $where['status'] = 0;
             $value['num'] = $this->where($where)->count();
             unset($where['status']);
+
+            // 获取最新消息
             $value['newMessage'] = $this->where($where)->order('chat_id desc')->field('content,create_time')->find();
+
+            // 获取对方用户信息
             $value['user'] = UserModel::detail($other_user_id);
         }
-        return $list;
+        return $validList;
     }
 
     //获取聊天信息

--
Gitblit v1.9.2