diff --git a/app/screens/CartScreen.tsx b/app/screens/CartScreen.tsx index f5a7142..88b59f9 100644 --- a/app/screens/CartScreen.tsx +++ b/app/screens/CartScreen.tsx @@ -7,6 +7,8 @@ import { TouchableOpacity, ScrollView, Modal, + TextInput, + Alert, } from "react-native"; import BackIcon from "../components/BackIcon"; import fontSize from "../utils/fontsizeUtils"; @@ -38,6 +40,13 @@ export const CartScreen = () => { cartItemId: number; cartId1: number; } | null>(null); + const [quantityInputVisible, setQuantityInputVisible] = useState(false); + const [editingItem, setEditingItem] = useState<{ + cartId: number; + cartItemId: number; + currentQuantity: number; + } | null>(null); + const [quantityInput, setQuantityInput] = useState(''); const navigation = useNavigation>(); // 计算选中商品的总金额 @@ -338,6 +347,78 @@ export const CartScreen = () => { console.log(items); navigation.navigate("Recipient", { items }); }; + + // 添加更新商品数量的方法 + const updateQuantity = async (cartId: number, cartItemId: number, newQuantity: number) => { + try { + // 更新本地状态 + setCartList((prev) => { + const newList = prev.map((item) => { + if (item.cart_id === cartId) { + return { + ...item, + skus: item.skus.map((sku) => { + if (sku.cart_item_id === cartItemId) { + return { ...sku, quantity: newQuantity }; + } + return sku; + }), + }; + } + return item; + }); + calculateTotalAmount(newList); + return newList; + }); + + // 调用API更新数量 + await updateCartItem(cartId, { + cart_item_id: cartItemId, + quantity: newQuantity, + selected: 1, + }); + } catch (error) { + console.error("更新商品数量失败:", error); + // 如果更新失败,回滚本地状态 + getCart(); + } + }; + + // 处理减少数量 + const handleDecreaseQuantity = (cartId: number, cartItemId: number, currentQuantity: number) => { + if (currentQuantity > 1) { + updateQuantity(cartId, cartItemId, currentQuantity - 1); + } + }; + + // 处理增加数量 + const handleIncreaseQuantity = (cartId: number, cartItemId: number, currentQuantity: number) => { + updateQuantity(cartId, cartItemId, currentQuantity + 1); + }; + + // 处理数量输入框确认 + const handleQuantityInputConfirm = () => { + if (!editingItem) return; + + const newQuantity = parseInt(quantityInput); + if (isNaN(newQuantity) || newQuantity < 1) { + Alert.alert('提示', '请输入有效的数量'); + return; + } + + updateQuantity(editingItem.cartId, editingItem.cartItemId, newQuantity); + setQuantityInputVisible(false); + setEditingItem(null); + setQuantityInput(''); + }; + + // 处理点击数量显示 + const handleQuantityPress = (cartId: number, cartItemId: number, currentQuantity: number) => { + setEditingItem({ cartId, cartItemId, currentQuantity }); + setQuantityInput(currentQuantity.toString()); + setQuantityInputVisible(true); + }; + return ( { - - - - - - {sku.quantity} - - - + - + handleDecreaseQuantity(item.cart_id, sku.cart_item_id, sku.quantity)} + > + - + + handleQuantityPress(item.cart_id, sku.cart_item_id, sku.quantity)} + > + {sku.quantity} + + handleIncreaseQuantity(item.cart_id, sku.cart_item_id, sku.quantity)} + > + + + @@ -592,6 +682,46 @@ export const CartScreen = () => { + + {/* 数量输入弹窗 */} + setQuantityInputVisible(false)} + > + + + 修改数量 + + + { + setQuantityInputVisible(false); + setEditingItem(null); + setQuantityInput(''); + }} + > + 取消 + + + 确定 + + + + + ); }; @@ -1154,6 +1284,7 @@ const styles = StyleSheet.create({ paddingHorizontal: 20, alignItems: "center", gap: 21, + width: '80%', }, image: { width: 80, @@ -1170,21 +1301,24 @@ const styles = StyleSheet.create({ buttonContainer: { flexDirection: "row", justifyContent: "center", - marginTop: 10, + alignItems: "center", + width: "100%", + marginTop: 20, + gap: 20, }, cancelButton1: { - width: 160, + minWidth: 120, height: 50, - borderRadius: 25, + borderRadius: 30, backgroundColor: "#f2f3f5", justifyContent: "center", alignItems: "center", }, confirmButton: { - width: 160, + minWidth: 120, height: 50, - borderRadius: 25, - backgroundColor: "#002fa7", + borderRadius: 30, + backgroundColor: "#ff5100", justifyContent: "center", alignItems: "center", marginLeft: 20, @@ -1201,4 +1335,24 @@ const styles = StyleSheet.create({ color: "#ffffff", fontFamily: "Source Han Sans CN", // 同上 }, + quantityButtonText: { + fontSize: fontSize(18), + color: "#333", + fontWeight: "500", + }, + quantityText: { + fontSize: fontSize(14), + color: "#333", + fontWeight: "500", + }, + quantityInput: { + width: '100%', + height: 40, + borderWidth: 1, + borderColor: '#ddd', + borderRadius: 5, + paddingHorizontal: 10, + fontSize: fontSize(16), + textAlign: 'center', + }, }); diff --git a/app/screens/HomeScreen.tsx b/app/screens/HomeScreen.tsx index a0d6f5a..ab68340 100644 --- a/app/screens/HomeScreen.tsx +++ b/app/screens/HomeScreen.tsx @@ -30,6 +30,7 @@ import { LinearGradient } from "expo-linear-gradient"; import fontSize from "../utils/fontsizeUtils"; import CloseIcon from "../components/CloseIcon"; import CheckmarkIcon from "../components/CheckmarkIcon"; +import { getSubjectTransLanguage } from "../utils/languageUtils"; // 为图标定义类型 type IconProps = { name: string; @@ -219,7 +220,7 @@ export const HomeScreen = () => { numberOfLines={2} ellipsizeMode="tail" > - {item.subject || "暂无内容"} + {getSubjectTransLanguage(item)} diff --git a/app/utils/languageUtils.ts b/app/utils/languageUtils.ts index cfc4033..90f66f1 100644 --- a/app/utils/languageUtils.ts +++ b/app/utils/languageUtils.ts @@ -1,6 +1,6 @@ import { getCurrentLanguage } from '../i18n'; -export const getSystemLanguage = >(data: T): string => { +export const getSubjectTransLanguage = >(data: T): string => { // 获取当前i18n语言 const currentLang = getCurrentLanguage();