骑手信息获取接口测试\n";
echo "
\n";
/**
* 测试获取骑手信息接口
*/
function testGetRiderInfo($base_url, $rider_id) {
$url = $base_url . '/index.php/Retail/Rider/getRiderInfo?id=' . $rider_id;
echo "测试获取骑手信息 (ID: $rider_id)
\n";
echo "请求URL: $url
\n";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'User-Agent: Test-Client/1.0'
]);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
if ($error) {
echo "CURL错误: $error
\n";
return false;
}
echo "HTTP状态码: $http_code
\n";
if ($http_code == 200) {
$data = json_decode($response, true);
if ($data) {
echo "✓ 接口调用成功
\n";
echo "返回数据:
\n";
echo "" . json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "
\n";
// 验证返回数据结构
if (isset($data['status']) && isset($data['msg']) && isset($data['data'])) {
echo "✓ 数据结构正确
\n";
if ($data['status'] == 1 && !empty($data['data'])) {
echo "✓ 骑手信息获取成功
\n";
// 检查关键字段
$rider = $data['data'];
$required_fields = ['id', 'rider_name', 'rider_phone', 'work_status', 'auth_status'];
$missing_fields = [];
foreach ($required_fields as $field) {
if (!isset($rider[$field])) {
$missing_fields[] = $field;
}
}
if (empty($missing_fields)) {
echo "✓ 必要字段完整
\n";
} else {
echo "⚠ 缺少字段: " . implode(', ', $missing_fields) . "
\n";
}
// 检查格式化字段
$formatted_fields = ['work_status_text', 'auth_status_text', 'status_text'];
$has_formatted = [];
foreach ($formatted_fields as $field) {
if (isset($rider[$field])) {
$has_formatted[] = $field;
}
}
if (!empty($has_formatted)) {
echo "✓ 包含格式化字段: " . implode(', ', $has_formatted) . "
\n";
}
} else {
echo "✗ 获取骑手信息失败: " . $data['msg'] . "
\n";
}
} else {
echo "✗ 返回数据结构不正确
\n";
}
} else {
echo "✗ JSON解析失败
\n";
echo "原始响应: " . htmlspecialchars($response) . "
\n";
}
} else {
echo "✗ HTTP请求失败
\n";
echo "响应内容: " . htmlspecialchars($response) . "
\n";
}
echo "
\n";
return $http_code == 200;
}
/**
* 测试无效参数
*/
function testInvalidParams($base_url) {
echo "测试无效参数
\n";
// 测试无ID参数
$url = $base_url . '/index.php/Retail/Rider/getRiderInfo';
echo "测试无ID参数: $url
\n";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http_code == 200) {
$data = json_decode($response, true);
if ($data && isset($data['status']) && $data['status'] == 0) {
echo "✓ 正确处理无效参数
\n";
} else {
echo "✗ 未正确处理无效参数
\n";
}
}
// 测试不存在的ID
$url = $base_url . '/index.php/Retail/Rider/getRiderInfo?id=99999';
echo "测试不存在的ID: $url
\n";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http_code == 200) {
$data = json_decode($response, true);
if ($data && isset($data['status']) && $data['status'] == 0) {
echo "✓ 正确处理不存在的骑手ID
\n";
} else {
echo "✗ 未正确处理不存在的骑手ID
\n";
}
}
echo "
\n";
}
// 执行测试
echo "开始测试骑手信息获取接口...
\n";
// 测试正常获取骑手信息
testGetRiderInfo($base_url, $test_rider_id);
// 测试无效参数
testInvalidParams($base_url);
echo "测试完成!
\n";
echo "注意:如果测试失败,请检查:
\n";
echo "\n";
echo "- 数据库中是否存在ID为 $test_rider_id 的骑手记录
\n";
echo "- 数据库连接是否正常
\n";
echo "- RiderModel 和 ImageGalleryModel 是否正确加载
\n";
echo "- 权限设置是否正确
\n";
echo "
\n";
?>