getUser(); $userId = $user['user_id']; $conversationId = $this->request->post('conversation_id/d', 0); $businessCardId = $this->request->post('business_card_id/d', 0); $content = $this->request->post('content/s', ''); if (empty($content)) { return $this->renderError('消息内容不能为空'); } // 如果没有会话ID,根据名片ID创建会话 if (empty($conversationId) && !empty($businessCardId)) { $conversationModel = new ConversationModel(); // 获取名片信息 $businessModel = new BusinessModel(); $businessCard = $businessModel->where('business_card_id', $businessCardId)->find(); if (!$businessCard) { return $this->renderError('名片不存在'); } // 创建会话,参与者包括当前用户和名片所有者 $participants = [$userId, $businessCard->user_id]; $conversationId = $conversationModel->createConversation($businessCardId, $participants); if (!$conversationId) { return $this->renderError('创建会话失败'); } } if (empty($conversationId)) { return $this->renderError('会话ID不能为空'); } $chatModel = new ChatModel(); $result = $chatModel->sendMessage($conversationId, $userId, $content); if ($result) { // 更新会话时间 $conversationModel = new ConversationModel(); $conversationModel->updateConversationTime($conversationId); // 准备WebSocket消息数据 $wsMessage = [ 'type' => 'message', 'user_id' => $userId, 'to_user_id' => 0, // 需要确定接收者ID 'content' => $content, 'conversation_id' => $conversationId, 'business_card_id' => $businessCardId, 'chat_id' => $chatModel->chat_id, 'time' => date('Y-m-d H:i:s') ]; // 确定接收者ID $participantModel = new Participant(); $participants = $participantModel->where('conversation_id', $conversationId) ->where('user_id', '<>', $userId) ->select(); if (!empty($participants)) { $wsMessage['to_user_id'] = $participants[0]['user_id']; } return $this->renderSuccess('发送成功', [ 'message' => $chatModel->toArray(), 'ws_message' => $wsMessage ]); } else { return $this->renderError('发送失败'); } } /** * 获取会话消息列表 */ public function getMessages() { $param=request()->param(); $user = $this->getUser(); $userId = $user['user_id']; if (empty($param['conversation_id']) && !empty($param['business_card_id'])) { $conversationModel = new ConversationModel(); $conversation = $conversationModel->getConversationByBusinessCard($param['business_card_id']); if ($conversation) { $param['conversation_id'] = $conversation['conversation_id']; } } if (empty($param['conversation_id'])) { return $this->renderError('会话不存在'); } $chatModel = new ChatModel(); $messages = $chatModel->getMessages($param); $hasParticipant = Participant::where('conversation_id', $param['conversation_id']) ->where('user_id','<>', $userId) ->find(); $heUser=(new UserModel())->detail($hasParticipant['user_id']); $card=(new BusinessModel())->where('user_id',$hasParticipant['user_id'])->where('is_default',1)->find(); return $this->renderSuccess('获取成功', compact('messages','heUser','card')); } /** * 获取会话列表 */ public function getConversations() { $param=request()->param(); $user = $this->getUser(); $param['user_id'] = $user['user_id']; $conversationModel = new ConversationModel(); $conversations = $conversationModel->getUserConversations($param); // 为每个会话添加未读消息数量 $chatModel = new ChatModel(); foreach ($conversations as &$conversation) { $conversation['unread_count'] = $chatModel->getUnreadCount($conversation['conversation_id'], $param['user_id']); // 获取最后一条消息 if (!empty($conversation['messages'])) { $conversation['last_message'] = end($conversation['messages']); } } return $this->renderSuccess('获取成功', compact('conversations')); } /** * 标记消息为已读 */ public function markAsRead() { $user = $this->getUser(); $userId = $user['user_id']; $param=request()->param(); if (empty($param['chat_id']) && empty($param['conversation_id'])) { return $this->renderError('参数错误'); } $chatModel = new ChatModel(); if (!empty($param['chat_id'])) { // 标记单条消息为已读 $result = $chatModel->markAsRead($param['chat_id'], $userId); $error=$chatModel->getError(); } else { $chatDate=$chatModel->where('conversation_id',$param['conversation_id'])->where('sender_id','<>',$userId)->find(); if (empty($chatDate)){ return $this->renderSuccess('标记成功'); } // 标记整个会话为已读 $participantModel = new Participant(); $result = $participantModel->markConversationAsRead($param['conversation_id'], $chatDate['sender_id']); // 更新会话中所有消息的已读状态 if ($result) { $chatModel->where('conversation_id', $param['conversation_id']) ->where('sender_id', '<>', $userId) ->where('is_read', 0) ->update([ 'is_read' => 1, 'read_time' => time(), 'update_time' => time() ]); } $error=$participantModel->getError(); } if ($result) { return $this->renderSuccess('标记成功'); } else { return $this->renderError('标记失败'.$error); } } /** * 根据名片ID获取或创建会话 */ public function getOrCreateConversation() { $user = $this->getUser(); $userId = $user['user_id']; $businessCardId = $this->request->post('business_card_id/d', 0); if (empty($businessCardId)) { return $this->renderError('名片ID不能为空'); } // 获取名片信息 $businessModel = new BusinessModel(); $businessCard = $businessModel->where('business_card_id', $businessCardId)->find(); if (!$businessCard) { return $this->renderError('名片不存在'); } $conversationModel = new ConversationModel(); // 检查是否已存在会话 $conversation = $conversationModel->getConversationByBusinessCard($businessCardId); if ($businessCard['user_id'] == $userId&&!$conversation) { return $this->renderError('不能与自己建立会话'); } if (!$conversation) { // 创建新会话 $participants = [$userId, $businessCard['user_id']]; $conversationId = $conversationModel->createConversation($businessCardId, $participants); if ($conversationId) { $conversation = $conversationModel->getConversationByBusinessCard($businessCardId); } } if ($conversation) { // 添加未读消息数量 $chatModel = new ChatModel(); $conversation['unread_count'] = $chatModel->getUnreadCount($conversation['conversation_id'], $userId); return $this->renderSuccess('获取成功', compact('conversation')); } else { return $this->renderError('创建会话失败'); } } }