feat: Add batch processing and upload functionality, including new backend APIs, logging system, SQLite database, and dedicated frontend pages.

This commit is contained in:
2026-02-01 02:53:37 +08:00
parent 94ba61528a
commit a605e46f2a
9 changed files with 953 additions and 99 deletions

View File

@@ -177,6 +177,85 @@ func HandleTeamProcessStop(w http.ResponseWriter, r *http.Request) {
Success(w, map[string]string{"message": "已发送停止信号"})
}
// HandleBatchHistory GET /api/batch/history - 获取历史批次(分页)
func HandleBatchHistory(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
Error(w, http.StatusMethodNotAllowed, "仅支持 GET")
return
}
if database.Instance == nil {
Error(w, http.StatusInternalServerError, "数据库未初始化")
return
}
// 获取分页参数
page := 1
pageSize := 5
if p := r.URL.Query().Get("page"); p != "" {
if v, err := fmt.Sscanf(p, "%d", &page); err == nil && v > 0 {
// page已设置
}
}
if ps := r.URL.Query().Get("page_size"); ps != "" {
if v, err := fmt.Sscanf(ps, "%d", &pageSize); err == nil && v > 0 {
// pageSize已设置
}
}
runs, total, err := database.Instance.GetBatchRunsWithPagination(page, pageSize)
if err != nil {
Error(w, http.StatusInternalServerError, fmt.Sprintf("查询失败: %v", err))
return
}
totalPages := (total + pageSize - 1) / pageSize
Success(w, map[string]interface{}{
"runs": runs,
"total": total,
"page": page,
"page_size": pageSize,
"total_pages": totalPages,
})
}
// HandleBatchDetail GET /api/batch/detail?id=xxx - 获取批次详情包含Team结果
func HandleBatchDetail(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
Error(w, http.StatusMethodNotAllowed, "仅支持 GET")
return
}
if database.Instance == nil {
Error(w, http.StatusInternalServerError, "数据库未初始化")
return
}
idStr := r.URL.Query().Get("id")
if idStr == "" {
Error(w, http.StatusBadRequest, "缺少批次ID")
return
}
var batchID int64
if _, err := fmt.Sscanf(idStr, "%d", &batchID); err != nil {
Error(w, http.StatusBadRequest, "无效的批次ID")
return
}
run, results, err := database.Instance.GetBatchRunWithResults(batchID)
if err != nil {
Error(w, http.StatusInternalServerError, fmt.Sprintf("查询失败: %v", err))
return
}
Success(w, map[string]interface{}{
"batch": run,
"results": results,
})
}
// runTeamProcess 执行 Team 批量处理 - 使用工作池模式
func runTeamProcess(req TeamProcessRequest) {
totalOwners := len(req.Owners)
@@ -272,6 +351,23 @@ func runTeamProcess(req TeamProcessRequest) {
totalRegistered += result.Registered
totalAddedToS2A += result.AddedToS2A
allErrors = append(allErrors, result.Errors...)
// 保存单个Team结果到数据库
if database.Instance != nil && batchID > 0 {
dbResult := database.BatchTeamResult{
TeamIndex: result.TeamIndex,
OwnerEmail: result.OwnerEmail,
TeamID: result.TeamID,
Registered: result.Registered,
AddedToS2A: result.AddedToS2A,
MemberEmails: result.MemberEmails,
Errors: result.Errors,
DurationMs: result.DurationMs,
}
if err := database.Instance.SaveBatchTeamResult(batchID, dbResult); err != nil {
logger.Error(fmt.Sprintf("保存Team结果失败: %v", err), result.OwnerEmail, "team")
}
}
}
// 计算成功率
@@ -324,6 +420,8 @@ func processSingleTeam(idx int, req TeamProcessRequest) (result TeamProcessResul
if database.Instance != nil {
if success {
database.Instance.MarkOwnerAsUsed(owner.Email)
database.Instance.DeleteTeamOwnerByEmail(owner.Email)
logger.Info(fmt.Sprintf("%s 母号已使用并删除: %s", logPrefix, owner.Email), owner.Email, "team")
} else {
// 失败时恢复为 valid允许重试
database.Instance.MarkOwnerAsFailed(owner.Email)
@@ -356,6 +454,8 @@ func processSingleTeam(idx int, req TeamProcessRequest) (result TeamProcessResul
logger.Warning(fmt.Sprintf("%s Token 无效或过期,标记为无效", logPrefix), owner.Email, "team")
if database.Instance != nil {
database.Instance.MarkOwnerAsInvalid(owner.Email)
database.Instance.DeleteTeamOwnerByEmail(owner.Email)
logger.Info(fmt.Sprintf("%s 母号无效已删除: %s", logPrefix, owner.Email), owner.Email, "team")
}
return result
}
@@ -377,6 +477,8 @@ func processSingleTeam(idx int, req TeamProcessRequest) (result TeamProcessResul
logger.Warning(fmt.Sprintf("%s Team 邀请已满,标记母号为已使用: %v", logPrefix, err), owner.Email, "team")
if database.Instance != nil {
database.Instance.MarkOwnerAsUsed(owner.Email)
database.Instance.DeleteTeamOwnerByEmail(owner.Email)
logger.Info(fmt.Sprintf("%s 母号已使用并删除(邀请已满): %s", logPrefix, owner.Email), owner.Email, "team")
}
result.Errors = append(result.Errors, "Team 邀请已满")
result.DurationMs = time.Since(startTime).Milliseconds()
@@ -390,6 +492,8 @@ func processSingleTeam(idx int, req TeamProcessRequest) (result TeamProcessResul
logger.Error(fmt.Sprintf("%s Team 被封禁,标记为无效: %v", logPrefix, err), owner.Email, "team")
if database.Instance != nil {
database.Instance.MarkOwnerAsInvalid(owner.Email)
database.Instance.DeleteTeamOwnerByEmail(owner.Email)
logger.Info(fmt.Sprintf("%s 母号无效已删除(Team被封禁): %s", logPrefix, owner.Email), owner.Email, "team")
}
result.Errors = append(result.Errors, "Team 被封禁")
result.DurationMs = time.Since(startTime).Milliseconds()
@@ -426,6 +530,8 @@ func processSingleTeam(idx int, req TeamProcessRequest) (result TeamProcessResul
logger.Warning(fmt.Sprintf("%s Team 邀请已满,标记母号为已使用,停止后续处理", logPrefix), owner.Email, "team")
if database.Instance != nil {
database.Instance.MarkOwnerAsUsed(owner.Email)
database.Instance.DeleteTeamOwnerByEmail(owner.Email)
logger.Info(fmt.Sprintf("%s 母号已使用并删除(Team耗尽): %s", logPrefix, owner.Email), owner.Email, "team")
}
}
}