2026-05-24 10:34:02 +08:00
|
|
|
|
const Router = require('koa-router');
|
|
|
|
|
|
const fetch = require('node-fetch');
|
2026-05-24 21:58:51 +08:00
|
|
|
|
const { query } = require('../config/database');
|
|
|
|
|
|
const { toRelativeUrl } = require('../utils/image-url');
|
2026-06-03 14:15:55 +08:00
|
|
|
|
const { requireStaffAuth } = require('../middleware/auth');
|
|
|
|
|
|
const { sanitizeKeyword, sanitizeImageUrl, sanitizeImageBase64, makeCacheKey, LRU, TokenBucket } = require('../utils/ai-utils');
|
2026-05-26 09:18:48 +08:00
|
|
|
|
require('dotenv').config();
|
2026-05-24 10:34:02 +08:00
|
|
|
|
|
|
|
|
|
|
const router = new Router();
|
|
|
|
|
|
|
2026-05-26 09:18:48 +08:00
|
|
|
|
const AI_API_KEY = process.env.DASHSCOPE_API_KEY;
|
2026-05-24 20:00:56 +08:00
|
|
|
|
const AI_API_URL = 'https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions';
|
2026-05-26 09:30:17 +08:00
|
|
|
|
if (!AI_API_KEY) {
|
|
|
|
|
|
console.error('DASHSCOPE_API_KEY is not set - AI features will fail')
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-03 14:15:55 +08:00
|
|
|
|
const cache = new LRU(200, 5 * 60 * 1000)
|
|
|
|
|
|
const bucket = new TokenBucket(20, 1)
|
|
|
|
|
|
|
|
|
|
|
|
async function callQwen(model, body, timeoutMs) {
|
|
|
|
|
|
const response = await fetch(AI_API_URL, {
|
|
|
|
|
|
method: 'POST',
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
'Authorization': `Bearer ${AI_API_KEY}`,
|
|
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
|
|
},
|
|
|
|
|
|
body: JSON.stringify(body),
|
|
|
|
|
|
timeout: timeoutMs || 30000
|
|
|
|
|
|
})
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
|
const err = new Error(`AI 服务调用失败: ${response.status}`)
|
|
|
|
|
|
err.status = response.status
|
|
|
|
|
|
err.body = await response.text()
|
|
|
|
|
|
throw err
|
|
|
|
|
|
}
|
|
|
|
|
|
return response.json()
|
|
|
|
|
|
}
|
2026-05-24 10:34:02 +08:00
|
|
|
|
|
2026-06-03 14:15:55 +08:00
|
|
|
|
function mapAIError(err) {
|
|
|
|
|
|
if (err.status === 401) return { code: 500, message: 'API Key 无效,请检查密钥配置' }
|
|
|
|
|
|
if (err.status === 403) return { code: 500, message: 'API 调用被拒绝,请检查账户权限' }
|
|
|
|
|
|
if (err.status === 429) return { code: 500, message: 'API 调用次数超限,请稍后重试' }
|
|
|
|
|
|
if (err.status === 503) return { code: 500, message: 'AI 服务暂时不可用,请稍后重试' }
|
|
|
|
|
|
if (err.message && err.message.includes('timeout')) return { code: 503, message: 'AI 服务响应超时,请稍后重试' }
|
|
|
|
|
|
if (err.message && err.message.includes('ENOTFOUND')) return { code: 503, message: '无法连接到 AI 服务,请检查网络' }
|
|
|
|
|
|
if (err.message && err.message.includes('ECONNRESET')) return { code: 503, message: 'AI 服务连接中断,请稍后重试' }
|
|
|
|
|
|
return { code: 503, message: 'AI 服务异常,请稍后重试' }
|
|
|
|
|
|
}
|
2026-05-24 19:33:45 +08:00
|
|
|
|
|
2026-06-03 14:15:55 +08:00
|
|
|
|
function tryParseJSON(text) {
|
|
|
|
|
|
if (!text) return null
|
|
|
|
|
|
const md = text.match(/```(?:json)?\s*([\s\S]*?)```/)
|
|
|
|
|
|
const jsonStr = md ? md[1].trim() : (text.match(/\{[\s\S]*\}/)?.[0] || text)
|
|
|
|
|
|
try { return JSON.parse(jsonStr) } catch { return null }
|
|
|
|
|
|
}
|
2026-05-24 19:33:45 +08:00
|
|
|
|
|
2026-06-03 14:15:55 +08:00
|
|
|
|
router.post('/generate-product', requireStaffAuth(), async (ctx) => {
|
|
|
|
|
|
if (!AI_API_KEY) {
|
|
|
|
|
|
ctx.status = 500
|
|
|
|
|
|
ctx.body = { code: 500, message: 'AI 功能未配置(缺少 DASHSCOPE_API_KEY)' }
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!bucket.take()) {
|
|
|
|
|
|
ctx.status = 429
|
|
|
|
|
|
ctx.body = { code: 429, message: 'AI 调用过于频繁,请稍后重试' }
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const { imageUrl, keywords } = ctx.request.body || {}
|
|
|
|
|
|
|
|
|
|
|
|
const kw = sanitizeKeyword(keywords)
|
|
|
|
|
|
if (kw.error) { ctx.status = 400; ctx.body = { code: 400, message: kw.error }; return }
|
|
|
|
|
|
const url = sanitizeImageUrl(imageUrl)
|
|
|
|
|
|
if (url.error) { ctx.status = 400; ctx.body = { code: 400, message: url.error }; return }
|
|
|
|
|
|
if (!kw.value && !url.value) { ctx.status = 400; ctx.body = { code: 400, message: '请提供图片或关键词' }; return }
|
2026-05-24 19:33:45 +08:00
|
|
|
|
|
2026-06-03 14:15:55 +08:00
|
|
|
|
const cacheKey = makeCacheKey('gen', { kw: kw.value, url: url.value })
|
|
|
|
|
|
const hit = cache.get(cacheKey)
|
|
|
|
|
|
if (hit) {
|
|
|
|
|
|
ctx.body = { code: 200, message: '生成成功', data: hit, cached: true }
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let prompt = '你是一个专业的便利店商品管理助手。'
|
|
|
|
|
|
if (url.value) prompt += `\n请分析这张商品图片:${url.value}`
|
|
|
|
|
|
if (kw.value) prompt += `\n关键词:${kw.value}`
|
|
|
|
|
|
prompt += `
|
2026-05-24 10:34:02 +08:00
|
|
|
|
请生成商品的详细信息,返回JSON格式,不要包含其他内容:
|
|
|
|
|
|
{
|
|
|
|
|
|
"name": "商品名称(简洁明了,2-10字)",
|
2026-05-24 19:33:45 +08:00
|
|
|
|
"category": "商品分类(请从以下选择:饮料,零食,日用品,食品,生鲜,烟酒,其他)",
|
2026-05-24 10:34:02 +08:00
|
|
|
|
"description": "商品详细描述(50-100字,突出产品特点)",
|
|
|
|
|
|
"suggestedPrice": 建议售价(数字)
|
2026-06-03 14:15:55 +08:00
|
|
|
|
}`
|
2026-05-24 10:34:02 +08:00
|
|
|
|
|
2026-06-03 14:15:55 +08:00
|
|
|
|
try {
|
|
|
|
|
|
const data = await callQwen('qwen3.5-flash', {
|
|
|
|
|
|
model: 'qwen3.5-flash',
|
|
|
|
|
|
messages: [{ role: 'user', content: prompt }],
|
|
|
|
|
|
temperature: 0.7,
|
|
|
|
|
|
max_tokens: 500
|
|
|
|
|
|
}, 30000)
|
|
|
|
|
|
const aiResponse = data.choices?.[0]?.message?.content
|
|
|
|
|
|
if (!aiResponse) { ctx.status = 500; ctx.body = { code: 500, message: 'AI 服务返回为空' }; return }
|
|
|
|
|
|
const productInfo = tryParseJSON(aiResponse)
|
|
|
|
|
|
if (!productInfo) { ctx.status = 500; ctx.body = { code: 500, message: '无法解析 AI 响应格式' }; return }
|
|
|
|
|
|
cache.set(cacheKey, productInfo)
|
|
|
|
|
|
ctx.body = { code: 200, message: '生成成功', data: productInfo }
|
2026-05-24 10:34:02 +08:00
|
|
|
|
} catch (error) {
|
2026-06-03 14:15:55 +08:00
|
|
|
|
const mapped = mapAIError(error)
|
|
|
|
|
|
ctx.status = mapped.code
|
|
|
|
|
|
ctx.body = mapped
|
2026-05-24 19:33:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-06-03 14:15:55 +08:00
|
|
|
|
router.post('/recognize-product', requireStaffAuth(), async (ctx) => {
|
|
|
|
|
|
if (!AI_API_KEY) {
|
|
|
|
|
|
ctx.status = 500
|
|
|
|
|
|
ctx.body = { code: 500, message: 'AI 功能未配置(缺少 DASHSCOPE_API_KEY)' }
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!bucket.take()) {
|
|
|
|
|
|
ctx.status = 429
|
|
|
|
|
|
ctx.body = { code: 429, message: 'AI 调用过于频繁,请稍后重试' }
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-05-24 19:33:45 +08:00
|
|
|
|
|
2026-06-03 14:15:55 +08:00
|
|
|
|
const { imageBase64, imageUrl } = ctx.request.body || {}
|
2026-05-26 14:04:14 +08:00
|
|
|
|
|
2026-06-03 14:15:55 +08:00
|
|
|
|
let inputImageUrl = ''
|
|
|
|
|
|
if (imageBase64) {
|
|
|
|
|
|
const b = sanitizeImageBase64(imageBase64)
|
|
|
|
|
|
if (b.error) { ctx.status = 400; ctx.body = { code: 400, message: b.error }; return }
|
|
|
|
|
|
inputImageUrl = `data:image/jpeg;base64,${b.value}`
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!inputImageUrl && imageUrl) {
|
|
|
|
|
|
const u = sanitizeImageUrl(imageUrl)
|
|
|
|
|
|
if (u.error) { ctx.status = 400; ctx.body = { code: 400, message: u.error }; return }
|
|
|
|
|
|
inputImageUrl = u.value
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!inputImageUrl) { ctx.status = 400; ctx.body = { code: 400, message: '请提供商品图片' }; return }
|
2026-05-24 19:33:45 +08:00
|
|
|
|
|
2026-06-03 14:15:55 +08:00
|
|
|
|
const cacheKey = makeCacheKey('recog', { img: inputImageUrl.slice(0, 4096) })
|
|
|
|
|
|
const hit = cache.get(cacheKey)
|
|
|
|
|
|
if (hit) {
|
|
|
|
|
|
ctx.body = { code: 200, message: '识别成功', data: hit, cached: true }
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-05-24 19:33:45 +08:00
|
|
|
|
|
2026-06-03 14:15:55 +08:00
|
|
|
|
const prompt = `你是一个专业的便利店商品识别助手。请分析这张商品图片,识别出商品信息。
|
|
|
|
|
|
请返回JSON格式的商品信息,只返回一个最可能的商品:
|
2026-05-24 19:33:45 +08:00
|
|
|
|
{
|
|
|
|
|
|
"name": "商品名称(根据图片识别,如果无法确定则返回空字符串)",
|
|
|
|
|
|
"category": "商品分类(从以下选择:饮料,零食,日用品,食品,生鲜,烟酒,其他,如果无法确定则返回空字符串)",
|
|
|
|
|
|
"description": "商品描述(根据图片识别,突出产品特点,如果无法确定则返回空字符串)",
|
|
|
|
|
|
"suggestedPrice": 数字(根据市场价估算,如果无法确定则返回0),
|
|
|
|
|
|
"confidence": 0到1之间的数字(识别置信度)
|
|
|
|
|
|
}`;
|
|
|
|
|
|
|
2026-06-03 14:15:55 +08:00
|
|
|
|
try {
|
|
|
|
|
|
const data = await callQwen('qwen3.5-omni', {
|
|
|
|
|
|
model: 'qwen3.5-omni',
|
|
|
|
|
|
messages: [{
|
|
|
|
|
|
role: 'user',
|
|
|
|
|
|
content: [
|
|
|
|
|
|
{ type: 'image_url', image_url: { url: inputImageUrl } },
|
|
|
|
|
|
{ type: 'text', text: prompt }
|
|
|
|
|
|
]
|
|
|
|
|
|
}],
|
|
|
|
|
|
temperature: 0.3,
|
|
|
|
|
|
max_tokens: 500
|
|
|
|
|
|
}, 60000)
|
|
|
|
|
|
|
|
|
|
|
|
const aiResponse = data.choices?.[0]?.message?.content
|
|
|
|
|
|
if (!aiResponse) { ctx.status = 500; ctx.body = { code: 500, message: 'AI 服务返回为空' }; return }
|
|
|
|
|
|
const productInfo = tryParseJSON(aiResponse)
|
|
|
|
|
|
if (!productInfo) { ctx.status = 500; ctx.body = { code: 500, message: '无法解析 AI 响应格式' }; return }
|
|
|
|
|
|
|
|
|
|
|
|
const keyword = (productInfo.name || '').slice(0, 50)
|
|
|
|
|
|
let matchedGoods = []
|
2026-05-24 21:58:51 +08:00
|
|
|
|
if (keyword) {
|
|
|
|
|
|
const dbResult = await query(
|
2026-05-26 14:04:14 +08:00
|
|
|
|
'SELECT id, name, price, unit, category_id, images, stock, pricing_type, is_hot, is_new, description FROM goods WHERE name LIKE ? LIMIT 20',
|
2026-05-24 21:58:51 +08:00
|
|
|
|
[`%${keyword}%`]
|
2026-06-03 14:15:55 +08:00
|
|
|
|
)
|
|
|
|
|
|
matchedGoods = dbResult
|
2026-05-24 21:58:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-03 14:15:55 +08:00
|
|
|
|
const { processGoodsImages } = require('../utils/image-url')
|
|
|
|
|
|
matchedGoods = processGoodsImages(matchedGoods)
|
2026-05-24 19:33:45 +08:00
|
|
|
|
|
2026-06-03 14:15:55 +08:00
|
|
|
|
const result = { aiInfo: productInfo, matchedGoods }
|
|
|
|
|
|
cache.set(cacheKey, result)
|
|
|
|
|
|
ctx.body = { code: 200, message: '识别成功', data: result }
|
2026-05-24 19:33:45 +08:00
|
|
|
|
} catch (error) {
|
2026-06-03 14:15:55 +08:00
|
|
|
|
const mapped = mapAIError(error)
|
|
|
|
|
|
ctx.status = mapped.code
|
|
|
|
|
|
ctx.body = mapped
|
2026-05-24 10:34:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-06-03 14:15:55 +08:00
|
|
|
|
module.exports = router.routes();
|