diff --git a/admin/customer_service.php b/admin/customer_service.php
index 97451b9..ac3c97c 100644
--- a/admin/customer_service.php
+++ b/admin/customer_service.php
@@ -327,16 +327,13 @@ let notifySound = new Audio('https://assets.mixkit.co/active_storage/sfx/2358/23
const users = await r.json();
if (users.error || !Array.isArray(users)) return;
- // Sound notification for new users or new messages
- let currentTotalMsgs = users.reduce((acc, u) => acc + (u.message ? 1 : 0), 0);
- if (lastMsgCount > 0 && currentTotalMsgs > lastMsgCount) {
+ // Sound notification for new messages based on total unread count
+ let currentUnread = users.reduce((acc, u) => acc + (parseInt(u.unread_count) || 0), 0);
+ if (lastMsgCount !== null && currentUnread > lastMsgCount) {
notifySound.play().catch(e => {});
- // Visual feedback
- if (document.hidden) {
- document.title = "【新消息】客服系统";
- }
+ if (document.hidden) document.title = "【新消息】客服系统";
}
- lastMsgCount = currentTotalMsgs;
+ lastMsgCount = currentUnread;
// Reset title when active
window.onfocus = () => { document.title = "客服系统"; };
diff --git a/admin/index.php b/admin/index.php
index b1394d8..8cc390a 100644
--- a/admin/index.php
+++ b/admin/index.php
@@ -6,7 +6,7 @@ $db = db();
$total_users = $db->query("SELECT COUNT(*) FROM users")->fetchColumn();
$total_recharge = $db->query("SELECT SUM(amount) FROM finance_requests WHERE type='recharge' AND status='3'")->fetchColumn() ?: 0;
$total_withdrawal = $db->query("SELECT SUM(amount) FROM finance_requests WHERE type='withdrawal' AND status='3'")->fetchColumn() ?: 0;
-$pending_finance = $db->query("SELECT COUNT(*) FROM finance_requests WHERE status='0'")->fetchColumn();
+$pending_finance = $db->query("SELECT COUNT(*) FROM finance_requests WHERE status IN ('0', '1', '2', 'pending', 'matched', 'account_sent', 'finished')")->fetchColumn();
$pending_kyc = $db->query("SELECT COUNT(*) FROM users WHERE kyc_status=1 AND kyc_name IS NOT NULL")->fetchColumn();
ob_start();
@@ -74,7 +74,7 @@ ob_start();
query("SELECT r.*, u.uid FROM finance_requests r JOIN users u ON r.user_id=u.id WHERE r.status='0' AND r.type='recharge' LIMIT 5")->fetchAll();
+ $pending_list = $db->query("SELECT r.*, u.uid FROM finance_requests r JOIN users u ON r.user_id=u.id WHERE r.status IN ('0', '1', '2', 'pending', 'matched', 'account_sent', 'finished') AND r.type='recharge' LIMIT 5")->fetchAll();
foreach ($pending_list as $p):
?>
@@ -133,7 +133,7 @@ ob_start();
充值审核
- = $db->query("SELECT COUNT(*) FROM finance_requests WHERE type='recharge' AND status='0'")->fetchColumn() ?>
+ = $db->query("SELECT COUNT(*) FROM finance_requests WHERE type='recharge' AND status IN ('0', '1', '2', 'pending', 'matched', 'account_sent', 'finished')")->fetchColumn() ?>
实名审核
diff --git a/api/admin_notifications.php b/api/admin_notifications.php
index 1d3d675..86a8acb 100644
--- a/api/admin_notifications.php
+++ b/api/admin_notifications.php
@@ -44,8 +44,8 @@ function getCount($db, $sql, $params) {
if ($admin['is_agent']) {
$agent_id = $admin_id;
- $pending_recharge = getCount($db, "SELECT COUNT(*) FROM finance_requests r JOIN users u ON r.user_id = u.id WHERE r.type = 'recharge' AND r.status IN ('0', 'pending') AND u.agent_id = ?", [$agent_id]);
- $pending_withdrawal = getCount($db, "SELECT COUNT(*) FROM finance_requests r JOIN users u ON r.user_id = u.id WHERE r.type = 'withdrawal' AND r.status IN ('0', 'pending') AND u.agent_id = ?", [$agent_id]);
+ $pending_recharge = getCount($db, "SELECT COUNT(*) FROM finance_requests r JOIN users u ON r.user_id = u.id WHERE r.type = 'recharge' AND r.status IN ('0', 'pending', 'matched', '1', 'account_sent', '2', 'finished') AND u.agent_id = ?", [$agent_id]);
+ $pending_withdrawal = getCount($db, "SELECT COUNT(*) FROM finance_requests r JOIN users u ON r.user_id = u.id WHERE r.type = 'withdrawal' AND r.status IN ('0', 'pending', '1', '2') AND u.agent_id = ?", [$agent_id]);
$pending_kyc = getCount($db, "SELECT COUNT(*) FROM users WHERE kyc_status = 1 AND agent_id = ?", [$agent_id]);
$active_binary = getCount($db, "SELECT COUNT(*) FROM binary_orders o JOIN users u ON o.user_id = u.id WHERE o.status = 'pending' AND u.agent_id = ?", [$agent_id]);
$active_spot = getCount($db, "SELECT COUNT(*) FROM spot_orders o JOIN users u ON o.user_id = u.id WHERE o.status = 0 AND u.agent_id = ?", [$agent_id]);
@@ -53,8 +53,8 @@ if ($admin['is_agent']) {
$new_messages = getCount($db, "SELECT COUNT(*) FROM messages m JOIN users u ON m.user_id = u.id WHERE m.sender = 'user' AND m.is_read = 0 AND u.agent_id = ?", [$agent_id]);
$new_registrations = getCount($db, "SELECT COUNT(*) FROM users WHERE agent_id = ? AND created_at > DATE_SUB(NOW(), INTERVAL 24 HOUR)", [$agent_id]);
} else {
- $pending_recharge = getCount($db, "SELECT COUNT(*) FROM finance_requests WHERE type = 'recharge' AND status IN ('0', 'pending')", []);
- $pending_withdrawal = getCount($db, "SELECT COUNT(*) FROM finance_requests WHERE type = 'withdrawal' AND status IN ('0', 'pending')", []);
+ $pending_recharge = getCount($db, "SELECT COUNT(*) FROM finance_requests WHERE type = 'recharge' AND status IN ('0', 'pending', 'matched', '1', 'account_sent', '2', 'finished')", []);
+ $pending_withdrawal = getCount($db, "SELECT COUNT(*) FROM finance_requests WHERE type = 'withdrawal' AND status IN ('0', 'pending', '1', '2')", []);
$pending_kyc = getCount($db, "SELECT COUNT(*) FROM users WHERE kyc_status = 1", []);
$active_binary = getCount($db, "SELECT COUNT(*) FROM binary_orders WHERE status = 'pending'", []);
$active_spot = getCount($db, "SELECT COUNT(*) FROM spot_orders WHERE status = 0", []);
diff --git a/api/chat.php b/api/chat.php
index c959a4e..b405aa6 100644
--- a/api/chat.php
+++ b/api/chat.php
@@ -233,8 +233,10 @@ if ($action === 'admin_get_all') {
exit;
}
try {
- // Robust query to get all active chat sessions, merging guest sessions into user sessions if IP or Session matches
- // Grouping by (final_user_id, effective_sid, effective_ip) to ensure "One ID, One Conversation"
+ // Improved query to get all active chat sessions.
+ // We group by user_id if it's set (>0).
+ // If user_id is 0, we group by session_id.
+ // If session_id is also empty, we group by IP.
$stmt = db()->query("
SELECT
v.final_user_id as user_id,
@@ -263,7 +265,7 @@ if ($action === 'admin_get_all') {
SUM(is_unread) as unread_count
FROM (
SELECT
- COALESCE(NULLIF(user_id, 0), (SELECT id FROM users WHERE registration_ip = m.ip_address AND m.ip_address != '---' AND m.ip_address != '' LIMIT 1), 0) as final_user_id,
+ IFNULL(user_id, 0) as final_user_id,
CASE WHEN ip_address = '---' THEN '' ELSE IFNULL(ip_address, '') END as effective_ip,
IFNULL(session_id, '') as effective_sid,
created_at as last_activity,
@@ -273,7 +275,7 @@ if ($action === 'admin_get_all') {
FROM messages m
UNION ALL
SELECT
- COALESCE(NULLIF(user_id, 0), (SELECT id FROM users WHERE registration_ip = cv.ip_address AND cv.ip_address != '---' AND cv.ip_address != '' LIMIT 1), 0) as final_user_id,
+ IFNULL(user_id, 0) as final_user_id,
CASE WHEN ip_address = '---' THEN '' ELSE IFNULL(ip_address, '') END as effective_ip,
IFNULL(session_id, '') as effective_sid,
last_ping as last_activity,
@@ -283,31 +285,43 @@ if ($action === 'admin_get_all') {
FROM chat_visitors cv
UNION ALL
SELECT
- COALESCE(NULLIF(user_id, 0), (SELECT id FROM users WHERE registration_ip = fr.ip_address AND fr.ip_address != '---' AND fr.ip_address != '' LIMIT 1), 0) as final_user_id,
+ IFNULL(user_id, 0) as final_user_id,
CASE WHEN ip_address = '---' THEN '' ELSE IFNULL(ip_address, '') END as effective_ip,
'' as effective_sid,
created_at as last_activity,
NULL as user_time,
1 as has_recharge,
0 as is_unread
- FROM finance_requests fr
+ FROM finance_requests fr WHERE fr.status NOT IN ('3', '4')
) t1
- GROUP BY final_user_id, (CASE WHEN final_user_id = 0 THEN effective_sid ELSE '' END), (CASE WHEN final_user_id = 0 AND effective_sid = '' THEN effective_ip ELSE '' END)
+ GROUP BY
+ CASE WHEN final_user_id > 0 THEN final_user_id ELSE 0 END,
+ CASE WHEN final_user_id = 0 THEN effective_sid ELSE '' END,
+ CASE WHEN final_user_id = 0 AND effective_sid = '' THEN effective_ip ELSE '' END
) v
LEFT JOIN (
- SELECT m1.*, m2.final_user_id as m_final_user_id, m2.effective_ip as m_effective_ip, m2.effective_sid as m_effective_sid FROM messages m1
+ SELECT m1.*,
+ IFNULL(m1.user_id, 0) as m_final_user_id,
+ IFNULL(m1.session_id, '') as m_effective_sid,
+ IFNULL(m1.ip_address, '') as m_effective_ip
+ FROM messages m1
INNER JOIN (
- SELECT MAX(id) as max_id, final_user_id, effective_ip, effective_sid FROM (
- SELECT id,
- COALESCE(NULLIF(user_id, 0), (SELECT id FROM users WHERE registration_ip = messages.ip_address AND messages.ip_address != '---' AND messages.ip_address != '' LIMIT 1), 0) as final_user_id,
- CASE WHEN ip_address = '---' THEN '' ELSE IFNULL(ip_address, '') END as effective_ip,
- IFNULL(session_id, '') as effective_sid
- FROM messages
- ) t2 GROUP BY final_user_id, (CASE WHEN final_user_id = 0 THEN effective_sid ELSE '' END), (CASE WHEN final_user_id = 0 AND effective_sid = '' THEN effective_ip ELSE '' END)
+ SELECT MAX(id) as max_id FROM messages GROUP BY
+ CASE WHEN user_id > 0 THEN user_id ELSE 0 END,
+ CASE WHEN user_id = 0 THEN session_id ELSE '' END,
+ CASE WHEN user_id = 0 AND (session_id = '' OR session_id IS NULL) THEN ip_address ELSE '' END
) m2 ON m1.id = m2.max_id
- ) m ON (v.final_user_id = m.m_final_user_id AND (v.final_user_id != 0 OR (v.effective_sid != '' AND v.effective_sid = m.m_effective_sid) OR (v.effective_sid = '' AND v.effective_ip = m.m_effective_ip)))
+ ) m ON (
+ (v.final_user_id > 0 AND v.final_user_id = m.m_final_user_id) OR
+ (v.final_user_id = 0 AND v.effective_sid != '' AND v.effective_sid = m.m_effective_sid) OR
+ (v.final_user_id = 0 AND v.effective_sid = '' AND v.effective_ip = m.m_effective_ip)
+ )
LEFT JOIN users u ON (v.final_user_id = u.id AND v.final_user_id != 0)
- LEFT JOIN chat_remarks r ON (v.final_user_id = r.user_id AND (v.final_user_id != 0 OR (v.effective_sid != '' AND v.effective_sid = r.session_id) OR (v.effective_sid = '' AND v.effective_ip = r.ip_address)))
+ LEFT JOIN chat_remarks r ON (
+ (v.final_user_id > 0 AND v.final_user_id = r.user_id) OR
+ (v.final_user_id = 0 AND v.effective_sid != '' AND v.effective_sid = r.session_id) OR
+ (v.final_user_id = 0 AND v.effective_sid = '' AND v.effective_ip = r.ip_address)
+ )
ORDER BY created_at DESC
");
$results = $stmt->fetchAll();
diff --git a/db/config.php b/db/config.php
index 120fd3e..fd53eee 100644
--- a/db/config.php
+++ b/db/config.php
@@ -24,6 +24,9 @@ function getRealIP() {
if (!empty($_SERVER['HTTP_CF_CONNECTING_IP'])) {
return $_SERVER['HTTP_CF_CONNECTING_IP'];
}
+ if (!empty($_SERVER['HTTP_X_REAL_IP'])) {
+ return $_SERVER['HTTP_X_REAL_IP'];
+ }
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
return trim($ips[0]);