| | |
| | | use app\api\model\plus\business\chat\Chat as ChatModel; |
| | | use app\api\model\plus\business\chat\Conversation as ConversationModel; |
| | | use app\api\model\plus\business\chat\Participant; |
| | | use app\api\model\settings\Setting as SettingModel; |
| | | use app\api\model\user\User as UserModel; |
| | | use app\common\model\plus\business\Business as BusinessModel; |
| | | |
| | |
| | | // 更新会话时间 |
| | | $conversationModel = new ConversationModel(); |
| | | $conversationModel->updateConversationTime($conversationId); |
| | | |
| | | // 获取刚发送的消息 |
| | | $message = $chatModel->with(['sender'])->find($chatModel->chat_id); |
| | | |
| | | return $this->renderSuccess('发送成功', $message ? $message->toArray() : []); |
| | | // 准备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('发送失败'); |
| | | } |
| | |
| | | |
| | | $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']); |
| | | |
| | | $chatData=$chatModel->where('conversation_id',$conversation['conversation_id'])->order('create_time','desc')->find(); |
| | | // 获取最后一条消息 |
| | | if (!empty($conversation['messages'])) { |
| | | $conversation['last_message'] = end($conversation['messages']); |
| | | if (!empty($chatData)) { |
| | | $conversation['last_message'] = $chatData; |
| | | } |
| | | } |
| | | |
| | |
| | | $user = $this->getUser(); |
| | | $userId = $user['user_id']; |
| | | |
| | | $chatId = $this->request->post('chat_id/d', 0); |
| | | $conversationId = $this->request->post('conversation_id/d', 0); |
| | | $param=request()->param(); |
| | | |
| | | if (empty($chatId) && empty($conversationId)) { |
| | | if (empty($param['chat_id']) && empty($param['conversation_id'])) { |
| | | return $this->renderError('参数错误'); |
| | | } |
| | | |
| | | $chatModel = new ChatModel(); |
| | | |
| | | if (!empty($chatId)) { |
| | | if (!empty($param['chat_id'])) { |
| | | // 标记单条消息为已读 |
| | | $result = $chatModel->markAsRead($chatId, $userId); |
| | | $result = $chatModel->markAsRead($param['chat_id'], $userId); |
| | | $error=$chatModel->getError(); |
| | | } else { |
| | | $chatDate=$chatModel->where('conversation_id',$conversationId)->where('sender_id','<>',$userId)->find(); |
| | | $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($conversationId, $chatDate['sender_id']); |
| | | $result = $participantModel->markConversationAsRead($param['conversation_id'], $chatDate['sender_id']); |
| | | // 更新会话中所有消息的已读状态 |
| | | if ($result) { |
| | | $chatModel->where('conversation_id', $conversationId) |
| | | $chatModel->where('conversation_id', $param['conversation_id']) |
| | | ->where('sender_id', '<>', $userId) |
| | | ->where('is_read', 0) |
| | | ->update([ |
| | |
| | | if (!$businessCard) { |
| | | return $this->renderError('名片不存在'); |
| | | } |
| | | if ($businessCard['user_id'] == $userId) { |
| | | return $this->renderError('不能与自己建立会话'); |
| | | } |
| | | |
| | | $conversationModel = new ConversationModel(); |
| | | |
| | | // 检查是否已存在会话 |
| | | $conversation = $conversationModel->getConversationByBusinessCard($businessCardId); |
| | | |
| | | |
| | | if ($businessCard['user_id'] == $userId&&!$conversation) { |
| | | return $this->renderError('不能与自己建立会话'); |
| | | } |
| | | if (!$conversation) { |
| | | |
| | | // 创建新会话 |
| | |
| | | // 添加未读消息数量 |
| | | $chatModel = new ChatModel(); |
| | | $conversation['unread_count'] = $chatModel->getUnreadCount($conversation['conversation_id'], $userId); |
| | | |
| | | return $this->renderSuccess('获取成功', compact('conversation')); |
| | | |
| | | $wsUrl = SettingModel::getSysConfig()['url']; |
| | | return $this->renderSuccess('获取成功', compact('conversation','wsUrl')); |
| | | } else { |
| | | return $this->renderError('创建会话失败'); |
| | | } |