feat: 统一文件上传接口 + 模板图片展示 + 导航页 + 侧边栏调整

This commit is contained in:
admin
2026-07-10 20:43:02 +08:00
parent cbd0430452
commit ce6823e282
25 changed files with 933 additions and 166 deletions

View File

@@ -55,6 +55,62 @@ var tableLabels = map[string]string{
"music_categories": "音乐分类",
}
// tablesWithSort 拥有 sort 列的表(用于排序)
var tablesWithSort = map[string]bool{
"enterprise_services": true,
"enterprise_team": true,
"shop_products": true,
"shop_categories": true,
"food_restaurants": true,
"food_categories": true,
"food_menu_items": true,
"food_menu_categories": true,
"game_games": true,
"game_categories": true,
"app_apps": true,
"app_categories": true,
"music_songs": true,
"music_categories": true,
}
// tablesWithName 拥有 name 列的表(用于关键词搜索)
var tablesWithName = map[string]bool{
"enterprise_company": true,
"enterprise_team": true,
"enterprise_services": false, // 有 title 无 name
"enterprise_news": false,
"shop_products": true,
"shop_categories": true,
"food_restaurants": true,
"food_categories": true,
"food_menu_items": true,
"food_menu_categories": true,
"game_games": true,
"game_categories": true,
"app_apps": true,
"app_categories": true,
"music_songs": false, // 有 title 无 name
"music_categories": true,
}
// tablesWithTitle 拥有 title 列的表(用于关键词搜索)
var tablesWithTitle = map[string]bool{
"enterprise_news": true,
"enterprise_services": true,
"music_songs": true,
}
// tablesWithStatus 拥有 status 列的表(用于状态过滤)
var tablesWithStatus = map[string]bool{
"enterprise_news": true,
"shop_products": true,
"food_restaurants": true,
"food_menu_items": true,
"game_games": true,
"app_apps": true,
"music_songs": true,
}
// DashboardHandler 管理后台仪表盘处理器(通用 CRUD + 统计)
type DashboardHandler struct {
db *gorm.DB
@@ -174,21 +230,36 @@ func (h *DashboardHandler) ListItems(c *gin.Context) {
query := h.db.Model(modelType)
// 关键词搜索(按 name 或 title 字段)
// 关键词搜索(根据表实际拥有的列动态构建条件)
if keyword != "" {
query = query.Where("name LIKE ? OR title LIKE ?", "%"+keyword+"%", "%"+keyword+"%")
hasName := tablesWithName[table]
hasTitle := tablesWithTitle[table]
switch {
case hasName && hasTitle:
query = query.Where("name LIKE ? OR title LIKE ?", "%"+keyword+"%", "%"+keyword+"%")
case hasName:
query = query.Where("name LIKE ?", "%"+keyword+"%")
case hasTitle:
query = query.Where("title LIKE ?", "%"+keyword+"%")
}
}
// 状态过滤
if status != "" {
// 状态过滤(仅对拥有 status 列的表生效)
if status != "" && tablesWithStatus[table] {
query = query.Where("status = ?", status)
}
var total int64
query.Count(&total)
// 排序:有 sort 列的表按 sort ASC, id DESC;否则按 id DESC
orderClause := "id DESC"
if tablesWithSort[table] {
orderClause = "sort ASC, id DESC"
}
var results []map[string]interface{}
if err := query.Order("sort ASC, id DESC").Offset(offset).Limit(pageSize).Find(&results).Error; err != nil {
if err := query.Order(orderClause).Offset(offset).Limit(pageSize).Find(&results).Error; err != nil {
FailInternal(c, "查询失败: "+err.Error())
return
}

View File

@@ -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})
}