/** * Dashboard 路由 — 首页统计数据 */ const router = require('express').Router(); const { Op } = require('sequelize'); const Model = require('../models/Model'); const Ota = require('../models/Ota'); const logger = require('../config/logger'); const { ApiResponse } = require('../utils/response'); const { authMiddleware } = require('../middleware/auth'); // 所有 dashboard 接口需登录 router.use(authMiddleware); /** * GET /api/dashboard/today * 返回今日新增的耳机型号和 OTA 数据 */ router.get('/api/dashboard/today', async (req, res) => { try { // 今日 00:00:00 (UTC+8) const now = new Date(); const todayStart = new Date(now.getFullYear(), now.getMonth(), now.getDate()); // 今日新增型号 const newModels = await Model.findAll({ where: { create_at: { [Op.gte]: todayStart }, }, order: [['id', 'DESC']], attributes: ['id', 'brand_name', 'name', 'create_at'], }); // 今日新增 OTA const newOtas = await Ota.findAll({ where: { create_at: { [Op.gte]: todayStart }, }, order: [['id', 'DESC']], attributes: ['id', 'verName', 'model', 'create_at'], }); return res.json( ApiResponse.success({ models: newModels.map((m) => ({ id: m.id, brand_name: m.brand_name, name: m.name, create_at: m.create_at ? m.create_at.toISOString() : null, })), otas: newOtas.map((o) => ({ id: o.id, verName: o.verName, model: o.model, create_at: o.create_at ? o.create_at.toISOString() : null, })), }) ); } catch (e) { logger.error(`Error getting dashboard today stats: ${e.message}`); return res.json(ApiResponse.error(`获取统计数据失败:${e.message}`)); } }); module.exports = router;