修复: 代码审查4个必改项+4个建议改进

- 工单编号改为业务格式 TK-yyMMdd-NNN
- 类型断言加 comma-ok 防 panic
- priority 用指针区分未传/P0
- json.Marshal 错误处理
- 提取 ParseID 公共函数消除重复
- HTTP client 包级别复用
- LIKE 查询特殊字符转义
- interface{} → any
- auth 中间件用 dto.Fail 统一响应
This commit is contained in:
2026-05-13 19:01:06 +08:00
parent c5c2a64a48
commit e94f160782
8 changed files with 67 additions and 70 deletions

View File

@@ -13,6 +13,8 @@ import (
"gorm.io/gorm"
)
var httpClient = &http.Client{Timeout: 30 * time.Second}
type GLMRequest struct {
Model string `json:"model"`
Messages []Message `json:"messages"`
@@ -32,10 +34,10 @@ type Choice struct {
}
type AnalysisResult struct {
Category string `json:"category"`
Category string `json:"category"`
Priority json.Number `json:"priority"`
Summary string `json:"summary"`
SuggestRole string `json:"suggest_role"`
Summary string `json:"summary"`
SuggestRole string `json:"suggest_role"`
}
func AnalyzeTicket(db *gorm.DB, ticketid int, apikey, baseURL, glmModel, title, content, contactname, contactphone string) (*model.TicketAiAnalysis, error) {
@@ -62,7 +64,10 @@ func AnalyzeTicket(db *gorm.DB, ticketid int, apikey, baseURL, glmModel, title,
},
}
jsonData, _ := json.Marshal(reqBody)
jsonData, err := json.Marshal(reqBody)
if err != nil {
return nil, fmt.Errorf("构建请求失败: %w", err)
}
httpReq, err := http.NewRequest("POST", baseURL+"/chat/completions", bytes.NewBuffer(jsonData))
if err != nil {
@@ -72,8 +77,7 @@ func AnalyzeTicket(db *gorm.DB, ticketid int, apikey, baseURL, glmModel, title,
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+apikey)
client := &http.Client{Timeout: 30 * time.Second}
resp, err := client.Do(httpReq)
resp, err := httpClient.Do(httpReq)
if err != nil {
return nil, err
}
@@ -90,7 +94,6 @@ func AnalyzeTicket(db *gorm.DB, ticketid int, apikey, baseURL, glmModel, title,
return nil, fmt.Errorf("AI返回为空")
}
// Clean markdown code block wrapping from AI response
aiContent := strings.TrimSpace(glmResp.Choices[0].Message.Content)
aiContent = strings.TrimPrefix(aiContent, "```json")
aiContent = strings.TrimPrefix(aiContent, "```")
@@ -102,7 +105,7 @@ func AnalyzeTicket(db *gorm.DB, ticketid int, apikey, baseURL, glmModel, title,
return nil, fmt.Errorf("解析AI响应失败: %v, 原始内容: %s", err, aiContent)
}
priority := int16(2) // default P2
priority := int16(2)
if n, err := result.Priority.Int64(); err == nil {
priority = int16(n)
}
@@ -142,9 +145,6 @@ func ConfirmAnalysis(db *gorm.DB, analysisid int, category string, priority int1
if category != "" {
updates["category"] = category
}
if priority >= 0 {
updates["priority"] = priority
}
if summary != "" {
updates["summary"] = summary
}