This commit is contained in:
董海洋
2026-05-24 18:18:44 +08:00
parent e532e48e47
commit 5674ef016e
4 changed files with 84 additions and 6 deletions
+10 -3
View File
@@ -1,4 +1,5 @@
const { query } = require('../config/database')
const { toRelativeUrl } = require('../utils/image-url')
async function getGoods(ctx) {
let sql = 'SELECT * FROM goods WHERE 1=1'
@@ -52,7 +53,7 @@ async function getGoods(ctx) {
async function getGoodsById(ctx) {
const goodsId = parseInt(ctx.params.id)
const goods = await query('SELECT * FROM goods WHERE id = ?', [goodsId])
if (goods.length > 0) {
ctx.body = {
code: 200,
@@ -77,6 +78,9 @@ async function createGoods(ctx) {
return
}
// 将图片URL转换为相对路径存储
const relativeImages = (images || []).map(img => toRelativeUrl(img))
const sql = `INSERT INTO goods
(name, price, cost_price, unit, category_id, images, stock, pricing_type, is_hot, is_new, remark, goods_no, barcode)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
@@ -87,7 +91,7 @@ async function createGoods(ctx) {
parseFloat(ctx.request.body.costPrice || 0),
unit,
categoryId || null,
JSON.stringify(images || []),
JSON.stringify(relativeImages),
parseInt(stock) || 0,
parseInt(pricingType) || 1,
parseInt(isHot) || 0,
@@ -125,6 +129,9 @@ async function updateGoods(ctx) {
return
}
// 将图片URL转换为相对路径存储
const relativeImages = (images || []).map(img => toRelativeUrl(img))
const sql = `UPDATE goods SET
name = ?, price = ?, original_price = ?, unit = ?, category_id = ?, images = ?,
stock = ?, pricing_type = ?, is_hot = ?, is_new = ?, description = ?
@@ -136,7 +143,7 @@ async function updateGoods(ctx) {
parseFloat(ctx.request.body.originalPrice || 0),
unit,
categoryId || null,
JSON.stringify(images || []),
JSON.stringify(relativeImages),
parseInt(stock) || 0,
parseInt(pricingType) || 1,
parseInt(isHot) || 0,