Files
GPT_Management/backend/internal/router/router.go
sar 8d60704eda feat: 实现前端卡密管理界面
- 卡密列表展示与分页功能

- 单个/批量创建卡密

- 卡密删除与批量删除

- 卡密导出功能 (file-saver)

- 启用/禁用状态切换

- 状态判断 (有效/已使用/已失效)

- Toast 通知系统 (vue-sonner)

- 登录页面错误提示优化

- 后端登录错误消息中文化
2026-01-13 21:34:56 +08:00

120 lines
4.1 KiB
Go

package router
import (
"database/sql"
"net/http"
"gpt-manager-go/internal/handler"
"gpt-manager-go/internal/middleware"
"gpt-manager-go/internal/repository"
"gpt-manager-go/internal/service"
)
// SetupRoutes 设置路由
func SetupRoutes(db *sql.DB) http.Handler {
mux := http.NewServeMux()
// 初始化仓储
adminRepo := repository.NewAdminRepository(db)
chatgptAccountRepo := repository.NewChatGPTAccountRepository(db)
invitationRepo := repository.NewInvitationRepository(db)
cardKeyRepo := repository.NewCardKeyRepository(db)
// 初始化服务
chatgptService := service.NewChatGPTService()
// 初始化处理器
authHandler := handler.NewAuthHandler(adminRepo)
accountHandler := handler.NewChatGPTAccountHandler(chatgptAccountRepo, chatgptService)
inviteHandler := handler.NewInviteHandler(chatgptAccountRepo, invitationRepo, cardKeyRepo, chatgptService)
cardKeyHandler := handler.NewCardKeyHandler(cardKeyRepo)
// 公开路由 (无需认证)
mux.HandleFunc("/api/login", authHandler.Login)
mux.HandleFunc("/api/invite/card", inviteHandler.InviteByCardKey) // 卡密邀请
// 需要认证的路由
protectedMux := http.NewServeMux()
protectedMux.HandleFunc("/api/profile", func(w http.ResponseWriter, r *http.Request) {
claims := middleware.GetUserFromContext(r.Context())
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"success":true,"user":{"id":` + itoa(claims.UserID) + `,"username":"` + claims.Username + `"}}`))
})
// ChatGPT 账号管理接口
protectedMux.HandleFunc("/api/accounts", accountHandler.List)
protectedMux.HandleFunc("/api/accounts/create", accountHandler.Create)
protectedMux.HandleFunc("/api/accounts/refresh", accountHandler.Refresh)
protectedMux.HandleFunc("/api/accounts/delete", accountHandler.Delete)
// 邀请接口 (管理员) - GET: 列表, POST: 邀请, DELETE: 移除
protectedMux.HandleFunc("/api/invite", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
inviteHandler.ListByAccount(w, r)
case http.MethodPost, http.MethodDelete:
inviteHandler.InviteByAdmin(w, r)
default:
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte(`{"success":false,"message":"Method not allowed"}`))
}
})
// 卡密管理接口
protectedMux.HandleFunc("/api/cardkeys", cardKeyHandler.Handle) // GET: 列表, POST: 创建
protectedMux.HandleFunc("/api/cardkeys/batch", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodPost:
cardKeyHandler.BatchCreate(w, r)
case http.MethodDelete:
cardKeyHandler.BatchDelete(w, r)
default:
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte(`{"success":false,"message":"Method not allowed"}`))
}
})
protectedMux.HandleFunc("/api/cardkeys/delete", cardKeyHandler.Delete) // DELETE: 单个删除
protectedMux.HandleFunc("/api/cardkeys/toggle", cardKeyHandler.ToggleActive) // POST: 切换激活状态
// 挂载受保护的路由
mux.Handle("/api/profile", middleware.AuthMiddleware(protectedMux))
mux.Handle("/api/accounts", middleware.AuthMiddleware(protectedMux))
mux.Handle("/api/accounts/", middleware.AuthMiddleware(protectedMux))
mux.Handle("/api/invite", middleware.AuthMiddleware(protectedMux))
mux.Handle("/api/cardkeys", middleware.AuthMiddleware(protectedMux))
mux.Handle("/api/cardkeys/", middleware.AuthMiddleware(protectedMux))
// CORS 中间件包装
return corsMiddleware(mux)
}
// corsMiddleware CORS 中间件
func corsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusOK)
return
}
next.ServeHTTP(w, r)
})
}
func itoa(i int) string {
if i == 0 {
return "0"
}
var s string
for i > 0 {
s = string(rune('0'+i%10)) + s
i /= 10
}
return s
}