|
@@ -42,6 +42,7 @@ export default function GoodsDetailPage() {
|
|
|
const [quantity, setQuantity] = useState(1)
|
|
const [quantity, setQuantity] = useState(1)
|
|
|
const [selectedSpec, setSelectedSpec] = useState<SelectedSpec | null>(null)
|
|
const [selectedSpec, setSelectedSpec] = useState<SelectedSpec | null>(null)
|
|
|
const [showSpecModal, setShowSpecModal] = useState(false)
|
|
const [showSpecModal, setShowSpecModal] = useState(false)
|
|
|
|
|
+ const [pendingAction, setPendingAction] = useState<'add-to-cart' | 'buy-now' | null>(null)
|
|
|
const { addToCart } = useCart()
|
|
const { addToCart } = useCart()
|
|
|
|
|
|
|
|
// 模拟评价数据
|
|
// 模拟评价数据
|
|
@@ -256,11 +257,92 @@ export default function GoodsDetailPage() {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 规格选择确认
|
|
// 规格选择确认
|
|
|
- const handleSpecConfirm = (spec: SelectedSpec | null, qty: number) => {
|
|
|
|
|
|
|
+ const handleSpecConfirm = (spec: SelectedSpec | null, qty: number, actionType?: 'add-to-cart' | 'buy-now') => {
|
|
|
if (spec) {
|
|
if (spec) {
|
|
|
setSelectedSpec(spec)
|
|
setSelectedSpec(spec)
|
|
|
setQuantity(qty)
|
|
setQuantity(qty)
|
|
|
|
|
+
|
|
|
|
|
+ // 确定要执行的操作:优先使用传入的actionType,否则使用pendingAction状态
|
|
|
|
|
+ const actionToExecute = actionType || pendingAction
|
|
|
|
|
+
|
|
|
|
|
+ // 如果有待处理的操作,执行该操作
|
|
|
|
|
+ if (actionToExecute && goods) {
|
|
|
|
|
+ if (actionToExecute === 'add-to-cart') {
|
|
|
|
|
+ // 执行添加到购物车操作
|
|
|
|
|
+ const targetGoodsId = spec.id
|
|
|
|
|
+ const targetGoodsName = spec.name
|
|
|
|
|
+ const targetPrice = spec.price
|
|
|
|
|
+ const targetStock = spec.stock
|
|
|
|
|
+ const finalQuantity = qty === 0 ? 1 : qty
|
|
|
|
|
+
|
|
|
|
|
+ if (finalQuantity > targetStock) {
|
|
|
|
|
+ Taro.showToast({
|
|
|
|
|
+ title: '库存不足',
|
|
|
|
|
+ icon: 'none'
|
|
|
|
|
+ })
|
|
|
|
|
+ setPendingAction(null)
|
|
|
|
|
+ setShowSpecModal(false)
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 计算parentGoodsId:选择了规格,假设goods是父商品
|
|
|
|
|
+ const parentGoodsId = goods.id
|
|
|
|
|
+
|
|
|
|
|
+ addToCart({
|
|
|
|
|
+ id: targetGoodsId,
|
|
|
|
|
+ parentGoodsId: parentGoodsId,
|
|
|
|
|
+ name: targetGoodsName,
|
|
|
|
|
+ price: targetPrice,
|
|
|
|
|
+ image: goods.imageFile?.fullUrl || '',
|
|
|
|
|
+ stock: targetStock,
|
|
|
|
|
+ quantity: finalQuantity
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ Taro.showToast({
|
|
|
|
|
+ title: '已添加到购物车',
|
|
|
|
|
+ icon: 'success'
|
|
|
|
|
+ })
|
|
|
|
|
+ } else if (actionToExecute === 'buy-now') {
|
|
|
|
|
+ // 执行立即购买操作
|
|
|
|
|
+ const targetGoodsId = spec.id
|
|
|
|
|
+ const targetGoodsName = spec.name
|
|
|
|
|
+ const targetPrice = spec.price
|
|
|
|
|
+ const targetStock = spec.stock
|
|
|
|
|
+ const finalQuantity = qty === 0 ? 1 : qty
|
|
|
|
|
+
|
|
|
|
|
+ if (finalQuantity > targetStock) {
|
|
|
|
|
+ Taro.showToast({
|
|
|
|
|
+ title: '库存不足',
|
|
|
|
|
+ icon: 'none'
|
|
|
|
|
+ })
|
|
|
|
|
+ setPendingAction(null)
|
|
|
|
|
+ setShowSpecModal(false)
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Taro.removeStorageSync('buyNow')
|
|
|
|
|
+ Taro.removeStorageSync('checkoutItems')
|
|
|
|
|
+
|
|
|
|
|
+ // 将商品信息存入临时存储,跳转到订单确认页
|
|
|
|
|
+ Taro.setStorageSync('buyNow', {
|
|
|
|
|
+ goods: {
|
|
|
|
|
+ id: targetGoodsId,
|
|
|
|
|
+ name: targetGoodsName,
|
|
|
|
|
+ price: targetPrice,
|
|
|
|
|
+ image: goods.imageFile?.fullUrl || '',
|
|
|
|
|
+ quantity: finalQuantity
|
|
|
|
|
+ },
|
|
|
|
|
+ totalAmount: targetPrice * finalQuantity
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ Taro.navigateTo({
|
|
|
|
|
+ url: '/pages/order-submit/index'
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ setPendingAction(null)
|
|
|
setShowSpecModal(false)
|
|
setShowSpecModal(false)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -273,6 +355,13 @@ export default function GoodsDetailPage() {
|
|
|
const handleAddToCart = () => {
|
|
const handleAddToCart = () => {
|
|
|
if (!goods) return
|
|
if (!goods) return
|
|
|
|
|
|
|
|
|
|
+ // 如果是多规格商品且未选择规格,弹出规格选择器
|
|
|
|
|
+ if (hasSpecOptions && !selectedSpec) {
|
|
|
|
|
+ setPendingAction('add-to-cart')
|
|
|
|
|
+ setShowSpecModal(true)
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// 如果有选中的规格,使用规格信息;否则使用父商品信息
|
|
// 如果有选中的规格,使用规格信息;否则使用父商品信息
|
|
|
const targetGoodsId = selectedSpec ? selectedSpec.id : goods.id
|
|
const targetGoodsId = selectedSpec ? selectedSpec.id : goods.id
|
|
|
const targetGoodsName = selectedSpec ? selectedSpec.name : goods.name
|
|
const targetGoodsName = selectedSpec ? selectedSpec.name : goods.name
|
|
@@ -331,6 +420,13 @@ export default function GoodsDetailPage() {
|
|
|
const handleBuyNow = () => {
|
|
const handleBuyNow = () => {
|
|
|
if (!goods) return
|
|
if (!goods) return
|
|
|
|
|
|
|
|
|
|
+ // 如果是多规格商品且未选择规格,弹出规格选择器
|
|
|
|
|
+ if (hasSpecOptions && !selectedSpec) {
|
|
|
|
|
+ setPendingAction('buy-now')
|
|
|
|
|
+ setShowSpecModal(true)
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// 如果有选中的规格,使用规格信息;否则使用父商品信息
|
|
// 如果有选中的规格,使用规格信息;否则使用父商品信息
|
|
|
const targetGoodsId = selectedSpec ? selectedSpec.id : goods.id
|
|
const targetGoodsId = selectedSpec ? selectedSpec.id : goods.id
|
|
|
const targetGoodsName = selectedSpec ? selectedSpec.name : goods.name
|
|
const targetGoodsName = selectedSpec ? selectedSpec.name : goods.name
|
|
@@ -428,9 +524,11 @@ export default function GoodsDetailPage() {
|
|
|
<View className="goods-info-section">
|
|
<View className="goods-info-section">
|
|
|
<View className="goods-price-row">
|
|
<View className="goods-price-row">
|
|
|
<View className="price-container">
|
|
<View className="price-container">
|
|
|
- <Text className="current-price">¥{goods.price.toFixed(2)}</Text>
|
|
|
|
|
|
|
+ <Text className="current-price">
|
|
|
|
|
+ ¥{(selectedSpec ? selectedSpec.price : goods.price).toFixed(2)}
|
|
|
|
|
+ </Text>
|
|
|
<Text className="original-price">¥{goods.costPrice.toFixed(2)}</Text>
|
|
<Text className="original-price">¥{goods.costPrice.toFixed(2)}</Text>
|
|
|
- <Text className="price-suffix">起</Text>
|
|
|
|
|
|
|
+ {hasSpecOptions && !selectedSpec && <Text className="price-suffix">起</Text>}
|
|
|
</View>
|
|
</View>
|
|
|
<View className="sales-info">
|
|
<View className="sales-info">
|
|
|
<Text className="sales-text">已售{goods.salesNum}件</Text>
|
|
<Text className="sales-text">已售{goods.salesNum}件</Text>
|
|
@@ -443,19 +541,16 @@ export default function GoodsDetailPage() {
|
|
|
{/* 规格选择区域 */}
|
|
{/* 规格选择区域 */}
|
|
|
<View className="spec-selection-section">
|
|
<View className="spec-selection-section">
|
|
|
<Text className="spec-label">规格</Text>
|
|
<Text className="spec-label">规格</Text>
|
|
|
- <Button
|
|
|
|
|
- size="sm"
|
|
|
|
|
- variant="outline"
|
|
|
|
|
- className="spec-select-btn"
|
|
|
|
|
- onClick={handleOpenSpecModal}
|
|
|
|
|
- >
|
|
|
|
|
- {selectedSpec ? selectedSpec.name : '选择规格'}
|
|
|
|
|
- </Button>
|
|
|
|
|
- {selectedSpec && (
|
|
|
|
|
|
|
+ {selectedSpec ? (
|
|
|
<View className="selected-spec-info">
|
|
<View className="selected-spec-info">
|
|
|
|
|
+ <Text className="spec-name">{selectedSpec.name}</Text>
|
|
|
<Text className="spec-price">¥{selectedSpec.price.toFixed(2)}</Text>
|
|
<Text className="spec-price">¥{selectedSpec.price.toFixed(2)}</Text>
|
|
|
<Text className="spec-stock">库存: {selectedSpec.stock}</Text>
|
|
<Text className="spec-stock">库存: {selectedSpec.stock}</Text>
|
|
|
</View>
|
|
</View>
|
|
|
|
|
+ ) : (
|
|
|
|
|
+ <Text className="spec-placeholder">
|
|
|
|
|
+ {hasSpecOptions ? '请点击"加入购物车"或"立即购买"选择规格' : '单规格商品'}
|
|
|
|
|
+ </Text>
|
|
|
)}
|
|
)}
|
|
|
</View>
|
|
</View>
|
|
|
</View>
|
|
</View>
|
|
@@ -517,6 +612,21 @@ export default function GoodsDetailPage() {
|
|
|
</View>
|
|
</View>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+ {/* 操作按钮区域规格信息显示 */}
|
|
|
|
|
+ {hasSpecOptions && (
|
|
|
|
|
+ <View className="action-spec-info">
|
|
|
|
|
+ {selectedSpec ? (
|
|
|
|
|
+ <Text className="action-spec-text">
|
|
|
|
|
+ 已选规格: {selectedSpec.name} (¥{selectedSpec.price.toFixed(2)})
|
|
|
|
|
+ </Text>
|
|
|
|
|
+ ) : (
|
|
|
|
|
+ <Text className="action-spec-hint">
|
|
|
|
|
+ 请选择规格后操作
|
|
|
|
|
+ </Text>
|
|
|
|
|
+ )}
|
|
|
|
|
+ </View>
|
|
|
|
|
+ )}
|
|
|
|
|
+
|
|
|
<View className="button-section">
|
|
<View className="button-section">
|
|
|
<Button
|
|
<Button
|
|
|
className="add-cart-btn"
|
|
className="add-cart-btn"
|
|
@@ -550,11 +660,15 @@ export default function GoodsDetailPage() {
|
|
|
{/* 规格选择弹窗 */}
|
|
{/* 规格选择弹窗 */}
|
|
|
<GoodsSpecSelector
|
|
<GoodsSpecSelector
|
|
|
visible={showSpecModal}
|
|
visible={showSpecModal}
|
|
|
- onClose={() => setShowSpecModal(false)}
|
|
|
|
|
|
|
+ onClose={() => {
|
|
|
|
|
+ setShowSpecModal(false)
|
|
|
|
|
+ setPendingAction(null) // 重置待处理操作
|
|
|
|
|
+ }}
|
|
|
onConfirm={handleSpecConfirm}
|
|
onConfirm={handleSpecConfirm}
|
|
|
parentGoodsId={goods?.id || 0}
|
|
parentGoodsId={goods?.id || 0}
|
|
|
currentSpec={selectedSpec?.name}
|
|
currentSpec={selectedSpec?.name}
|
|
|
currentQuantity={quantity}
|
|
currentQuantity={quantity}
|
|
|
|
|
+ actionType={pendingAction}
|
|
|
/>
|
|
/>
|
|
|
</View>
|
|
</View>
|
|
|
)
|
|
)
|