This commit is contained in:
2025-09-17 14:00:46 +08:00
parent ecc9e3046e
commit cb0b401b6e
4 changed files with 25 additions and 205 deletions

View File

@@ -186,7 +186,7 @@ router.post('/login', async (req, res) => {
});
// 获取代理的商户列表(包含所有商户,标注早期商户状态)
router.get('/merchants/:agent_id', async (req, res) => {
router.get('/merchants/:agent_id',auth, async (req, res) => {
try {
const {agent_id} = req.params;
const {page = 1, limit = 10} = req.query;
@@ -235,15 +235,15 @@ router.get('/merchants/:agent_id', async (req, res) => {
WHERE (u.inviter = ? OR (u.created_at < ? AND u.district_id = ? AND u.role = 'user'))
ORDER BY u.created_at DESC
LIMIT ${parseInt(limit)} OFFSET ${parseInt(offset)}`,
[agentCreatedAt, parseInt(regionId), agentCreatedAt, parseInt(regionId), parseInt(userId), parseInt(agent_id), parseInt(agent_id), agentCreatedAt, parseInt(regionId)]
[agentCreatedAt, parseInt(regionId), agentCreatedAt, parseInt(regionId), parseInt(userId), agentCreatedAt, parseInt(regionId)]
);
// 获取总数(包括代理关联的商户和符合条件的早期商户)
const [countResult] = await getDB().execute(
`SELECT COUNT(*) as total
FROM users u
WHERE (u.inviter = ? OR (u.created_at < ? AND u.district_id = ? AND u.role = 'user'))`,
[parseInt(agent_id), parseInt(agent_id), agentCreatedAt, parseInt(regionId)]
WHERE (u.inviter = ? OR (u.created_at < ? AND u.district_id = ?))`,
[parseInt(userId), agentCreatedAt, parseInt(regionId)]
);
// 获取早期商户统计从user表获取所有符合条件的早期商户
@@ -262,7 +262,7 @@ router.get('/merchants/:agent_id', async (req, res) => {
`SELECT COUNT(*) as normal_merchant_count
FROM users u
WHERE (u.inviter = ? AND (u.created_at >= ? OR u.district_id != ?))`,
[parseInt(agent_id), parseInt(agent_id), agentCreatedAt, parseInt(regionId)]
[parseInt(userId), agentCreatedAt, parseInt(regionId)]
);
res.json({
@@ -286,18 +286,19 @@ router.get('/merchants/:agent_id', async (req, res) => {
});
// 获取代理的佣金记录
router.get('/commissions/:agent_id', async (req, res) => {
router.get('/commissions/:agent_id',auth, async (req, res) => {
try {
const {agent_id} = req.params;
const {id} = req.user
const {page = 1, limit = 10} = req.query;
const offset = (page - 1) * limit;
// 获取佣金记录
const [commissions] = await getDB().execute(
`SELECT acr.*, u.username, u.real_name
FROM agent_commission_records acr
JOIN users u ON acr.merchant_id = u.id
WHERE acr.agent_id = ${parseInt(agent_id)}
FROM transfers acr
JOIN users u ON acr.from_user_id = u.id
WHERE acr.to_user_id = ${parseInt(id)} AND source_type='agent'
ORDER BY acr.created_at DESC
LIMIT ${parseInt(limit)} OFFSET ${parseInt(offset)}`
);
@@ -305,9 +306,9 @@ router.get('/commissions/:agent_id', async (req, res) => {
// 获取总数和总佣金
const [summary] = await getDB().execute(
`SELECT COUNT(*) as total_records,
COALESCE(SUM(commission_amount), 0) as total_commission
FROM agent_commission_records
WHERE agent_id = ${parseInt(agent_id)}`
COALESCE(SUM(amount), 0) as total_commission
FROM transfers
WHERE source_type = 'agent' AND to_user_id=${id}`
);
// 由于agent_commission_records表没有status字段设置默认值
@@ -330,7 +331,7 @@ router.get('/commissions/:agent_id', async (req, res) => {
});
// 获取代理统计信息
router.get('/stats/:agent_id', async (req, res) => {
router.get('/stats/:agent_id', auth,async (req, res) => {
try {
const {agent_id} = req.params;
@@ -369,7 +370,7 @@ router.get('/stats/:agent_id', async (req, res) => {
});
// 获取代理列表
router.get('/list', async (req, res) => {
router.get('/list', auth,async (req, res) => {
try {
const {page = 1, limit = 10, status, region_id} = req.query;
const offset = (page - 1) * limit;
@@ -442,7 +443,7 @@ router.get('/list', async (req, res) => {
* @param {string} period - 时间周期 (7d, 30d, 3m)
* @returns {Object} 佣金趋势数据
*/
router.get('/commission-trend/:agent_id', async (req, res) => {
router.get('/commission-trend/:agent_id',auth, async (req, res) => {
try {
console.log(req.params, 'req.params')
const {agent_id} = req.params;
@@ -522,7 +523,7 @@ router.get('/commission-trend/:agent_id', async (req, res) => {
* @param {string} agent_id - 代理ID
* @returns {Object} 商户状态分布数据
*/
router.get('/merchant-status/:agent_id', async (req, res) => {
router.get('/merchant-status/:agent_id',auth, async (req, res) => {
try {
const {agent_id} = req.params;
let [agent] = await getDB().execute(`
@@ -564,7 +565,7 @@ router.get('/merchant-status/:agent_id', async (req, res) => {
* @param {string} agent_id - 代理ID
* @returns {Object} 详细统计数据
*/
router.get('/detailed-stats/:agent_id', async (req, res) => {
router.get('/detailed-stats/:agent_id',auth, async (req, res) => {
try {
const {agent_id} = req.params;
let [agentUserInfo] = await getDB().execute(`
@@ -656,7 +657,7 @@ router.get('/detailed-stats/:agent_id', async (req, res) => {
* @param {string} limit - 每页数量
* @returns {Object} 转账记录列表
*/
router.get('/merchants/:agent_id/transfers', async (req, res) => {
router.get('/merchants/:agent_id/transfers', auth,async (req, res) => {
try {
const {agent_id} = req.params;
const {page = 1, limit = 10} = req.query;