feat: 统一文件上传接口 + 模板图片展示 + 导航页 + 侧边栏调整
This commit is contained in:
@@ -16,8 +16,11 @@ func NewUploadHandler(uploadService *service.UploadService) *UploadHandler {
|
||||
return &UploadHandler{uploadService: uploadService}
|
||||
}
|
||||
|
||||
// Upload 文件上传 POST /api/v1/upload
|
||||
// Upload 文件上传 POST /api/v1/upload?module=enterprise
|
||||
// module 参数指定上传到哪个模块(enterprise/shop/food/game/app/music/common)
|
||||
func (h *UploadHandler) Upload(c *gin.Context) {
|
||||
module := c.DefaultQuery("module", "common")
|
||||
|
||||
file, err := c.FormFile("file")
|
||||
if err != nil {
|
||||
FailBadRequest(c, "请选择要上传的文件")
|
||||
@@ -31,7 +34,7 @@ func (h *UploadHandler) Upload(c *gin.Context) {
|
||||
}
|
||||
defer src.Close()
|
||||
|
||||
url, err := h.uploadService.Upload(file.Filename, file.Header.Get("Content-Type"), file.Size, src)
|
||||
url, err := h.uploadService.Upload(module, file.Filename, file.Header.Get("Content-Type"), file.Size, src)
|
||||
if err != nil {
|
||||
FailBadRequest(c, err.Error())
|
||||
return
|
||||
@@ -39,3 +42,42 @@ func (h *UploadHandler) Upload(c *gin.Context) {
|
||||
|
||||
Success(c, gin.H{"url": url})
|
||||
}
|
||||
|
||||
// ListFiles 文件列表 GET /api/v1/upload/list?module=enterprise
|
||||
func (h *UploadHandler) ListFiles(c *gin.Context) {
|
||||
module := c.DefaultQuery("module", "common")
|
||||
|
||||
files, err := h.uploadService.ListFiles(module)
|
||||
if err != nil {
|
||||
FailInternal(c, "获取文件列表失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
Success(c, gin.H{
|
||||
"list": files,
|
||||
"total": len(files),
|
||||
})
|
||||
}
|
||||
|
||||
// DeleteFile 删除文件 DELETE /api/v1/upload?module=enterprise&filename=xxx.jpg
|
||||
func (h *UploadHandler) DeleteFile(c *gin.Context) {
|
||||
module := c.DefaultQuery("module", "common")
|
||||
filename := c.Query("filename")
|
||||
if filename == "" {
|
||||
FailBadRequest(c, "文件名不能为空")
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.uploadService.DeleteFile(module, filename); err != nil {
|
||||
FailBadRequest(c, "删除失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
SuccessMsg(c, "删除成功")
|
||||
}
|
||||
|
||||
// GetModules 获取支持的模块列表 GET /api/v1/upload/modules
|
||||
func (h *UploadHandler) GetModules(c *gin.Context) {
|
||||
modules := h.uploadService.GetModules()
|
||||
Success(c, gin.H{"modules": modules})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user