## 高优先级修复 - stats_service_impl: 使用 strings.SplitN 替代错误的索引分割 - provider_handler: 使用 errors.Is(err, gorm.ErrDuplicatedKey) 替代字符串匹配 - client: 重写 isNetworkError 使用 errors.As/Is 类型安全判断 - proxy_handler: 使用 encoding/json 标准库解析 JSON(extractModelName、isStreamRequest) ## 中优先级修复 - stats_handler: 添加 parseDateParam 辅助函数消除重复日期解析 - pkg/errors: 新增 ErrRequestCreate/Send/ResponseRead 错误类型和 WithCause 方法 - client: 使用结构化错误替代 fmt.Errorf - ConversionEngine: logger 依赖注入,替换所有 zap.L() 调用 ## 低优先级修复 - encoder: 删除 joinStrings,使用 strings.Join - adapter: 删除 modelInfoRegex 正则,使用 isModelInfoPath 字符串函数 ## 文档更新 - README.md: 添加公共库使用指南和编码规范章节 - specs: 同步 delta specs 到 main specs(error-handling、structured-logging、request-validation) ## 归档 - openspec/changes/archive/2026-04-20-refactor-backend-code-quality/
167 lines
3.4 KiB
Go
167 lines
3.4 KiB
Go
package handler
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
|
|
appErrors "nex/backend/pkg/errors"
|
|
|
|
"nex/backend/internal/domain"
|
|
"nex/backend/internal/service"
|
|
)
|
|
|
|
// ProviderHandler 供应商管理处理器
|
|
type ProviderHandler struct {
|
|
providerService service.ProviderService
|
|
}
|
|
|
|
// NewProviderHandler 创建供应商处理器
|
|
func NewProviderHandler(providerService service.ProviderService) *ProviderHandler {
|
|
return &ProviderHandler{providerService: providerService}
|
|
}
|
|
|
|
// CreateProvider 创建供应商
|
|
func (h *ProviderHandler) CreateProvider(c *gin.Context) {
|
|
var req struct {
|
|
ID string `json:"id" binding:"required"`
|
|
Name string `json:"name" binding:"required"`
|
|
APIKey string `json:"api_key" binding:"required"`
|
|
BaseURL string `json:"base_url" binding:"required"`
|
|
Protocol string `json:"protocol"`
|
|
}
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"error": "缺少必需字段: id, name, api_key, base_url",
|
|
})
|
|
return
|
|
}
|
|
|
|
protocol := req.Protocol
|
|
if protocol == "" {
|
|
protocol = "openai"
|
|
}
|
|
|
|
provider := &domain.Provider{
|
|
ID: req.ID,
|
|
Name: req.Name,
|
|
APIKey: req.APIKey,
|
|
BaseURL: req.BaseURL,
|
|
Protocol: protocol,
|
|
}
|
|
|
|
err := h.providerService.Create(provider)
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrDuplicatedKey) {
|
|
c.JSON(http.StatusConflict, gin.H{
|
|
"error": "供应商 ID 已存在",
|
|
})
|
|
return
|
|
}
|
|
writeError(c, err)
|
|
return
|
|
}
|
|
|
|
provider.MaskAPIKey()
|
|
c.JSON(http.StatusCreated, provider)
|
|
}
|
|
|
|
// ListProviders 列出所有供应商
|
|
func (h *ProviderHandler) ListProviders(c *gin.Context) {
|
|
providers, err := h.providerService.List()
|
|
if err != nil {
|
|
writeError(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, providers)
|
|
}
|
|
|
|
// GetProvider 获取供应商
|
|
func (h *ProviderHandler) GetProvider(c *gin.Context) {
|
|
id := c.Param("id")
|
|
|
|
provider, err := h.providerService.Get(id, true)
|
|
if err != nil {
|
|
if err == gorm.ErrRecordNotFound {
|
|
c.JSON(http.StatusNotFound, gin.H{
|
|
"error": "供应商未找到",
|
|
})
|
|
return
|
|
}
|
|
writeError(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, provider)
|
|
}
|
|
|
|
// UpdateProvider 更新供应商
|
|
func (h *ProviderHandler) UpdateProvider(c *gin.Context) {
|
|
id := c.Param("id")
|
|
|
|
var req map[string]interface{}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"error": "无效的请求格式",
|
|
})
|
|
return
|
|
}
|
|
|
|
err := h.providerService.Update(id, req)
|
|
if err != nil {
|
|
if err == gorm.ErrRecordNotFound {
|
|
c.JSON(http.StatusNotFound, gin.H{
|
|
"error": "供应商未找到",
|
|
})
|
|
return
|
|
}
|
|
writeError(c, err)
|
|
return
|
|
}
|
|
|
|
provider, err := h.providerService.Get(id, true)
|
|
if err != nil {
|
|
writeError(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, provider)
|
|
}
|
|
|
|
// DeleteProvider 删除供应商
|
|
func (h *ProviderHandler) DeleteProvider(c *gin.Context) {
|
|
id := c.Param("id")
|
|
|
|
err := h.providerService.Delete(id)
|
|
if err != nil {
|
|
if err == gorm.ErrRecordNotFound {
|
|
c.JSON(http.StatusNotFound, gin.H{
|
|
"error": "供应商未找到",
|
|
})
|
|
return
|
|
}
|
|
writeError(c, err)
|
|
return
|
|
}
|
|
|
|
c.Status(http.StatusNoContent)
|
|
}
|
|
|
|
// writeError 统一错误响应处理
|
|
func writeError(c *gin.Context, err error) {
|
|
if appErr, ok := appErrors.AsAppError(err); ok {
|
|
c.JSON(appErr.HTTPStatus, gin.H{
|
|
"error": appErr.Message,
|
|
"code": appErr.Code,
|
|
})
|
|
return
|
|
}
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"error": err.Error(),
|
|
})
|
|
}
|