@@ -66,6 +66,10 @@ func (y *Cloud189TV) AppKeySignatureHeader(url, method string) map[string]string
66
66
}
67
67
68
68
func (y * Cloud189TV ) request (url , method string , callback base.ReqCallback , params map [string ]string , resp interface {}, isFamily ... bool ) ([]byte , error ) {
69
+ return y .requestWithRetry (url , method , callback , params , resp , 0 , isFamily ... )
70
+ }
71
+
72
+ func (y * Cloud189TV ) requestWithRetry (url , method string , callback base.ReqCallback , params map [string ]string , resp interface {}, retryCount int , isFamily ... bool ) ([]byte , error ) {
69
73
req := y .client .R ().SetQueryParams (clientSuffix ())
70
74
71
75
if params != nil {
@@ -91,7 +95,22 @@ func (y *Cloud189TV) request(url, method string, callback base.ReqCallback, para
91
95
92
96
if strings .Contains (res .String (), "userSessionBO is null" ) ||
93
97
strings .Contains (res .String (), "InvalidSessionKey" ) {
94
- return nil , errors .New ("session expired" )
98
+ // 限制重试次数,避免无限递归
99
+ if retryCount >= 3 {
100
+ y .Addition .AccessToken = ""
101
+ op .MustSaveDriverStorage (y )
102
+ return nil , errors .New ("session expired after retry" )
103
+ }
104
+
105
+ // 尝试刷新会话
106
+ if err := y .refreshSession (); err != nil {
107
+ // 如果刷新失败,说明AccessToken也已过期,需要重新登录
108
+ y .Addition .AccessToken = ""
109
+ op .MustSaveDriverStorage (y )
110
+ return nil , errors .New ("session expired" )
111
+ }
112
+ // 如果刷新成功,则重试原始请求(增加重试计数)
113
+ return y .requestWithRetry (url , method , callback , params , resp , retryCount + 1 , isFamily ... )
95
114
}
96
115
97
116
// 处理错误
@@ -211,7 +230,7 @@ func (y *Cloud189TV) login() (err error) {
211
230
var erron RespErr
212
231
var tokenInfo AppSessionResp
213
232
if y .Addition .AccessToken == "" {
214
- if y .Addition . TempUuid == "" {
233
+ if y .TempUuid == "" {
215
234
// 获取登录参数
216
235
var uuidInfo UuidInfoResp
217
236
req .SetResult (& uuidInfo ).SetError (& erron )
@@ -230,7 +249,7 @@ func (y *Cloud189TV) login() (err error) {
230
249
if uuidInfo .Uuid == "" {
231
250
return errors .New ("uuidInfo is empty" )
232
251
}
233
- y .Addition . TempUuid = uuidInfo .Uuid
252
+ y .TempUuid = uuidInfo .Uuid
234
253
op .MustSaveDriverStorage (y )
235
254
236
255
// 展示二维码
@@ -258,7 +277,7 @@ func (y *Cloud189TV) login() (err error) {
258
277
// Signature
259
278
req .SetHeaders (y .AppKeySignatureHeader (ApiUrl + "/family/manage/qrcodeLoginResult.action" ,
260
279
http .MethodGet ))
261
- req .SetQueryParam ("uuid" , y .Addition . TempUuid )
280
+ req .SetQueryParam ("uuid" , y .TempUuid )
262
281
_ , err = req .Execute (http .MethodGet , ApiUrl + "/family/manage/qrcodeLoginResult.action" )
263
282
if err != nil {
264
283
return
@@ -270,7 +289,6 @@ func (y *Cloud189TV) login() (err error) {
270
289
return errors .New ("E189AccessToken is empty" )
271
290
}
272
291
y .Addition .AccessToken = accessTokenResp .E189AccessToken
273
- y .Addition .TempUuid = ""
274
292
}
275
293
}
276
294
// 获取SessionKey 和 SessionSecret
@@ -294,6 +312,44 @@ func (y *Cloud189TV) login() (err error) {
294
312
return
295
313
}
296
314
315
+ // refreshSession 尝试使用现有的 AccessToken 刷新会话
316
+ func (y * Cloud189TV ) refreshSession () (err error ) {
317
+ var erron RespErr
318
+ var tokenInfo AppSessionResp
319
+ reqb := y .client .R ().SetQueryParams (clientSuffix ())
320
+ reqb .SetResult (& tokenInfo ).SetError (& erron )
321
+ // Signature
322
+ reqb .SetHeaders (y .AppKeySignatureHeader (ApiUrl + "/family/manage/loginFamilyMerge.action" ,
323
+ http .MethodGet ))
324
+ reqb .SetQueryParam ("e189AccessToken" , y .Addition .AccessToken )
325
+ _ , err = reqb .Execute (http .MethodGet , ApiUrl + "/family/manage/loginFamilyMerge.action" )
326
+ if err != nil {
327
+ return
328
+ }
329
+
330
+ if erron .HasError () {
331
+ return & erron
332
+ }
333
+
334
+ y .tokenInfo = & tokenInfo
335
+ return nil
336
+ }
337
+
338
+ func (y * Cloud189TV ) keepAlive () {
339
+ _ , err := y .get (ApiUrl + "/keepUserSession.action" , func (r * resty.Request ) {
340
+ r .SetQueryParams (clientSuffix ())
341
+ }, nil )
342
+ if err != nil {
343
+ utils .Log .Warnf ("189tv: Failed to keep user session alive: %v" , err )
344
+ // 如果keepAlive失败,尝试刷新session
345
+ if refreshErr := y .refreshSession (); refreshErr != nil {
346
+ utils .Log .Errorf ("189tv: Failed to refresh session after keepAlive error: %v" , refreshErr )
347
+ }
348
+ } else {
349
+ utils .Log .Debugf ("189tv: User session kept alive successfully." )
350
+ }
351
+ }
352
+
297
353
func (y * Cloud189TV ) RapidUpload (ctx context.Context , dstDir model.Obj , stream model.FileStreamer , isFamily bool , overwrite bool ) (model.Obj , error ) {
298
354
fileMd5 := stream .GetHash ().GetHash (utils .MD5 )
299
355
if len (fileMd5 ) < utils .MD5 .Width {
0 commit comments