使用了AI重构项目,并完善了一部分后台问题
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"home-vue-go/internal/analytics"
|
||||
"home-vue-go/internal/config"
|
||||
"home-vue-go/internal/database"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestUmamiConnection(db *database.Database, appConfig *config.Config) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
var req struct {
|
||||
Mode *string `json:"mode"`
|
||||
APIURL *string `json:"apiUrl"`
|
||||
WebsiteID *string `json:"websiteId"`
|
||||
Credential *string `json:"credential"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil && !errors.Is(err, io.EOF) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "测试参数格式错误"})
|
||||
return
|
||||
}
|
||||
settings, err := db.LoadSiteSettings(c.Request.Context())
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "加载统计配置失败"})
|
||||
return
|
||||
}
|
||||
credential := ""
|
||||
if req.Credential != nil {
|
||||
credential = strings.TrimSpace(*req.Credential)
|
||||
} else if settings.UmamiCredential != "" && appConfig != nil {
|
||||
credential, err = appConfig.DecryptSecret(settings.UmamiCredential)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "已保存的统计凭据无法解密"})
|
||||
return
|
||||
}
|
||||
}
|
||||
mode := settings.UmamiAPIMode
|
||||
apiURL := settings.UmamiAPIURL
|
||||
websiteID := settings.UmamiWebsiteID
|
||||
if req.Mode != nil {
|
||||
mode = strings.TrimSpace(*req.Mode)
|
||||
}
|
||||
if req.APIURL != nil {
|
||||
apiURL = strings.TrimSpace(*req.APIURL)
|
||||
}
|
||||
if req.WebsiteID != nil {
|
||||
websiteID = strings.TrimSpace(*req.WebsiteID)
|
||||
}
|
||||
clientConfig := analytics.Config{Mode: mode, APIURL: apiURL, Credential: credential, WebsiteID: websiteID}
|
||||
if err := umamiClient.Test(c.Request.Context(), clientConfig); err != nil {
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": "Umami连接失败,请检查地址、Website ID 和凭据"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"ok": true, "message": "Umami连接成功"})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user