feat: Establish core backend services including SQLite database, structured logging, and initial owner management capabilities.

This commit is contained in:
2026-02-06 20:53:05 +08:00
parent 98ac10987c
commit 6c8a036018
4 changed files with 199 additions and 6 deletions

View File

@@ -133,6 +133,7 @@ func startServer(cfg *config.Config) {
mux.HandleFunc("/api/db/owners/clear-used", api.CORS(handleClearUsedOwners)) // 清理已使用
mux.HandleFunc("/api/db/owners/delete/", api.CORS(handleDeleteOwner)) // DELETE /api/db/owners/delete/{id}
mux.HandleFunc("/api/db/owners/batch-delete", api.CORS(handleBatchDeleteOwners)) // POST 批量删除
mux.HandleFunc("/api/db/owners/ids", api.CORS(handleGetOwnerIDs)) // GET 获取所有ID全选用
mux.HandleFunc("/api/db/owners/refetch-account-ids", api.CORS(api.HandleRefetchAccountIDs))
mux.HandleFunc("/api/upload/validate", api.CORS(api.HandleUploadValidate))
@@ -977,6 +978,31 @@ func handleBatchDeleteOwners(w http.ResponseWriter, r *http.Request) {
})
}
// handleGetOwnerIDs GET /api/db/owners/ids?status=xxx - 获取所有符合条件的 owner ID全选用
func handleGetOwnerIDs(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
api.Error(w, http.StatusMethodNotAllowed, "仅支持 GET")
return
}
if database.Instance == nil {
api.Error(w, http.StatusInternalServerError, "数据库未初始化")
return
}
status := r.URL.Query().Get("status")
ids, err := database.Instance.GetTeamOwnerIDs(status)
if err != nil {
api.Error(w, http.StatusInternalServerError, fmt.Sprintf("查询失败: %v", err))
return
}
api.Success(w, map[string]interface{}{
"ids": ids,
"total": len(ids),
})
}
// handleRegisterTest POST /api/register/test - 测试注册流程
func handleRegisterTest(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {

View File

@@ -459,6 +459,33 @@ func (d *DB) UpdateOwnerAccountID(id int64, accountID string) error {
return err
}
// GetTeamOwnerIDs 获取所有符合条件的 owner ID用于全选
func (d *DB) GetTeamOwnerIDs(status string) ([]int64, error) {
query := "SELECT id FROM team_owners WHERE 1=1"
args := []interface{}{}
if status != "" {
query += " AND status = ?"
args = append(args, status)
}
query += " ORDER BY created_at DESC"
rows, err := d.db.Query(query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
var ids []int64
for rows.Next() {
var id int64
if err := rows.Scan(&id); err != nil {
continue
}
ids = append(ids, id)
}
return ids, nil
}
// GetOwnerStats 获取统计
func (d *DB) GetOwnerStats() map[string]int {
stats := map[string]int{

View File

@@ -238,7 +238,38 @@ func GetLogs(limit int) []LogEntry {
}
// GetLogsByModule 按模块筛选日志并分页(最新的在前)
// 优先从内存读取,内存无数据时回退到数据库
func GetLogsByModule(module string, page, pageSize int) ([]LogEntry, int) {
logsMu.RLock()
// 检查内存中是否有该模块的日志
hasMemoryLogs := false
for i := len(logs) - 1; i >= 0; i-- {
if logs[i].Module == module {
hasMemoryLogs = true
break
}
}
logsMu.RUnlock()
// 内存中无数据,回退到数据库查询
if !hasMemoryLogs && database.Instance != nil {
dbLogs, total, err := database.Instance.GetLogsByModule(module, page, pageSize)
if err == nil && total > 0 {
entries := make([]LogEntry, len(dbLogs))
for i, dl := range dbLogs {
entries[i] = LogEntry{
Timestamp: dl.Timestamp,
Level: dl.Level,
Message: dl.Message,
Email: dl.Email,
Module: dl.Module,
}
}
return entries, total
}
}
// 从内存读取
logsMu.RLock()
defer logsMu.RUnlock()
@@ -283,7 +314,40 @@ func ClearLogs() {
}
// GetLogsByModuleAndLevel 按模块和级别筛选日志并分页(最新的在前)
// 优先从内存读取,内存无数据时回退到数据库
func GetLogsByModuleAndLevel(module, level string, page, pageSize int) ([]LogEntry, int) {
logsMu.RLock()
// 检查内存中是否有该模块+级别的日志
hasMemoryLogs := false
for i := len(logs) - 1; i >= 0; i-- {
if logs[i].Module == module {
if level == "" || logs[i].Level == level {
hasMemoryLogs = true
break
}
}
}
logsMu.RUnlock()
// 内存中无数据,回退到数据库查询
if !hasMemoryLogs && database.Instance != nil {
dbLogs, total, err := database.Instance.GetLogsByModuleAndLevel(module, level, page, pageSize)
if err == nil && total > 0 {
entries := make([]LogEntry, len(dbLogs))
for i, dl := range dbLogs {
entries[i] = LogEntry{
Timestamp: dl.Timestamp,
Level: dl.Level,
Message: dl.Message,
Email: dl.Email,
Module: dl.Module,
}
}
return entries, total
}
}
// 从内存读取
logsMu.RLock()
defer logsMu.RUnlock()