quanwei
2025-11-26 6ae85f8ddbae19f5586f4b0c37680fe21889ef14
admin/app/api/controller/plus/business/chat/Chat.php
@@ -61,11 +61,32 @@
            // 更新会话时间
            $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('发送失败');
        }
@@ -137,30 +158,29 @@
        $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([
@@ -200,15 +220,15 @@
        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) {
            // 创建新会话