This commit is contained in:
董海洋
2026-05-24 21:26:53 +08:00
parent ed34731312
commit c621cbf46e
+3 -21
View File
@@ -1,7 +1,5 @@
const Router = require('koa-router');
const fetch = require('node-fetch');
const path = require('path');
const fs = require('fs');
const router = new Router();
@@ -126,13 +124,13 @@ router.post('/generate-product', async (ctx) => {
router.post('/recognize-product', async (ctx) => {
try {
const { imageBase64 } = ctx.request.body;
const { imageUrl } = ctx.request.body;
if (!imageBase64) {
if (!imageUrl) {
ctx.status = 400;
ctx.body = {
code: 400,
message: '请提供商品图片'
message: '请提供商品图片地址'
};
return;
}
@@ -148,22 +146,6 @@ router.post('/recognize-product', async (ctx) => {
"confidence": 0到1之间的数字(识别置信度)
}`;
// omni 模型不支持 data URI,先保存为文件再用 URL
const imageBuffer = Buffer.from(imageBase64, 'base64');
const fileName = `ai_${Date.now()}_${Math.random().toString(36).slice(2, 8)}.jpg`;
const filePath = path.join(__dirname, '..', 'public', 'uploads', fileName);
// 确保 uploads 目录存在
const uploadsDir = path.join(__dirname, '..', 'public', 'uploads');
if (!fs.existsSync(uploadsDir)) {
fs.mkdirSync(uploadsDir, { recursive: true });
}
fs.writeFileSync(filePath, imageBuffer);
// 构造公网可访问的 URL
const serverHost = process.env.SERVER_HOST || 'http://110.42.255.239:3006';
const imageUrl = `${serverHost}/uploads/${fileName}`;
console.log('Calling Qwen Omni API with image:', imageUrl);
const response = await fetch(AI_API_URL, {
method: 'POST',