api
This commit is contained in:
+19
-2
@@ -18,11 +18,28 @@ async function getGoods(ctx) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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}%`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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'
|
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)
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -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
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user