import React, { useState } from 'react' import { View, Text } from '@tarojs/components' import { Button } from '@nutui/nutui-react-taro' import { getUserGifts } from '@/api/shop/shopGift' import Taro from '@tarojs/taro' const ApiTest: React.FC = () => { const [loading, setLoading] = useState(false) const [results, setResults] = useState([]) const [logs, setLogs] = useState([]) const addLog = (message: string) => { const timestamp = new Date().toLocaleTimeString() setLogs(prev => [`[${timestamp}] ${message}`, ...prev]) } const testApiCall = async (status: number, statusName: string) => { setLoading(true) addLog(`开始测试 status=${status} (${statusName})`) try { const params = { page: 1, limit: 10, userId: Taro.getStorageSync('UserId'), status: status } addLog(`API参数: ${JSON.stringify(params)}`) const res = await getUserGifts(params) addLog(`API返回: ${res?.list?.length || 0} 条数据`) if (res?.list && res.list.length > 0) { const statusCounts = res.list.reduce((acc: any, item: any) => { const itemStatus = item.status acc[itemStatus] = (acc[itemStatus] || 0) + 1 return acc }, {}) addLog(`返回数据状态分布: ${JSON.stringify(statusCounts)}`) // 检查是否所有返回的数据都是期望的状态 const allCorrectStatus = res.list.every((item: any) => item.status === status) if (allCorrectStatus) { addLog(`✅ 状态筛选正确: 所有数据都是 status=${status}`) } else { addLog(`❌ 状态筛选错误: 返回了其他状态的数据`) } } else { addLog(`ℹ️ 无数据返回`) } setResults(prev => [...prev, { status, statusName, count: res?.list?.length || 0, data: res?.list || [], success: true }]) } catch (error) { addLog(`❌ API调用失败: ${error}`) setResults(prev => [...prev, { status, statusName, count: 0, data: [], success: false, error: String(error) }]) } finally { setLoading(false) } } const clearResults = () => { setResults([]) setLogs([]) } const testAllStatus = async () => { clearResults() await testApiCall(0, '未使用') await new Promise(resolve => setTimeout(resolve, 1000)) // 延迟1秒 await testApiCall(1, '已使用') await new Promise(resolve => setTimeout(resolve, 1000)) // 延迟1秒 await testApiCall(2, '失效') } return ( {/* 页面标题 */} API 状态参数测试 测试 getUserGifts API 的 status 参数传递 {/* 测试按钮 */} 测试操作: {/* 测试结果 */} {results.length > 0 && ( 测试结果: {results.map((result, index) => ( status={result.status} ({result.statusName}) {result.success ? '成功' : '失败'} 返回数据: {result.count} 条 {result.error && ( 错误: {result.error} )} {result.data.length > 0 && ( 示例数据: {result.data.slice(0, 2).map((item: any, idx: number) => ( ID:{item.id}, status:{item.status}, name:{item.goodsName || item.name} ))} )} ))} )} {/* 调试日志 */} 调试日志: {logs.length > 0 ? ( logs.map((log, index) => ( {log} )) ) : ( 暂无日志 )} {/* 使用说明 */} 使用说明: 1. 点击测试按钮调用 getUserGifts API 2. 检查返回的数据状态是否与请求参数一致 3. 查看调试日志了解详细的API调用过程 4. 如果状态筛选不正确,说明后端或前端有问题 ) } export default ApiTest