feat(auth): add Casbin RBAC and user activity tracking
Integrate Casbin for role-based access control with admin routes for role management. Add user activity logging service to track login and refresh events. Refactor audit service to use AI-based content moderation.
This commit is contained in:
@@ -10,22 +10,13 @@ import (
|
||||
"time"
|
||||
|
||||
"carrot_bbs/internal/model"
|
||||
"carrot_bbs/internal/pkg/openai"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// ==================== 内容审核服务接口和实现 ====================
|
||||
|
||||
// AuditServiceProvider 内容审核服务提供商接口
|
||||
type AuditServiceProvider interface {
|
||||
// AuditText 审核文本
|
||||
AuditText(ctx context.Context, text string, scene string) (*AuditResult, error)
|
||||
// AuditImage 审核图片
|
||||
AuditImage(ctx context.Context, imageURL string) (*AuditResult, error)
|
||||
// GetName 获取提供商名称
|
||||
GetName() string
|
||||
}
|
||||
|
||||
// AuditResult 审核结果
|
||||
type AuditResult struct {
|
||||
Pass bool `json:"pass"` // 是否通过
|
||||
@@ -42,169 +33,293 @@ type AuditService interface {
|
||||
AuditText(ctx context.Context, text string, auditType string) (*AuditResult, error)
|
||||
// AuditImage 审核图片
|
||||
AuditImage(ctx context.Context, imageURL string) (*AuditResult, error)
|
||||
// AuditPost 审核帖子
|
||||
AuditPost(ctx context.Context, title, content string, images []string) (*AuditResult, error)
|
||||
// AuditComment 审核评论
|
||||
AuditComment(ctx context.Context, content string, images []string) (*AuditResult, error)
|
||||
// GetAuditResult 获取审核结果
|
||||
GetAuditResult(ctx context.Context, auditID string) (*AuditResult, error)
|
||||
// SetProvider 设置审核服务提供商
|
||||
SetProvider(provider AuditServiceProvider)
|
||||
// GetProvider 获取当前审核服务提供商
|
||||
GetProvider() AuditServiceProvider
|
||||
// IsEnabled 检查审核服务是否启用
|
||||
IsEnabled() bool
|
||||
}
|
||||
|
||||
// auditServiceImpl 内容审核服务实现
|
||||
type auditServiceImpl struct {
|
||||
db *gorm.DB
|
||||
provider AuditServiceProvider
|
||||
config *AuditConfig
|
||||
mu sync.RWMutex
|
||||
db *gorm.DB
|
||||
openaiClient openai.Client
|
||||
config *AuditConfig
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
// AuditConfig 内容审核服务配置
|
||||
type AuditConfig struct {
|
||||
Enabled bool `mapstructure:"enabled" yaml:"enabled"`
|
||||
// 审核服务提供商: local, aliyun, tencent, baidu
|
||||
Provider string `mapstructure:"provider" yaml:"provider"`
|
||||
// 阿里云配置
|
||||
AliyunAccessKey string `mapstructure:"aliyun_access_key" yaml:"aliyun_access_key"`
|
||||
AliyunSecretKey string `mapstructure:"aliyun_secret_key" yaml:"aliyun_secret_key"`
|
||||
AliyunRegion string `mapstructure:"aliyun_region" yaml:"aliyun_region"`
|
||||
// 腾讯云配置
|
||||
TencentSecretID string `mapstructure:"tencent_secret_id" yaml:"tencent_secret_id"`
|
||||
TencentSecretKey string `mapstructure:"tencent_secret_key" yaml:"tencent_secret_key"`
|
||||
// 百度云配置
|
||||
BaiduAPIKey string `mapstructure:"baidu_api_key" yaml:"baidu_api_key"`
|
||||
BaiduSecretKey string `mapstructure:"baidu_secret_key" yaml:"baidu_secret_key"`
|
||||
// 是否自动审核
|
||||
AutoAudit bool `mapstructure:"auto_audit" yaml:"auto_audit"`
|
||||
// 审核超时时间(秒)
|
||||
Timeout int `mapstructure:"timeout" yaml:"timeout"`
|
||||
Enabled bool `mapstructure:"enabled" yaml:"enabled"`
|
||||
StrictModeration bool `mapstructure:"strict_moderation" yaml:"strict_moderation"` // 严格模式:审核失败时拒绝;非严格模式:审核失败时放行
|
||||
Timeout int `mapstructure:"timeout" yaml:"timeout"`
|
||||
}
|
||||
|
||||
// NewAuditService 创建内容审核服务
|
||||
func NewAuditService(db *gorm.DB, config *AuditConfig) AuditService {
|
||||
s := &auditServiceImpl{
|
||||
db: db,
|
||||
config: config,
|
||||
func NewAuditService(db *gorm.DB, openaiClient openai.Client, config *AuditConfig) AuditService {
|
||||
return &auditServiceImpl{
|
||||
db: db,
|
||||
openaiClient: openaiClient,
|
||||
config: config,
|
||||
}
|
||||
|
||||
// 根据配置初始化提供商
|
||||
if config.Enabled {
|
||||
provider := s.initProvider(config.Provider)
|
||||
s.provider = provider
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
// initProvider 根据配置初始化审核服务提供商
|
||||
func (s *auditServiceImpl) initProvider(providerType string) AuditServiceProvider {
|
||||
switch strings.ToLower(providerType) {
|
||||
case "aliyun":
|
||||
return NewAliyunAuditProvider(s.config.AliyunAccessKey, s.config.AliyunSecretKey, s.config.AliyunRegion)
|
||||
case "tencent":
|
||||
return NewTencentAuditProvider(s.config.TencentSecretID, s.config.TencentSecretKey)
|
||||
case "baidu":
|
||||
return NewBaiduAuditProvider(s.config.BaiduAPIKey, s.config.BaiduSecretKey)
|
||||
case "local":
|
||||
fallthrough
|
||||
default:
|
||||
// 默认使用本地审核服务
|
||||
return NewLocalAuditProvider()
|
||||
}
|
||||
// IsEnabled 检查审核服务是否启用
|
||||
func (s *auditServiceImpl) IsEnabled() bool {
|
||||
return s != nil && s.config != nil && s.config.Enabled && s.openaiClient != nil && s.openaiClient.IsEnabled()
|
||||
}
|
||||
|
||||
// AuditText 审核文本
|
||||
func (s *auditServiceImpl) AuditText(ctx context.Context, text string, auditType string) (*AuditResult, error) {
|
||||
if !s.config.Enabled {
|
||||
// 如果审核服务未启用,直接返回通过
|
||||
if !s.IsEnabled() {
|
||||
return &AuditResult{
|
||||
Pass: true,
|
||||
Risk: "low",
|
||||
Suggest: "pass",
|
||||
Detail: "Audit service disabled",
|
||||
Pass: true,
|
||||
Risk: "low",
|
||||
Suggest: "pass",
|
||||
Detail: "Audit service disabled",
|
||||
Provider: "ai",
|
||||
}, nil
|
||||
}
|
||||
|
||||
if text == "" {
|
||||
return &AuditResult{
|
||||
Pass: true,
|
||||
Risk: "low",
|
||||
Suggest: "pass",
|
||||
Detail: "Empty text",
|
||||
Pass: true,
|
||||
Risk: "low",
|
||||
Suggest: "pass",
|
||||
Detail: "Empty text",
|
||||
Provider: "ai",
|
||||
}, nil
|
||||
}
|
||||
|
||||
var result *AuditResult
|
||||
var err error
|
||||
|
||||
// 使用提供商审核
|
||||
if s.provider != nil {
|
||||
result, err = s.provider.AuditText(ctx, text, auditType)
|
||||
} else {
|
||||
// 如果没有设置提供商,使用本地审核
|
||||
localProvider := NewLocalAuditProvider()
|
||||
result, err = localProvider.AuditText(ctx, text, auditType)
|
||||
// 使用 AI 审核文本
|
||||
approved, reason, err := s.openaiClient.ModerateComment(ctx, text, nil)
|
||||
if err != nil {
|
||||
log.Printf("AI audit text error: %v", err)
|
||||
if s.config.StrictModeration {
|
||||
return &AuditResult{
|
||||
Pass: false,
|
||||
Risk: "high",
|
||||
Suggest: "review",
|
||||
Detail: fmt.Sprintf("Audit error: %v", err),
|
||||
Provider: "ai",
|
||||
}, err
|
||||
}
|
||||
// 非严格模式下,审核失败时放行
|
||||
return &AuditResult{
|
||||
Pass: true,
|
||||
Risk: "low",
|
||||
Suggest: "pass",
|
||||
Detail: fmt.Sprintf("Audit fallback pass due to error: %v", err),
|
||||
Provider: "ai",
|
||||
}, nil
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Audit text error: %v", err)
|
||||
return &AuditResult{
|
||||
Pass: false,
|
||||
Risk: "high",
|
||||
Suggest: "review",
|
||||
Detail: fmt.Sprintf("Audit error: %v", err),
|
||||
}, err
|
||||
result := &AuditResult{
|
||||
Pass: approved,
|
||||
Provider: "ai",
|
||||
Detail: reason,
|
||||
}
|
||||
|
||||
if approved {
|
||||
result.Risk = "low"
|
||||
result.Suggest = "pass"
|
||||
} else {
|
||||
result.Risk = "high"
|
||||
result.Suggest = "block"
|
||||
result.Labels = []string{"ai_rejected"}
|
||||
}
|
||||
|
||||
// 记录审核日志
|
||||
go s.saveAuditLog(ctx, "text", "", text, auditType, result)
|
||||
go s.saveAuditLog(ctx, "text", text, "", auditType, result)
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// AuditImage 审核图片
|
||||
func (s *auditServiceImpl) AuditImage(ctx context.Context, imageURL string) (*AuditResult, error) {
|
||||
if !s.config.Enabled {
|
||||
if !s.IsEnabled() {
|
||||
return &AuditResult{
|
||||
Pass: true,
|
||||
Risk: "low",
|
||||
Suggest: "pass",
|
||||
Detail: "Audit service disabled",
|
||||
Pass: true,
|
||||
Risk: "low",
|
||||
Suggest: "pass",
|
||||
Detail: "Audit service disabled",
|
||||
Provider: "ai",
|
||||
}, nil
|
||||
}
|
||||
|
||||
if imageURL == "" {
|
||||
return &AuditResult{
|
||||
Pass: true,
|
||||
Risk: "low",
|
||||
Suggest: "pass",
|
||||
Detail: "Empty image URL",
|
||||
Pass: true,
|
||||
Risk: "low",
|
||||
Suggest: "pass",
|
||||
Detail: "Empty image URL",
|
||||
Provider: "ai",
|
||||
}, nil
|
||||
}
|
||||
|
||||
var result *AuditResult
|
||||
var err error
|
||||
|
||||
// 使用提供商审核
|
||||
if s.provider != nil {
|
||||
result, err = s.provider.AuditImage(ctx, imageURL)
|
||||
} else {
|
||||
// 如果没有设置提供商,使用本地审核
|
||||
localProvider := NewLocalAuditProvider()
|
||||
result, err = localProvider.AuditImage(ctx, imageURL)
|
||||
// 使用 AI 审核图片
|
||||
approved, reason, err := s.openaiClient.ModerateComment(ctx, "", []string{imageURL})
|
||||
if err != nil {
|
||||
log.Printf("AI audit image error: %v", err)
|
||||
if s.config.StrictModeration {
|
||||
return &AuditResult{
|
||||
Pass: false,
|
||||
Risk: "high",
|
||||
Suggest: "review",
|
||||
Detail: fmt.Sprintf("Audit error: %v", err),
|
||||
Provider: "ai",
|
||||
}, err
|
||||
}
|
||||
// 非严格模式下,审核失败时放行
|
||||
return &AuditResult{
|
||||
Pass: true,
|
||||
Risk: "low",
|
||||
Suggest: "pass",
|
||||
Detail: fmt.Sprintf("Audit fallback pass due to error: %v", err),
|
||||
Provider: "ai",
|
||||
}, nil
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Audit image error: %v", err)
|
||||
return &AuditResult{
|
||||
Pass: false,
|
||||
Risk: "high",
|
||||
Suggest: "review",
|
||||
Detail: fmt.Sprintf("Audit error: %v", err),
|
||||
}, err
|
||||
result := &AuditResult{
|
||||
Pass: approved,
|
||||
Provider: "ai",
|
||||
Detail: reason,
|
||||
}
|
||||
|
||||
if approved {
|
||||
result.Risk = "low"
|
||||
result.Suggest = "pass"
|
||||
} else {
|
||||
result.Risk = "high"
|
||||
result.Suggest = "block"
|
||||
result.Labels = []string{"ai_rejected"}
|
||||
}
|
||||
|
||||
// 记录审核日志
|
||||
go s.saveAuditLog(ctx, "image", "", "", "image", result)
|
||||
go s.saveAuditLog(ctx, "image", "", imageURL, "image", result)
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// AuditPost 审核帖子
|
||||
func (s *auditServiceImpl) AuditPost(ctx context.Context, title, content string, images []string) (*AuditResult, error) {
|
||||
if !s.IsEnabled() {
|
||||
return &AuditResult{
|
||||
Pass: true,
|
||||
Risk: "low",
|
||||
Suggest: "pass",
|
||||
Detail: "Audit service disabled",
|
||||
Provider: "ai",
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 使用 AI 审核帖子
|
||||
approved, reason, err := s.openaiClient.ModeratePost(ctx, title, content, images)
|
||||
if err != nil {
|
||||
log.Printf("AI audit post error: %v", err)
|
||||
if s.config.StrictModeration {
|
||||
return &AuditResult{
|
||||
Pass: false,
|
||||
Risk: "high",
|
||||
Suggest: "review",
|
||||
Detail: fmt.Sprintf("Audit error: %v", err),
|
||||
Provider: "ai",
|
||||
}, err
|
||||
}
|
||||
// 非严格模式下,审核失败时放行
|
||||
return &AuditResult{
|
||||
Pass: true,
|
||||
Risk: "low",
|
||||
Suggest: "pass",
|
||||
Detail: fmt.Sprintf("Audit fallback pass due to error: %v", err),
|
||||
Provider: "ai",
|
||||
}, nil
|
||||
}
|
||||
|
||||
result := &AuditResult{
|
||||
Pass: approved,
|
||||
Provider: "ai",
|
||||
Detail: reason,
|
||||
}
|
||||
|
||||
if approved {
|
||||
result.Risk = "low"
|
||||
result.Suggest = "pass"
|
||||
} else {
|
||||
result.Risk = "high"
|
||||
result.Suggest = "block"
|
||||
result.Labels = []string{"ai_rejected"}
|
||||
}
|
||||
|
||||
// 记录审核日志
|
||||
contentSummary := fmt.Sprintf("标题: %s, 内容: %s", title, content)
|
||||
if len(content) > 200 {
|
||||
contentSummary = fmt.Sprintf("标题: %s, 内容: %s...", title, content[:200])
|
||||
}
|
||||
go s.saveAuditLog(ctx, "post", contentSummary, strings.Join(images, ","), "post", result)
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// AuditComment 审核评论
|
||||
func (s *auditServiceImpl) AuditComment(ctx context.Context, content string, images []string) (*AuditResult, error) {
|
||||
if !s.IsEnabled() {
|
||||
return &AuditResult{
|
||||
Pass: true,
|
||||
Risk: "low",
|
||||
Suggest: "pass",
|
||||
Detail: "Audit service disabled",
|
||||
Provider: "ai",
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 使用 AI 审核评论
|
||||
approved, reason, err := s.openaiClient.ModerateComment(ctx, content, images)
|
||||
if err != nil {
|
||||
log.Printf("AI audit comment error: %v", err)
|
||||
if s.config.StrictModeration {
|
||||
return &AuditResult{
|
||||
Pass: false,
|
||||
Risk: "high",
|
||||
Suggest: "review",
|
||||
Detail: fmt.Sprintf("Audit error: %v", err),
|
||||
Provider: "ai",
|
||||
}, err
|
||||
}
|
||||
// 非严格模式下,审核失败时放行
|
||||
return &AuditResult{
|
||||
Pass: true,
|
||||
Risk: "low",
|
||||
Suggest: "pass",
|
||||
Detail: fmt.Sprintf("Audit fallback pass due to error: %v", err),
|
||||
Provider: "ai",
|
||||
}, nil
|
||||
}
|
||||
|
||||
result := &AuditResult{
|
||||
Pass: approved,
|
||||
Provider: "ai",
|
||||
Detail: reason,
|
||||
}
|
||||
|
||||
if approved {
|
||||
result.Risk = "low"
|
||||
result.Suggest = "pass"
|
||||
} else {
|
||||
result.Risk = "high"
|
||||
result.Suggest = "block"
|
||||
result.Labels = []string{"ai_rejected"}
|
||||
}
|
||||
|
||||
// 记录审核日志
|
||||
contentSummary := content
|
||||
if len(content) > 200 {
|
||||
contentSummary = content[:200] + "..."
|
||||
}
|
||||
go s.saveAuditLog(ctx, "comment", contentSummary, strings.Join(images, ","), "comment", result)
|
||||
|
||||
return result, nil
|
||||
}
|
||||
@@ -235,32 +350,25 @@ func (s *auditServiceImpl) GetAuditResult(ctx context.Context, auditID string) (
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// SetProvider 设置审核服务提供商
|
||||
func (s *auditServiceImpl) SetProvider(provider AuditServiceProvider) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.provider = provider
|
||||
}
|
||||
|
||||
// GetProvider 获取当前审核服务提供商
|
||||
func (s *auditServiceImpl) GetProvider() AuditServiceProvider {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return s.provider
|
||||
}
|
||||
|
||||
// saveAuditLog 保存审核日志
|
||||
func (s *auditServiceImpl) saveAuditLog(ctx context.Context, contentType, content, imageURL, auditType string, result *AuditResult) {
|
||||
if s.db == nil {
|
||||
return
|
||||
}
|
||||
|
||||
labelsJSON := ""
|
||||
if len(result.Labels) > 0 {
|
||||
if data, err := json.Marshal(result.Labels); err == nil {
|
||||
labelsJSON = string(data)
|
||||
}
|
||||
}
|
||||
|
||||
auditLog := model.AuditLog{
|
||||
ContentType: contentType,
|
||||
Content: content,
|
||||
ContentURL: imageURL,
|
||||
AuditType: auditType,
|
||||
Labels: strings.Join(result.Labels, ","),
|
||||
Labels: labelsJSON,
|
||||
Suggestion: result.Suggest,
|
||||
Detail: result.Detail,
|
||||
Source: model.AuditSourceAuto,
|
||||
@@ -291,294 +399,6 @@ func (s *auditServiceImpl) saveAuditLog(ctx context.Context, contentType, conten
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== 本地审核服务提供商 ====================
|
||||
|
||||
// localAuditProvider 本地审核服务提供商
|
||||
type localAuditProvider struct {
|
||||
// 可以注入敏感词服务进行本地审核
|
||||
sensitiveService SensitiveService
|
||||
}
|
||||
|
||||
// NewLocalAuditProvider 创建本地审核服务提供商
|
||||
func NewLocalAuditProvider() AuditServiceProvider {
|
||||
return &localAuditProvider{
|
||||
sensitiveService: nil,
|
||||
}
|
||||
}
|
||||
|
||||
// GetName 获取提供商名称
|
||||
func (p *localAuditProvider) GetName() string {
|
||||
return "local"
|
||||
}
|
||||
|
||||
// AuditText 审核文本
|
||||
func (p *localAuditProvider) AuditText(ctx context.Context, text string, scene string) (*AuditResult, error) {
|
||||
// 本地审核逻辑
|
||||
// 1. 敏感词检查
|
||||
// 2. 规则匹配
|
||||
// 3. 简单的关键词检测
|
||||
|
||||
result := &AuditResult{
|
||||
Pass: true,
|
||||
Risk: "low",
|
||||
Suggest: "pass",
|
||||
Labels: []string{},
|
||||
Provider: "local",
|
||||
}
|
||||
|
||||
// 如果有敏感词服务,使用它进行检测
|
||||
if p.sensitiveService != nil {
|
||||
hasSensitive, words := p.sensitiveService.Check(ctx, text)
|
||||
if hasSensitive {
|
||||
result.Pass = false
|
||||
result.Risk = "high"
|
||||
result.Suggest = "block"
|
||||
result.Detail = fmt.Sprintf("包含敏感词: %s", strings.Join(words, ","))
|
||||
result.Labels = append(result.Labels, "sensitive")
|
||||
}
|
||||
}
|
||||
|
||||
// 简单的关键词检测规则
|
||||
// 实际项目中应该从数据库加载
|
||||
suspiciousPatterns := []string{
|
||||
"诈骗",
|
||||
"钓鱼",
|
||||
"木马",
|
||||
"病毒",
|
||||
}
|
||||
|
||||
for _, pattern := range suspiciousPatterns {
|
||||
if strings.Contains(text, pattern) {
|
||||
result.Pass = false
|
||||
result.Risk = "high"
|
||||
result.Suggest = "block"
|
||||
result.Labels = append(result.Labels, "suspicious")
|
||||
if result.Detail == "" {
|
||||
result.Detail = fmt.Sprintf("包含可疑内容: %s", pattern)
|
||||
} else {
|
||||
result.Detail += fmt.Sprintf(", %s", pattern)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// AuditImage 审核图片
|
||||
func (p *localAuditProvider) AuditImage(ctx context.Context, imageURL string) (*AuditResult, error) {
|
||||
// 本地图片审核逻辑
|
||||
// 1. 图片URL合法性检查
|
||||
// 2. 图片格式检查
|
||||
// 3. 可以扩展接入本地图片识别服务
|
||||
|
||||
result := &AuditResult{
|
||||
Pass: true,
|
||||
Risk: "low",
|
||||
Suggest: "pass",
|
||||
Labels: []string{},
|
||||
Provider: "local",
|
||||
}
|
||||
|
||||
// 检查URL是否为空
|
||||
if imageURL == "" {
|
||||
result.Detail = "Empty image URL"
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// 检查是否为支持的图片URL格式
|
||||
validPrefixes := []string{"http://", "https://", "s3://", "oss://", "cos://"}
|
||||
isValid := false
|
||||
for _, prefix := range validPrefixes {
|
||||
if strings.HasPrefix(strings.ToLower(imageURL), prefix) {
|
||||
isValid = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !isValid {
|
||||
result.Pass = false
|
||||
result.Risk = "medium"
|
||||
result.Suggest = "review"
|
||||
result.Detail = "Invalid image URL format"
|
||||
result.Labels = append(result.Labels, "invalid_url")
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// SetSensitiveService 设置敏感词服务
|
||||
func (p *localAuditProvider) SetSensitiveService(ss SensitiveService) {
|
||||
p.sensitiveService = ss
|
||||
}
|
||||
|
||||
// ==================== 阿里云审核服务提供商 ====================
|
||||
|
||||
// aliyunAuditProvider 阿里云审核服务提供商
|
||||
type aliyunAuditProvider struct {
|
||||
accessKey string
|
||||
secretKey string
|
||||
region string
|
||||
}
|
||||
|
||||
// NewAliyunAuditProvider 创建阿里云审核服务提供商
|
||||
func NewAliyunAuditProvider(accessKey, secretKey, region string) AuditServiceProvider {
|
||||
return &aliyunAuditProvider{
|
||||
accessKey: accessKey,
|
||||
secretKey: secretKey,
|
||||
region: region,
|
||||
}
|
||||
}
|
||||
|
||||
// GetName 获取提供商名称
|
||||
func (p *aliyunAuditProvider) GetName() string {
|
||||
return "aliyun"
|
||||
}
|
||||
|
||||
// AuditText 审核文本
|
||||
func (p *aliyunAuditProvider) AuditText(ctx context.Context, text string, scene string) (*AuditResult, error) {
|
||||
// 阿里云内容安全API调用
|
||||
// 实际项目中需要实现阿里云SDK调用
|
||||
// 这里预留接口
|
||||
|
||||
result := &AuditResult{
|
||||
Pass: true,
|
||||
Risk: "low",
|
||||
Suggest: "pass",
|
||||
Labels: []string{},
|
||||
Provider: "aliyun",
|
||||
Detail: "Aliyun audit not implemented, using pass",
|
||||
}
|
||||
|
||||
// TODO: 实现阿里云内容安全API调用
|
||||
// 具体参考: https://help.aliyun.com/document_detail/28417.html
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// AuditImage 审核图片
|
||||
func (p *aliyunAuditProvider) AuditImage(ctx context.Context, imageURL string) (*AuditResult, error) {
|
||||
result := &AuditResult{
|
||||
Pass: true,
|
||||
Risk: "low",
|
||||
Suggest: "pass",
|
||||
Labels: []string{},
|
||||
Provider: "aliyun",
|
||||
Detail: "Aliyun image audit not implemented, using pass",
|
||||
}
|
||||
|
||||
// TODO: 实现阿里云图片审核API调用
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// ==================== 腾讯云审核服务提供商 ====================
|
||||
|
||||
// tencentAuditProvider 腾讯云审核服务提供商
|
||||
type tencentAuditProvider struct {
|
||||
secretID string
|
||||
secretKey string
|
||||
}
|
||||
|
||||
// NewTencentAuditProvider 创建腾讯云审核服务提供商
|
||||
func NewTencentAuditProvider(secretID, secretKey string) AuditServiceProvider {
|
||||
return &tencentAuditProvider{
|
||||
secretID: secretID,
|
||||
secretKey: secretKey,
|
||||
}
|
||||
}
|
||||
|
||||
// GetName 获取提供商名称
|
||||
func (p *tencentAuditProvider) GetName() string {
|
||||
return "tencent"
|
||||
}
|
||||
|
||||
// AuditText 审核文本
|
||||
func (p *tencentAuditProvider) AuditText(ctx context.Context, text string, scene string) (*AuditResult, error) {
|
||||
result := &AuditResult{
|
||||
Pass: true,
|
||||
Risk: "low",
|
||||
Suggest: "pass",
|
||||
Labels: []string{},
|
||||
Provider: "tencent",
|
||||
Detail: "Tencent audit not implemented, using pass",
|
||||
}
|
||||
|
||||
// TODO: 实现腾讯云内容审核API调用
|
||||
// 具体参考: https://cloud.tencent.com/document/product/1124/64508
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// AuditImage 审核图片
|
||||
func (p *tencentAuditProvider) AuditImage(ctx context.Context, imageURL string) (*AuditResult, error) {
|
||||
result := &AuditResult{
|
||||
Pass: true,
|
||||
Risk: "low",
|
||||
Suggest: "pass",
|
||||
Labels: []string{},
|
||||
Provider: "tencent",
|
||||
Detail: "Tencent image audit not implemented, using pass",
|
||||
}
|
||||
|
||||
// TODO: 实现腾讯云图片审核API调用
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// ==================== 百度云审核服务提供商 ====================
|
||||
|
||||
// baiduAuditProvider 百度云审核服务提供商
|
||||
type baiduAuditProvider struct {
|
||||
apiKey string
|
||||
secretKey string
|
||||
}
|
||||
|
||||
// NewBaiduAuditProvider 创建百度云审核服务提供商
|
||||
func NewBaiduAuditProvider(apiKey, secretKey string) AuditServiceProvider {
|
||||
return &baiduAuditProvider{
|
||||
apiKey: apiKey,
|
||||
secretKey: secretKey,
|
||||
}
|
||||
}
|
||||
|
||||
// GetName 获取提供商名称
|
||||
func (p *baiduAuditProvider) GetName() string {
|
||||
return "baidu"
|
||||
}
|
||||
|
||||
// AuditText 审核文本
|
||||
func (p *baiduAuditProvider) AuditText(ctx context.Context, text string, scene string) (*AuditResult, error) {
|
||||
result := &AuditResult{
|
||||
Pass: true,
|
||||
Risk: "low",
|
||||
Suggest: "pass",
|
||||
Labels: []string{},
|
||||
Provider: "baidu",
|
||||
Detail: "Baidu audit not implemented, using pass",
|
||||
}
|
||||
|
||||
// TODO: 实现百度云内容审核API调用
|
||||
// 具体参考: https://cloud.baidu.com/doc/ANTISPAM/s/Jjw0r1iF6
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// AuditImage 审核图片
|
||||
func (p *baiduAuditProvider) AuditImage(ctx context.Context, imageURL string) (*AuditResult, error) {
|
||||
result := &AuditResult{
|
||||
Pass: true,
|
||||
Risk: "low",
|
||||
Suggest: "pass",
|
||||
Labels: []string{},
|
||||
Provider: "baidu",
|
||||
Detail: "Baidu image audit not implemented, using pass",
|
||||
}
|
||||
|
||||
// TODO: 实现百度云图片审核API调用
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// ==================== 审核结果回调处理 ====================
|
||||
|
||||
// AuditCallback 审核回调处理
|
||||
|
||||
Reference in New Issue
Block a user