- 重命名模块 jwts -> schedule_converter
- 新增 service 包,统一业务逻辑层
- 新增 pkg/logger 包,使用 slog 结构化日志
- 新增 pkg/errors 包,统一错误处理
- 拆分 captcha 包,图片识别移至 service/recognizer
- 清理 models 包,只保留数据结构定义
- 消除 grpc/handler.go 中的重复代码
- 简化 main.go,使用 slog 和 context
- 配置支持单例模式和结构化配置
- 使用 any 替代 interface{}
- 使用 sync.Map 替代 map + RWMutex
68 lines
1.3 KiB
Go
68 lines
1.3 KiB
Go
package captcha
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
|
|
"schedule_converter/client"
|
|
"schedule_converter/config"
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
)
|
|
|
|
func SaveCaptchaImage(httpClient *http.Client) ([]byte, error) {
|
|
fmt.Println("[2] 获取验证码...")
|
|
|
|
req, err := http.NewRequest("GET", config.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()
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := os.WriteFile("captcha.png", body, 0644); err != nil {
|
|
fmt.Printf(" 警告: 无法保存验证码图片: %v\n", err)
|
|
} else {
|
|
fmt.Println(" 验证码已保存到: captcha.png")
|
|
}
|
|
|
|
return body, nil
|
|
}
|
|
|
|
func Recognize(imagePath string) (string, error) {
|
|
fmt.Println("[3] 使用固定验证码...")
|
|
return "....", 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
|
|
}
|
|
|
|
fmt.Printf(" 识别结果: %s\n", captchaCode)
|
|
return captchaCode, nil
|
|
}
|
|
|
|
func ParseHTML(html string) (*goquery.Document, error) {
|
|
return goquery.NewDocumentFromReader(strings.NewReader(html))
|
|
}
|