充值/订单(易支付 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>
232 lines
6.1 KiB
Go
232 lines
6.1 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"backend/internal/model"
|
|
"backend/internal/service"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type AdminReadHandler struct {
|
|
admin *service.AdminReadService
|
|
}
|
|
|
|
func NewAdminReadHandler(admin *service.AdminReadService) *AdminReadHandler {
|
|
return &AdminReadHandler{admin: admin}
|
|
}
|
|
|
|
func (h *AdminReadHandler) Users(c *gin.Context) {
|
|
users, stats, err := h.admin.Users(c.Request.Context())
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"detail": "failed to load users"})
|
|
return
|
|
}
|
|
|
|
out := make([]gin.H, 0, len(users))
|
|
for _, user := range users {
|
|
row := userPublic(user)
|
|
row["generation_count"] = user.GenerationCount
|
|
out = append(out, row)
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"data": out, "stats": stats})
|
|
}
|
|
|
|
func (h *AdminReadHandler) Models(c *gin.Context) {
|
|
items, err := h.admin.ModelsView(c.Request.Context())
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"detail": "failed to load models"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"data": items})
|
|
}
|
|
|
|
func (h *AdminReadHandler) Logs(c *gin.Context) {
|
|
limit := parseInt(c.Query("limit"), 50)
|
|
offset := parseInt(c.Query("offset"), 0)
|
|
kind := c.Query("kind")
|
|
status := c.Query("status")
|
|
var since *time.Time
|
|
if raw := c.Query("since"); raw != "" {
|
|
if f, err := strconv.ParseFloat(raw, 64); err == nil {
|
|
t := time.Unix(int64(f), 0)
|
|
since = &t
|
|
}
|
|
}
|
|
|
|
items, total, stats, err := h.admin.Logs(c.Request.Context(), limit, offset, kind, status, nil, since, "", "", c.Query("source"), false)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"detail": "failed to load logs"})
|
|
return
|
|
}
|
|
// Resolve user_id -> display name once for the page (mirrors admin.py).
|
|
nameByID, err := h.admin.UserNameMap(c.Request.Context())
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"detail": "failed to load logs"})
|
|
return
|
|
}
|
|
out := make([]gin.H, 0, len(items))
|
|
for _, item := range items {
|
|
var userName any
|
|
if item.UserID == "" {
|
|
userName = "匿名"
|
|
} else if name, ok := nameByID[item.UserID]; ok {
|
|
userName = name
|
|
} else {
|
|
userName = item.UserID
|
|
}
|
|
out = append(out, gin.H{
|
|
"id": item.ID,
|
|
"ts": item.TS.Unix(),
|
|
"kind": item.Kind,
|
|
"status": item.Status,
|
|
"model": item.Model,
|
|
"provider": item.Provider,
|
|
"prompt": item.Prompt,
|
|
"ratio": item.Ratio,
|
|
"resolution": item.Resolution,
|
|
"duration": item.Duration,
|
|
"refs": item.Refs,
|
|
"source": item.Source,
|
|
"user_id": emptyStringNil(item.UserID),
|
|
"user_name": userName,
|
|
"cost": item.Cost,
|
|
"elapsed_ms": item.ElapsedMS,
|
|
"file": emptyStringNil(item.File),
|
|
"error": emptyStringNil(item.Error),
|
|
"created_at": unixSec(item.CreatedAt),
|
|
"updated_at": unixSec(item.UpdatedAt),
|
|
})
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"data": out,
|
|
"total": total,
|
|
"limit": limit,
|
|
"offset": offset,
|
|
"stats": stats,
|
|
})
|
|
}
|
|
|
|
func (h *AdminReadHandler) Stats(c *gin.Context) {
|
|
stats, err := h.admin.Stats(c.Request.Context())
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"detail": "failed to load stats"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, stats)
|
|
}
|
|
|
|
func (h *AdminReadHandler) Dashboard(c *gin.Context) {
|
|
data, err := h.admin.Dashboard(c.Request.Context())
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"detail": "failed to load dashboard"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, data)
|
|
}
|
|
|
|
func (h *AdminReadHandler) Invites(c *gin.Context) {
|
|
items, stats, err := h.admin.Invites(c.Request.Context())
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"detail": "failed to load invites"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"data": items, "stats": stats})
|
|
}
|
|
|
|
func (h *AdminReadHandler) Providers(c *gin.Context) {
|
|
items, err := h.admin.Providers(c.Request.Context())
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"detail": "failed to load providers"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"data": items})
|
|
}
|
|
|
|
func (h *AdminReadHandler) Images(c *gin.Context) {
|
|
limit := parseInt(c.Query("limit"), 30)
|
|
offset := parseInt(c.Query("offset"), 0)
|
|
kind := c.Query("kind")
|
|
items, total, stats, err := h.admin.Images(c.Request.Context(), limit, offset, kind)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"detail": "failed to load images"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"data": items,
|
|
"total": total,
|
|
"limit": limit,
|
|
"offset": offset,
|
|
"stats": stats,
|
|
})
|
|
}
|
|
|
|
func userPublic(user model.User) gin.H {
|
|
keys := make([]gin.H, 0, len(user.APIKeys))
|
|
for _, key := range user.APIKeys {
|
|
keys = append(keys, gin.H{
|
|
"id": key.ID,
|
|
"name": key.Name,
|
|
"key_preview": key.KeyPreview,
|
|
"created_at": unixSec(key.CreatedAt),
|
|
"last_used_at": unixSecPtr(key.LastUsedAt),
|
|
})
|
|
}
|
|
return gin.H{
|
|
"id": user.ID,
|
|
"email": user.Email,
|
|
"name": user.Name,
|
|
"role": user.Role,
|
|
"status": user.Status,
|
|
"credits": user.Credits,
|
|
"notes": user.Notes,
|
|
"recharge_total": user.RechargeTotal,
|
|
"concurrency_group_id": user.ConcurrencyGroupID,
|
|
"created_at": unixSec(user.CreatedAt),
|
|
"last_login_at": unixSecPtr(user.LastLoginAt),
|
|
"last_login_ip": user.LastLoginIP,
|
|
"invite_code": user.InviteCode,
|
|
"invited_by": user.InvitedBy,
|
|
"checkin_last": user.CheckinLast,
|
|
"checkin_streak": user.CheckinStreak,
|
|
"api_keys": keys,
|
|
"has_password": user.PasswordHash != "",
|
|
}
|
|
}
|
|
|
|
// unixSec / unixSecPtr render timestamps as unix SECONDS — the frontend's
|
|
// fmtTs/fmtRelative expect seconds (matching the Python reference's time.time()),
|
|
// not the RFC3339 string Go marshals a time.Time into (which parses to NaN → "—").
|
|
func unixSec(t time.Time) any {
|
|
if t.IsZero() {
|
|
return nil
|
|
}
|
|
return t.Unix()
|
|
}
|
|
|
|
func unixSecPtr(t *time.Time) any {
|
|
if t == nil || t.IsZero() {
|
|
return nil
|
|
}
|
|
return t.Unix()
|
|
}
|
|
|
|
func parseInt(raw string, fallback int) int {
|
|
if raw == "" {
|
|
return fallback
|
|
}
|
|
if n, err := strconv.Atoi(raw); err == nil {
|
|
return n
|
|
}
|
|
return fallback
|
|
}
|
|
|
|
func emptyStringNil(v string) any {
|
|
if v == "" {
|
|
return nil
|
|
}
|
|
return v
|
|
}
|