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 { query } = require('../config/database')
const { toRelativeUrl } = require('../utils/image-url')
async function getGoods(ctx) { async function getGoods(ctx) {
let sql = 'SELECT * FROM goods WHERE 1=1' let sql = 'SELECT * FROM goods WHERE 1=1'
@@ -52,7 +53,7 @@ async function getGoods(ctx) {
async function getGoodsById(ctx) { async function getGoodsById(ctx) {
const goodsId = parseInt(ctx.params.id) const goodsId = parseInt(ctx.params.id)
const goods = await query('SELECT * FROM goods WHERE id = ?', [goodsId]) const goods = await query('SELECT * FROM goods WHERE id = ?', [goodsId])
if (goods.length > 0) { if (goods.length > 0) {
ctx.body = { ctx.body = {
code: 200, code: 200,
@@ -77,6 +78,9 @@ async function createGoods(ctx) {
return return
} }
// 将图片URL转换为相对路径存储
const relativeImages = (images || []).map(img => toRelativeUrl(img))
const sql = `INSERT INTO goods const sql = `INSERT INTO goods
(name, price, cost_price, unit, category_id, images, stock, pricing_type, is_hot, is_new, remark, goods_no, barcode) (name, price, cost_price, unit, category_id, images, stock, pricing_type, is_hot, is_new, remark, goods_no, barcode)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)` VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
@@ -87,7 +91,7 @@ async function createGoods(ctx) {
parseFloat(ctx.request.body.costPrice || 0), parseFloat(ctx.request.body.costPrice || 0),
unit, unit,
categoryId || null, categoryId || null,
JSON.stringify(images || []), JSON.stringify(relativeImages),
parseInt(stock) || 0, parseInt(stock) || 0,
parseInt(pricingType) || 1, parseInt(pricingType) || 1,
parseInt(isHot) || 0, parseInt(isHot) || 0,
@@ -125,6 +129,9 @@ async function updateGoods(ctx) {
return return
} }
// 将图片URL转换为相对路径存储
const relativeImages = (images || []).map(img => toRelativeUrl(img))
const sql = `UPDATE goods SET const sql = `UPDATE goods SET
name = ?, price = ?, original_price = ?, unit = ?, category_id = ?, images = ?, name = ?, price = ?, original_price = ?, unit = ?, category_id = ?, images = ?,
stock = ?, pricing_type = ?, is_hot = ?, is_new = ?, description = ? stock = ?, pricing_type = ?, is_hot = ?, is_new = ?, description = ?
@@ -136,7 +143,7 @@ async function updateGoods(ctx) {
parseFloat(ctx.request.body.originalPrice || 0), parseFloat(ctx.request.body.originalPrice || 0),
unit, unit,
categoryId || null, categoryId || null,
JSON.stringify(images || []), JSON.stringify(relativeImages),
parseInt(stock) || 0, parseInt(stock) || 0,
parseInt(pricingType) || 1, parseInt(pricingType) || 1,
parseInt(isHot) || 0, parseInt(isHot) || 0,
+1 -1
View File
@@ -47,7 +47,7 @@ router.post('/generate-product', async (ctx) => {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
body: JSON.stringify({ body: JSON.stringify({
model: 'deepseek-ai/deepseek-v4-pro', model: 'deepseek-v4-flash',
messages: [ messages: [
{ {
role: 'user', role: 'user',
+2 -2
View File
@@ -2,7 +2,6 @@ const Router = require('koa-router')
const multer = require('@koa/multer') const multer = require('@koa/multer')
const path = require('path') const path = require('path')
const fs = require('fs') const fs = require('fs')
const { IMG_BASE_URL } = require('../config/domain')
const router = new Router() const router = new Router()
@@ -37,7 +36,8 @@ router.post('/', upload.single('file'), async (ctx) => {
return return
} }
const fileUrl = `${IMG_BASE_URL}/${ctx.file.filename}` // 存储相对路径,前端使用时拼接域名
const fileUrl = `/img/${ctx.file.filename}`
ctx.body = { ctx.body = {
code: 200, code: 200,
message: '上传成功', message: '上传成功',
+71
View File
@@ -0,0 +1,71 @@
// 图片URL处理工具
const DOMAIN_CONFIG = require('./domain').DOMAIN_CONFIG;
// 将完整URL转换为相对路径(用于存储到数据库)
function toRelativeUrl(url) {
if (!url) return '';
// 移除域名前缀,保留相对路径
const baseUrl = DOMAIN_CONFIG.BASE_URL;
if (url.startsWith(baseUrl)) {
return url.replace(baseUrl, '');
}
// 移除其他可能的前缀
const patterns = [
/^https?:\/\/donghy\.top/,
/^https?:\/\/110\.42\.255\.239(:\d+)?/,
/^https?:\/\/localhost(:\d+)?/
];
for (const pattern of patterns) {
if (pattern.test(url)) {
return url.replace(pattern, '');
}
}
return url;
}
// 将相对路径转换为完整URL(用于返回给前端)
function toFullUrl(url) {
if (!url) return '';
// 如果已经是完整URL,直接返回
if (url.startsWith('http://') || url.startsWith('https://')) {
return url;
}
// 如果是相对路径,拼接当前域名
const baseUrl = DOMAIN_CONFIG.BASE_URL;
if (url.startsWith('/')) {
return baseUrl + url;
}
if (url.startsWith('img/')) {
return baseUrl + '/' + url;
}
return url;
}
// 处理商品图片字段
function processGoodsImages(goods) {
if (!goods) return goods;
const processItem = (item) => {
if (item.images) {
try {
const images = JSON.parse(item.images);
const fullUrls = images.map(img => toFullUrl(img));
item.images = JSON.stringify(fullUrls);
} catch (e) {
// 如果不是JSON格式,保持原样
}
}
return item;
};
if (Array.isArray(goods)) {
return goods.map(processItem);
}
return processItem(goods);
}
module.exports = {
toRelativeUrl,
toFullUrl,
processGoodsImages
};