package handlers import ( "net/http" "time" "github.com/gin-gonic/gin" "qr-scanner/config" "qr-scanner/models" "qr-scanner/services" ) type ScanHandler struct { cfg config.Config store *services.TaskStore scanner *services.Scanner } func NewScanHandler(cfg config.Config, store *services.TaskStore, scanner *services.Scanner) *ScanHandler { return &ScanHandler{cfg: cfg, store: store, scanner: scanner} } func (h *ScanHandler) StartScan(c *gin.Context) { var req models.ScanRequest if err := c.ShouldBindJSON(&req); err != nil || req.TaskID == "" { fail(c, http.StatusBadRequest, "请求参数错误") return } task, ok := h.store.Get(req.TaskID) if !ok { fail(c, http.StatusNotFound, "任务不存在") return } if task.Status == services.TaskExpired { fail(c, http.StatusGone, "任务已过期") return } concurrency := req.Concurrency if concurrency <= 0 { concurrency = h.cfg.DefaultWorkers } if concurrency <= 0 { concurrency = 1 } if h.cfg.MaxWorkers > 0 && concurrency > h.cfg.MaxWorkers { concurrency = h.cfg.MaxWorkers } timeout := req.Timeout if timeout <= 0 { timeout = h.cfg.DefaultTimeoutS } if timeout <= 0 { timeout = 30 } if h.cfg.MaxTimeoutS > 0 && timeout > h.cfg.MaxTimeoutS { timeout = h.cfg.MaxTimeoutS } task.Concurrency = concurrency task.TimeoutS = timeout task.UpdatedAt = time.Now() h.store.Put(task) _ = h.scanner.Start(task) respondOK(c, models.ScanStartResponse{TaskID: task.ID, Status: string(task.Status)}) }