修改商城逻辑
This commit is contained in:
291
routes/upload.js
291
routes/upload.js
@@ -7,6 +7,13 @@ const { authenticateToken } = require('./auth');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* tags:
|
||||
* name: Upload
|
||||
* description: 文件上传API
|
||||
*/
|
||||
|
||||
// 确保上传目录存在
|
||||
const uploadDir = path.join(__dirname, '../uploads');
|
||||
const documentsDir = path.join(uploadDir, 'documents');
|
||||
@@ -50,16 +57,17 @@ const storage = multer.diskStorage({
|
||||
}
|
||||
});
|
||||
|
||||
// 文件过滤器
|
||||
// 文件过滤器 - 支持图片和视频
|
||||
const fileFilter = (req, file, cb) => {
|
||||
// 只允许图片文件
|
||||
if (file.mimetype.startsWith('image/')) {
|
||||
// 允许图片和视频文件
|
||||
if (file.mimetype.startsWith('image/') || file.mimetype.startsWith('video/')) {
|
||||
cb(null, true);
|
||||
} else {
|
||||
cb(new Error('只能上传图片文件'), false);
|
||||
cb(new Error('只能上传图片或视频文件'), false);
|
||||
}
|
||||
};
|
||||
|
||||
// 单文件上传配置
|
||||
const upload = multer({
|
||||
storage: storage,
|
||||
fileFilter: fileFilter,
|
||||
@@ -69,10 +77,63 @@ const upload = multer({
|
||||
}
|
||||
});
|
||||
|
||||
// 多文件上传配置
|
||||
const multiUpload = multer({
|
||||
storage: storage,
|
||||
fileFilter: fileFilter,
|
||||
limits: {
|
||||
fileSize: 10 * 1024 * 1024, // 10MB (视频文件更大)
|
||||
files: 10 // 最多10个文件
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @route POST /api/upload/image
|
||||
* @desc 上传图片
|
||||
* @access Private
|
||||
* @swagger
|
||||
* /upload/image:
|
||||
* post:
|
||||
* summary: 上传图片
|
||||
* tags: [Upload]
|
||||
* security:
|
||||
* - bearerAuth: []
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* multipart/form-data:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* file:
|
||||
* type: string
|
||||
* format: binary
|
||||
* description: 要上传的图片文件
|
||||
* type:
|
||||
* type: string
|
||||
* enum: [avatar, product, document]
|
||||
* default: document
|
||||
* description: 上传文件类型
|
||||
* responses:
|
||||
* 200:
|
||||
* description: 图片上传成功
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* success:
|
||||
* type: boolean
|
||||
* example: true
|
||||
* url:
|
||||
* type: string
|
||||
* description: 上传后的文件URL
|
||||
* filename:
|
||||
* type: string
|
||||
* description: 上传后的文件名
|
||||
* 400:
|
||||
* description: 请求参数错误
|
||||
* 401:
|
||||
* description: 未授权
|
||||
* 500:
|
||||
* description: 服务器错误
|
||||
*/
|
||||
router.post('/image', authenticateToken, (req, res) => {
|
||||
upload.single('file')(req, res, (err) => {
|
||||
@@ -134,8 +195,213 @@ router.post('/image', authenticateToken, (req, res) => {
|
||||
});
|
||||
});
|
||||
|
||||
// 保持原有的上传接口兼容性
|
||||
router.post('/', auth, upload.single('file'), (req, res) => {
|
||||
/**
|
||||
* @swagger
|
||||
* /upload:
|
||||
* post:
|
||||
* summary: 多文件上传接口 (支持MediaUpload组件)
|
||||
* tags: [Upload]
|
||||
* security:
|
||||
* - bearerAuth: []
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* multipart/form-data:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* files:
|
||||
* type: array
|
||||
* items:
|
||||
* type: string
|
||||
* format: binary
|
||||
* description: 要上传的文件列表
|
||||
* type:
|
||||
* type: string
|
||||
* enum: [avatar, product, document]
|
||||
* default: document
|
||||
* description: 上传文件类型
|
||||
* responses:
|
||||
* 200:
|
||||
* description: 文件上传成功
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* success:
|
||||
* type: boolean
|
||||
* example: true
|
||||
* message:
|
||||
* type: string
|
||||
* example: 文件上传成功
|
||||
* data:
|
||||
* type: array
|
||||
* items:
|
||||
* type: object
|
||||
* properties:
|
||||
* filename:
|
||||
* type: string
|
||||
* originalname:
|
||||
* type: string
|
||||
* mimetype:
|
||||
* type: string
|
||||
* size:
|
||||
* type: integer
|
||||
* path:
|
||||
* type: string
|
||||
* url:
|
||||
* type: string
|
||||
* 400:
|
||||
* description: 请求参数错误
|
||||
* 401:
|
||||
* description: 未授权
|
||||
* 500:
|
||||
* description: 服务器错误
|
||||
* @access Private
|
||||
*/
|
||||
router.post('/', authenticateToken, (req, res) => {
|
||||
multiUpload.array('file', 10)(req, res, (err) => {
|
||||
if (err instanceof multer.MulterError) {
|
||||
if (err.code === 'LIMIT_FILE_SIZE') {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: '文件大小不能超过 10MB'
|
||||
});
|
||||
}
|
||||
if (err.code === 'LIMIT_FILE_COUNT') {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: '一次最多只能上传10个文件'
|
||||
});
|
||||
}
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: '文件上传失败:' + err.message
|
||||
});
|
||||
} else if (err) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: err.message
|
||||
});
|
||||
}
|
||||
|
||||
if (!req.files || req.files.length === 0) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: '请选择要上传的文件'
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
// 处理多个文件
|
||||
const uploadedFiles = req.files.map(file => {
|
||||
const type = req.body.type || 'documents';
|
||||
let folderName = type;
|
||||
if (type === 'product') {
|
||||
folderName = 'products';
|
||||
} else if (type === 'avatar') {
|
||||
folderName = 'avatars';
|
||||
}
|
||||
const relativePath = path.join(folderName, file.filename).replace(/\\/g, '/');
|
||||
const fileUrl = `/uploads/${relativePath}`;
|
||||
|
||||
return {
|
||||
filename: file.filename,
|
||||
originalname: file.originalname,
|
||||
mimetype: file.mimetype,
|
||||
size: file.size,
|
||||
path: relativePath,
|
||||
url: fileUrl
|
||||
};
|
||||
});
|
||||
|
||||
// 如果只上传了一个文件,返回单文件格式以保持兼容性
|
||||
if (uploadedFiles.length === 1) {
|
||||
res.json({
|
||||
success: true,
|
||||
message: '文件上传成功',
|
||||
data: {
|
||||
...uploadedFiles[0],
|
||||
urls: [uploadedFiles[0].url] // 同时提供urls数组格式
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// 多文件返回数组格式
|
||||
res.json({
|
||||
success: true,
|
||||
message: `成功上传${uploadedFiles.length}个文件`,
|
||||
data: {
|
||||
files: uploadedFiles,
|
||||
urls: uploadedFiles.map(file => file.url)
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('文件上传错误:', error);
|
||||
res.status(500).json({ success: false, message: '文件上传失败' });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /upload/single:
|
||||
* post:
|
||||
* summary: 单文件上传接口(兼容性接口)
|
||||
* tags: [Upload]
|
||||
* security:
|
||||
* - bearerAuth: []
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* multipart/form-data:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* file:
|
||||
* type: string
|
||||
* format: binary
|
||||
* description: 要上传的文件
|
||||
* type:
|
||||
* type: string
|
||||
* enum: [avatar, product, document]
|
||||
* default: document
|
||||
* description: 上传文件类型
|
||||
* responses:
|
||||
* 200:
|
||||
* description: 文件上传成功
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* success:
|
||||
* type: boolean
|
||||
* example: true
|
||||
* message:
|
||||
* type: string
|
||||
* example: 文件上传成功
|
||||
* url:
|
||||
* type: string
|
||||
* description: 上传后的文件URL
|
||||
* filename:
|
||||
* type: string
|
||||
* description: 上传后的文件名
|
||||
* originalname:
|
||||
* type: string
|
||||
* description: 原始文件名
|
||||
* size:
|
||||
* type: integer
|
||||
* description: 文件大小
|
||||
* 400:
|
||||
* description: 请求参数错误
|
||||
* 401:
|
||||
* description: 未授权
|
||||
* 500:
|
||||
* description: 服务器错误
|
||||
*/
|
||||
router.post('/single', auth, upload.single('file'), (req, res) => {
|
||||
try {
|
||||
if (!req.file) {
|
||||
return res.status(400).json({ success: false, message: '没有上传文件' });
|
||||
@@ -171,11 +437,14 @@ router.post('/', auth, upload.single('file'), (req, res) => {
|
||||
router.use((error, req, res, next) => {
|
||||
if (error instanceof multer.MulterError) {
|
||||
if (error.code === 'LIMIT_FILE_SIZE') {
|
||||
return res.status(400).json({ success: false, message: '文件大小不能超过5MB' });
|
||||
return res.status(400).json({ success: false, message: '文件大小不能超过10MB' });
|
||||
}
|
||||
if (error.code === 'LIMIT_FILE_COUNT') {
|
||||
return res.status(400).json({ success: false, message: '一次最多只能上传10个文件' });
|
||||
}
|
||||
}
|
||||
|
||||
if (error.message === '只能上传图片文件') {
|
||||
if (error.message === '只能上传图片或视频文件') {
|
||||
return res.status(400).json({ success: false, message: error.message });
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user