update pay function
This commit is contained in:
@@ -33,14 +33,13 @@ import { TrendingUp, TrendingDown, DollarSign, AlertCircle, Zap } from 'lucide-r
|
||||
import { motion } from 'framer-motion';
|
||||
import { forumColors } from '@theme/forumTheme';
|
||||
import {
|
||||
buyPosition,
|
||||
sellPosition,
|
||||
buyShares,
|
||||
getUserAccount,
|
||||
calculateBuyCost,
|
||||
calculateSellRevenue,
|
||||
calculateTax,
|
||||
getTopic,
|
||||
} from '@services/predictionMarketService';
|
||||
import { getUserAccount, CREDIT_CONFIG } from '@services/creditSystemService';
|
||||
MARKET_CONFIG,
|
||||
} from '@services/predictionMarketService.api';
|
||||
import { CREDIT_CONFIG } from '@services/creditSystemService';
|
||||
import { useAuth } from '@contexts/AuthContext';
|
||||
|
||||
const MotionBox = motion(Box);
|
||||
@@ -53,9 +52,23 @@ const TradeModal = ({ isOpen, onClose, topic, mode = 'buy', onTradeSuccess }) =>
|
||||
const [selectedOption, setSelectedOption] = useState('yes');
|
||||
const [shares, setShares] = useState(1);
|
||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||
const [userAccount, setUserAccount] = useState(null);
|
||||
|
||||
// 获取用户账户
|
||||
const userAccount = user ? getUserAccount(user.id) : null;
|
||||
// 异步获取用户账户
|
||||
useEffect(() => {
|
||||
const fetchAccount = async () => {
|
||||
if (!user || !isOpen) return;
|
||||
try {
|
||||
const response = await getUserAccount();
|
||||
if (response.success) {
|
||||
setUserAccount(response.data);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取账户失败:', error);
|
||||
}
|
||||
};
|
||||
fetchAccount();
|
||||
}, [user, isOpen]);
|
||||
|
||||
// 重置状态
|
||||
useEffect(() => {
|
||||
@@ -79,15 +92,22 @@ const TradeModal = ({ isOpen, onClose, topic, mode = 'buy', onTradeSuccess }) =>
|
||||
let avgPrice = 0;
|
||||
|
||||
if (mode === 'buy') {
|
||||
cost = calculateBuyCost(selectedSide.total_shares, otherSide.total_shares, shares);
|
||||
tax = calculateTax(cost);
|
||||
totalCost = cost + tax;
|
||||
avgPrice = cost / shares;
|
||||
const costData = calculateBuyCost(
|
||||
selectedOption === 'yes' ? selectedSide.total_shares : otherSide.total_shares,
|
||||
selectedOption === 'yes' ? otherSide.total_shares : selectedSide.total_shares,
|
||||
shares
|
||||
);
|
||||
cost = costData.amount;
|
||||
tax = costData.tax;
|
||||
totalCost = costData.total;
|
||||
avgPrice = costData.avgPrice;
|
||||
} else {
|
||||
cost = calculateSellRevenue(selectedSide.total_shares, otherSide.total_shares, shares);
|
||||
// 卖出功能暂未实现,使用简化计算
|
||||
const currentPrice = selectedSide.current_price || MARKET_CONFIG.BASE_PRICE;
|
||||
cost = currentPrice * shares;
|
||||
tax = calculateTax(cost);
|
||||
totalCost = cost - tax;
|
||||
avgPrice = cost / shares;
|
||||
avgPrice = currentPrice;
|
||||
}
|
||||
|
||||
// 获取用户在该方向的持仓
|
||||
@@ -128,43 +148,49 @@ const TradeModal = ({ isOpen, onClose, topic, mode = 'buy', onTradeSuccess }) =>
|
||||
try {
|
||||
setIsSubmitting(true);
|
||||
|
||||
let result;
|
||||
if (mode === 'buy') {
|
||||
result = buyPosition({
|
||||
user_id: user.id,
|
||||
user_name: user.name || user.username,
|
||||
user_avatar: user.avatar,
|
||||
// 调用买入 API
|
||||
const response = await buyShares({
|
||||
topic_id: topic.id,
|
||||
option_id: selectedOption,
|
||||
direction: selectedOption,
|
||||
shares,
|
||||
});
|
||||
|
||||
if (response.success) {
|
||||
toast({
|
||||
title: '购买成功!',
|
||||
description: `花费${totalCost}积分,剩余 ${response.data.new_balance} 积分`,
|
||||
status: 'success',
|
||||
duration: 3000,
|
||||
});
|
||||
|
||||
// 刷新账户数据
|
||||
const accountResponse = await getUserAccount();
|
||||
if (accountResponse.success) {
|
||||
setUserAccount(accountResponse.data);
|
||||
}
|
||||
|
||||
// 通知父组件刷新
|
||||
if (onTradeSuccess) {
|
||||
onTradeSuccess(response.data);
|
||||
}
|
||||
|
||||
onClose();
|
||||
}
|
||||
} else {
|
||||
result = sellPosition({
|
||||
user_id: user.id,
|
||||
topic_id: topic.id,
|
||||
option_id: selectedOption,
|
||||
shares,
|
||||
// 卖出功能暂未实现
|
||||
toast({
|
||||
title: '功能暂未开放',
|
||||
description: '卖出功能正在开发中,敬请期待',
|
||||
status: 'warning',
|
||||
duration: 3000,
|
||||
});
|
||||
}
|
||||
|
||||
toast({
|
||||
title: mode === 'buy' ? '购买成功!' : '卖出成功!',
|
||||
description: mode === 'buy' ? `花费${totalCost}积分` : `获得${totalCost}积分`,
|
||||
status: 'success',
|
||||
duration: 3000,
|
||||
});
|
||||
|
||||
// 通知父组件刷新
|
||||
if (onTradeSuccess) {
|
||||
onTradeSuccess(result);
|
||||
}
|
||||
|
||||
onClose();
|
||||
} catch (error) {
|
||||
console.error('交易失败:', error);
|
||||
toast({
|
||||
title: '交易失败',
|
||||
description: error.message,
|
||||
description: error.response?.data?.message || error.message,
|
||||
status: 'error',
|
||||
duration: 3000,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user