后端: 添加用户统计API
This commit is contained in:
parent
51acd4294a
commit
2372b94ba5
@ -9,6 +9,7 @@ const userRoutes = require('./routes/user');
|
||||
const searchRoutes = require('./routes/search');
|
||||
const batchRoutes = require('./routes/batch');
|
||||
const trashRoutes = require('./routes/trash');
|
||||
const statsRoutes = require('./routes/stats');
|
||||
const errorHandler = require('./middleware/errorHandler');
|
||||
|
||||
const app = express();
|
||||
@ -26,6 +27,7 @@ app.use('/api/user', userRoutes);
|
||||
app.use('/api/search', searchRoutes);
|
||||
app.use('/api/batch', batchRoutes);
|
||||
app.use('/api/trash', trashRoutes);
|
||||
app.use('/api/stats', statsRoutes);
|
||||
|
||||
app.use(errorHandler);
|
||||
|
||||
|
||||
33
backend/src/routes/stats.js
Normal file
33
backend/src/routes/stats.js
Normal file
@ -0,0 +1,33 @@
|
||||
const express = require('express');
|
||||
const db = require('../db');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
// Get user statistics
|
||||
router.get('/', (req, res) => {
|
||||
const token = req.headers.authorization?.replace('Bearer ', '');
|
||||
if (!token) return res.status(401).json({ error: 'No token' });
|
||||
|
||||
try {
|
||||
const jwt = require('jsonwebtoken');
|
||||
const decoded = jwt.verify(token, process.env.JWT_SECRET || 'clouddisk-secret-key');
|
||||
|
||||
Promise.all([
|
||||
db.query('SELECT COUNT(*) as total FROM files WHERE user_id = ? AND deleted_at IS NULL', [decoded.userId]),
|
||||
db.query('SELECT SUM(size) as used FROM files WHERE user_id = ? AND deleted_at IS NULL AND is_folder = 0', [decoded.userId]),
|
||||
db.query('SELECT COUNT(*) as total FROM files WHERE user_id = ? AND deleted_at IS NOT NULL', [decoded.userId]),
|
||||
db.query('SELECT COUNT(*) as total FROM shares WHERE file_id IN (SELECT id FROM files WHERE user_id = ?)', [decoded.userId])
|
||||
]).then(([files, storage, trash, shares]) => {
|
||||
res.json({
|
||||
totalFiles: files[0]?.total || 0,
|
||||
storageUsed: storage[0]?.used || 0,
|
||||
trashCount: trash[0]?.total || 0,
|
||||
shareCount: shares[0]?.total || 0
|
||||
});
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Loading…
Reference in New Issue
Block a user