接口更改

This commit is contained in:
dzl
2025-10-11 17:33:00 +08:00
parent 2a16a7fd97
commit 3abcd5e46a
4 changed files with 112 additions and 38 deletions

37
routes/category.js Normal file
View File

@@ -0,0 +1,37 @@
const express = require('express');
const { getDB } = require('../database');
const router = express.Router();
router.get('/', async (req, res) => {
try {
const db = await getDB();
const [firstCategory] = await db.query('SELECT * FROM category WHERE level = 1');
console.log(firstCategory);
for (const category of firstCategory) {
const [secondCategories] = await db.query('SELECT * FROM category WHERE parent_id = ?', [category.id]);
category.relative = secondCategories;
}
res.json({ success: true, data: firstCategory.map(category => ({
name: category.category_name,
relative: category.relative.map(secondCategory => ({
name: secondCategory.category_name,
}))
}))});
} catch (error) {
console.error('Error fetching categories:', error);
res.status(500).json({ success: false, error: 'Internal server error' });
}
});
module.exports = router;