2026-03-12 11:23:10 +08:00
|
|
|
const express = require('express');
|
|
|
|
|
const cors = require('cors');
|
|
|
|
|
const path = require('path');
|
2026-03-25 10:25:37 +08:00
|
|
|
const compression = require('compression');
|
2026-03-12 11:23:10 +08:00
|
|
|
|
2026-03-26 10:39:58 +08:00
|
|
|
const config = require('./config');
|
|
|
|
|
const logger = require('./utils/logger');
|
2026-03-31 16:59:32 +08:00
|
|
|
const { clearUserCache } = require('./utils/cache');
|
2026-03-12 11:23:10 +08:00
|
|
|
const db = require('./db');
|
|
|
|
|
const { router: authRouter, auth } = require('./routes/auth');
|
|
|
|
|
const statsRouter = require('./routes/stats');
|
|
|
|
|
const recordsRouter = require('./routes/records');
|
|
|
|
|
const ioRouter = require('./routes/io');
|
|
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
|
|
2026-03-26 10:39:58 +08:00
|
|
|
// 请求日志中间件
|
|
|
|
|
app.use(logger.requestLogger);
|
|
|
|
|
|
2026-03-12 11:23:10 +08:00
|
|
|
app.use(cors());
|
|
|
|
|
app.use(express.json());
|
2026-03-25 10:25:37 +08:00
|
|
|
app.use(compression());
|
|
|
|
|
app.use(express.static(path.join(__dirname, '..', 'public'), {
|
|
|
|
|
maxAge: '1h',
|
|
|
|
|
etag: true
|
|
|
|
|
}));
|
2026-03-12 11:23:10 +08:00
|
|
|
|
|
|
|
|
// 联系人自动补全
|
2026-03-25 10:25:37 +08:00
|
|
|
app.use('/api', (req, res, next) => {
|
|
|
|
|
res.setHeader('Cache-Control', 'no-cache');
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
2026-03-31 16:59:32 +08:00
|
|
|
// 全局缓存清除中间件:在写操作完成后清除用户缓存
|
|
|
|
|
app.use('/api', (req, res, next) => {
|
|
|
|
|
res.once('finish', () => {
|
|
|
|
|
if (['POST', 'DELETE', 'PUT', 'PATCH'].includes(req.method) && req.user?.id) {
|
|
|
|
|
clearUserCache(req.user.id);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
2026-03-12 11:23:10 +08:00
|
|
|
app.get('/api/contacts', auth, (req, res) => {
|
|
|
|
|
const field = req.query.field;
|
|
|
|
|
const allowed = ['boss', 'partner', 'source', 'destination'];
|
|
|
|
|
if (!field || !allowed.includes(field)) return res.status(400).json({ error: '无效字段' });
|
|
|
|
|
|
|
|
|
|
const rows = db.prepare(`
|
|
|
|
|
SELECT ${field} as name, MAX(created_at) as last_used, COUNT(*) as freq
|
|
|
|
|
FROM records WHERE user_id = ? AND ${field} IS NOT NULL AND ${field} != ''
|
|
|
|
|
GROUP BY ${field} ORDER BY MAX(created_at) DESC
|
|
|
|
|
`).all(req.user.id);
|
|
|
|
|
res.json(rows.map(r => r.name));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 路由挂载
|
|
|
|
|
app.use('/api', authRouter);
|
|
|
|
|
app.use('/api/stats', auth, statsRouter);
|
|
|
|
|
app.use('/api/records', auth, recordsRouter);
|
|
|
|
|
app.use('/api', auth, ioRouter);
|
|
|
|
|
|
2026-03-26 10:39:58 +08:00
|
|
|
// 404 处理
|
|
|
|
|
app.use((req, res) => {
|
|
|
|
|
res.status(404).json({ error: '接口不存在' });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 全局错误处理中间件
|
|
|
|
|
app.use((err, req, res, next) => {
|
|
|
|
|
// 记录错误日志
|
|
|
|
|
logger.error('Unhandled error:', err.message, err.stack);
|
|
|
|
|
|
|
|
|
|
// 处理特定错误类型
|
|
|
|
|
if (err.name === 'UnauthorizedError') {
|
|
|
|
|
return res.status(401).json({ error: '未授权访问' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (err.name === 'SyntaxError' && err.status === 400 && 'body' in err) {
|
|
|
|
|
return res.status(400).json({ error: 'JSON 格式错误' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 默认错误响应
|
|
|
|
|
const status = err.status || err.statusCode || 500;
|
|
|
|
|
const message = err.expose ? err.message : '服务器内部错误';
|
|
|
|
|
|
|
|
|
|
res.status(status).json({ error: message });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const PORT = config.port;
|
|
|
|
|
const HOST = config.host;
|
|
|
|
|
app.listen(PORT, HOST, () => {
|
|
|
|
|
logger.info(`服务器运行在 http://${HOST}:${PORT}`);
|
2026-03-12 11:23:10 +08:00
|
|
|
});
|