2026-03-13 20:43:44 +08:00
|
|
|
package captcha
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
2026-06-09 23:08:03 +08:00
|
|
|
"log/slog"
|
2026-03-13 20:43:44 +08:00
|
|
|
"net/http"
|
|
|
|
|
|
2026-03-16 13:40:53 +08:00
|
|
|
"schedule_converter/client"
|
|
|
|
|
"schedule_converter/config"
|
2026-03-13 20:43:44 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func SaveCaptchaImage(httpClient *http.Client) ([]byte, error) {
|
2026-06-09 23:08:03 +08:00
|
|
|
req, err := http.NewRequest("GET", config.Get().BaseURL+"/captchaImage", nil)
|
2026-03-13 20:43:44 +08:00
|
|
|
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()
|
|
|
|
|
|
2026-06-09 23:08:03 +08:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
|
return nil, fmt.Errorf("获取验证码失败, 状态码: %d", resp.StatusCode)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-13 20:43:44 +08:00
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return body, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 13:40:53 +08:00
|
|
|
func GetAndRecognize(httpClient *http.Client) (string, error) {
|
|
|
|
|
_, err := SaveCaptchaImage(httpClient)
|
2026-03-13 20:43:44 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
captchaCode, err := Recognize("captcha.png")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-09 23:08:03 +08:00
|
|
|
slog.Debug("验证码识别完成", "result", captchaCode)
|
2026-03-13 20:43:44 +08:00
|
|
|
return captchaCode, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-09 23:08:03 +08:00
|
|
|
func Recognize(imagePath string) (string, error) {
|
|
|
|
|
return "....", nil
|
|
|
|
|
}
|