This commit is contained in:
董海洋
2026-05-24 17:05:26 +08:00
parent 0404d1770c
commit f6c88e5dd2
2 changed files with 28 additions and 11 deletions
+27 -10
View File
@@ -3,29 +3,46 @@ const { query } = require('../config/database')
async function getGoods(ctx) { async function getGoods(ctx) {
let sql = 'SELECT * FROM goods WHERE 1=1' let sql = 'SELECT * FROM goods WHERE 1=1'
const params = [] const params = []
if (ctx.query.hot === '1') { if (ctx.query.hot === '1') {
sql += ' AND is_hot = 1' sql += ' AND is_hot = 1'
} }
if (ctx.query.isNew === '1') { if (ctx.query.isNew === '1') {
sql += ' AND is_new = 1' sql += ' AND is_new = 1'
} }
if (ctx.query.category_id) { if (ctx.query.category_id) {
sql += ' AND category_id = ?' sql += ' AND category_id = ?'
params.push(parseInt(ctx.query.category_id)) params.push(parseInt(ctx.query.category_id))
} }
if (ctx.query.keyword) { if (ctx.query.keyword) {
sql += ' AND name LIKE ?' sql += ' AND (name LIKE ? OR barcode LIKE ?)'
params.push(`%${ctx.query.keyword}%`) params.push(`%${ctx.query.keyword}%`, `%${ctx.query.keyword}%`)
} }
sql += ' ORDER BY id DESC' if (ctx.query.inStock === '1') {
sql += ' AND stock > 0'
}
const sortField = ctx.query.sortBy || 'id'
const sortOrder = ctx.query.sortOrder === 'asc' ? 'ASC' : 'DESC'
const validSortFields = ['id', 'price', 'sales', 'stock', 'created_at']
if (validSortFields.includes(sortField)) {
sql += ` ORDER BY ${sortField} ${sortOrder}`
} else {
sql += ' ORDER BY id DESC'
}
if (ctx.query.limit) {
sql += ' LIMIT ?'
params.push(parseInt(ctx.query.limit))
}
const goods = await query(sql, params) const goods = await query(sql, params)
ctx.body = { ctx.body = {
code: 200, code: 200,
data: goods data: goods
+1 -1
View File
@@ -39,7 +39,7 @@ router.post('/', upload.single('file'), async (ctx) => {
const fileUrl = `https://donghy.top/img/${ctx.file.filename}` const fileUrl = `https://donghy.top/img/${ctx.file.filename}`
ctx.body = { ctx.body = {
code: 200, code: 200,
message: '上传成功**', message: '上传成功',
url: fileUrl url: fileUrl
} }
}) })