Files
schedule_converter/captcha/recognize.go
lan accc6567f2
All checks were successful
Build / build (push) Successful in 5m28s
refactor(grpc): restructure task handling and migrate to slog
Refactor the gRPC client and task handling logic to improve modularity,
error handling, and observability.

- Split `grpc/handler.go` into specialized handler files (login,
  classroom, exams, grades, schedule) to reduce complexity.
- Replace standard `log` with `log/slog` throughout the project for
  structured logging.
- Refactor `grpc/client.go` to use a thread-safe connection management
  pattern with `sync.RWMutex`.
- Remove the legacy `service` package and HTTP server implementation,
  transitioning the application to a pure gRPC runner mode.
- Clean up `config` and `pkg/errors` to remove unused utilities and
  simplify the API.
- Improve error reporting in parsers and HTTP clients by checking
  response status codes.
2026-06-09 23:08:03 +08:00

55 lines
1.0 KiB
Go

package captcha
import (
"fmt"
"io"
"log/slog"
"net/http"
"schedule_converter/client"
"schedule_converter/config"
)
func SaveCaptchaImage(httpClient *http.Client) ([]byte, error) {
req, err := http.NewRequest("GET", config.Get().BaseURL+"/captchaImage", nil)
if err != nil {
return nil, err
}
req.Header = client.GetHeaders()
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("获取验证码失败, 状态码: %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}
func GetAndRecognize(httpClient *http.Client) (string, error) {
_, err := SaveCaptchaImage(httpClient)
if err != nil {
return "", err
}
captchaCode, err := Recognize("captcha.png")
if err != nil {
return "", err
}
slog.Debug("验证码识别完成", "result", captchaCode)
return captchaCode, nil
}
func Recognize(imagePath string) (string, error) {
return "....", nil
}