更新完善页面
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
const { query } = require('../config/database')
|
||||
|
||||
async function getHomeCategories(ctx) {
|
||||
const sql = `
|
||||
SELECT
|
||||
hc.id,
|
||||
hc.sort_order,
|
||||
c.id as category_id,
|
||||
c.name as category_name,
|
||||
c.icon as category_icon,
|
||||
c.image as category_image
|
||||
FROM home_categories hc
|
||||
LEFT JOIN categories c ON hc.category_id = c.id
|
||||
WHERE hc.is_enabled = 1 AND c.status = 1
|
||||
ORDER BY hc.sort_order ASC
|
||||
`
|
||||
|
||||
const categories = await query(sql)
|
||||
|
||||
const data = categories.map(item => ({
|
||||
id: item.category_id,
|
||||
name: item.category_name,
|
||||
icon: item.category_icon,
|
||||
image: item.category_image,
|
||||
sortOrder: item.sort_order
|
||||
}))
|
||||
|
||||
ctx.body = {
|
||||
code: 200,
|
||||
data
|
||||
}
|
||||
}
|
||||
|
||||
async function updateHomeCategories(ctx) {
|
||||
const { categories } = ctx.request.body
|
||||
|
||||
if (!Array.isArray(categories)) {
|
||||
ctx.body = {
|
||||
code: 400,
|
||||
message: '分类数据格式错误'
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
await query('UPDATE home_categories SET is_enabled = 0')
|
||||
|
||||
for (const item of categories) {
|
||||
if (item.categoryId && item.isEnabled) {
|
||||
await query(
|
||||
'UPDATE home_categories SET is_enabled = 1, sort_order = ? WHERE category_id = ?',
|
||||
[item.sortOrder, item.categoryId]
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
ctx.body = {
|
||||
code: 200,
|
||||
message: '更新成功'
|
||||
}
|
||||
}
|
||||
|
||||
async function getAllCategoriesForConfig(ctx) {
|
||||
const categories = await query('SELECT id, name, icon, image FROM categories WHERE status = 1 ORDER BY sort_order ASC')
|
||||
const homeCategories = await query('SELECT category_id, sort_order, is_enabled FROM home_categories')
|
||||
|
||||
const homeMap = {}
|
||||
for (const hc of homeCategories) {
|
||||
homeMap[hc.category_id] = {
|
||||
sortOrder: hc.sort_order,
|
||||
isEnabled: hc.is_enabled === 1
|
||||
}
|
||||
}
|
||||
|
||||
const data = categories.map(cat => ({
|
||||
id: cat.id,
|
||||
name: cat.name,
|
||||
icon: cat.icon,
|
||||
image: cat.image,
|
||||
...(homeMap[cat.id] || { sortOrder: 999, isEnabled: false })
|
||||
}))
|
||||
|
||||
ctx.body = {
|
||||
code: 200,
|
||||
data
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getHomeCategories,
|
||||
updateHomeCategories,
|
||||
getAllCategoriesForConfig
|
||||
}
|
||||
Reference in New Issue
Block a user