Files
nxxmdata/bank-backend/test-api.js

130 lines
3.7 KiB
JavaScript

const express = require('express');
const cors = require('cors');
const app = express();
// 中间件
app.use(cors());
app.use(express.json());
// 模拟认证中间件
const mockAuthMiddleware = (req, res, next) => {
req.user = { id: 1, username: 'test' };
next();
};
// 模拟仪表盘控制器
const mockDashboardController = {
getStats: (req, res) => {
res.json({
success: true,
message: '获取统计数据成功(模拟数据)',
data: {
overview: {
totalUsers: 1250,
totalAccounts: 3420,
totalTransactions: 15680,
totalBalance: 12500000.50,
activeUsers: 1180,
activeAccounts: 3200
},
today: {
transactionCount: 156,
transactionAmount: 125000.00
},
accountTypes: [
{ type: 'savings', count: 2100, totalBalance: 8500000.00 },
{ type: 'checking', count: 800, totalBalance: 3200000.00 },
{ type: 'credit', count: 400, totalBalance: 500000.00 },
{ type: 'loan', count: 120, totalBalance: 300000.00 }
],
trends: [
{ date: '2024-01-15', count: 45, totalAmount: 12500.00 },
{ date: '2024-01-16', count: 52, totalAmount: 15200.00 },
{ date: '2024-01-17', count: 38, totalAmount: 9800.00 },
{ date: '2024-01-18', count: 61, totalAmount: 18500.00 },
{ date: '2024-01-19', count: 48, totalAmount: 13200.00 },
{ date: '2024-01-20', count: 55, totalAmount: 16800.00 },
{ date: '2024-01-21', count: 42, totalAmount: 11200.00 }
]
}
});
},
getRecentTransactions: (req, res) => {
const { limit = 10 } = req.query;
const mockTransactions = [
{
id: 1,
type: 'deposit',
amount: 1000.00,
description: '存款',
status: 'completed',
created_at: new Date(),
account: {
account_number: '1234567890',
user: {
username: 'user1',
real_name: '张三'
}
}
},
{
id: 2,
type: 'withdrawal',
amount: 500.00,
description: '取款',
status: 'completed',
created_at: new Date(Date.now() - 3600000),
account: {
account_number: '1234567891',
user: {
username: 'user2',
real_name: '李四'
}
}
},
{
id: 3,
type: 'transfer',
amount: 200.00,
description: '转账',
status: 'completed',
created_at: new Date(Date.now() - 7200000),
account: {
account_number: '1234567892',
user: {
username: 'user3',
real_name: '王五'
}
}
}
].slice(0, parseInt(limit));
res.json({
success: true,
message: '获取最近交易记录成功(模拟数据)',
data: mockTransactions
});
}
};
// 路由
app.get('/health', (req, res) => {
res.json({ message: '银行后端服务运行正常', timestamp: new Date().toISOString() });
});
app.get('/api/dashboard', mockAuthMiddleware, mockDashboardController.getStats);
app.get('/api/dashboard/recent-transactions', mockAuthMiddleware, mockDashboardController.getRecentTransactions);
// 启动服务器
const PORT = 5351;
app.listen(PORT, () => {
console.log(`🚀 银行后端模拟服务已启动`);
console.log(`📡 服务地址: http://localhost:${PORT}`);
console.log(`🏥 健康检查: http://localhost:${PORT}/health`);
console.log(`📊 仪表盘API: http://localhost:${PORT}/api/dashboard`);
console.log(`💳 最近交易API: http://localhost:${PORT}/api/dashboard/recent-transactions`);
});