first commit
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
import { Router } from 'express';
|
||||
import db from '../db.js';
|
||||
import { auth } from '../middleware.js';
|
||||
|
||||
const router = Router();
|
||||
|
||||
// 物业通知列表
|
||||
router.get('/', (req, res) => {
|
||||
const notices = db.prepare('SELECT * FROM notices ORDER BY created_at DESC').all();
|
||||
res.json(notices);
|
||||
});
|
||||
|
||||
// 发布通知(管理员)
|
||||
router.post('/', auth, (req, res) => {
|
||||
if (req.user.role !== 'admin') {
|
||||
return res.status(403).json({ message: '仅管理员可发布通知' });
|
||||
}
|
||||
const { title, content, type } = req.body;
|
||||
if (!title || !content) {
|
||||
return res.status(400).json({ message: '请填写标题和内容' });
|
||||
}
|
||||
const info = db
|
||||
.prepare('INSERT INTO notices (title, content, type) VALUES (?, ?, ?)')
|
||||
.run(title, content, type || '通知');
|
||||
res.json(db.prepare('SELECT * FROM notices WHERE id = ?').get(info.lastInsertRowid));
|
||||
});
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user