Files
2025-12-06 11:26:05 +08:00

78 lines
1.9 KiB
Go

package controllers
import (
models2 "cmii-uav-watchdog-common/models"
"cmii-uav-watchdog-common/wdd_log"
"cmii-uav-watchdog/services"
"net/http"
"github.com/gin-gonic/gin"
)
// AuthController 授权控制器
type AuthController struct {
authService *services.AuthService
}
// NewAuthController 创建授权控制器
func NewAuthController() *AuthController {
return &AuthController{
authService: services.NewAuthService(),
}
}
// GenerateAuthFile 生成授权文件
func (ac *AuthController) GenerateAuthFile(c *gin.Context) {
// 生成授权文件
authFile, err := ac.authService.GenerateAuthorizationFile()
if err != nil {
c.JSON(http.StatusInternalServerError, authFile)
return
}
c.JSON(http.StatusOK, authFile)
}
// ReceiveAuthCode 接收授权码
func (ac *AuthController) ReceiveAuthCode(c *gin.Context) {
var authCode models2.AuthorizationCode
if err := c.ShouldBindJSON(&authCode); err != nil {
c.JSON(http.StatusBadRequest, authCode)
return
}
// 处理授权码
err := ac.authService.ProcessAuthorizationCode(authCode)
if err != nil {
wdd_log.Error("处理授权码失败: %v", err)
c.JSON(http.StatusInternalServerError, authCode)
return
}
c.JSON(http.StatusOK, authCode)
}
// NotifyAuthInfo 通知授权信息
func (ac *AuthController) NotifyAuthInfo(c *gin.Context) {
// 获取授权信息
authInfo, err := ac.authService.GetAuthorizationInfo()
if err != nil {
c.JSON(http.StatusInternalServerError, err)
return
}
c.JSON(http.StatusOK, authInfo)
}
// GetAllAuthorizedHosts 获取所有已授权的主机信息
func (ac *AuthController) GetAllAuthorizedHosts(c *gin.Context) {
// 获取所有已授权的主机信息
authorizedHosts := ac.authService.GetAllAuthorizedHosts()
// 返回已授权主机信息
c.JSON(http.StatusOK, gin.H{
"authorized_host_count": len(authorizedHosts),
"authorized_hosts": authorizedHosts,
})
}