public.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. const express = require('express');
  2. const router = express.Router();
  3. const Transaction = require('../models/Transaction');
  4. const Group = require('../models/Group');
  5. // 获取群组交易记录(不需要认证)
  6. router.get('/transactions', async (req, res) => {
  7. try {
  8. const { page = 1, limit = 10, groupId } = req.query;
  9. if (!groupId) {
  10. return res.status(400).json({ message: '缺少群组ID参数' });
  11. }
  12. console.log('查询参数:', { page, limit, groupId });
  13. // 获取交易记录
  14. const result = await Transaction.findAll(
  15. { groupId: groupId.toString() },
  16. parseInt(page),
  17. parseInt(limit)
  18. );
  19. console.log('查询结果:', result);
  20. // 获取统计数据
  21. const today = new Date();
  22. today.setHours(0, 0, 0, 0);
  23. const firstDayOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
  24. // 获取今日收入
  25. const todayResult = await Transaction.findAll(
  26. {
  27. groupId: groupId.toString(),
  28. startDate: today.toISOString().split('T')[0],
  29. type: 'deposit'
  30. },
  31. 1,
  32. 1000000
  33. );
  34. const todayIncome = todayResult.transactions.reduce((sum, t) => sum + parseFloat(t.amount), 0);
  35. // 获取本月收入
  36. const monthResult = await Transaction.findAll(
  37. {
  38. groupId: groupId.toString(),
  39. startDate: firstDayOfMonth.toISOString().split('T')[0],
  40. type: 'deposit'
  41. },
  42. 1,
  43. 1000000
  44. );
  45. const monthIncome = monthResult.transactions.reduce((sum, t) => sum + parseFloat(t.amount), 0);
  46. // 获取总收入
  47. const totalResult = await Transaction.findAll(
  48. {
  49. groupId: groupId.toString(),
  50. type: 'deposit'
  51. },
  52. 1,
  53. 1000000
  54. );
  55. const totalIncome = totalResult.transactions.reduce((sum, t) => sum + parseFloat(t.amount), 0);
  56. // 获取今日订单数
  57. const todayOrdersResult = await Transaction.findAll(
  58. {
  59. groupId: groupId.toString(),
  60. startDate: today.toISOString().split('T')[0]
  61. },
  62. 1,
  63. 1000000
  64. );
  65. const todayOrders = todayOrdersResult.transactions.length;
  66. // 获取群组信息(包含费率和汇率)
  67. const groupInfo = await Group.findByGroupId(groupId.toString());
  68. if (!groupInfo) {
  69. return res.status(404).json({ message: '群组不存在' });
  70. }
  71. // 计算入款相关数据
  72. const depositCount = totalResult.transactions.length;
  73. const depositFeeRate = parseFloat(groupInfo.in_fee_rate) || 0;
  74. const depositExchangeRate = parseFloat(groupInfo.in_exchange_rate) || 1;
  75. // 计算出款相关数据
  76. const withdrawResult = await Transaction.findAll(
  77. {
  78. groupId: groupId.toString(),
  79. type: 'withdrawal'
  80. },
  81. 1,
  82. 1000000
  83. );
  84. const withdrawCount = withdrawResult.transactions.length;
  85. const totalWithdraw = withdrawResult.transactions.reduce((sum, t) => sum + parseFloat(t.amount), 0);
  86. const withdrawFeeRate = parseFloat(groupInfo.out_fee_rate) || 0;
  87. const withdrawExchangeRate = parseFloat(groupInfo.out_exchange_rate) || 1;
  88. // 计算余额
  89. const balance = totalIncome - totalWithdraw;
  90. res.json({
  91. transactions: result.transactions,
  92. total: result.total,
  93. statistics: {
  94. todayIncome,
  95. monthIncome,
  96. totalIncome,
  97. todayOrders
  98. },
  99. summary: {
  100. totalDeposit: totalIncome,
  101. depositCount,
  102. depositFeeRate,
  103. depositExchangeRate,
  104. totalWithdraw,
  105. withdrawCount,
  106. withdrawFeeRate,
  107. withdrawExchangeRate,
  108. balance
  109. }
  110. });
  111. } catch (error) {
  112. console.error('获取群组交易记录失败:', error);
  113. res.status(500).json({ message: '获取群组交易记录失败' });
  114. }
  115. });
  116. module.exports = router;