5c185da0a9
- Implement dynamic SEO meta tags and sitemap.xml for toolbox.pengzhan.dev - Fix SPA title/meta refresh issue in layout.html - Address golangci-lint findings: - Refactor switch-case for tool path and SVG token parsing - Replace math.Pow with direct multiplication for performance - Fix unhandled error returns and unused imports - Omit redundant type declarations
70 lines
1.8 KiB
Go
70 lines
1.8 KiB
Go
package learnnumber
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"toolbox/pkg/base"
|
|
"toolbox/pkg/learnnumber/data"
|
|
"toolbox/pkg/learnnumber/logic"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type learnNumberTool struct{}
|
|
|
|
func init() {
|
|
base.Register(&learnNumberTool{})
|
|
}
|
|
|
|
func (t *learnNumberTool) ID() string { return "learn-number" }
|
|
func (t *learnNumberTool) Name() string { return "幼儿数学助手" }
|
|
func (t *learnNumberTool) Description() string {
|
|
return "包含数图形、基础加减法等趣味数学练习"
|
|
}
|
|
func (t *learnNumberTool) Emoji() string { return "🔢" }
|
|
|
|
func (t *learnNumberTool) Init() error {
|
|
fmt.Println("Initializing Learn-Number tool...")
|
|
// 从内嵌资源加载匿名对象
|
|
return data.LoadIconsFromEmbed()
|
|
}
|
|
|
|
func (t *learnNumberTool) RegisterRoutes(r *gin.RouterGroup) {
|
|
r.POST("/counting", t.handleCounting)
|
|
r.POST("/writing", t.handleWriting)
|
|
r.GET("/categories", t.handleCategories)
|
|
}
|
|
|
|
func (t *learnNumberTool) handleWriting(c *gin.Context) {
|
|
var req logic.WritingRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
pdfBytes, err := logic.GenerateWritingPDF(req)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.Data(http.StatusOK, "application/pdf", pdfBytes)
|
|
}
|
|
|
|
func (t *learnNumberTool) handleCategories(c *gin.Context) {
|
|
// 返回分类信息供前端预览
|
|
c.JSON(http.StatusOK, data.IconCategories)
|
|
}
|
|
|
|
func (t *learnNumberTool) handleCounting(c *gin.Context) {
|
|
var req logic.CountingRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
pdfBytes, err := logic.GenerateCountingPDF(req)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.Data(http.StatusOK, "application/pdf", pdfBytes)
|
|
}
|