Переглянути джерело

一番赏锁盒按钮修复

zbb 3 місяців тому
батько
коміт
17fed95863

+ 40 - 14
app/award-detail-yfs/index.tsx

@@ -23,7 +23,7 @@ import ProductSwiper from '@/components/award-detail-yfs/ProductSwiper';
 import PurchaseBar from '@/components/award-detail-yfs/PurchaseBar';
 import { RuleModal, RuleModalRef } from '@/components/award-detail-yfs/RuleModal';
 import { Images } from '@/constants/images';
-import { getBoxDetail, getNextBox, getPoolDetail, getPreBox, poolIn, poolOut } from '@/services/award';
+import { getBoxDetail, getNextBox, getPoolDetail, getPreBox, lockBox, poolIn, poolOut, unlockBox } from '@/services/award';
 import { CheckoutModal, CheckoutModalRef } from '../award-detail/components/CheckoutModal';
 
 const { width } = Dimensions.get('window');
@@ -270,22 +270,15 @@ export default function AwardDetailYfsScreen() {
                     <View style={{ height: 180 }} /> 
                     
                     <ProductSwiper 
-                        products={data?.luckGoodsList?.map((item: any) => {
-                            let prob = '0%';
-                            const total = data.leftQuantity || 1; 
-                            if (item.level === 'A') prob = ((data.leftQuantityA / total) * 100).toFixed(4) + '%';
-                            else if (item.level === 'B') prob = ((data.leftQuantityB / total) * 100).toFixed(4) + '%';
-                            else if (item.level === 'C') prob = ((data.leftQuantityC / total) * 100).toFixed(4) + '%';
-                            else if (item.level === 'D') prob = ((data.leftQuantityD / total) * 100).toFixed(4) + '%';
-                            
-                            return {
+                        box={currentBox} // Pass current box
+                        products={data?.luckGoodsList?.map((item: any) => ({
                                 cover: item.spu.cover,
                                 name: item.spu.name,
                                 level: item.level,
-                                probability: prob,
-                                quantity: item.quantity
-                            };
-                        }) || []} 
+                                quantity: item.quantity,
+                                id: item.id, // Ensure IDs are passed
+                                spu: item.spu
+                        })) || []} 
                     />
                     
                     <ImageBackground 
@@ -342,6 +335,39 @@ export default function AwardDetailYfsScreen() {
                         <Text style={styles.slantedR}>仓库</Text>
                      </ImageBackground>
                 </TouchableOpacity>
+                
+                {/* Lock Button */}
+                 <TouchableOpacity style={[styles.positionBut, { top: 300, right: 0 }]} onPress={() => {
+                     if (!currentBox) return;
+                     const isLocked = currentBox.lockTime && new Date(currentBox.lockTime).getTime() > Date.now();
+                     
+                     Alert.alert('提示', isLocked ? '是否解锁盒子?' : '是否锁定盒子(锁定后他人无法购买)?', [
+                         { text: '取消', style: 'cancel' },
+                         { text: '确定', onPress: async () => {
+                             try {
+                                 const poolId = getSafePoolId();
+                                 let success = false;
+                                 if (isLocked) {
+                                     success = await unlockBox(poolId, currentBox.number);
+                                 } else {
+                                     success = await lockBox(poolId, currentBox.number);
+                                 }
+                                 
+                                 if (success) {
+                                     Alert.alert('成功', isLocked ? '解锁成功' : '锁定成功');
+                                     loadBox(currentBox.number); // Refresh
+                                 }
+                                 // System handles error msg via interceptor, no need for manual alert
+                             } catch (e) {
+                                 // console.error(e); // Silent error
+                             }
+                         }}
+                     ]);
+                 }}>
+                     <ImageBackground source={{ uri: Images.box.detail.positionBgRight }} style={styles.btnBg} resizeMode="contain">
+                        <Text style={styles.slantedR}>{currentBox?.lockTime && new Date(currentBox.lockTime).getTime() > Date.now() ? '解锁' : '锁盒'}</Text>
+                     </ImageBackground>
+                </TouchableOpacity>
 
                 <TouchableOpacity style={[styles.positionBut, styles.positionRefresh]} onPress={() => loadBox(currentBox?.number)}>
                      <ImageBackground source={{ uri: Images.box.detail.positionBgRight }} style={styles.btnBg} resizeMode="contain">

+ 29 - 2
components/award-detail-yfs/ProductSwiper.tsx

@@ -24,14 +24,41 @@ interface Product {
 
 interface ProductSwiperProps {
   products: Product[];
+  box?: any; // Add box prop
   onShowSwipe?: (index: number) => void;
 }
 
-export default function ProductSwiper({ products, onShowSwipe }: ProductSwiperProps) {
+export default function ProductSwiper({ products, box, onShowSwipe }: ProductSwiperProps) {
     const [current, setCurrent] = useState(0);
 
+    const getLeftNum = (item: any) => {
+        const usedStat = box?.usedStat || box?.used_stat;
+        if (!box || !usedStat) return item.quantity;
+        
+        const spuId = String(item.spu?.id || item.spu_id || item.id); // Try to match ID
+        const itemId = String(item.id);
+
+        let used: any = null;
+        if (Array.isArray(usedStat)) {
+             used = usedStat.find((u: any) => {
+                 const uId = String(u.spuId || u.spu_id || u.id);
+                 return uId === spuId || uId === itemId;
+             });
+        } else {
+            used = usedStat[spuId] || usedStat[itemId];
+        }
+
+        if (used) {
+            return item.quantity - (used.quantity || 0);
+        }
+        return item.quantity;
+    };
+
     const getProbability = (item: Product) => {
-        return item.probability || "";
+        if (!box || !box.leftQuantity) return item.probability || '0%';
+        const left = getLeftNum(item);
+        const prob = (left / box.leftQuantity * 100).toFixed(4);
+        return parseFloat(prob) === 0 ? '0%' : `${prob}%`;
     };
 
     if (!products || products.length === 0) return null;