package handlers import ( "net/http" "github.com/gin-gonic/gin" "gorm.io/gorm" "goalfymax-admin/internal/models" "goalfymax-admin/internal/services" ) type InviteCodeApplicationHandler struct { service *services.InviteCodeApplicationService } // NewInviteCodeApplicationHandler 创建邀请码申请处理器 func NewInviteCodeApplicationHandler(db *gorm.DB) *InviteCodeApplicationHandler { return &InviteCodeApplicationHandler{ service: services.NewInviteCodeApplicationService(db), } } // SubmitApplication 提交邀请码申请(公开接口,官网使用) func (h *InviteCodeApplicationHandler) SubmitApplication(c *gin.Context) { var req models.InviteCodeApplicationCreateRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{ "error": "请求参数无效", }) return } application, err := h.service.SubmitApplication(&req) if err != nil { c.JSON(http.StatusBadRequest, gin.H{ "error": err.Error(), }) return } c.JSON(http.StatusOK, gin.H{ "code": 0, "message": "申请已提交,我们将在1-2个工作日内处理您的申请", "data": application, }) } // GetApplicationList 获取申请列表(后台管理接口,需要权限) func (h *InviteCodeApplicationHandler) GetApplicationList(c *gin.Context) { var req models.InviteCodeApplicationListRequest if err := c.ShouldBindQuery(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{ "error": "请求参数无效", }) return } // 设置默认值 if req.Page <= 0 { req.Page = 1 } if req.Size <= 0 { req.Size = 20 } response, err := h.service.GetApplicationList(&req) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "error": "获取申请列表失败", }) return } c.JSON(http.StatusOK, gin.H{ "code": 0, "message": "success", "data": response, }) } // GetStatistics 获取申请统计(后台管理接口,需要权限) func (h *InviteCodeApplicationHandler) GetStatistics(c *gin.Context) { stats, err := h.service.GetStatistics() if err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "error": "获取统计信息失败", }) return } c.JSON(http.StatusOK, gin.H{ "code": 0, "message": "success", "data": stats, }) } // ApproveApplication 审批通过申请(后台管理接口,需要权限) func (h *InviteCodeApplicationHandler) ApproveApplication(c *gin.Context) { var req models.InviteCodeApplicationApproveRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{ "error": "请求参数无效", }) return } // 从上下文获取操作人信息(需要在中间件中设置) approvedBy := c.GetString("username") if approvedBy == "" { approvedBy = "admin" } if err := h.service.ApproveApplication(&req, approvedBy); err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "error": err.Error(), }) return } c.JSON(http.StatusOK, gin.H{ "code": 0, "message": "审批通过成功", }) } // RejectApplication 审批拒绝申请(后台管理接口,需要权限) func (h *InviteCodeApplicationHandler) RejectApplication(c *gin.Context) { var req models.InviteCodeApplicationRejectRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{ "error": "请求参数无效", }) return } // 从上下文获取操作人信息(需要在中间件中设置) approvedBy := c.GetString("username") if approvedBy == "" { approvedBy = "admin" } if err := h.service.RejectApplication(&req, approvedBy); err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "error": err.Error(), }) return } c.JSON(http.StatusOK, gin.H{ "code": 0, "message": "审批拒绝成功", }) } // BatchApproveApplications 批量审批通过(后台管理接口,需要权限) func (h *InviteCodeApplicationHandler) BatchApproveApplications(c *gin.Context) { var req models.InviteCodeApplicationBatchApproveRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{ "error": "请求参数无效", }) return } // 从上下文获取操作人信息(需要在中间件中设置) approvedBy := c.GetString("username") if approvedBy == "" { approvedBy = "admin" } if err := h.service.BatchApproveApplications(&req, approvedBy); err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "error": err.Error(), }) return } c.JSON(http.StatusOK, gin.H{ "code": 0, "message": "批量审批通过成功", }) } // BatchRejectApplications 批量审批拒绝(后台管理接口,需要权限) func (h *InviteCodeApplicationHandler) BatchRejectApplications(c *gin.Context) { var req models.InviteCodeApplicationBatchRejectRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{ "error": "请求参数无效", }) return } // 从上下文获取操作人信息(需要在中间件中设置) approvedBy := c.GetString("username") if approvedBy == "" { approvedBy = "admin" } if err := h.service.BatchRejectApplications(&req, approvedBy); err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "error": err.Error(), }) return } c.JSON(http.StatusOK, gin.H{ "code": 0, "message": "批量审批拒绝成功", }) } // GetPendingCount 获取待处理申请数量(后台管理接口,需要权限) func (h *InviteCodeApplicationHandler) GetPendingCount(c *gin.Context) { count, err := h.service.GetPendingApplicationsCount() if err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "error": "获取待处理数量失败", }) return } c.JSON(http.StatusOK, gin.H{ "code": 0, "message": "success", "data": gin.H{ "count": count, }, }) } // RegisterRoutes 注册路由 func (h *InviteCodeApplicationHandler) RegisterRoutes(router *gin.RouterGroup, authMiddleware gin.HandlerFunc) { // 公开接口(官网提交申请) public := router.Group("/public") { public.POST("/invite-code/apply", h.SubmitApplication) } // 需要认证的接口(后台管理) protected := router.Group("/invite-code/applications") protected.Use(authMiddleware) { protected.GET("", h.GetApplicationList) protected.GET("/statistics", h.GetStatistics) protected.GET("/pending-count", h.GetPendingCount) protected.POST("/approve", h.ApproveApplication) protected.POST("/reject", h.RejectApplication) protected.POST("/batch-approve", h.BatchApproveApplications) protected.POST("/batch-reject", h.BatchRejectApplications) } }