修复 ocr 错误
This commit is contained in:
@@ -96,7 +96,7 @@ func (h *Handler) GetQrCodeStatsHandler(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"data": stats})
|
||||
}
|
||||
|
||||
// RecognizeQrCodeGroupNameHandler 调用 PaddleOCR API 识别二维码图片中的企业微信群名
|
||||
// RecognizeQrCodeGroupNameHandler 同步调用 PaddleOCR API 识别二维码图片中的企业微信群名(保留向后兼容)
|
||||
func (h *Handler) RecognizeQrCodeGroupNameHandler(c *gin.Context) {
|
||||
file, header, err := c.Request.FormFile("file")
|
||||
if err != nil {
|
||||
@@ -131,6 +131,70 @@ func (h *Handler) RecognizeQrCodeGroupNameHandler(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"data": result})
|
||||
}
|
||||
|
||||
// SubmitOCRJobHandler 提交 OCR 任务到 PaddleOCR,立即返回任务 ID
|
||||
func (h *Handler) SubmitOCRJobHandler(c *gin.Context) {
|
||||
file, header, err := c.Request.FormFile("file")
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "请上传二维码图片"})
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
jobID, err := h.service.repo.SubmitOCRJob(
|
||||
c.Request.Context(),
|
||||
header.Filename,
|
||||
header.Header.Get("Content-Type"),
|
||||
file,
|
||||
)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrQrCodeOCRNotConfigured) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "请先在系统配置中填写 PaddleOCR API Token"})
|
||||
return
|
||||
}
|
||||
if errors.Is(err, ErrQrCodeOCRInvalidFile) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的二维码图片"})
|
||||
return
|
||||
}
|
||||
if errors.Is(err, ErrQrCodeOCRUnavailable) {
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": "PaddleOCR 服务暂时不可用"})
|
||||
return
|
||||
}
|
||||
if errors.Is(err, ErrQrCodeOCRRedisDisabled) {
|
||||
c.JSON(http.StatusServiceUnavailable, gin.H{"error": "OCR 异步模式需要 Redis"})
|
||||
return
|
||||
}
|
||||
if errors.Is(err, ErrQrCodeOCRJobNotFound) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "OCR 任务不存在或已过期"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"data": gin.H{"job_id": jobID}})
|
||||
}
|
||||
|
||||
// GetOCRJobResultHandler 查询异步 OCR 任务状态和结果
|
||||
func (h *Handler) GetOCRJobResultHandler(c *gin.Context) {
|
||||
jobID := c.Param("jobId")
|
||||
if jobID == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "缺少任务 ID"})
|
||||
return
|
||||
}
|
||||
|
||||
record, err := h.service.repo.PollOCRJobResult(c.Request.Context(), jobID)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrQrCodeOCRJobNotFound) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "OCR 任务不存在或已过期"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"data": record})
|
||||
}
|
||||
|
||||
// UpdateQrCodeHandler 更新二维码
|
||||
func (h *Handler) UpdateQrCodeHandler(c *gin.Context) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
|
||||
Reference in New Issue
Block a user