import { Image } from 'expo-image'; import React from 'react'; import { ImageBackground, StyleSheet, Text, View } from 'react-native'; import { Images } from '@/constants/images'; interface ProductItem { id: string; name: string; cover: string; level: string; probability: number; price?: number; quantity?: number; } interface ProductListProps { products: ProductItem[]; levelList?: any[]; poolId: string; price: number; } const LEVEL_CONFIG: Record = { A: { title: '超神款', bg: Images.box.detail.productItemA }, B: { title: '欧皇款', bg: Images.box.detail.productItemB }, C: { title: '隐藏款', bg: Images.box.detail.productItemC }, D: { title: '普通款', bg: Images.box.detail.productItemD }, }; const ignoreRatio0 = (val: number) => { const str = String(val); const match = str.match(/^(\d+\.?\d*?)0*$/); return match ? match[1].replace(/\.$/, '') : str; }; export const ProductList: React.FC = ({ products, levelList }) => { // 按等级分组 const groupedProducts = products.reduce( (acc, item) => { const level = item.level || 'D'; if (!acc[level]) acc[level] = []; acc[level].push(item); return acc; }, {} as Record ); // 计算各等级概率 const levelProbabilities = levelList?.reduce( (acc, item) => { acc[item.level] = item.probability; return acc; }, {} as Record ) || {}; const renderLevelSection = (level: string, items: ProductItem[]) => { const config = LEVEL_CONFIG[level] || { title: level, bg: Images.box.detail.productItemD }; const probability = levelProbabilities[level] || items.reduce((sum, i) => sum + (i.probability || 0), 0); return ( {/* 等级标题 */} {config.title} 概率: {ignoreRatio0(probability * 100)}% {/* 商品网格 */} {items.map((item, index) => ( {item.name} 概率: {ignoreRatio0((item.probability || 0) * 100)}% ))} ); }; const levelOrder = ['A', 'B', 'C', 'D']; return ( {/* 标题 */} {levelOrder.map((level) => { const items = groupedProducts[level]; if (!items || items.length === 0) return null; return renderLevelSection(level, items); })} ); }; const styles = StyleSheet.create({ container: { paddingHorizontal: 10, marginTop: -40, }, titleBox: { alignItems: 'center', marginBottom: 10, }, titleImg: { width: 121, height: 29, }, levelSection: { marginBottom: 15, }, levelHeader: { height: 40, flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', paddingHorizontal: 15, marginBottom: 10, }, levelTitle: { color: '#fff', fontSize: 16, fontWeight: 'bold', }, levelProbability: { color: 'rgba(255,255,255,0.8)', fontSize: 12, }, productGrid: { flexDirection: 'row', flexWrap: 'wrap', marginHorizontal: -5, }, productItem: { width: '33.33%', paddingHorizontal: 5, marginBottom: 10, }, productItemBg: { width: '100%', aspectRatio: 0.75, padding: 8, alignItems: 'center', }, productImageBox: { width: '100%', aspectRatio: 1, borderRadius: 5, overflow: 'hidden', backgroundColor: 'rgba(255,255,255,0.1)', }, productImage: { width: '100%', height: '100%', }, productName: { color: '#fff', fontSize: 10, marginTop: 5, textAlign: 'center', height: 26, }, productProbability: { color: '#FBC400', fontSize: 9, marginTop: 2, }, });