Skip to content

Commit 447a743

Browse files
committed
fix
1 parent f1249d3 commit 447a743

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

server/internal/bili/client.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ import (
1010
"github.com/imroc/req/v3"
1111
)
1212

13+
// min 辅助函数
14+
func min(a, b int) int {
15+
if a < b {
16+
return a
17+
}
18+
return b
19+
}
20+
1321
type BiliClient struct {
1422
Cookies string
1523
UID int64
@@ -469,18 +477,24 @@ func PollWebQRCodeStatus(oauthKey string) (*QRCodePollResponse, error) {
469477
}
470478

471479
// ExtractCookiesFromWebPollResponse 从Web端轮询响应中提取Cookie
472-
func ExtractCookiesFromWebPollResponse(pollResp *QRCodePollResponse) string {
480+
func ExtractCookiesFromWebPollResponse(pollResp *QRCodePollResponse, client *req.Client) string {
473481
if pollResp == nil || pollResp.Data.Code != 0 {
482+
log.Printf("[WEB_COOKIE] 登录未完成,跳过Cookie提取 - code=%d", pollResp.Data.Code)
474483
return ""
475484
}
476485

477486
if pollResp.Data.URL == "" {
487+
log.Printf("[WEB_COOKIE] 错误:URL为空")
478488
return ""
479489
}
480490

491+
// Web端登录成功后,URL中包含Cookie参数
492+
log.Printf("[WEB_COOKIE] 解析登录URL: %s", pollResp.Data.URL[:min(100, len(pollResp.Data.URL))])
493+
481494
// 解析URL中的Cookie参数
482495
parts := strings.Split(pollResp.Data.URL, "?")
483496
if len(parts) < 2 {
497+
log.Printf("[WEB_COOKIE] URL没有查询参数")
484498
return ""
485499
}
486500

@@ -499,6 +513,8 @@ func ExtractCookiesFromWebPollResponse(pollResp *QRCodePollResponse) string {
499513
sid := params["sid"]
500514

501515
if dedeUserID == "" || sessdata == "" || biliJct == "" {
516+
log.Printf("[WEB_COOKIE] 关键字段缺失 - DedeUserID: %v, SESSDATA: %v, bili_jct: %v",
517+
dedeUserID != "", sessdata != "", biliJct != "")
502518
return ""
503519
}
504520

@@ -515,5 +531,9 @@ func ExtractCookiesFromWebPollResponse(pollResp *QRCodePollResponse) string {
515531
cookieStrs = append(cookieStrs, fmt.Sprintf("sid=%s", sid))
516532
}
517533

518-
return strings.Join(cookieStrs, "; ")
534+
result := strings.Join(cookieStrs, "; ")
535+
log.Printf("[WEB_COOKIE] 提取成功 - DedeUserID: %s, SESSDATA长度: %d, bili_jct长度: %d",
536+
dedeUserID, len(sessdata), len(biliJct))
537+
538+
return result
519539
}

server/internal/controllers/user.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"time"
1111

1212
"github.com/gin-gonic/gin"
13+
"github.com/imroc/req/v3"
1314
"github.com/spiritlhl/goban/internal/bili"
1415
"github.com/spiritlhl/goban/internal/database"
1516
"github.com/spiritlhl/goban/internal/models"
@@ -175,11 +176,16 @@ func LoginCheck(c *gin.Context) {
175176
return
176177
}
177178

178-
log.Printf("[POLL] 轮询响应 - code: %d", pollResp.Data.Code)
179+
log.Printf("[POLL] 轮询响应 - code: %d, status: %v, message: %s, url: %s",
180+
pollResp.Data.Code, pollResp.Status, pollResp.Data.Message, pollResp.Data.URL)
181+
182+
log.Printf("[DEBUG] 会话类型: %s, AuthCode: %s", session.Status, session.AuthCode)
179183

180184
switch pollResp.Data.Code {
181185
case 0: // 登录成功
182-
cookieStr := bili.ExtractCookiesFromWebPollResponse(pollResp)
186+
// 创建HTTP客户端用于Cookie提取
187+
client := req.C().ImpersonateChrome()
188+
cookieStr := bili.ExtractCookiesFromWebPollResponse(pollResp, client)
183189
log.Printf("[Web] 提取到的Cookie: %s", cookieStr)
184190

185191
if cookieStr == "" {

0 commit comments

Comments
 (0)