147 lines
4.1 KiB
Dart
147 lines
4.1 KiB
Dart
// 2026-04-14 | test_discover_image.dart | Discover图片数据验证 | 验证api_discover.php返回的cover/picId字段
|
||
// 运行: dart run scripts/test_discover_image.dart
|
||
|
||
import 'dart:convert';
|
||
import 'dart:io';
|
||
|
||
const String baseUrl = 'https://eat.wktyl.com/api';
|
||
|
||
Future<void> main() async {
|
||
print('=== Discover 图片数据验证 ===\n');
|
||
|
||
await testDiscoverApi();
|
||
await testDetailApi();
|
||
await testImageUrl();
|
||
}
|
||
|
||
Future<void> testDiscoverApi() async {
|
||
print('📡 测试 api_discover.php 接口');
|
||
final client = HttpClient();
|
||
try {
|
||
final url = Uri.parse('$baseUrl/api_discover.php?total=10');
|
||
print(' 请求URL: $url');
|
||
|
||
final request = await client.getUrl(url);
|
||
request.headers.set('Accept', 'application/json');
|
||
|
||
final response = await request.close();
|
||
final body = await response.transform(utf8.decoder).join();
|
||
|
||
if (body.isEmpty) {
|
||
print(' ❌ 响应体为空');
|
||
return;
|
||
}
|
||
|
||
final json = jsonDecode(body) as Map<String, dynamic>;
|
||
print(' code: ${json['code']}');
|
||
|
||
final data = json['data'] as Map<String, dynamic>?;
|
||
if (data == null) {
|
||
print(' ❌ data字段为null');
|
||
return;
|
||
}
|
||
|
||
final recipes = data['recipes'] as List?;
|
||
if (recipes == null || recipes.isEmpty) {
|
||
print(' ❌ recipes为空');
|
||
return;
|
||
}
|
||
|
||
print(' 菜谱数量: ${recipes.length}');
|
||
print('');
|
||
|
||
for (int i = 0; i < recipes.length && i < 5; i++) {
|
||
final recipe = recipes[i] as Map<String, dynamic>;
|
||
print(' ─── 菜谱 #${i + 1} ───');
|
||
print(' id: ${recipe['id']}');
|
||
print(' title: ${recipe['title']}');
|
||
print(' cover: ${recipe['cover']}');
|
||
print(' pic_id: ${recipe['pic_id']}');
|
||
print(' picId: ${recipe['picId']}');
|
||
print(' pic: ${recipe['pic']}');
|
||
|
||
final allKeys = recipe.keys.toList();
|
||
print(' 所有字段: $allKeys');
|
||
|
||
final cover = recipe['cover'] ?? '';
|
||
if (cover.isNotEmpty) {
|
||
final regex = RegExp(r'/pic/(\d+)[ab]?\.(jpg|png|webp)$');
|
||
final match = regex.firstMatch(cover.toString());
|
||
if (match != null) {
|
||
print(' ✅ 从cover提取picId: ${match.group(1)}');
|
||
} else {
|
||
print(' ⚠️ cover不匹配picId正则: $cover');
|
||
}
|
||
} else {
|
||
print(' ❌ cover为空');
|
||
}
|
||
print('');
|
||
}
|
||
} catch (e) {
|
||
print(' ❌ 请求失败: $e');
|
||
} finally {
|
||
client.close();
|
||
}
|
||
}
|
||
|
||
Future<void> testDetailApi() async {
|
||
print('\n📡 测试 api.php?act=detail 接口(对比picId字段)');
|
||
final client = HttpClient();
|
||
try {
|
||
final url = Uri.parse('$baseUrl/api.php?act=detail&id=32390');
|
||
print(' 请求URL: $url');
|
||
|
||
final request = await client.getUrl(url);
|
||
request.headers.set('Accept', 'application/json');
|
||
|
||
final response = await request.close();
|
||
final body = await response.transform(utf8.decoder).join();
|
||
|
||
if (body.isEmpty) return;
|
||
|
||
final json = jsonDecode(body) as Map<String, dynamic>;
|
||
final data = json['data'] as Map<String, dynamic>?;
|
||
if (data == null) return;
|
||
|
||
print(' id: ${data['id']}');
|
||
print(' title: ${data['title']}');
|
||
print(' cover: ${data['cover']}');
|
||
print(' pic_id: ${data['pic_id']}');
|
||
print(' picId: ${data['picId']}');
|
||
print(' pic: ${data['pic']}');
|
||
|
||
final allKeys = data.keys.toList();
|
||
print(' 所有字段: $allKeys');
|
||
} catch (e) {
|
||
print(' ❌ 请求失败: $e');
|
||
} finally {
|
||
client.close();
|
||
}
|
||
}
|
||
|
||
Future<void> testImageUrl() async {
|
||
print('\n📡 测试图片URL可访问性');
|
||
final client = HttpClient();
|
||
try {
|
||
final testUrls = [
|
||
'https://eat.wktyl.com/api/assets/pic/32390a.jpg',
|
||
'https://eat.wktyl.com/api/assets/pic/32390b.jpg',
|
||
'https://eat.wktyl.com/api/assets/pic/32390.jpg',
|
||
];
|
||
|
||
for (final url in testUrls) {
|
||
try {
|
||
final request = await client.getUrl(Uri.parse(url));
|
||
final response = await request.close();
|
||
print(
|
||
' $url → ${response.statusCode} (${response.contentLength} bytes)',
|
||
);
|
||
} catch (e) {
|
||
print(' $url → ❌ $e');
|
||
}
|
||
}
|
||
} finally {
|
||
client.close();
|
||
}
|
||
}
|