52 lines
1.7 KiB
Go
52 lines
1.7 KiB
Go
package yidun
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/yidun/yidun-golang-sdk/yidun/service/antispam/text/v5/check/async/single"
|
|
)
|
|
|
|
// TextDetectionService 文本检测服务
|
|
type TextDetectionService struct{}
|
|
|
|
var TextDetection = new(TextDetectionService)
|
|
|
|
// DetectText 提交文本异步检测任务
|
|
func (s *TextDetectionService) DetectText(ctx context.Context, req *single.TextAsyncCheckRequest) (string, error) {
|
|
if DefaultClients == nil || DefaultClients.TextClient == nil {
|
|
return "", fmt.Errorf("易盾文本检测客户端未初始化")
|
|
}
|
|
|
|
response, err := DefaultClients.TextClient.AsyncCheckText(req)
|
|
if err != nil {
|
|
g.Log().Errorf(ctx, "文本检测提交失败: %v", err)
|
|
return "", fmt.Errorf("文本检测提交失败: %w", err)
|
|
}
|
|
|
|
if response.GetCode() != 200 {
|
|
g.Log().Errorf(ctx, "文本检测API错误: code=%d, msg=%s", response.GetCode(), response.GetMsg())
|
|
return "", fmt.Errorf("文本检测API错误: code=%d, msg=%s", response.GetCode(), response.GetMsg())
|
|
}
|
|
|
|
var taskID string
|
|
if response.Result != nil && len(response.Result.CheckTexts) > 0 {
|
|
taskID = *response.Result.CheckTexts[0].TaskID
|
|
}
|
|
|
|
g.Log().Infof(ctx, "文本检测任务提交成功, taskID: %s", taskID)
|
|
return taskID, nil
|
|
}
|
|
|
|
// GetTextResult 获取文本检测结果
|
|
func (s *TextDetectionService) GetTextResult(ctx context.Context, taskID string) (interface{}, error) {
|
|
if DefaultClients == nil || DefaultClients.TextClient == nil {
|
|
return nil, fmt.Errorf("易盾文本检测客户端未初始化")
|
|
}
|
|
|
|
g.Log().Infof(ctx, "查询文本检测结果, taskID: %s", taskID)
|
|
// TODO: 根据实际业务需求实现查询逻辑
|
|
return nil, nil
|
|
}
|