This commit is contained in:
2025-09-16 17:39:51 +08:00
parent 14a3c39f9d
commit 61476e2b8e
9 changed files with 768 additions and 722 deletions

View File

@@ -341,59 +341,7 @@ router.post('/confirm-not-received',
}
);
// 触发返还转账逻辑
async function triggerReturnTransfers(db, user_id, total_amount) {
// 将总金额分成3笔随机金额
const amounts = generateRandomAmounts(total_amount, 3);
const batch_id = `return_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
// 获取公户ID
const [publicAccount] = await db.execute(`
SELECT u.id
FROM users u
WHERE u.username = 'public_account'
`);
if (publicAccount.length === 0) {
throw new Error('公户不存在');
}
const public_user_id = publicAccount[0].id;
// 创建3笔返还转账记录
for (let i = 0; i < amounts.length; i++) {
await db.execute(`
INSERT INTO transfers (from_user_id, to_user_id, amount, transfer_type, description, batch_id, status)
VALUES (?, ?, ?, 'return', ?, ?, 'pending')
`, [
public_user_id,
user_id,
amounts[i],
`返还转账 ${i + 1}/3`,
batch_id
]);
}
}
// 生成随机金额分配
function generateRandomAmounts(total, count) {
const amounts = [];
let remaining = parseFloat(total);
for (let i = 0; i < count - 1; i++) {
// 确保每笔至少1元最多不超过剩余金额的80%
const min = 1;
const max = Math.max(min, remaining * 0.8);
const amount = Math.round((Math.random() * (max - min) + min) * 100) / 100;
amounts.push(amount);
remaining -= amount;
}
// 最后一笔是剩余金额
amounts.push(Math.round(remaining * 100) / 100);
return amounts;
}
// 获取用户转账记录
router.get('/user/:userId', authenticateToken, async (req, res) => {
@@ -413,7 +361,7 @@ router.get('/user/:userId', authenticateToken, async (req, res) => {
const limitNum = Math.max(1, Math.min(100, parseInt(limit) || 10));
const offset = Math.max(0, (pageNum - 1) * limitNum);
let whereClause = 'WHERE (t.from_user_id = ? OR t.to_user_id = ?)';
let whereClause = `WHERE source_type='manual' AND (t.from_user_id = ? OR t.to_user_id = ?)`;
const userIdInt = parseInt(userId);
let listParams = [userIdInt, userIdInt];
let countParams = [userIdInt, userIdInt];