From c0fc1ea13ac9566d3381fff560085f67b7b72cd4 Mon Sep 17 00:00:00 2001 From: bisnsh Date: Fri, 20 Mar 2026 22:02:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=85=A5=E5=8F=A3=E6=96=87?= =?UTF-8?q?=E4=BB=B6main.go=EF=BC=8C=E6=9B=B4=E6=96=B0=E4=BA=86=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E5=A4=84=E7=90=86=E5=99=A8=E5=92=8C=E8=80=83=E8=AF=95?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E7=9A=84=E9=80=BB=E8=BE=91=EF=BC=8C=E5=90=8C?= =?UTF-8?q?=E6=97=B6=E4=BF=AE=E6=94=B9=E4=BA=86=E6=88=90=E7=BB=A9=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E7=9A=84=E5=AE=9E=E7=8E=B0=E3=80=82=E8=AF=B7=E6=B3=A8?= =?UTF-8?q?=E6=84=8F=EF=BC=8Cconfig.yaml=E6=96=87=E4=BB=B6=E6=9C=89?= =?UTF-8?q?=E6=9C=AA=E6=9A=82=E5=AD=98=E7=9A=84=E6=9B=B4=E6=94=B9=EF=BC=8C?= =?UTF-8?q?=E9=9C=80=E8=A6=81=E5=85=88=E5=B0=86=E5=85=B6=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=88=B0=E6=9A=82=E5=AD=98=E5=8C=BA=E6=89=8D=E8=83=BD=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/main.go | 24 ++++++++++++++++++++++++ internal/handler/user_handler.go | 1 - internal/service/exam_service.go | 4 ++-- internal/service/score_service.go | 3 ++- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 21a79fc..5193827 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -8,6 +8,8 @@ import ( "fmt" "github.com/gin-gonic/gin" "log" + "net/http" + "os" ) func main() { @@ -27,6 +29,28 @@ func main() { // 全局中间件 r.Use(middleware.Cors()) + // 静态文件服务和前端路由回退处理 + distPath := "frontend/dist" + if _, err := os.Stat(distPath); err == nil { + // dist 目录存在,提供静态文件服务 + r.Static("/static", distPath) + + // 前端路由回退处理(SPA 支持) + r.NoRoute(func(c *gin.Context) { + // API 路由未找到,返回 404 + if len(c.Request.URL.Path) >= 4 && c.Request.URL.Path[:4] == "/api" { + c.JSON(http.StatusNotFound, gin.H{"error": "API not found"}) + return + } + // 其他路由,返回前端 index.html(由 vue-router 处理) + c.File(distPath + "/index.html") + }) + } else { + // dist 目录不存在,提示需要构建前端 + fmt.Println("⚠️ Frontend dist directory not found. Please build frontend first.") + fmt.Println(" Run: cd frontend && npm install && npm run build") + } + // 设置路由 routes.SetupRouter(r) diff --git a/internal/handler/user_handler.go b/internal/handler/user_handler.go index 1ef8b4f..19e55b9 100644 --- a/internal/handler/user_handler.go +++ b/internal/handler/user_handler.go @@ -1,7 +1,6 @@ package handler import ( - "exam_registration/internal/middleware" "exam_registration/internal/service" "exam_registration/pkg/response" "github.com/gin-gonic/gin" diff --git a/internal/service/exam_service.go b/internal/service/exam_service.go index 50214bf..7196e76 100644 --- a/internal/service/exam_service.go +++ b/internal/service/exam_service.go @@ -4,7 +4,7 @@ import ( "exam_registration/internal/dao" "exam_registration/internal/model" "errors" - "fmt" + "gorm.io/gorm" "time" ) @@ -57,7 +57,7 @@ func (s *ExamService) DeleteExam(id uint64) error { func (s *ExamService) UpdateExamStatus(id uint64, status int) error { // 更新考试状态并同步更新相关报名记录的状态 - return dao.DB.Transaction(func(tx *dao.DB) error { + return dao.DB.Transaction(func(tx *gorm.DB) error { if err := tx.Model(&model.Exam{}).Where("id = ?", id).Update("status", status).Error; err != nil { return err } diff --git a/internal/service/score_service.go b/internal/service/score_service.go index 5605f8a..6eb15ec 100644 --- a/internal/service/score_service.go +++ b/internal/service/score_service.go @@ -4,6 +4,7 @@ import ( "exam_registration/internal/dao" "exam_registration/internal/model" "errors" + "gorm.io/gorm" ) type ScoreService struct{} @@ -22,7 +23,7 @@ func (s *ScoreService) CreateScore(score *model.ExamScore) error { } func (s *ScoreService) BatchCreateScores(scores []model.ExamScore) error { - return dao.DB.Transaction(func(tx *dao.DB) error { + return dao.DB.Transaction(func(tx *gorm.DB) error { for i := range scores { scores[i].Pass = scores[i].Score >= 60