Ai config
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
const Router = require('koa-router')
|
||||
const addressController = require('../controllers/addresses')
|
||||
|
||||
const router = new Router()
|
||||
|
||||
router.get('/', addressController.getAddresses)
|
||||
router.get('/:id', addressController.getAddressById)
|
||||
router.post('/', addressController.createAddress)
|
||||
router.put('/:id', addressController.updateAddress)
|
||||
router.delete('/:id', addressController.deleteAddress)
|
||||
router.put('/:id/default', addressController.setDefault)
|
||||
|
||||
module.exports = router.routes()
|
||||
@@ -0,0 +1,11 @@
|
||||
const Router = require('koa-router')
|
||||
const exportController = require('../controllers/export')
|
||||
|
||||
const router = new Router()
|
||||
|
||||
router.get('/goods', exportController.exportGoods)
|
||||
router.get('/orders', exportController.exportOrders)
|
||||
router.get('/stock', exportController.exportStock)
|
||||
router.get('/purchases', exportController.exportPurchases)
|
||||
|
||||
module.exports = router.routes()
|
||||
@@ -0,0 +1,12 @@
|
||||
const Router = require('koa-router')
|
||||
const specController = require('../controllers/goods-specs')
|
||||
|
||||
const router = new Router()
|
||||
|
||||
router.get('/', specController.getSpecs)
|
||||
router.post('/', specController.createSpec)
|
||||
router.put('/:id', specController.updateSpec)
|
||||
router.delete('/:id', specController.deleteSpec)
|
||||
router.post('/batch', specController.batchSave)
|
||||
|
||||
module.exports = router.routes()
|
||||
+3
-82
@@ -1,87 +1,8 @@
|
||||
const Router = require('koa-router')
|
||||
const { query } = require('../config/database')
|
||||
const fetch = require('node-fetch')
|
||||
require('dotenv').config()
|
||||
|
||||
const router = new Router()
|
||||
const AI_API_KEY = process.env.DASHSCOPE_API_KEY
|
||||
const AI_API_URL = 'https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions'
|
||||
const { getByBarcode, recognizeImage } = require('../controllers/recognize')
|
||||
|
||||
router.post('/barcode', async (ctx) => {
|
||||
try {
|
||||
const { barcode } = ctx.request.body
|
||||
if (!barcode) {
|
||||
ctx.body = { code: 400, message: '请提供条形码' }
|
||||
return
|
||||
}
|
||||
|
||||
const goods = await query(
|
||||
'SELECT * FROM goods WHERE barcode = ? LIMIT 1',
|
||||
[barcode]
|
||||
)
|
||||
|
||||
if (goods.length > 0) {
|
||||
ctx.body = { code: 200, data: goods[0] }
|
||||
} else {
|
||||
ctx.body = { code: 404, message: '未找到该商品' }
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Barcode lookup failed:', error)
|
||||
ctx.body = { code: 500, message: '查询失败' }
|
||||
}
|
||||
})
|
||||
|
||||
router.post('/image', async (ctx) => {
|
||||
try {
|
||||
const { imageData } = ctx.request.body
|
||||
if (!imageData) {
|
||||
ctx.body = { code: 400, message: '请提供图片数据' }
|
||||
return
|
||||
}
|
||||
|
||||
if (!AI_API_KEY) {
|
||||
ctx.body = { code: 500, message: 'AI 识别未配置' }
|
||||
return
|
||||
}
|
||||
|
||||
const response = await fetch(AI_API_URL, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${AI_API_KEY}`,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model: 'qwen-vl-max',
|
||||
messages: [
|
||||
{
|
||||
role: 'user',
|
||||
content: [
|
||||
{ type: 'text', text: '请识别这张图片中的商品,返回商品名称。只返回名称,不要其他内容。' },
|
||||
{ type: 'image_url', image_url: { url: imageData } }
|
||||
]
|
||||
}
|
||||
]
|
||||
})
|
||||
})
|
||||
|
||||
const result = await response.json()
|
||||
const name = result?.choices?.[0]?.message?.content?.trim() || ''
|
||||
|
||||
const goods = name
|
||||
? await query('SELECT * FROM goods WHERE name LIKE ? LIMIT 5', [`%${name}%`])
|
||||
: []
|
||||
|
||||
ctx.body = {
|
||||
code: 200,
|
||||
data: {
|
||||
message: name ? `识别到: ${name}` : '未识别到商品',
|
||||
goods: goods || []
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Image recognition failed:', error)
|
||||
ctx.body = { code: 500, message: '识别失败' }
|
||||
}
|
||||
})
|
||||
router.post('/barcode', getByBarcode)
|
||||
router.post('/image', recognizeImage)
|
||||
|
||||
module.exports = router.routes()
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
const Router = require('koa-router')
|
||||
const reportsController = require('../controllers/reports')
|
||||
|
||||
const router = new Router()
|
||||
|
||||
router.get('/sales-trend', reportsController.getSalesTrend)
|
||||
router.get('/hot-products', reportsController.getHotProducts)
|
||||
router.get('/profit', reportsController.getProfitAnalysis)
|
||||
router.get('/inventory-turnover', reportsController.getInventoryTurnover)
|
||||
|
||||
module.exports = router.routes()
|
||||
@@ -0,0 +1,8 @@
|
||||
const Router = require('koa-router')
|
||||
const router = new Router()
|
||||
const { bindOpenId, notifyOrder } = require('../controllers/subscribe')
|
||||
|
||||
router.post('/bind-openid', bindOpenId)
|
||||
router.post('/orders/notify', notifyOrder)
|
||||
|
||||
module.exports = router.routes()
|
||||
+24
-15
@@ -5,16 +5,19 @@ const fs = require('fs')
|
||||
|
||||
const router = new Router()
|
||||
|
||||
// 确保上传目录存在
|
||||
const uploadDir = path.join(__dirname, '..', 'public', 'uploads')
|
||||
if (!fs.existsSync(uploadDir)) {
|
||||
fs.mkdirSync(uploadDir, { recursive: true })
|
||||
}
|
||||
const ALLOWED_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp']
|
||||
const MAX_SIZE = 5 * 1024 * 1024
|
||||
|
||||
const uploadDir = path.join(__dirname, '..', 'public', 'uploads')
|
||||
|
||||
// 配置 multer
|
||||
const storage = multer.diskStorage({
|
||||
destination: (req, file, cb) => {
|
||||
cb(null, uploadDir)
|
||||
const type = req.query.type || 'goods'
|
||||
const dir = path.join(uploadDir, type)
|
||||
if (!fs.existsSync(dir)) {
|
||||
fs.mkdirSync(dir, { recursive: true })
|
||||
}
|
||||
cb(null, dir)
|
||||
},
|
||||
filename: (req, file, cb) => {
|
||||
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1e9)
|
||||
@@ -23,21 +26,27 @@ const storage = multer.diskStorage({
|
||||
}
|
||||
})
|
||||
|
||||
const upload = multer({ storage })
|
||||
const upload = multer({
|
||||
storage,
|
||||
limits: { fileSize: MAX_SIZE },
|
||||
fileFilter: (req, file, cb) => {
|
||||
if (ALLOWED_TYPES.includes(file.mimetype)) {
|
||||
cb(null, true)
|
||||
} else {
|
||||
cb(new Error('不支持的文件类型,仅支持 jpg/png/gif/webp'))
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// 上传接口
|
||||
router.post('/', upload.single('file'), async (ctx) => {
|
||||
if (!ctx.file) {
|
||||
ctx.status = 400
|
||||
ctx.body = {
|
||||
code: 400,
|
||||
message: '没有上传文件'
|
||||
}
|
||||
ctx.body = { code: 400, message: '没有上传文件' }
|
||||
return
|
||||
}
|
||||
|
||||
// 存储相对路径,前端使用时拼接域名
|
||||
const fileUrl = `/uploads/${ctx.file.filename}`
|
||||
const type = ctx.query.type || 'goods'
|
||||
const fileUrl = `/uploads/${type}/${ctx.file.filename}`
|
||||
ctx.body = {
|
||||
code: 200,
|
||||
message: '上传成功',
|
||||
|
||||
Reference in New Issue
Block a user