From 295eac7925c57244163cc4782d7605bab878e6f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=A3=E6=B5=B7=E6=B4=8B?= Date: Sun, 24 May 2026 21:58:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=BE=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 ++-- routes/ai.js | 21 ++++++++++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 8a5de81..cfbc82e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ # 依赖目录 node_modules - +public # 日志 logs/ *.log @@ -18,4 +18,4 @@ logs/ # 系统文件 .DS_Store -Thumbs.db \ No newline at end of file +Thumbs.db diff --git a/routes/ai.js b/routes/ai.js index 9f62761..fc785c2 100644 --- a/routes/ai.js +++ b/routes/ai.js @@ -1,5 +1,7 @@ const Router = require('koa-router'); const fetch = require('node-fetch'); +const { query } = require('../config/database'); +const { toRelativeUrl } = require('../utils/image-url'); const router = new Router(); @@ -262,10 +264,27 @@ router.post('/recognize-product', async (ctx) => { return; } + // 用 AI 识别的商品名去数据库模糊匹配 + const keyword = productInfo.name || ''; + let matchedGoods = []; + if (keyword) { + const dbResult = await query( + 'SELECT id, name, price, unit, category_id, images, stock, pricing_type, is_hot, is_new, description, goods_no, barcode FROM goods WHERE name LIKE ? LIMIT 20', + [`%${keyword}%`] + ); + matchedGoods = dbResult; + } + + // 处理图片 URL + matchedGoods = processGoodsImages(matchedGoods); + ctx.body = { code: 200, message: '识别成功', - data: productInfo + data: { + aiInfo: productInfo, + matchedGoods: matchedGoods + } }; } catch (error) {