request->param('id/d', 0); if (!$techId) { return json([ 'code' => 400, 'msg' => '技师ID不能为空' ]); } try { // 查询技师信息 $tech = Coach::find($techId); if (!$tech) { return json([ 'code' => 404, 'msg' => '技师不存在' ]); } // 格式化返回数据 $result = [ 'id' => $tech['id'], 'name' => $tech['name'], 'avatar' => $tech['work_photo'], 'service_count' => $tech['service_count'], 'rating' => $tech['rating'], 'skills' => $tech['skills'], 'description' => $tech['description'], 'status' => $tech['status'], 'create_time' => $tech['create_time'] ]; return json([ 'code' => 200, 'msg' => '成功', 'data' => $result ]); } catch (\Exception $e) { return json([ 'code' => 500, 'msg' => '获取技师信息失败: ' . $e->getMessage() ]); } } // 获取会话ID public function conversation_id() { $techId = $this->request->param('tech_id/d', 0); $userId = $this->userId; if (!$techId||!$userId) { return json(['code' => 400, 'msg' => '用户ID和技师ID不能为空']); } try { // 查询会话 $conversation = Db::name('chat_conversation') ->where('user_id', $userId) ->where('tech_id', $techId) ->find(); if ($conversation) { return json(['code' => 200, 'data' => ['conversation_id' => $conversation['id']]]); } // 创建新会话 $conversationId = Db::name('chat_conversation') ->insertGetId([ 'user_id' => $userId, 'tech_id' => $techId, 'unread_count' => 0, 'update_time' => date('Y-m-d H:i:s') ]); return json(['code' => 200, 'data' => ['conversation_id' => $conversationId]]); } catch (\Exception $e) { return json(['code' => 500, 'msg' => '获取会话ID失败: ' . $e->getMessage()]); } } /** * 获取聊天历史记录 */ public function history() { $params = $this->request->param(); $conversationId = $params['conversation_id'] ?? 0; $page = $params['page'] ?? 1; $pageSize = $params['page_size'] ?? 20; $where = [ 'conversation_id' => $conversationId ]; $query = ChatMessage::where($where) ->order('id', 'asc'); $list = $query->page($page, $pageSize) ->select(); $total = $query->count(); foreach ($list as &$item) { if ($item['sender_type']==1){//发送者类型是用户的话 则获取用户信息 $item['user'] = User::find($item['sender_id']); }else{ $item['user'] = Coach::find($item['sender_id']); } } return json([ 'code' => 200, 'data' => [ 'list' => $list, 'total' => $total, 'has_more' => $total > $page * $pageSize ] ]); } /** * 发送消息 */ public function send() { $params = $this->request->param(); $techId = $params['tech_id'] ?? 0; $orderId = $params['order_id'] ?? 0; $content = $params['content'] ?? ''; if (empty($content)) { return json(['code' => 400, 'msg' => '消息内容不能为空']); } // 生成会话ID $conversation = ChatConversation::where(['tech_id'=>$techId,'user_id'=>$this->userId])->find(); if (empty($conversation)) { $cccc = new ChatConversation; $conversationId = $cccc->insertGetId(['tech_id'=>$techId,'user_id'=>$this->userId]); }else{ $conversationId = $conversation['id']; } // 创建消息 $message = new ChatMessage(); $message->conversation_id = $conversationId; $message->sender_id = $this->userId; $message->sender_type = ChatMessage::SENDER_USER; // 用户 $message->receiver_id = $techId; $message->receiver_type = ChatMessage::SENDER_TECH; // 技师 $message->content = $content; $message->message_type = ChatMessage::TYPE_TEXT; // 文本 $message->read_status = ChatMessage::UNREAD; // 未读 $message->order_id = $orderId; $message->save(); // TODO: 这里应该通过WebSocket推送给技师 return json(['code' => 200, 'msg' => '发送成功', 'data' => $message]); } /** * 标记消息为已读 */ public function markAsRead() { $params = $this->request->param(); $conversationId = $params['conversation_id'] ?? ''; $userId = $this->request->uid; if (empty($conversationId)) { return json(['code' => 400, 'msg' => '会话ID不能为空']); } // 标记消息为已读 Db::name('chat_message') ->where('conversation_id', $conversationId) ->where('receiver_id', $userId) ->where('read_status', ChatMessage::UNREAD) ->update(['read_status' => ChatMessage::READ]); return json(['code' => 200, 'msg' => '已标记为已读']); } /** * 获取未读消息数量 */ public function unreadCount() { $unreadCount = ChatMessage::where([ 'conversation_id' => $this->request->uid, 'receiver_type' => ChatMessage::SENDER_USER, // 用户 'read_status' => ChatMessage::UNREAD ])->count(); return json(['code' => 200, 'data' => ['count' => $unreadCount]]); } /** * 获取最后一条消息 */ public function lastMessage() { // 生成会话ID $conversationId = $this->request->param('conversation_id/d', 0); $message = ChatMessage::where([ 'conversation_id' => $conversationId, ]) ->order('id', 'desc') ->find(); return json(['code' => 200, 'data' => $message]); } }