feat(enterprise-site): add WordPress support and scheduled auto-publish to sites
Frontend CI / Frontend (push) Successful in 4m17s
Backend CI / Backend (push) Failing after 6m42s

Add WordPress as an enterprise-site CMS type alongside pbootcms, and let
schedule tasks auto-publish generated articles to enterprise sites.

- WordPress connection/publisher service and tests; register wordpress
  media platform and relax cms_type CHECK constraint
- New publish_enterprise_site_targets JSONB column on schedule_tasks with
  array type constraint; thread targets through scheduler dispatch,
  prompt/KOL generation, and worker
- Admin UI: enterprise-site targets in generate/schedule/publish flows,
  KOL package form, MediaView, and i18n strings

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-09 19:44:24 +08:00
parent 41f5623791
commit c7bad83496
30 changed files with 2946 additions and 696 deletions
@@ -41,68 +41,70 @@ func (s *ScheduleTaskService) WithCache(c sharedcache.Cache) *ScheduleTaskServic
}
type ScheduleTaskRequest struct {
TargetType string `json:"target_type"`
PromptRuleID *int64 `json:"prompt_rule_id"`
SubscriptionPromptID *int64 `json:"subscription_prompt_id"`
BrandID *int64 `json:"brand_id"`
Name string `json:"name" binding:"required"`
CronExpr string `json:"cron_expr"`
ScheduleKind string `json:"schedule_kind"`
ScheduleDays []string `json:"schedule_days"`
ScheduleTimeMode string `json:"schedule_time_mode"`
RandomWindowStart string `json:"random_window_start"`
RandomWindowEnd string `json:"random_window_end"`
GenerationInput map[string]interface{} `json:"generation_input"`
AutoPublish *bool `json:"auto_publish"`
PublishAccountIDs []string `json:"publish_account_ids"`
CoverAssetURL *string `json:"cover_asset_url"`
CoverImageAssetID *int64 `json:"cover_image_asset_id"`
EnableWebSearch *bool `json:"enable_web_search"`
GenerateCount *int `json:"generate_count"`
StartAt *string `json:"start_at"`
EndAt *string `json:"end_at"`
TargetType string `json:"target_type"`
PromptRuleID *int64 `json:"prompt_rule_id"`
SubscriptionPromptID *int64 `json:"subscription_prompt_id"`
BrandID *int64 `json:"brand_id"`
Name string `json:"name" binding:"required"`
CronExpr string `json:"cron_expr"`
ScheduleKind string `json:"schedule_kind"`
ScheduleDays []string `json:"schedule_days"`
ScheduleTimeMode string `json:"schedule_time_mode"`
RandomWindowStart string `json:"random_window_start"`
RandomWindowEnd string `json:"random_window_end"`
GenerationInput map[string]interface{} `json:"generation_input"`
AutoPublish *bool `json:"auto_publish"`
PublishAccountIDs []string `json:"publish_account_ids"`
PublishEnterpriseSiteTargets []ScheduleEnterpriseSiteTarget `json:"publish_enterprise_site_targets"`
CoverAssetURL *string `json:"cover_asset_url"`
CoverImageAssetID *int64 `json:"cover_image_asset_id"`
EnableWebSearch *bool `json:"enable_web_search"`
GenerateCount *int `json:"generate_count"`
StartAt *string `json:"start_at"`
EndAt *string `json:"end_at"`
}
type ScheduleTaskResponse struct {
ID int64 `json:"id"`
WorkspaceID *int64 `json:"workspace_id"`
TargetType string `json:"target_type"`
PromptRuleID *int64 `json:"prompt_rule_id"`
PromptRuleName *string `json:"prompt_rule_name"`
SubscriptionPromptID *int64 `json:"subscription_prompt_id"`
SubscriptionPromptName *string `json:"subscription_prompt_name"`
SubscriptionPackageName *string `json:"subscription_package_name"`
SubscriptionKolName *string `json:"subscription_kol_name"`
BrandID *int64 `json:"brand_id"`
BrandName *string `json:"brand_name"`
Name string `json:"name"`
CronExpr string `json:"cron_expr"`
ScheduleKind string `json:"schedule_kind"`
ScheduleDays []string `json:"schedule_days"`
ScheduleTimeMode string `json:"schedule_time_mode"`
RandomWindowStart string `json:"random_window_start"`
RandomWindowEnd string `json:"random_window_end"`
GenerationInput map[string]interface{} `json:"generation_input"`
AutoPublish bool `json:"auto_publish"`
PublishAccountIDs []string `json:"publish_account_ids"`
AutoPublishPlatforms []string `json:"auto_publish_platforms"`
CoverAssetURL *string `json:"cover_asset_url"`
CoverImageAssetID *int64 `json:"cover_image_asset_id"`
EnableWebSearch bool `json:"enable_web_search"`
GenerateCount int `json:"generate_count"`
StartAt *string `json:"start_at"`
EndAt *string `json:"end_at"`
NextRunAt *string `json:"next_run_at"`
Status string `json:"status"`
LastRunAt *string `json:"last_run_at"`
LastDispatchStatus *string `json:"last_dispatch_status"`
LastDispatchError *string `json:"last_dispatch_error"`
ConsecutiveFailures int `json:"consecutive_failures"`
GenerationStatus *string `json:"generation_status"`
ExecutionTime *string `json:"execution_time"`
GeneratedArticles []InstantTaskArticleLink `json:"generated_articles"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
ID int64 `json:"id"`
WorkspaceID *int64 `json:"workspace_id"`
TargetType string `json:"target_type"`
PromptRuleID *int64 `json:"prompt_rule_id"`
PromptRuleName *string `json:"prompt_rule_name"`
SubscriptionPromptID *int64 `json:"subscription_prompt_id"`
SubscriptionPromptName *string `json:"subscription_prompt_name"`
SubscriptionPackageName *string `json:"subscription_package_name"`
SubscriptionKolName *string `json:"subscription_kol_name"`
BrandID *int64 `json:"brand_id"`
BrandName *string `json:"brand_name"`
Name string `json:"name"`
CronExpr string `json:"cron_expr"`
ScheduleKind string `json:"schedule_kind"`
ScheduleDays []string `json:"schedule_days"`
ScheduleTimeMode string `json:"schedule_time_mode"`
RandomWindowStart string `json:"random_window_start"`
RandomWindowEnd string `json:"random_window_end"`
GenerationInput map[string]interface{} `json:"generation_input"`
AutoPublish bool `json:"auto_publish"`
PublishAccountIDs []string `json:"publish_account_ids"`
PublishEnterpriseSiteTargets []ScheduleEnterpriseSiteTarget `json:"publish_enterprise_site_targets"`
AutoPublishPlatforms []string `json:"auto_publish_platforms"`
CoverAssetURL *string `json:"cover_asset_url"`
CoverImageAssetID *int64 `json:"cover_image_asset_id"`
EnableWebSearch bool `json:"enable_web_search"`
GenerateCount int `json:"generate_count"`
StartAt *string `json:"start_at"`
EndAt *string `json:"end_at"`
NextRunAt *string `json:"next_run_at"`
Status string `json:"status"`
LastRunAt *string `json:"last_run_at"`
LastDispatchStatus *string `json:"last_dispatch_status"`
LastDispatchError *string `json:"last_dispatch_error"`
ConsecutiveFailures int `json:"consecutive_failures"`
GenerationStatus *string `json:"generation_status"`
ExecutionTime *string `json:"execution_time"`
GeneratedArticles []InstantTaskArticleLink `json:"generated_articles"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
type ScheduleTaskListParams struct {
@@ -209,22 +211,22 @@ func (s *ScheduleTaskService) Create(ctx context.Context, req ScheduleTaskReques
tenant_id, workspace_id, operator_id, target_type, prompt_rule_id, subscription_prompt_id,
brand_id, name, cron_expr, schedule_kind, schedule_days, schedule_time_mode,
random_window_start, random_window_end, generation_input_json,
enable_web_search, generate_count, auto_publish, publish_account_ids, cover_asset_url,
cover_image_asset_id, start_at, end_at
enable_web_search, generate_count, auto_publish, publish_account_ids,
publish_enterprise_site_targets, cover_asset_url, cover_image_asset_id, start_at, end_at
)
VALUES (
$1, $2, $3, $4, $5, $6,
$7, $8, $9, $10, $11::jsonb, $12,
$13::time, $14::time, $15::jsonb,
$16, $17, $18, $19::jsonb, $20,
$21, $22::timestamptz, $23::timestamptz
$16, $17, $18, $19::jsonb,
$20::jsonb, $21, $22, $23::timestamptz, $24::timestamptz
)
RETURNING id, created_at, start_at, end_at, status
`, actor.TenantID, actor.PrimaryWorkspaceID, actor.UserID, targetType, promptRuleID, subscriptionPromptID,
req.BrandID, req.Name, plan.CronExpr, plan.ScheduleKind, encodeScheduleDayKeys(plan.ScheduleDays), plan.ScheduleTimeMode,
plan.RandomWindowStart, plan.RandomWindowEnd, encodeScheduleGenerationInput(generationInput),
enableWebSearch, generateCount, publishConfig.AutoPublish, publishConfig.AccountIDsJSON,
publishConfig.CoverAssetURL, publishConfig.CoverImageAssetID, req.StartAt, req.EndAt).
publishConfig.EnterpriseTargetsJSON, publishConfig.CoverAssetURL, publishConfig.CoverImageAssetID, req.StartAt, req.EndAt).
Scan(&id, &createdAt, &startAt, &endAt, &status)
if err != nil {
return nil, response.ErrInternal(50010, "create_failed", "failed to create schedule task")
@@ -282,7 +284,8 @@ func (s *ScheduleTaskService) Create(ctx context.Context, req ScheduleTaskReques
Name: req.Name, CronExpr: plan.CronExpr, ScheduleKind: plan.ScheduleKind, ScheduleDays: plan.ScheduleDays,
ScheduleTimeMode: plan.ScheduleTimeMode, RandomWindowStart: plan.RandomWindowStart,
RandomWindowEnd: plan.RandomWindowEnd, GenerationInput: generationInput, AutoPublish: publishConfig.AutoPublish,
PublishAccountIDs: publishConfig.AccountIDs, AutoPublishPlatforms: publishConfig.PlatformIDs, CoverAssetURL: publishConfig.CoverAssetURL,
PublishAccountIDs: publishConfig.AccountIDs, PublishEnterpriseSiteTargets: publishConfig.EnterpriseTargets,
AutoPublishPlatforms: publishConfig.PlatformIDs, CoverAssetURL: publishConfig.CoverAssetURL,
CoverImageAssetID: publishConfig.CoverImageAssetID, EnableWebSearch: enableWebSearch,
GenerateCount: generateCount,
Status: status, CreatedAt: timeStringFromDBValue(createdAt),
@@ -345,18 +348,19 @@ func (s *ScheduleTaskService) Update(ctx context.Context, id int64, req Schedule
generation_input_json = $12::jsonb,
enable_web_search = $13, generate_count = $14,
workspace_id = $15, auto_publish = $16, publish_account_ids = $17::jsonb,
cover_asset_url = $18, cover_image_asset_id = $19,
start_at = $20::timestamptz, end_at = $21::timestamptz, operator_id = $22,
publish_enterprise_site_targets = $18::jsonb,
cover_asset_url = $19, cover_image_asset_id = $20,
start_at = $21::timestamptz, end_at = $22::timestamptz, operator_id = $23,
last_dispatch_status = NULL, last_dispatch_error = NULL,
consecutive_failures = 0,
updated_at = NOW()
WHERE id = $23 AND tenant_id = $24 AND brand_id = $25 AND deleted_at IS NULL
WHERE id = $24 AND tenant_id = $25 AND brand_id = $26 AND deleted_at IS NULL
RETURNING status, start_at, end_at
`, targetType, promptRuleID, subscriptionPromptID, req.BrandID, req.Name, plan.CronExpr,
plan.ScheduleKind, encodeScheduleDayKeys(plan.ScheduleDays), plan.ScheduleTimeMode,
plan.RandomWindowStart, plan.RandomWindowEnd, encodeScheduleGenerationInput(generationInput),
enableWebSearch, generateCount, actor.PrimaryWorkspaceID, publishConfig.AutoPublish,
publishConfig.AccountIDsJSON, publishConfig.CoverAssetURL, publishConfig.CoverImageAssetID,
publishConfig.AccountIDsJSON, publishConfig.EnterpriseTargetsJSON, publishConfig.CoverAssetURL, publishConfig.CoverImageAssetID,
req.StartAt, req.EndAt, actor.UserID, id, actor.TenantID, brandID).
Scan(&status, &startAt, &endAt)
if err != nil {
@@ -498,8 +502,8 @@ func (s *ScheduleTaskService) loadScheduleTasks(ctx context.Context, tenantID, b
st.subscription_prompt_id, st.brand_id, st.name,
st.cron_expr, st.schedule_kind, st.schedule_days, st.schedule_time_mode,
st.random_window_start::text, st.random_window_end::text, st.generation_input_json,
st.auto_publish, st.publish_account_ids, st.cover_asset_url,
st.cover_image_asset_id, st.enable_web_search, st.generate_count, st.start_at, st.end_at,
st.auto_publish, st.publish_account_ids, st.publish_enterprise_site_targets,
st.cover_asset_url, st.cover_image_asset_id, st.enable_web_search, st.generate_count, st.start_at, st.end_at,
st.next_run_at, st.status, st.last_run_at, st.last_dispatch_status,
st.last_dispatch_error, st.consecutive_failures, st.created_at, st.updated_at,
pr.name AS prompt_rule_name,
@@ -553,8 +557,8 @@ func (s *ScheduleTaskService) loadScheduleTaskDetail(ctx context.Context, tenant
st.subscription_prompt_id, st.brand_id, st.name,
st.cron_expr, st.schedule_kind, st.schedule_days, st.schedule_time_mode,
st.random_window_start::text, st.random_window_end::text, st.generation_input_json,
st.auto_publish, st.publish_account_ids, st.cover_asset_url,
st.cover_image_asset_id, st.enable_web_search, st.generate_count, st.start_at, st.end_at,
st.auto_publish, st.publish_account_ids, st.publish_enterprise_site_targets,
st.cover_asset_url, st.cover_image_asset_id, st.enable_web_search, st.generate_count, st.start_at, st.end_at,
st.next_run_at, st.status, st.last_run_at, st.last_dispatch_status,
st.last_dispatch_error, st.consecutive_failures, st.created_at, st.updated_at,
pr.name AS prompt_rule_name,
@@ -924,13 +928,14 @@ func scanScheduleTaskResponse(scanner interface {
var nextRunAt interface{}
var lastRunAt interface{}
var publishAccountIDsJSON []byte
var publishEnterpriseSiteTargetsJSON []byte
var scheduleDaysJSON []byte
var generationInputJSON []byte
if err := scanner.Scan(&item.ID, &item.WorkspaceID, &item.TargetType, &item.PromptRuleID,
&item.SubscriptionPromptID, &item.BrandID, &item.Name,
&item.CronExpr, &item.ScheduleKind, &scheduleDaysJSON, &item.ScheduleTimeMode,
&item.RandomWindowStart, &item.RandomWindowEnd, &generationInputJSON,
&item.AutoPublish, &publishAccountIDsJSON,
&item.AutoPublish, &publishAccountIDsJSON, &publishEnterpriseSiteTargetsJSON,
&item.CoverAssetURL, &item.CoverImageAssetID, &item.EnableWebSearch, &item.GenerateCount, &startAt, &endAt,
&nextRunAt, &item.Status, &lastRunAt, &item.LastDispatchStatus,
&item.LastDispatchError, &item.ConsecutiveFailures, &createdAt, &updatedAt,
@@ -959,6 +964,7 @@ func scanScheduleTaskResponse(scanner interface {
item.ScheduleDays = decodeScheduleDayKeys(scheduleDaysJSON)
item.GenerationInput = decodeScheduleGenerationInput(generationInputJSON)
item.PublishAccountIDs = decodeSchedulePublishAccountIDs(publishAccountIDsJSON)
item.PublishEnterpriseSiteTargets = DecodeScheduleEnterpriseSiteTargets(publishEnterpriseSiteTargetsJSON)
item.CreatedAt = timeStringFromDBValue(createdAt)
item.UpdatedAt = timeStringFromDBValue(updatedAt)
item.StartAt = timeStringPtrFromDBValue(startAt)
@@ -1061,54 +1067,72 @@ func (s *ScheduleTaskService) validateKolScheduleGenerationInput(ctx context.Con
}
type scheduleAutoPublishConfig struct {
AutoPublish bool
AccountIDs []string
PlatformIDs []string
AccountIDsJSON []byte
CoverAssetURL *string
CoverImageAssetID *int64
AutoPublish bool
AccountIDs []string
EnterpriseTargets []ScheduleEnterpriseSiteTarget
PlatformIDs []string
AccountIDsJSON []byte
EnterpriseTargetsJSON []byte
CoverAssetURL *string
CoverImageAssetID *int64
}
func (s *ScheduleTaskService) normalizeAutoPublishConfig(ctx context.Context, actor auth.Actor, req ScheduleTaskRequest) (scheduleAutoPublishConfig, error) {
autoPublish := boolValue(req.AutoPublish)
accountIDs := normalizeSchedulePublishAccountIDs(req.PublishAccountIDs)
enterpriseTargets := NormalizeScheduleEnterpriseSiteTargets(req.PublishEnterpriseSiteTargets)
config := scheduleAutoPublishConfig{
AutoPublish: autoPublish,
AccountIDs: accountIDs,
AutoPublish: autoPublish,
AccountIDs: accountIDs,
EnterpriseTargets: enterpriseTargets,
}
if !autoPublish {
config.AccountIDs = []string{}
config.EnterpriseTargets = []ScheduleEnterpriseSiteTarget{}
config.AccountIDsJSON = []byte("[]")
config.EnterpriseTargetsJSON = []byte("[]")
return config, nil
}
if actor.PrimaryWorkspaceID <= 0 {
return config, response.ErrBadRequest(40023, "workspace_required", "workspace context is required for automatic publishing")
return config, response.ErrBadRequest(40023, "workspace_required", "自动发布需要工作区上下文")
}
if len(accountIDs) == 0 {
return config, response.ErrBadRequest(40024, "publish_accounts_required", "publish_account_ids is required when auto_publish is enabled")
if len(accountIDs) == 0 && len(enterpriseTargets) == 0 {
return config, response.ErrBadRequest(40024, "publish_targets_required", "开启自动发布后,请至少选择一个发布目标")
}
if err := validateOptionalPositiveInt64(req.CoverImageAssetID, "cover_image_asset_id"); err != nil {
return config, err
}
coverURL := strings.TrimSpace(stringPointerValue(req.CoverAssetURL))
platformIDs, err := s.loadPublishAccountPlatformIDs(ctx, actor.TenantID, actor.PrimaryWorkspaceID, accountIDs)
if err != nil {
return config, err
if len(accountIDs) > 0 {
platformIDs, err := s.loadPublishAccountPlatformIDs(ctx, actor.TenantID, actor.PrimaryWorkspaceID, accountIDs)
if err != nil {
return config, err
}
config.PlatformIDs = normalizePlatformIDs(platformIDs)
if schedulePublishPlatformsRequireCover(platformIDs) && coverURL == "" {
return config, response.ErrBadRequest(40025, "cover_required", "已选媒体账号的平台要求封面图,请先设置封面")
}
}
config.PlatformIDs = normalizePlatformIDs(platformIDs)
if schedulePublishPlatformsRequireCover(platformIDs) && coverURL == "" {
return config, response.ErrBadRequest(40025, "cover_required", "selected publish accounts require cover_asset_url")
if len(enterpriseTargets) > 0 {
if err := s.validateScheduleEnterpriseSiteTargets(ctx, actor.TenantID, actor.PrimaryWorkspaceID, enterpriseTargets); err != nil {
return config, err
}
}
accountIDsJSON, err := json.Marshal(accountIDs)
if err != nil {
return config, response.ErrBadRequest(40026, "invalid_publish_account_ids", "publish_account_ids must be serializable")
return config, response.ErrBadRequest(40026, "invalid_publish_account_ids", "媒体账号发布目标格式不正确")
}
enterpriseTargetsJSON, err := encodeScheduleEnterpriseSiteTargets(enterpriseTargets)
if err != nil {
return config, err
}
config.AccountIDsJSON = accountIDsJSON
config.EnterpriseTargetsJSON = enterpriseTargetsJSON
if coverURL != "" {
config.CoverAssetURL = &coverURL
config.CoverImageAssetID = req.CoverImageAssetID
@@ -1116,6 +1140,48 @@ func (s *ScheduleTaskService) normalizeAutoPublishConfig(ctx context.Context, ac
return config, nil
}
func (s *ScheduleTaskService) validateScheduleEnterpriseSiteTargets(ctx context.Context, tenantID, workspaceID int64, targets []ScheduleEnterpriseSiteTarget) error {
for _, target := range targets {
var status string
if err := s.pool.QueryRow(ctx, `
SELECT status
FROM enterprise_site_connections
WHERE id = $1
AND tenant_id = $2
AND workspace_id = $3
AND deleted_at IS NULL
`, target.SiteID, tenantID, workspaceID).Scan(&status); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return response.ErrBadRequest(40050, "invalid_publish_enterprise_site_targets", "企业站点连接不存在或已删除")
}
return response.ErrInternal(50231, "enterprise_site_lookup_failed", "企业站点连接读取失败")
}
if status == "disabled" {
return response.ErrConflict(40942, "enterprise_site_disabled", "企业站点已禁用")
}
if strings.TrimSpace(target.CategoryID) == "" {
return response.ErrBadRequest(40047, "enterprise_site_category_required", "请选择企业站点栏目")
}
var categoryExists bool
if err := s.pool.QueryRow(ctx, `
SELECT EXISTS (
SELECT 1
FROM enterprise_site_categories
WHERE tenant_id = $1
AND workspace_id = $2
AND connection_id = $3
AND remote_id = $4
)
`, tenantID, workspaceID, target.SiteID, target.CategoryID).Scan(&categoryExists); err != nil {
return response.ErrInternal(50230, "enterprise_site_category_lookup_failed", "企业站点栏目校验失败")
}
if !categoryExists {
return response.ErrBadRequest(40049, "enterprise_site_category_not_found", "企业站点栏目不存在,请先同步栏目")
}
}
return nil
}
func (s *ScheduleTaskService) loadPublishAccountPlatformIDs(ctx context.Context, tenantID, workspaceID int64, accountIDs []string) ([]string, error) {
rows, err := s.pool.Query(ctx, `
SELECT platform_id