代理后端出版

This commit is contained in:
2025-09-05 16:49:23 +08:00
parent e704c8abca
commit 141d1313d6
27 changed files with 6395 additions and 49 deletions

View File

@@ -3,6 +3,7 @@ const router = express.Router();
const { getDB } = require('../database');
const { agentAuth } = require('../middleware/agentAuth');
const { logger } = require('../config/logger');
const dayjs = require('dayjs');
/**
* 获取代理统计数据
@@ -17,8 +18,8 @@ router.get('/stats', agentAuth, async (req, res) => {
SELECT
COUNT(*) as total_users,
COUNT(CASE WHEN DATE(created_at) = CURDATE() THEN 1 END) as today_new_users,
COUNT(CASE WHEN last_login_at >= DATE_SUB(NOW(), INTERVAL 7 DAY) THEN 1 END) as active_users,
CAST(COALESCE(SUM(balance), 0) AS DECIMAL(10,2)) as total_balance
COUNT(CASE WHEN updated_at >= DATE_SUB(NOW(), INTERVAL 7 DAY) THEN 1 END) as active_users,
0 as total_balance
FROM agent_merchants
WHERE agent_id = ?
`, [agentId]);
@@ -28,8 +29,8 @@ router.get('/stats', agentAuth, async (req, res) => {
SELECT
CAST(COALESCE(SUM(commission_amount), 0) AS DECIMAL(10,2)) as total_commission,
CAST(COALESCE(SUM(CASE WHEN DATE(created_at) = CURDATE() THEN commission_amount ELSE 0 END), 0) AS DECIMAL(10,2)) as today_commission,
CAST(COALESCE(SUM(CASE WHEN status = 'paid' THEN commission_amount ELSE 0 END), 0) AS DECIMAL(10,2)) as paid_commission,
CAST(COALESCE(SUM(CASE WHEN status = 'pending' THEN commission_amount ELSE 0 END), 0) AS DECIMAL(10,2)) as pending_commission
CAST(COALESCE(SUM(commission_amount), 0) AS DECIMAL(10,2)) as paid_commission,
0 as pending_commission
FROM agent_commission_records
WHERE agent_id = ?
`, [agentId]);
@@ -38,9 +39,9 @@ router.get('/stats', agentAuth, async (req, res) => {
const [transferStats] = await getDB().execute(`
SELECT
COUNT(*) as total_transfers,
COUNT(CASE WHEN DATE(created_at) = CURDATE() THEN 1 END) as today_transfers,
CAST(COALESCE(SUM(amount), 0) AS DECIMAL(10,2)) as total_amount,
CAST(COALESCE(SUM(CASE WHEN DATE(created_at) = CURDATE() THEN amount ELSE 0 END), 0) AS DECIMAL(10,2)) as today_amount
COUNT(CASE WHEN DATE(t.created_at) = CURDATE() THEN 1 END) as today_transfers,
CAST(COALESCE(SUM(t.amount), 0) AS DECIMAL(10,2)) as total_amount,
CAST(COALESCE(SUM(CASE WHEN DATE(t.created_at) = CURDATE() THEN t.amount ELSE 0 END), 0) AS DECIMAL(10,2)) as today_amount
FROM transfers t
INNER JOIN agent_merchants am ON (t.from_user_id = am.merchant_id OR t.to_user_id = am.merchant_id)
WHERE am.agent_id = ?
@@ -78,7 +79,7 @@ router.get('/stats', agentAuth, async (req, res) => {
stack: error.stack,
agentId: req.agent?.id
});
res.status(500).json({
success: false,
message: '获取统计数据失败'
@@ -105,10 +106,27 @@ router.get('/user-growth-trend', agentAuth, async (req, res) => {
GROUP BY DATE(created_at)
ORDER BY date ASC
`, [agentId, parseInt(days)]);
// 填充缺失的日期注册数为0
const result = [];
for (let i = days - 1; i >= 0; i--) {
const date = dayjs().subtract(i, 'day');
const dateStr = date.format('YYYY-MM-DD');
// 修复日期比较将数据库返回的Date对象转换为字符串进行比较
const existingData = trendData.find(item => {
const itemDateStr = dayjs(item.date).format('YYYY-MM-DD');
return itemDateStr === dateStr;
});
result.push({
date: date.format('MM-DD'),
count: existingData ? existingData.count : 0
});
}
res.json({
success: true,
data: trendData
data: result
});
} catch (error) {
@@ -117,7 +135,7 @@ router.get('/user-growth-trend', agentAuth, async (req, res) => {
stack: error.stack,
agentId: req.agent?.id
});
res.status(500).json({
success: false,
message: '获取趋势数据失败'
@@ -145,9 +163,27 @@ router.get('/commission-trend', agentAuth, async (req, res) => {
ORDER BY date ASC
`, [agentId, parseInt(days)]);
// 填充缺失的日期佣金为0
const result = [];
for (let i = days - 1; i >= 0; i--) {
const date = dayjs().subtract(i, 'day');
const dateStr = date.format('YYYY-MM-DD');
const existingData = trendData.find(item => {
const itemDateStr = dayjs(item.date).format('YYYY-MM-DD');
return itemDateStr === dateStr;
});
result.push({
date: date.format('MM-DD'),
amount: existingData ? parseFloat(existingData.amount) : 0
});
}
res.json({
success: true,
data: trendData
data: result
});
} catch (error) {
@@ -156,7 +192,7 @@ router.get('/commission-trend', agentAuth, async (req, res) => {
stack: error.stack,
agentId: req.agent?.id
});
res.status(500).json({
success: false,
message: '获取趋势数据失败'
@@ -164,6 +200,95 @@ router.get('/commission-trend', agentAuth, async (req, res) => {
}
});
/**
* 获取用户活跃度数据
* GET /api/agent/user-activity
*/
router.get('/user-activity', agentAuth, async (req, res) => {
try {
const agentId = req.agent.id;
const { days = 7 } = req.query;
// 获取活跃用户趋势(基于转账活动)
const [activityTrend] = await getDB().execute(`
SELECT
DATE(t.created_at) as date,
COUNT(DISTINCT CASE WHEN t.from_user_id = am.merchant_id THEN t.from_user_id END) as active_senders,
COUNT(DISTINCT CASE WHEN t.to_user_id = am.merchant_id THEN t.to_user_id END) as active_receivers,
COUNT(DISTINCT am.merchant_id) as total_active_users
FROM transfers t
INNER JOIN agent_merchants am ON (t.from_user_id = am.merchant_id OR t.to_user_id = am.merchant_id)
WHERE am.agent_id = ?
AND t.created_at >= DATE_SUB(CURDATE(), INTERVAL ? DAY)
AND t.status = 'completed'
GROUP BY DATE(t.created_at)
ORDER BY date ASC
`, [agentId, parseInt(days)]);
// 获取用户活跃度统计
const [activityStats] = await getDB().execute(`
SELECT
COUNT(DISTINCT am.merchant_id) as total_users,
COUNT(DISTINCT CASE WHEN t.created_at >= DATE_SUB(NOW(), INTERVAL 1 DAY) THEN am.merchant_id END) as daily_active_users,
COUNT(DISTINCT CASE WHEN t.created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY) THEN am.merchant_id END) as weekly_active_users,
COUNT(DISTINCT CASE WHEN t.created_at >= DATE_SUB(NOW(), INTERVAL 30 DAY) THEN am.merchant_id END) as monthly_active_users,
ROUND(
COUNT(DISTINCT CASE WHEN t.created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY) THEN am.merchant_id END) * 100.0 /
NULLIF(COUNT(DISTINCT am.merchant_id), 0), 2
) as weekly_activity_rate
FROM agent_merchants am
LEFT JOIN transfers t ON (t.from_user_id = am.merchant_id OR t.to_user_id = am.merchant_id)
AND t.status = 'completed'
WHERE am.agent_id = ?
`, [agentId]);
// 填充缺失的日期
const trendResult = [];
for (let i = days - 1; i >= 0; i--) {
const date = dayjs().subtract(i, 'day');
const dateStr = date.format('YYYY-MM-DD');
const existingData = activityTrend.find(item => {
const itemDateStr = dayjs(item.date).format('YYYY-MM-DD');
return itemDateStr === dateStr;
});
trendResult.push({
date: date.format('MM-DD'),
active_users: existingData ? existingData.total_active_users : 0,
active_senders: existingData ? existingData.active_senders : 0,
active_receivers: existingData ? existingData.active_receivers : 0
});
}
res.json({
success: true,
data: {
trend: trendResult,
stats: activityStats[0] || {
total_users: 0,
daily_active_users: 0,
weekly_active_users: 0,
monthly_active_users: 0,
weekly_activity_rate: 0
}
}
});
} catch (error) {
logger.error('获取用户活跃度失败', {
error: error.message,
stack: error.stack,
agentId: req.agent?.id
});
res.status(500).json({
success: false,
message: '获取用户活跃度数据失败'
});
}
});
/**
* 获取佣金类型分布数据
* GET /api/agent/commission-distribution
@@ -194,7 +319,7 @@ router.get('/commission-distribution', agentAuth, async (req, res) => {
stack: error.stack,
agentId: req.agent?.id
});
res.status(500).json({
success: false,
message: '获取分布数据失败'
@@ -211,6 +336,7 @@ router.get('/recent-users', agentAuth, async (req, res) => {
const agentId = req.agent.id;
const { limit = 10 } = req.query;
const limitValue = Math.max(1, Math.min(100, parseInt(limit))); // 限制在1-100之间
const [recentUsers] = await getDB().execute(`
SELECT
u.id,
@@ -225,8 +351,8 @@ router.get('/recent-users', agentAuth, async (req, res) => {
LEFT JOIN users u ON am.merchant_id = u.id
WHERE am.agent_id = ?
ORDER BY am.created_at DESC
LIMIT ?
`, [agentId, parseInt(limit)]);
LIMIT ${limitValue}
`, [agentId]);
res.json({
success: true,
@@ -239,7 +365,7 @@ router.get('/recent-users', agentAuth, async (req, res) => {
stack: error.stack,
agentId: req.agent?.id
});
res.status(500).json({
success: false,
message: '获取最新用户失败'
@@ -256,12 +382,12 @@ router.get('/recent-commissions', agentAuth, async (req, res) => {
const agentId = req.agent.id;
const { limit = 10 } = req.query;
const limitValue = Math.max(1, Math.min(100, parseInt(limit))); // 限制在1-100之间
const [recentCommissions] = await getDB().execute(`
SELECT
acr.id,
acr.commission_type,
acr.commission_amount,
acr.status,
acr.created_at,
u.username,
u.real_name
@@ -269,8 +395,8 @@ router.get('/recent-commissions', agentAuth, async (req, res) => {
LEFT JOIN users u ON acr.merchant_id = u.id
WHERE acr.agent_id = ?
ORDER BY acr.created_at DESC
LIMIT ?
`, [agentId, parseInt(limit)]);
LIMIT ${limitValue}
`, [agentId]);
res.json({
success: true,
@@ -283,7 +409,7 @@ router.get('/recent-commissions', agentAuth, async (req, res) => {
stack: error.stack,
agentId: req.agent?.id
});
res.status(500).json({
success: false,
message: '获取最新佣金失败'