diff --git a/app/chat/controller/MessageController.php b/app/chat/controller/MessageController.php index 4aa1366..226cabe 100644 --- a/app/chat/controller/MessageController.php +++ b/app/chat/controller/MessageController.php @@ -183,10 +183,43 @@ class MessageController extends ApiController } $list = Redis::get('usermsg:list:' . $this->user_type . ':' . $this->uid); if($list) { - return $this->success(['list' => json_decode($list, true)]); + $data = json_decode($list, true); }else { - return $this->success(['list' => ChatMessage::getconverlist($this->uid, $this->user_type)]); + $data = ChatMessage::getconverlist($this->uid, $this->user_type); } + // 最新一条消息 + foreach ($data as $k => $session) { + $lastMsg = ''; + $unreadCount = 0; + if(!empty($session['session_id'])) { + // 最后一条消息 + $lastMsg = Db::name('chat_message') + ->where('session_id', $session['session_id']) + ->order('send_time', 'desc') + ->find(); + // 未读计数 + $unreadCount = Db::name('chat_unread_count')->where(['user_id' => $this->uid, 'session_id' => $session['session_id']])->value('count')?:0; + } + if($session['chat_type'] == 2) { + if($lastMsg) { + $chat_user = Db::name('chat_user')->where(['user_id' => $lastMsg['from_id']])->field('nick_name,avatar')->find(); + } + } + $msg = ''; + if($lastMsg) { + // 如果是自己发的不显示发送人 + if($lastMsg['from_id'] == $this->uid) { + $msg = $lastMsg['content']; + }else { + // 如果是别人发的显示昵称 + $msg = $chat_user['nick_name'] . ':' . $lastMsg['content']; + } + $data[$k]['send_time'] = formatWeChatTime($lastMsg['send_time']); + } + $data[$k]['msg'] = $msg; + $data[$k]['unread_count'] = $unreadCount; + } + return $this->success(['list' => $data]); } /** diff --git a/app/chat/model/ChatMessage.php b/app/chat/model/ChatMessage.php index 4d684f3..63c59dd 100644 --- a/app/chat/model/ChatMessage.php +++ b/app/chat/model/ChatMessage.php @@ -32,81 +32,81 @@ class ChatMessage extends BaseModel } $sessionIds = array_merge($sessionIds, $group_arr); $group_id = ''; - $i = 0; foreach ($sessionIds as $k => $session) { - if($session['session_id']) { - // 未读计数 - $unreadCount = Db::name('chat_unread_count')->where(['user_id' => $uid, 'session_id' => $session['session_id']])->value('count')?:0; - }else { - $unreadCount = 0; - } - $data[$i]['unread_count'] = $unreadCount; +// if($session['session_id']) { +// // 未读计数 +// $unreadCount = Db::name('chat_unread_count')->where(['user_id' => $uid, 'session_id' => $session['session_id']])->value('count')?:0; +// }else { +// $unreadCount = 0; +// } +// $data[$k]['unread_count'] = $unreadCount; - if(!empty($session['session_id'])) { - // 最后一条消息 - $lastMsg = Db::name('chat_message') - ->where('session_id', $session['session_id']) - ->order('send_time', 'desc') - ->find(); - }else { - $lastMsg = ''; - } +// if(!empty($session['session_id'])) { +// // 最后一条消息 +// $lastMsg = Db::name('chat_message') +// ->where('session_id', $session['session_id']) +// ->order('send_time', 'desc') +// ->find(); +// }else { +// $lastMsg = ''; +// } if($session['chat_type'] == 1) { $chat_user = Db::name('chat_user')->where(['user_id' => $session['group_id']])->field('nick_name,avatar')->find(); $avatar = $chat_user['avatar']; $name = $chat_user['nick_name']; $group_id = ''; - $data[$i]['user_id'] = $session['group_id']; + $data[$k]['user_id'] = $session['group_id']; }elseif($session['chat_type'] == 2) { $group_info = Db::name('chat_group')->where(['id' => $session['group_id']])->field('id,name,avatar')->find(); $avatar = $group_info['avatar']; $group_id = $group_info['id']; $name = $group_info['name']; - if($lastMsg) { - $chat_user = Db::name('chat_user')->where(['user_id' => $lastMsg['from_id']])->field('nick_name,avatar')->find(); - } - $data[$i]['user_id'] = $group_id; +// if($lastMsg) { +// $chat_user = Db::name('chat_user')->where(['user_id' => $lastMsg['from_id']])->field('nick_name,avatar')->find(); +// } + $data[$k]['user_id'] = $group_id; } // 单聊的话 两个人对话用一样的会话ID - $msg = ''; - if($lastMsg) { - // 如果是自己发的不显示发送人 - if($lastMsg['from_id'] == $uid) { - $msg = $lastMsg['content']; - }else { - // 如果是别人发的显示昵称 - $msg = $chat_user['nick_name'] . ':' . $lastMsg['content']; - } - $data[$i]['send_time'] = formatWeChatTime($lastMsg['send_time']); - } - $data[$i]['avatar'] = $avatar; - $data[$i]['chat_type'] = $session['chat_type']; - $data[$i]['session_id'] = $session['session_id']; - $data[$i]['name'] = $name; - $data[$i]['msg'] = $msg; - $data[$i]['group_id'] = $group_id; - $i ++; +// $msg = ''; +// if($lastMsg) { +// // 如果是自己发的不显示发送人 +// if($lastMsg['from_id'] == $uid) { +// $msg = $lastMsg['content']; +// }else { +// // 如果是别人发的显示昵称 +// $msg = $chat_user['nick_name'] . ':' . $lastMsg['content']; +// } +// $data[$k]['send_time'] = formatWeChatTime($lastMsg['send_time']); +// } + $data[$k]['avatar'] = $avatar; + $data[$k]['chat_type'] = $session['chat_type']; + $data[$k]['session_id'] = $session['session_id']; + $data[$k]['name'] = $name; +// $data[$k]['msg'] = $msg; + $data[$k]['group_id'] = $group_id; + $data[$k]['is_del'] = 0; } - Redis::setEx('usermsg:list:' . $user_type . ':' . $uid, 30 * 86400, json_encode($data)); + Redis::set('usermsg:list:' . $user_type . ':' . $uid, json_encode($data)); } return $data; } // 更新会话列表 - public static function updateConverList($uid, $user_type, $session_id) + public static function updateConverList($uid, $user_type, $group_id) { - $is_update = false; + $is_update = 'ok_up'; $list_json = Redis::get('usermsg:list:' . $user_type . ':' . $uid); if($list_json) { $list_arr = json_decode($list_json, true); foreach ($list_arr as $k => $list) { - if($list['session_id'] == $session_id) { - $is_update = true; + // 如果找到会话列表并且没有删除,则不更新 + if($list['group_id'] == $group_id && $list['is_del'] == 0) { + $is_update = 'no_up'; } } } - if($is_update) { + if($is_update == 'ok_up') { self::getconverlist($uid, $user_type); } } diff --git a/extend/chat/model/Base.php b/extend/chat/model/Base.php index 1763cf9..25838c1 100644 --- a/extend/chat/model/Base.php +++ b/extend/chat/model/Base.php @@ -283,7 +283,7 @@ namespace extend\chat\model; } } // 更新对方会话列表 - ChatMessage::updateConverList($grep['user_id'], $user_type, $session_id); + ChatMessage::updateConverList($grep['user_id'], $user_type, $group_id); } } }