38350-vm/spot.php
2026-02-11 11:52:19 +00:00

468 lines
27 KiB
PHP

<?php
session_start();
include 'header.php';
require_once 'db/config.php';
$user_id = $_SESSION['user_id'] ?? null;
$balance = 0;
if ($user_id) {
$stmt = db()->prepare("SELECT balance FROM users WHERE id = ?");
$stmt->execute([$user_id]);
$user = $stmt->fetch();
$balance = $user['balance'] ?? 0;
}
?>
<div style="background: #0b0e11; min-height: calc(100vh - 64px); padding: 10px 20px;">
<div class="spot-trading-layout" style="display: grid; grid-template-columns: 280px 1fr 320px; height: calc(100vh - 84px); background: #161a1e; border-radius: 8px; overflow: hidden; color: #EAECEF; font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif; border: 1px solid #1e2329;">
<!-- Column 1: Pairs List -->
<div style="border-right: 1px solid #1e2329; display: flex; flex-direction: column; background: #161a1e;">
<div style="padding: 12px; border-bottom: 1px solid #1e2329;">
<div style="position: relative; margin-bottom: 12px; display: flex; align-items: center;">
<div style="position: relative; width: 100%;">
<input type="text" id="pair-search" placeholder="<?php echo __('search_market', '搜索币对'); ?>" style="width: 100%; background: #0b0e11; border: 1px solid #2b3139; color: white; padding: 8px 32px 8px 12px; border-radius: 4px; font-size: 13px; outline: none; transition: border-color 0.2s;" onfocus="this.style.borderColor='#f0b90b'" onblur="this.style.borderColor='#2b3139'">
<i class="fas fa-search" style="position: absolute; right: 10px; top: 10px; color: #5e6673; font-size: 14px;"></i>
</div>
</div>
<div style="display: flex; gap: 15px;">
<button class="market-tab active" onclick="switchMarket('all')"><?php echo __('crypto', '加密货币'); ?></button>
</div>
</div>
<div style="display: flex; padding: 10px 12px; font-size: 11px; color: #848e9c; justify-content: space-between; border-bottom: 1px solid #1e2329;">
<span style="flex: 1.5;"><?php echo __('pair', '币对'); ?></span>
<span style="flex: 1; text-align: right;"><?php echo __('price', '价格'); ?></span>
<span style="flex: 1; text-align: right;"><?php echo __('change', '涨跌'); ?></span>
</div>
<div id="pairs-list" style="flex: 1; overflow-y: auto;">
<!-- JS Filled -->
</div>
</div>
<!-- Column 2: K-Line & Orders -->
<div style="display: flex; flex-direction: column; border-right: 1px solid #1e2329; overflow: hidden; background: #0b0e11;">
<!-- Top Info Bar -->
<div style="height: 60px; border-bottom: 1px solid #1e2329; display: flex; align-items: center; padding: 0 20px; gap: 20px; background: #161a1e;">
<div style="display: flex; align-items: center; gap: 10px;">
<img id="current-icon" src="https://raw.githubusercontent.com/spothq/cryptocurrency-icons/master/128/color/btc.png" width="32" onerror="this.src='https://cdn-icons-png.flaticon.com/512/2585/2585274.png'">
<div style="display: flex; flex-direction: column;">
<span style="font-weight: bold; font-size: 1.1rem; line-height: 1;" id="current-pair">BTC/USDT</span>
<span id="top-price" style="font-weight: bold; color: #f6465d; font-size: 1.1rem;">--.---</span>
</div>
<span id="top-change" style="color: #f6465d; font-size: 0.85rem; margin-top: 15px;">--%</span>
</div>
<div style="flex: 1;"></div>
<div style="display: flex; gap: 20px;">
<div class="stat-box-small">
<div style="font-size: 11px; color: #848e9c;"><?php echo __('high_24h', '24h最高'); ?></div>
<div id="top-high" style="color: white; font-size: 13px;">--.---</div>
</div>
<div class="stat-box-small">
<div style="font-size: 11px; color: #848e9c;"><?php echo __('low_24h', '24h最低'); ?></div>
<div id="top-low" style="color: white; font-size: 13px;">--.---</div>
</div>
</div>
</div>
<!-- Chart Section -->
<div style="flex: 2; border-bottom: 1px solid #1e2329; position: relative;">
<div id="tradingview_chart" style="width: 100%; height: 100%;"></div>
</div>
<!-- Orders Section -->
<div style="flex: 1; display: flex; flex-direction: column; background: #161a1e; overflow: hidden;">
<div style="display: flex; gap: 25px; padding: 0 20px; border-bottom: 1px solid #1e2329; height: 40px; align-items: center;">
<button class="order-tab-link active" onclick="switchOrderTab(this, 'open')"><?php echo __('current_order', '当前委托'); ?></button>
<button class="order-tab-link" onclick="switchOrderTab(this, 'history')"><?php echo __('order_history', '历史委托'); ?></button>
</div>
<div style="flex: 1; overflow-y: auto;">
<div id="orders-empty" style="display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100%; opacity: 0.3;">
<i class="fas fa-file-invoice" style="font-size: 48px; margin-bottom: 10px;"></i>
<div style="font-size: 13px;"><?php echo __('no_records', '暂无委托记录'); ?></div>
</div>
<table id="orders-table" style="width: 100%; font-size: 12px; color: #848e9c; text-align: left; border-collapse: collapse; display: none;">
<thead style="position: sticky; top: 0; background: #161a1e; z-index: 1;">
<tr style="border-bottom: 1px solid #1e2329;">
<th style="padding: 10px 15px;"><?php echo __('symbol', '币种'); ?></th>
<th style="padding: 10px 15px;"><?php echo __('side', '方向'); ?></th>
<th style="padding: 10px 15px;"><?php echo __('price', '价格'); ?></th>
<th style="padding: 10px 15px;"><?php echo __('amount', '数量'); ?></th>
<th style="padding: 10px 15px;"><?php echo __('pnl', '盈亏'); ?></th>
<th style="padding: 10px 15px;"><?php echo __('status', '状态'); ?></th>
<th style="padding: 10px 15px; text-align: right;"><?php echo __('action', '操作'); ?></th>
</tr>
</thead>
<tbody id="orders-list-body">
<!-- JS Filled -->
</tbody>
</table>
</div>
</div>
</div>
<!-- Column 3: Order Panel & Recent Trades -->
<div style="display: flex; flex-direction: column; background: #161a1e; overflow: hidden;">
<!-- Order Panel -->
<div style="padding: 0; border-bottom: 1px solid #1e2329; background: #161a1e;">
<div style="display: flex; background: #0b0e11; border-bottom: 1px solid #1e2329;">
<button class="side-tab buy active" onclick="switchSide('buy')"><?php echo __('buy', '买入'); ?></button>
<button class="side-tab sell" onclick="switchSide('sell')"><?php echo __('sell', '卖出'); ?></button>
</div>
<div style="padding: 15px;">
<div style="display: flex; gap: 15px; margin-bottom: 15px; font-size: 13px;">
<span id="tab-market" class="type-tab active" onclick="switchTradeType('market')"><?php echo __('market_order', '市价'); ?></span>
<span id="tab-limit" class="type-tab" onclick="switchTradeType('limit')"><?php echo __('limit_order', '限价'); ?></span>
</div>
<div style="margin-bottom: 12px; font-size: 12px; color: #848e9c; display: flex; justify-content: space-between;">
<span><?php echo __('available', '可用'); ?></span>
<span style="color: white;"><span id="display-balance"><?php echo number_format($balance, 2); ?></span> USDT</span>
</div>
<!-- Price Box -->
<div class="input-group" style="margin-bottom: 12px;">
<span style="position: absolute; left: 12px; top: 12px; color: #848e9c; font-size: 13px;"><?php echo __('price', '价格'); ?></span>
<input type="text" id="trade-price-display" value="<?php echo __('market_price', '市价'); ?>" readonly style="width: 100%; background: #2b3139; border: 1px solid #2b3139; color: #848e9c; padding: 12px 12px 12px 60px; border-radius: 4px; outline: none; font-size: 14px; text-align: right;">
<input type="number" id="trade-price" placeholder="0.00" style="display: none; width: 100%; background: #2b3139; border: 1px solid #2b3139; color: white; padding: 12px 60px 12px 60px; border-radius: 4px; outline: none; font-size: 14px; text-align: right;">
<span style="position: absolute; right: 12px; top: 12px; color: #848e9c; font-size: 13px;">USDT</span>
</div>
<!-- Quantity Box -->
<div class="input-group" style="margin-bottom: 15px;">
<span style="position: absolute; left: 12px; top: 12px; color: #848e9c; font-size: 13px;"><?php echo __('amount', '数量'); ?></span>
<input type="number" id="trade-amount" placeholder="0.00" style="width: 100%; background: #2b3139; border: 1px solid #2b3139; color: white; padding: 12px 60px 12px 60px; border-radius: 4px; outline: none; font-size: 14px; text-align: right;">
<span id="amount-unit" style="position: absolute; right: 12px; top: 12px; color: #848e9c; font-size: 13px;">USDT</span>
</div>
<!-- Percentage Slider -->
<div style="margin-bottom: 20px;">
<div style="position: relative; height: 20px; display: flex; align-items: center;">
<input type="range" class="custom-slider" min="0" max="100" value="0" id="trade-slider" oninput="updateFromSlider(this.value)">
<div class="slider-marks">
<span onclick="setPercent(0)"></span>
<span onclick="setPercent(25)"></span>
<span onclick="setPercent(50)"></span>
<span onclick="setPercent(75)"></span>
<span onclick="setPercent(100)"></span>
</div>
</div>
</div>
<button class="main-action-btn buy" id="main-trade-btn" onclick="executeOrder()"><?php echo __('buy', '买入'); ?> <span class="symbol-text">BTC</span></button>
</div>
</div>
<!-- Recent Trades -->
<div style="flex: 1; display: flex; flex-direction: column; overflow: hidden;">
<div style="padding: 12px 15px; font-size: 13px; font-weight: bold; border-bottom: 1px solid #1e2329;">
<?php echo __('recent_trades', '最新成交'); ?>
</div>
<div style="padding: 8px 15px; font-size: 11px; color: #848e9c; display: flex; justify-content: space-between;">
<span style="flex: 1;"><?php echo __('time', '时间'); ?></span>
<span style="flex: 1; text-align: center;"><?php echo __('price', '价格'); ?></span>
<span style="flex: 1; text-align: right;"><?php echo __('amount', '数量'); ?></span>
</div>
<div id="recent-trades-list" style="flex: 1; overflow-y: auto;">
<!-- JS Filled -->
</div>
</div>
</div>
</div>
</div>
<style>
.market-tab { background: transparent; border: none; color: #848e9c; font-size: 13px; font-weight: bold; padding: 5px 0; cursor: pointer; border-bottom: 2px solid transparent; }
.market-tab.active { color: #f0b90b; border-bottom-color: #f0b90b; }
.stat-box-small { display: flex; flex-direction: column; align-items: flex-end; }
.order-tab-link { background: transparent; border: none; color: #848e9c; font-size: 13px; font-weight: bold; cursor: pointer; padding: 0; border-bottom: 2px solid transparent; height: 100%; transition: color 0.2s; }
.order-tab-link.active { color: #f0b90b; border-bottom-color: #f0b90b; }
.side-tab { flex: 1; border: none; padding: 12px; font-weight: bold; cursor: pointer; background: transparent; color: #848e9c; font-size: 15px; transition: 0.2s; }
.side-tab.buy.active { color: #00c087; background: rgba(0, 192, 135, 0.1); border-bottom: 2px solid #00c087; }
.side-tab.sell.active { color: #f6465d; background: rgba(246, 70, 93, 0.1); border-bottom: 2px solid #f6465d; }
.type-tab { cursor: pointer; color: #848e9c; padding-bottom: 2px; border-bottom: 2px solid transparent; }
.type-tab.active { color: #f0b90b; border-bottom-color: #f0b90b; }
.input-group { position: relative; width: 100%; }
.main-action-btn { width: 100%; padding: 12px; border: none; border-radius: 4px; font-weight: bold; cursor: pointer; color: white; font-size: 15px; transition: 0.2s; }
.main-action-btn.buy { background: #00c087; }
.main-action-btn.buy:hover { background: #00d696; }
.main-action-btn.sell { background: #f6465d; }
.main-action-btn.sell:hover { background: #ff526a; }
.custom-slider { -webkit-appearance: none; width: 100%; height: 4px; background: #2b3139; border-radius: 2px; outline: none; z-index: 2; position: relative; }
.custom-slider::-webkit-slider-thumb { -webkit-appearance: none; width: 12px; height: 12px; background: white; border-radius: 50%; cursor: pointer; border: 2px solid #f0b90b; }
.slider-marks { position: absolute; left: 0; right: 0; top: 0; bottom: 0; display: flex; justify-content: space-between; align-items: center; padding: 0 2px; z-index: 1; pointer-events: none; }
.slider-marks span { width: 6px; height: 6px; background: #2b3139; border-radius: 50%; border: 1px solid #1e2329; pointer-events: auto; cursor: pointer; }
.slider-marks span:hover { background: #f0b90b; }
.trade-row { display: flex; padding: 4px 15px; font-size: 11px; }
.trade-row span { flex: 1; }
</style>
<script type="text/javascript" src="https://s3.tradingview.com/tv.js"></script>
<script>
let currentSymbol = 'BTCUSDT';
let currentPrice = 0;
let currentSide = 'buy';
let currentTradeType = 'market';
let currentOrderTab = 'open';
let marketData = {};
let pairs = ['BTCUSDT', 'ETHUSDT', 'XRPUSDT', 'LTCUSDT', 'BNBUSDT', 'SOLUSDT', 'TRXUSDT', 'LINKUSDT', 'RUNEUSDT', 'COMPUSDT', 'DOGEUSDT', 'RAYUSDT', 'DGBUSDT', 'COTIUSDT', 'KAVAUSDT', 'LRCUSDT', 'PEPEUSDT', 'FLOKIUSDT', 'SHIBUSDT', 'AVAXUSDT'];
function initChart(symbol) {
new TradingView.widget({
"width": "100%", "height": "100%", "symbol": "BINANCE:" + symbol, "interval": "1", "timezone": "Etc/UTC", "theme": "dark", "style": "1", "locale": "zh_CN", "container_id": "tradingview_chart", "backgroundColor": "#0b0e11", "gridColor": "rgba(42, 46, 57, 0.05)", "hide_side_toolbar": false, "allow_symbol_change": false, "save_image": false, "toolbar_bg": "#161a1e"
});
}
initChart(currentSymbol);
let socket;
function connectWS() {
if (socket) socket.close();
socket = new WebSocket('wss://stream.binance.com:9443/ws/' + pairs.map(p => p.toLowerCase() + '@ticker').join('/'));
socket.onmessage = (event) => {
const data = JSON.parse(event.data);
const s = data.s;
marketData[s] = data;
renderPairsList();
if (s === currentSymbol) {
currentPrice = parseFloat(data.c);
document.getElementById('top-price').innerText = currentPrice.toLocaleString();
document.getElementById('top-price').style.color = data.P >= 0 ? '#00c087' : '#f6465d';
document.getElementById('top-change').innerText = (data.P >= 0 ? '+' : '') + data.P + '%';
document.getElementById('top-change').style.color = data.P >= 0 ? '#00c087' : '#f6465d';
document.getElementById('top-high').innerText = parseFloat(data.h).toLocaleString();
document.getElementById('top-low').innerText = parseFloat(data.l).toLocaleString();
addRandomTrade(currentPrice);
}
};
}
connectWS();
function renderPairsList() {
const list = document.getElementById('pairs-list');
const searchTerm = document.getElementById('pair-search').value.toUpperCase();
if (!list) return;
let html = '';
pairs.forEach(p => {
if (searchTerm && !p.includes(searchTerm)) return;
const d = marketData[p] || {c: 0, P: 0};
const color = d.P >= 0 ? '#00c087' : '#f6465d';
const name = p.replace('USDT', '');
html += `
<div onclick="changePair('${p}')" style="display: flex; justify-content: space-between; padding: 10px 12px; cursor: pointer; border-bottom: 1px solid #1e2329; ${currentSymbol === p ? 'background: #2b3139;' : ''} transition: background 0.2s;">
<div style="display: flex; align-items: center; gap: 8px; flex: 1.5;">
<img src="https://raw.githubusercontent.com/spothq/cryptocurrency-icons/master/128/color/${name.toLowerCase()}.png" width="20" height="20" onerror="this.src='https://cdn-icons-png.flaticon.com/512/2585/2585274.png'">
<span style="font-weight: bold; font-size: 13px;">${name}/USDT</span>
</div>
<span style="font-size: 13px; flex: 1; text-align: right;">${parseFloat(d.c).toLocaleString()}</span>
<span style="color: ${color}; font-size: 13px; flex: 1; text-align: right;">${d.P >= 0 ? '+' : ''}${d.P}%</span>
</div>
`;
});
list.innerHTML = html;
}
async function changePair(pair) {
if (!pair.includes('USDT')) pair = pair + 'USDT';
currentSymbol = pair;
const name = pair.replace('USDT', '');
document.getElementById('current-pair').innerText = name + '/USDT';
document.getElementById('current-icon').src = `https://raw.githubusercontent.com/spothq/cryptocurrency-icons/master/128/color/${name.toLowerCase()}.png`;
document.querySelectorAll('.symbol-text').forEach(el => el.innerText = name);
initChart(pair);
renderPairsList();
document.getElementById('recent-trades-list').innerHTML = '';
document.getElementById('trade-price').value = '';
document.getElementById('trade-amount').value = '';
document.getElementById('trade-slider').value = 0;
}
function switchSide(side) {
currentSide = side;
document.querySelectorAll('.side-tab').forEach(btn => btn.classList.remove('active'));
document.querySelector('.side-tab.' + side).classList.add('active');
const btn = document.getElementById('main-trade-btn');
btn.className = 'main-action-btn ' + side;
btn.innerHTML = (side === 'buy' ? '<?php echo __("buy", "买入"); ?>' : '<?php echo __("sell", "卖出"); ?>') + ' <span class="symbol-text">' + currentSymbol.replace('USDT', '') + '</span>';
updateFromSlider(document.getElementById('trade-slider').value);
}
function switchTradeType(type) {
currentTradeType = type;
document.getElementById('tab-market').classList.toggle('active', type === 'market');
document.getElementById('tab-limit').classList.toggle('active', type === 'limit');
if (type === 'market') {
document.getElementById('trade-price').style.display = 'none';
document.getElementById('trade-price-display').style.display = 'block';
document.getElementById('amount-unit').innerText = 'USDT';
} else {
document.getElementById('trade-price').style.display = 'block';
document.getElementById('trade-price-display').style.display = 'none';
document.getElementById('trade-price').value = currentPrice;
document.getElementById('amount-unit').innerText = currentSymbol.replace('USDT', '');
}
}
function setPercent(p) {
document.getElementById('trade-slider').value = p;
updateFromSlider(p);
}
function updateFromSlider(val) {
const balance = <?php echo $balance; ?>;
const mockAssetBalance = 1.5;
if (currentSide === 'buy') {
const spend = (balance * val / 100);
if (currentTradeType === 'market') {
document.getElementById('trade-amount').value = spend.toFixed(2);
} else {
const price = parseFloat(document.getElementById('trade-price').value) || currentPrice;
document.getElementById('trade-amount').value = (spend / price).toFixed(6);
}
} else {
const sellAmount = (mockAssetBalance * val / 100);
if (currentTradeType === 'market') {
document.getElementById('trade-amount').value = (sellAmount * currentPrice).toFixed(2);
} else {
document.getElementById('trade-amount').value = sellAmount.toFixed(6);
}
}
}
function addRandomTrade(price) {
const list = document.getElementById('recent-trades-list');
const now = new Date();
const time = now.getHours().toString().padStart(2, '0') + ':' + now.getMinutes().toString().padStart(2, '0') + ':' + now.getSeconds().toString().padStart(2, '0');
const side = Math.random() > 0.5 ? 'buy' : 'sell';
const color = side === 'buy' ? '#00c087' : '#f6465d';
const amount = (Math.random() * 2).toFixed(4);
const row = document.createElement('div');
row.className = 'trade-row';
row.innerHTML = `
<span style="color: #848e9c;">${time}</span>
<span style="color: ${color}; text-align: center;">${price.toLocaleString()}</span>
<span style="text-align: right;">${amount}</span>
`;
list.prepend(row);
if (list.children.length > 20) list.lastElementChild.remove();
}
async function executeOrder() {
const price = currentTradeType === 'market' ? currentPrice : parseFloat(document.getElementById('trade-price').value);
let amount = parseFloat(document.getElementById('trade-amount').value);
if (!amount) return alert('Please enter amount');
let total = amount;
if (currentTradeType === 'limit') {
total = amount * price;
} else {
if (document.getElementById('amount-unit').innerText === 'USDT') {
amount = amount / price;
} else {
total = amount * price;
}
}
const response = await fetch('api/place_order.php', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
symbol: currentSymbol,
type: 'spot',
side: currentSide,
order_type: currentTradeType,
price: price,
amount: amount,
total: total
})
});
const result = await response.json();
if (result.success) {
alert('Order placed successfully!');
fetchOrders();
} else {
alert('Error: ' + result.error);
}
}
function switchOrderTab(el, tab) {
currentOrderTab = tab;
document.querySelectorAll('.order-tab-link').forEach(t => t.classList.remove('active'));
el.classList.add('active');
fetchOrders();
}
async function fetchOrders() {
const response = await fetch(`api/get_orders.php?type=spot&status=${currentOrderTab}`);
const result = await response.json();
const tbody = document.getElementById('orders-list-body');
const table = document.getElementById('orders-table');
const empty = document.getElementById('orders-empty');
if (result.success && result.data.length > 0) {
table.style.display = 'table';
empty.style.display = 'none';
let html = '';
result.data.forEach(o => {
const color = o.side === 'buy' ? '#00c087' : '#f6465d';
let pnl = '0.00';
let pnlColor = '#848e9c';
if (o.win_loss === 'win') {
pnl = '+' + (o.total * 0.05).toFixed(2);
pnlColor = '#00c087';
} else if (o.win_loss === 'loss') {
pnl = '-' + (o.total * 0.05).toFixed(2);
pnlColor = '#f6465d';
}
html += `
<tr style="border-bottom: 1px solid #1e2329;">
<td style="padding: 10px 15px; color: white; font-weight: bold;">${o.symbol}</td>
<td style="padding: 10px 15px; color: ${color}; font-weight: bold;">${o.side === 'buy' ? '买入' : '卖出'}</td>
<td style="padding: 10px 15px;">${parseFloat(o.price).toLocaleString()}</td>
<td style="padding: 10px 15px;">${parseFloat(o.amount).toFixed(6)}</td>
<td style="padding: 10px 15px; color: ${pnlColor}; font-weight: bold;">${pnl} USDT</td>
<td style="padding: 10px 15px;"><span style="background: rgba(255,255,255,0.05); padding: 2px 6px; border-radius: 2px;">${o.status === 'open' ? '进行中' : '已完成'}</span></td>
<td style="padding: 10px 15px; text-align: right;">
${o.status === 'open' ? `<button onclick="cancelOrder(${o.id})" style="background: rgba(246, 70, 93, 0.1); border: 1px solid #f6465d; color: #f6465d; padding: 2px 8px; border-radius: 4px; cursor: pointer; font-size: 11px;">取消</button>` : '--'}
</td>
</tr>
`;
});
tbody.innerHTML = html;
} else {
table.style.display = 'none';
empty.style.display = 'flex';
}
}
async function cancelOrder(id) {
if (!confirm('确定取消?')) return;
const response = await fetch('api/cancel_order.php', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ order_id: id })
});
const result = await response.json();
if (result.success) {
fetchOrders();
}
}
document.getElementById('pair-search').addEventListener('input', renderPairsList);
fetchOrders();
setInterval(fetchOrders, 5000);
</script>
<?php include 'footer.php'; ?>