Files
schedule_converter/auth/login.go

69 lines
1.6 KiB
Go
Raw Normal View History

2026-03-13 20:43:44 +08:00
package auth
import (
"fmt"
"io"
"net/http"
"os"
"regexp"
"strings"
"jwts/client"
"jwts/config"
)
// 登录
func Login(captchaCode string) (bool, error) {
return LoginWithClient(client.Client, captchaCode, config.Username, config.Password)
}
// LoginWithClient 使用指定的 HTTP 客户端和凭据登录
func LoginWithClient(httpClient *http.Client, captchaCode, username, password string) (bool, error) {
fmt.Println("[4] 执行登录...")
data := fmt.Sprintf("usercode=%s&password=%s&code=%s", username, password, captchaCode)
req, err := http.NewRequest("POST", config.BaseURL+"/login", strings.NewReader(data))
if err != nil {
return false, err
}
req.Header = client.GetHeadersWithContentType("application/x-www-form-urlencoded")
resp, err := httpClient.Do(req)
if err != nil {
return false, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
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)
} else {
fmt.Println(" 登录后页面已保存到: login_result.html")
}
return true, nil
}
fmt.Println(" ✗ 登录失败")
if client.IsCaptchaError(string(body)) {
fmt.Println(" 错误: 验证码错误")
}
return false, nil
}