From e88eef4bb27265c65a4c22e3b731d841b05dbaee Mon Sep 17 00:00:00 2001 From: Mac Date: Fri, 23 May 2025 11:57:57 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B4=AD=E7=89=A9=E8=BD=A6=E7=A7=91=E7=89=B9?= =?UTF-8?q?=E8=BF=AA=E7=93=A6=E8=B4=A7=E5=B8=81=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/locales/en/translation.json | 2 + app/locales/fr/translation.json | 2 + app/locales/zh/translation.json | 22 +++++ app/screens/CartScreen.tsx | 158 ++++++++++++++++++++++++++------ app/services/api/userApi.ts | 1 + 5 files changed, 157 insertions(+), 28 deletions(-) diff --git a/app/locales/en/translation.json b/app/locales/en/translation.json index 93bfa04..0c601a0 100644 --- a/app/locales/en/translation.json +++ b/app/locales/en/translation.json @@ -446,6 +446,8 @@ "pieces": "pieces", "delete": "Delete", "min_order_quantity": "Minimum order quantity", + "minimum_order_required": "The order amount must be at least {{amount}} {{currency}}", + "minimum_order": "Minimum order amount:", "all": "All", "submit": "Submit", "delete_item": "Delete this item?", diff --git a/app/locales/fr/translation.json b/app/locales/fr/translation.json index 3b98df9..7873538 100644 --- a/app/locales/fr/translation.json +++ b/app/locales/fr/translation.json @@ -441,6 +441,8 @@ "pieces": "pièces", "delete": "Supprimer", "min_order_quantity": "Quantité minimale de commande", + "minimum_order_required": "Le montant de la commande doit être d'au moins {{amount}} {{currency}}", + "minimum_order": "Montant minimum de commande:", "all": "Tout", "submit": "Soumettre", "delete_item": "Supprimer cet article ?", diff --git a/app/locales/zh/translation.json b/app/locales/zh/translation.json index 761dd64..6bee015 100644 --- a/app/locales/zh/translation.json +++ b/app/locales/zh/translation.json @@ -131,5 +131,27 @@ "chooseFromGallery": "从相册选择", "cancel": "取消", "sales": "销量" + }, + "cart": { + "cart": "购物车", + "add_failed": "添加失败", + "login_required": "请先登录", + "select_products": "请选择商品", + "notice": "提示", + "enter_valid_quantity": "请输入有效数量", + "min_order": "最少订购", + "pieces": "件", + "delete": "删除", + "min_order_quantity": "最小订单数量", + "minimum_order_required": "订单金额必须至少为 {{amount}} {{currency}}", + "minimum_order": "最低订单金额:", + "all": "全部", + "submit": "提交", + "delete_item": "删除此商品?", + "no": "否", + "yes": "是", + "modify_quantity": "修改数量", + "cancel": "取消", + "confirm": "确认" } } diff --git a/app/screens/CartScreen.tsx b/app/screens/CartScreen.tsx index f2234af..d34e034 100644 --- a/app/screens/CartScreen.tsx +++ b/app/screens/CartScreen.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useState, useCallback } from "react"; +import React, { useState, useCallback } from "react"; import { View, Text, @@ -13,7 +13,6 @@ import { StatusBar, SafeAreaView, } from "react-native"; -import BackIcon from "../components/BackIcon"; import fontSize from "../utils/fontsizeUtils"; import CircleOutlineIcon from "../components/CircleOutlineIcon"; import { @@ -32,17 +31,19 @@ import { useFocusEffect } from "@react-navigation/native"; import useCreateOrderStore from "../store/createOrder"; import useUserStore from "../store/user"; import { t } from "../i18n"; +import { payApi } from "../services/api/payApi"; export const CartScreen = () => { const [cartList, setCartList] = useState([]); const { - user: { user_id, currency, vip_discount }, + user: { user_id, currency, vip_discount, country_code }, } = useUserStore(); const [selectedItems, setSelectedItems] = useState<{ [key: string]: boolean; }>({}); const [allSelected, setAllSelected] = useState(false); const [totalAmount, setTotalAmount] = useState(0); + const [convertedMinAmount, setConvertedMinAmount] = useState(null); const [deleteModalVisible, setDeleteModalVisible] = useState(false); const [itemToDelete, setItemToDelete] = useState<{ cartId: number; @@ -58,6 +59,50 @@ export const CartScreen = () => { const [quantityInput, setQuantityInput] = useState(""); const navigation = useNavigation>(); const { setItems } = useCreateOrderStore(); + + // 货币转换函数 + const convertCurrency = async () => { + console.log(country_code); + + // 如果 country_code 是 255,不需要转换 + if (country_code === 225) { + setConvertedMinAmount(null); + return; + } + + try { + console.log(`Converting currency for country_code: ${country_code}, from FCFA to ${currency}`); + + const data = { + from_currency: "FCFA", // 固定使用 FCFA + to_currency: currency, // 使用用户的货币 + amounts: { + total_amount: 50000, // 固定的最低订单金额 50,000 FCFA + }, + }; + + const response = await payApi.convertCurrency(data); + console.log("Currency conversion response:", response); + + if (response && response.converted_amounts_list && response.converted_amounts_list.length > 0) { + const convertedTotal = response.converted_amounts_list.find( + (item: any) => item.item_key === "total_amount" + ); + if (convertedTotal) { + console.log(`Converted minimum amount: ${convertedTotal.converted_amount} ${currency}`); + setConvertedMinAmount(convertedTotal.converted_amount); + } + } else { + console.warn("No converted amounts found in response"); + setConvertedMinAmount(null); + } + } catch (error) { + console.error("货币转换失败:", error); + // 转换失败时不设置转换金额,使用原始逻辑 + setConvertedMinAmount(null); + } + }; + // 计算选中商品的总金额 const calculateTotalAmount = (list: GetCartList[]) => { let total = 0; @@ -83,12 +128,13 @@ export const CartScreen = () => { } }; - const changeAllSelected = () => { - const allSkusSelected = cartList.every((item) => + const changeAllSelected = (newList: GetCartList[]) => { + const allSkusSelected = newList.every((item) => item.skus.every((sku) => sku.selected === 1) ); - setAllSelected(!allSkusSelected); + setAllSelected(allSkusSelected); }; + const toggleSelection = async ( cartItemId: string, index1: number, @@ -123,6 +169,8 @@ export const CartScreen = () => { return item; }); calculateTotalAmount(newList); + // 在状态更新后检查全选状态 + changeAllSelected(newList); return newList; }); @@ -157,6 +205,7 @@ export const CartScreen = () => { return item; }); calculateTotalAmount(newList); + changeAllSelected(newList); return newList; }); }); @@ -180,6 +229,8 @@ export const CartScreen = () => { return item; }); calculateTotalAmount(newList); + // 在状态更新后检查全选状态 + changeAllSelected(newList); return newList; }); @@ -209,28 +260,35 @@ export const CartScreen = () => { return item; }); calculateTotalAmount(newList); + changeAllSelected(newList); return newList; }); }); } - - changeAllSelected(); }; const getCart = async () => { const res = await getCartList(); - setCartList(res.items); - calculateTotalAmount(res.items); + + // 修正父商品的选择状态,确保与子商品状态一致 + const correctedItems = res.items.map((item) => { + // 检查该商品下所有子商品是否都被选中 + const allSkusSelected = item.skus.every((sku) => sku.selected === 1); + return { + ...item, + selected: allSkusSelected ? 1 : 0, // 根据子商品状态设置父商品状态 + }; + }); + + setCartList(correctedItems); + calculateTotalAmount(correctedItems); - if (res.items.length === 0) { + if (correctedItems.length === 0) { // 如果购物车为空,直接设置全选为false setAllSelected(false); } else { // 检查所有商品的 skus 数组中的 selected 属性是否都为 1 - const allSkusSelected = res.items.every((item) => - item.skus.every((sku) => sku.selected === 1) - ); - setAllSelected(allSkusSelected); + changeAllSelected(correctedItems); } }; @@ -264,9 +322,9 @@ export const CartScreen = () => { }; }); calculateTotalAmount(newList); + setAllSelected(!newAllSelected); return newList; }); - setAllSelected(!newAllSelected); }); return { @@ -369,6 +427,7 @@ export const CartScreen = () => { useFocusEffect( useCallback(() => { getCart(); + convertCurrency(); // 添加货币转换调用 }, []) ); @@ -377,6 +436,8 @@ export const CartScreen = () => { Alert.alert(t("cart.add_failed"), t("cart.login_required")); return; } + + // 检查是否有选中的商品 const items: { cart_item_id: number }[] = []; cartList.forEach((item) => { item.skus.forEach((sku) => { @@ -393,6 +454,40 @@ export const CartScreen = () => { Alert.alert(t("cart.add_failed"), t("cart.select_products")); return; } + + // 检查最低订单金额 + if (country_code !== 225) { + // 只有当 country_code 不是 255 时才进行最低订单金额检查 + if (convertedMinAmount !== null) { + // 如果有转换后的最低金额,检查当前总价是否满足要求 + console.log(`Checking converted minimum amount: ${convertedMinAmount} ${currency} vs current total: ${totalAmount}`); + if (totalAmount < convertedMinAmount) { + Alert.alert( + t("cart.notice"), + t("cart.minimum_order_required", { + amount: convertedMinAmount.toFixed(2), + currency: currency + }) + ); + return; + } + } else { + // 如果转换失败,使用原始的 50,000 FCFA 检查 + console.log(`Checking original minimum amount: 50000 FCFA vs current total: ${totalAmount}`); + if (totalAmount < 50000) { + Alert.alert( + t("cart.notice"), + t("cart.minimum_order_required", { + amount: "50,000", + currency: "FCFA" + }) + ); + return; + } + } + } + // 如果 country_code === 255,则不进行任何最低订单金额检查 + setItems(items); navigation.navigate("PreviewAddress"); }; @@ -735,19 +830,26 @@ export const CartScreen = () => { {/* Fixed Bottom Section */} {/* Order Summary */} - - - - + {country_code !== 225 && ( + + + + - - {t("cart.min_order_quantity")} - 50,000FCFA - - + + {t("cart.min_order_quantity")} + + {convertedMinAmount !== null + ? `${convertedMinAmount.toFixed(2)} ${currency}` + : "50,000FCFA" + } + + + + )} diff --git a/app/services/api/userApi.ts b/app/services/api/userApi.ts index 4a1320f..b9cffea 100644 --- a/app/services/api/userApi.ts +++ b/app/services/api/userApi.ts @@ -16,6 +16,7 @@ export interface User { balance:number, currency:string, country:string, + balance_currency:string, country_en:string, language:string, vip_level:number