From 2c0207c45f87ab8678fafc9da1606ab8eb36ae13 Mon Sep 17 00:00:00 2001 From: YGXB_net Date: Mon, 27 Jul 2026 00:41:14 +0800 Subject: [PATCH] =?UTF-8?q?refactor(api):=20=E4=BC=98=E5=8C=96=E6=8A=96?= =?UTF-8?q?=E9=9F=B3API=E8=AF=B7=E6=B1=82=E9=87=8D=E8=AF=95=E6=9C=BA?= =?UTF-8?q?=E5=88=B6=E5=92=8C=E5=AE=A2=E6=88=B7=E7=AB=AF=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 实现了抖音API请求失败时的自动重试逻辑,间隔100毫秒 - 添加了上下文取消支持,避免无限重试 - 将HTTP客户端改为全局变量,提升性能和连接复用 - 前端上传线程数限制从100调整为500 - 更新类型声明以符合最新Go语言规范 --- frontend/src/App.vue | 2 +- internal/api/api.go | 19 ++++++++++++++++--- main.go | 2 +- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/frontend/src/App.vue b/frontend/src/App.vue index 7798064..91b44da 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -200,7 +200,7 @@ try {
-
diff --git a/internal/api/api.go b/internal/api/api.go index 7ab9eeb..61adeb7 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -64,7 +64,19 @@ func UploadDataToServer(ctx context.Context, data string) error { secUserId := strings.Split(data, "----")[1] hasVideo, err := CheckHasVideo(secUserId) if err != nil { - return err + for err != nil { + fmt.Println("抖音请求api重试") + //定时器定时100毫秒 + ticker := time.NewTicker(100 * time.Millisecond) + + select { + case <-ctx.Done(): + return err + case <-ticker.C: + ticker.Stop() + hasVideo, err = CheckHasVideo(secUserId) + } + } } if hasVideo { params.Set("token", config.APPConfig.HasVideoToken) @@ -97,8 +109,9 @@ func UploadDataToServer(ctx context.Context, data string) error { return err } +var douYinClient = &http.Client{} + func CheckHasVideo(secUserId string) (bool, error) { - client := &http.Client{} req, err := http.NewRequest( "GET", "https://imdesktop.douyin.com/aweme/v1/web/user/profile/other/?sec_user_id="+secUserId, @@ -114,7 +127,7 @@ func CheckHasVideo(secUserId string) (bool, error) { req.Header.Add("Host", "imdesktop.douyin.com") req.Header.Add("Connection", "keep-alive") - res, err := client.Do(req) + res, err := douYinClient.Do(req) if err != nil { fmt.Println(err) return false, err diff --git a/main.go b/main.go index 47d9eae..c4c4121 100644 --- a/main.go +++ b/main.go @@ -30,7 +30,7 @@ func main() { }, BackgroundColour: &options.RGBA{R: 27, G: 38, B: 54, A: 1}, OnStartup: app.startup, - Bind: []interface{}{ + Bind: []any{ app, }, })