[ ['id' => 1, 'sender' => 'system', 'text' => '交易单价:200 元', 'time' => date('Y-m-d H:i:s')], ['id' => 2, 'sender' => 'user', 'text' => 'Y', 'time' => date('Y-m-d H:i:s')], ['id' => 3, 'sender' => 'agent', 'text' => '为保护您的隐私,请通过“安全认证”入口上传证件照片或联系客服的安全渠道进行实名核验。', 'time' => date('Y-m-d H:i:s')] ], 'next_id' => 4 ], JSON_UNESCAPED_UNICODE)); } if ($action === 'send') { header('Content-Type: application/json; charset=utf-8'); // 发送消息接口 $sender = $_POST['sender'] ?? 'user'; $text = trim($_POST['text'] ?? ''); // 上传图片(如果有) $imageFile = ''; if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) { $ext = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION)); $allowed = ['jpg', 'jpeg', 'png', 'gif', 'webp']; if (!in_array($ext, $allowed)) { echo json_encode(['success' => false, 'error' => '不支持的图片格式']); exit; } $filename = 'img_' . time() . '_' . uniqid() . '.' . $ext; if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadDir . $filename)) { $imageFile = $filename; } } if (empty($text) && empty($imageFile)) { echo json_encode(['success' => false, 'error' => '消息内容不能为空']); exit; } // 读取并写入数据文件 $data = json_decode(file_get_contents($dataFile), true); if (!$data) $data = ['messages' => [], 'next_id' => 1]; $msgId = $data['next_id']; $msg = [ 'id' => $msgId, 'sender' => $sender, 'text' => $text, 'image' => $imageFile, 'time' => date('Y-m-d H:i:s') ]; $data['messages'][] = $msg; $data['next_id'] = $msgId + 1; file_put_contents($dataFile, json_encode($data, JSON_UNESCAPED_UNICODE)); echo json_encode(['success' => true, 'msg_id' => $msgId, 'server_time' => date('Y-m-d H:i:s')]); exit; } elseif ($action === 'list') { header('Content-Type: application/json; charset=utf-8'); // 获取消息列表(since 参数,返回 id 大于 since 的消息) $since = intval($_GET['since'] ?? 0); $data = json_decode(file_get_contents($dataFile), true); if (!$data) $data = ['messages' => [], 'next_id' => 1]; $newMessages = []; foreach ($data['messages'] as $m) { if ($m['id'] > $since) $newMessages[] = $m; } echo json_encode(['success' => true, 'messages' => $newMessages, 'server_time' => date('Y-m-d H:i:s')]); exit; } // 若没有 action,则输出前端页面(HTML + JS) ?> 在线客服 - 单文件示例
头像
闲鱼在线交易客服
用热情服务,换客户微笑
交易单价: 200 元
请您核对商品相关信息,确认请回复【Y】