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) {