Files
image2api/backend/internal/bootstrap/seed.go
T
chiyiandClaude Opus 4.8 8ec4562f81 feat: 易支付积分充值 + 站内公告 + OpenAI 视频修复 + grok/adobe 失败处理
充值/订单(易支付 mapi):
- 订单表 + 30 分钟自动取消;支付弹窗(二维码/跳转监控、倒计时、轮询、5s 倒计时关闭)
- 系统设置可配:开关/商户ID/密钥/支付地址(根地址拼 /mapi)/支付方式/最低额/积分比例(默认 1元=100积分)
- 异步通知 MD5 验签、幂等到账;用户累计充值;前台/后台订单页(筛选+搜索+分页,前后台分风格);用户管理累计充值列

站内公告:
- Markdown 公告,登录用户首次访问/刷新弹出;内容哈希做版本,改了就重新推;管理员不弹;空内容=下线

OpenAI 视频(/v1/videos)修复:
- /content 拿不到视频:grok 资源 URL 需鉴权,改为用生成账号 token 取流;adobe/runway 公开 URL 直代理(不存 RustFS)
- size→分辨率用短边判定(1280x720 = 720p,之前误判 1080p 被拒)

失败处理:
- grok 429「Too many requests」/403 anti-bot 改判临时错误(不再误封号),真额度耗尽才算 quota
- adobe 视频 408 / system under load 归为临时错误 → tempAsDead 封号

其它:
- 充值默认关闭;签到格子浅色可见;登录验证码按钮浅色可读;并发/账户信息展示
- 创作记录/画图台只显示画图台作品(排除 API);日志页 API 视频预览显示 —
- 视频去画中画/下载/投屏(全局);图片缩略图改背景图规避 Edge 视觉搜索
- 订单/兑换码/配置/日志菜单文案与图标;充值版块样式

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-01 01:32:33 +08:00

90 lines
3.8 KiB
Go

package bootstrap
import (
"context"
"backend/internal/model"
"gorm.io/gorm"
)
func seedDefaults(ctx context.Context, db *gorm.DB) error {
defaults := []model.SiteSetting{
{Key: "site.title", Value: "Vivid"},
{Key: "site.logo", Value: ""},
{Key: "site.subtitle", Value: ""},
{Key: "contact.qq", Value: "1114639355"},
{Key: "contact.qq_link", Value: "https://qm.qq.com/q/ItgCcNA7ac"},
{Key: "contact.qq_group", Value: "1106849765"},
{Key: "contact.qq_group_link", Value: "https://qm.qq.com/q/976LeMFoHu"},
{Key: "contact.email", Value: "vividairun@gmail.com"},
{Key: "contact.shop", Value: "https://pay.ldxp.cn/shop/chiyi"},
{Key: "auth.open", Value: "true"},
{Key: "auth.email_code", Value: "false"},
{Key: "auth.allow_password_reset", Value: "false"},
{Key: "auth.allowed_email_domains", Value: ""},
{Key: "auth.code_ttl_seconds", Value: "600"},
{Key: "smtp.host", Value: ""},
{Key: "smtp.port", Value: "587"},
{Key: "smtp.username", Value: ""},
{Key: "smtp.password", Value: ""},
{Key: "smtp.from_addr", Value: ""},
{Key: "smtp.use_tls", Value: "true"},
{Key: "proxy.url", Value: ""},
{Key: "credits.checkin_enabled", Value: "true"},
{Key: "credits.checkin_reward", Value: "3"},
{Key: "credits.invite_enabled", Value: "true"},
{Key: "credits.invite_reward", Value: "3"},
{Key: "credits.cdk_redeem_enabled", Value: "true"},
{Key: "pay.enabled", Value: "false"},
{Key: "pay.api_base", Value: "https://pay.v8jisu.cn/api/pay"},
{Key: "pay.methods", Value: "wxpay,alipay"},
{Key: "pay.min_amount", Value: "1"},
{Key: "pay.points_ratio", Value: "100"},
{Key: "logs.retention_days", Value: "30"},
{Key: "media.retention_days", Value: "30"},
}
for _, item := range defaults {
var count int64
if err := db.WithContext(ctx).Model(&model.SiteSetting{}).Where("key = ?", item.Key).Count(&count).Error; err != nil {
return err
}
if count > 0 {
continue
}
if err := db.WithContext(ctx).Create(&item).Error; err != nil {
return err
}
}
// One-time backfill of the persistent per-model generation counter from
// historical success logs, so the admin "次数" keeps its running total when we
// switch it off the (retention-pruned) event_log. Only touches models still at
// 0, so it never double-counts after the first run; increments take over next.
if err := db.WithContext(ctx).Exec(
`UPDATE model_configs m SET generation_count = COALESCE(
(SELECT COUNT(*) FROM event_logs e WHERE e.model = m.id AND e.status = 'success'), 0)
WHERE m.generation_count = 0`).Error; err != nil {
return err
}
// Same one-time backfill for the per-user generation counter.
if err := db.WithContext(ctx).Exec(
`UPDATE users u SET generation_count = COALESCE(
(SELECT COUNT(*) FROM event_logs e WHERE e.user_id = u.id AND e.status = 'success'), 0)
WHERE u.generation_count = 0`).Error; err != nil {
return err
}
// Seed the dashboard lifetime counters from logs ONCE (only when empty), so the
// all-time cards start from real history then track forward via the hooks.
var counterRows int64
if err := db.WithContext(ctx).Model(&model.StatCounter{}).Count(&counterRows).Error; err == nil && counterRows == 0 {
_ = db.WithContext(ctx).Exec(`INSERT INTO stat_counters (key, value, updated_at)
SELECT 'total', COUNT(*), now() FROM event_logs
UNION ALL SELECT 'success', COUNT(*) FILTER (WHERE status='success'), now() FROM event_logs
UNION ALL SELECT 'failed', COUNT(*) FILTER (WHERE status='failed'), now() FROM event_logs
UNION ALL SELECT 'image', COUNT(*) FILTER (WHERE kind='image'), now() FROM event_logs
UNION ALL SELECT 'video', COUNT(*) FILTER (WHERE kind='video'), now() FROM event_logs
UNION ALL SELECT 'api', COUNT(*) FILTER (WHERE source='v1'), now() FROM event_logs
ON CONFLICT (key) DO NOTHING`).Error
}
return nil
}