/* * 文件: test_recipe_code.dart * 作用: 测试菜谱code字段,验证二维码海报功能所需数据 * 创建: 2026-04-13 */ import 'dart:convert'; import 'dart:io'; const String baseUrl = 'http://eat.wktyl.com/api'; Future main() async { final client = HttpClient(); print('=== 菜谱 Code 字段测试 ===\n'); // 测试1: detail接口是否返回code字段 print('📋 测试1: detail接口 code字段'); try { final url = Uri.parse('$baseUrl/api.php?act=detail&id=32892'); final request = await client.getUrl(url); final response = await request.close(); final body = await response.transform(utf8.decoder).join(); final data = json.decode(body); if (data['code'] == 200) { final recipe = data['data']; print(' ✅ 菜谱标题: ${recipe['title']}'); print(' ✅ Code字段: ${recipe['code']}'); print(' ✅ Pic_id: ${recipe['pic_id']}'); print(' ✅ Statistics: ${recipe['statistics']}'); } else { print(' ❌ 接口返回错误: ${data['message']}'); } } catch (e) { print(' ❌ 请求失败: $e'); } print(''); // 测试2: full接口是否返回code字段 print('📋 测试2: full接口 code字段'); try { final url = Uri.parse('$baseUrl/api.php?act=full&id=32892'); final request = await client.getUrl(url); final response = await request.close(); final body = await response.transform(utf8.decoder).join(); final data = json.decode(body); if (data['code'] == 200) { final recipe = data['data']; print(' ✅ 菜谱标题: ${recipe['title']}'); print(' ✅ Code字段: ${recipe['code']}'); print(' ✅ Category: ${recipe['category']}'); print(' ✅ Allergens: ${recipe['allergens']}'); } } catch (e) { print(' ❌ 请求失败: $e'); } print(''); // 测试3: what_to_eat detail接口通过code查询 print('📋 测试3: what_to_eat detail接口 code查询'); try { final url = Uri.parse('$baseUrl/api_what_to_eat.php?act=detail&code=CP032892'); final request = await client.getUrl(url); final response = await request.close(); final body = await response.transform(utf8.decoder).join(); final data = json.decode(body); if (data['code'] == 200) { final recipe = data['data']; print(' ✅ 通过code查询成功: ${recipe['title']}'); } else { print(' ❌ code查询失败: ${data['message']}'); } } catch (e) { print(' ❌ 请求失败: $e'); } print(''); // 测试4: mini接口是否返回code字段 print('📋 测试4: mini接口 code字段'); try { final url = Uri.parse('$baseUrl/api.php?act=mini&id=32892'); final request = await client.getUrl(url); final response = await request.close(); final body = await response.transform(utf8.decoder).join(); final data = json.decode(body); if (data['code'] == 200) { final recipe = data['data']; print(' ✅ 菜谱标题: ${recipe['title']}'); print(' ✅ Code字段: ${recipe['code']}'); print(' ✅ 返回字段: ${recipe.keys.toList()}'); } } catch (e) { print(' ❌ 请求失败: $e'); } print(''); // 测试5: feed recommend接口 print('📋 测试5: feed recommend接口'); try { final url = Uri.parse('$baseUrl/api_feed.php?act=recommend&page=1&limit=3'); final request = await client.getUrl(url); final response = await request.close(); final body = await response.transform(utf8.decoder).join(); final data = json.decode(body); if (data['code'] == 200) { final items = data['data'] as List; print(' ✅ 推荐数量: ${items.length}'); for (final item in items) { print( ' - ${item['title']} (code: ${item['code']}, views: ${item['statistics']?['view_count']})'); } } } catch (e) { print(' ❌ 请求失败: $e'); } print(''); // 测试6: filter_apply接口(每日菜单规划用) print('📋 测试6: filter_apply接口 三餐推荐'); for (final meal in ['早餐', '中餐', '晚餐']) { try { final url = Uri.parse( '$baseUrl/api_what_to_eat.php?act=filter_apply&count=2&meal_time=$meal'); final request = await client.getUrl(url); final response = await request.close(); final body = await response.transform(utf8.decoder).join(); final data = json.decode(body); if (data['code'] == 200) { final items = data['data'] as List; final titles = items.map((e) => e['title']).join(', '); print(' ✅ $meal: $titles'); } } catch (e) { print(' ❌ $meal 请求失败: $e'); } } print('\n=== 测试完成 ==='); client.close(); }