114 lines
2.5 KiB
Go
114 lines
2.5 KiB
Go
package template
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/gofiber/fiber/v3"
|
|
)
|
|
|
|
type RendererV2 struct {
|
|
loader *TemplateLoader
|
|
config TemplateConfig
|
|
}
|
|
|
|
type RenderData struct {
|
|
Title string
|
|
CurrentTable string
|
|
Tables []TableInfo
|
|
Data []map[string]interface{}
|
|
Columns []ColumnInfo
|
|
Total int
|
|
Page int
|
|
PerPage int
|
|
Pages int
|
|
Pagination []int
|
|
TemplateType string
|
|
}
|
|
|
|
type TableInfo struct {
|
|
Name string
|
|
Alias string
|
|
}
|
|
|
|
type ColumnInfo struct {
|
|
Name string
|
|
Alias string
|
|
RenderType string
|
|
ShowInList bool
|
|
Sortable bool
|
|
Values map[string]TagValue
|
|
}
|
|
|
|
type TagValue struct {
|
|
Label string `json:"label"`
|
|
Color string `json:"color"`
|
|
}
|
|
|
|
func NewRendererV2(loader *TemplateLoader, config TemplateConfig) *RendererV2 {
|
|
return &RendererV2{
|
|
loader: loader,
|
|
config: config,
|
|
}
|
|
}
|
|
|
|
func (r *RendererV2) RenderList(c fiber.Ctx, data RenderData) error {
|
|
if data.TemplateType == "" {
|
|
data.TemplateType = "list"
|
|
}
|
|
|
|
templateName := fmt.Sprintf("list:%s", data.CurrentTable)
|
|
return r.loader.Render(data, templateName, c)
|
|
}
|
|
|
|
func (r *RendererV2) GetTemplatePreview(c fiber.Ctx) error {
|
|
tableName := c.Params("table")
|
|
templateType := c.Query("type", "list")
|
|
|
|
data := RenderData{
|
|
Title: fmt.Sprintf("%s - 预览", tableName),
|
|
CurrentTable: tableName,
|
|
TemplateType: templateType,
|
|
Data: []map[string]interface{}{
|
|
{"id": 1, "title": "示例数据", "category": "示例", "created_at": "2024-01-01 12:00:00"},
|
|
},
|
|
Columns: []ColumnInfo{
|
|
{Name: "id", Alias: "ID", RenderType: "raw", ShowInList: true},
|
|
{Name: "title", Alias: "标题", RenderType: "raw", ShowInList: true},
|
|
{Name: "category", Alias: "分类", RenderType: "category", ShowInList: true},
|
|
{Name: "created_at", Alias: "创建时间", RenderType: "time", ShowInList: true},
|
|
},
|
|
Total: 1,
|
|
Page: 1,
|
|
PerPage: 20,
|
|
Pages: 1,
|
|
Tables: []TableInfo{
|
|
{Alias: tableName},
|
|
},
|
|
}
|
|
|
|
return r.loader.Render(data, fmt.Sprintf("%s:%s", templateType, tableName), c)
|
|
}
|
|
|
|
func (r *RendererV2) GetAvailableTemplates(c fiber.Ctx) error {
|
|
tableName := c.Params("table")
|
|
|
|
templates, err := r.loader.GetAvailableTemplates(tableName)
|
|
if err != nil {
|
|
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
|
|
"error": "获取模板列表失败",
|
|
})
|
|
}
|
|
|
|
return c.JSON(fiber.Map{
|
|
"templates": templates,
|
|
"table": tableName,
|
|
})
|
|
}
|
|
|
|
type PaginationData struct {
|
|
Total int
|
|
Page int
|
|
PerPage int
|
|
Pages int
|
|
} |