refactor(grpc): restructure task handling and migrate to slog
All checks were successful
Build / build (push) Successful in 5m28s
All checks were successful
Build / build (push) Successful in 5m28s
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.
This commit is contained in:
@@ -3,8 +3,8 @@ package auth
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
@@ -12,17 +12,15 @@ import (
|
||||
"schedule_converter/config"
|
||||
)
|
||||
|
||||
// 登录
|
||||
func Login(captchaCode string) (bool, error) {
|
||||
return LoginWithClient(client.Client, captchaCode, config.Username, config.Password)
|
||||
}
|
||||
var nameRegexp = regexp.MustCompile(`(\w+)同学`)
|
||||
|
||||
// LoginWithClient 使用指定的 HTTP 客户端和凭据登录
|
||||
func LoginWithClient(httpClient *http.Client, captchaCode, username, password string) (bool, error) {
|
||||
fmt.Println("[4] 执行登录...")
|
||||
log := slog.With("username", username)
|
||||
log.Debug("执行登录")
|
||||
|
||||
data := fmt.Sprintf("usercode=%s&password=%s&code=%s", username, password, captchaCode)
|
||||
req, err := http.NewRequest("POST", config.BaseURL+"/login", strings.NewReader(data))
|
||||
req, err := http.NewRequest("POST", config.Get().BaseURL+"/login", strings.NewReader(data))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
@@ -40,29 +38,20 @@ func LoginWithClient(httpClient *http.Client, captchaCode, username, password st
|
||||
return false, err
|
||||
}
|
||||
|
||||
if client.CheckLoginSuccess(string(body)) {
|
||||
fmt.Println(" ✓ 登录成功!")
|
||||
|
||||
// 提取用户名
|
||||
nameMatch := regexp.MustCompile(`(\w+)同学`).FindStringSubmatch(string(body))
|
||||
if len(nameMatch) > 1 {
|
||||
fmt.Printf(" 欢迎, %s!\n", nameMatch[1])
|
||||
}
|
||||
|
||||
// 保存登录后的页面
|
||||
if err := os.WriteFile("login_result.html", body, 0644); err != nil {
|
||||
fmt.Printf(" 警告: 无法保存登录页面: %v\n", err)
|
||||
bodyStr := string(body)
|
||||
if client.CheckLoginSuccess(bodyStr) {
|
||||
if match := nameRegexp.FindStringSubmatch(bodyStr); len(match) > 1 {
|
||||
log.Info("登录成功", "name", match[1])
|
||||
} else {
|
||||
fmt.Println(" 登录后页面已保存到: login_result.html")
|
||||
log.Info("登录成功")
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
fmt.Println(" ✗ 登录失败")
|
||||
if client.IsCaptchaError(string(body)) {
|
||||
fmt.Println(" 错误: 验证码错误")
|
||||
if client.IsCaptchaError(bodyStr) {
|
||||
log.Warn("登录失败:验证码错误")
|
||||
} else {
|
||||
log.Warn("登录失败")
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user