@@ -66,6 +66,10 @@ func (y *Cloud189TV) AppKeySignatureHeader(url, method string) map[string]string
6666}
6767
6868func (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 ) {
6973 req := y .client .R ().SetQueryParams (clientSuffix ())
7074
7175 if params != nil {
@@ -91,7 +95,22 @@ func (y *Cloud189TV) request(url, method string, callback base.ReqCallback, para
9195
9296 if strings .Contains (res .String (), "userSessionBO is null" ) ||
9397 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 ... )
95114 }
96115
97116 // 处理错误
@@ -211,7 +230,7 @@ func (y *Cloud189TV) login() (err error) {
211230 var erron RespErr
212231 var tokenInfo AppSessionResp
213232 if y .Addition .AccessToken == "" {
214- if y .Addition . TempUuid == "" {
233+ if y .TempUuid == "" {
215234 // 获取登录参数
216235 var uuidInfo UuidInfoResp
217236 req .SetResult (& uuidInfo ).SetError (& erron )
@@ -230,7 +249,7 @@ func (y *Cloud189TV) login() (err error) {
230249 if uuidInfo .Uuid == "" {
231250 return errors .New ("uuidInfo is empty" )
232251 }
233- y .Addition . TempUuid = uuidInfo .Uuid
252+ y .TempUuid = uuidInfo .Uuid
234253 op .MustSaveDriverStorage (y )
235254
236255 // 展示二维码
@@ -258,7 +277,7 @@ func (y *Cloud189TV) login() (err error) {
258277 // Signature
259278 req .SetHeaders (y .AppKeySignatureHeader (ApiUrl + "/family/manage/qrcodeLoginResult.action" ,
260279 http .MethodGet ))
261- req .SetQueryParam ("uuid" , y .Addition . TempUuid )
280+ req .SetQueryParam ("uuid" , y .TempUuid )
262281 _ , err = req .Execute (http .MethodGet , ApiUrl + "/family/manage/qrcodeLoginResult.action" )
263282 if err != nil {
264283 return
@@ -270,7 +289,6 @@ func (y *Cloud189TV) login() (err error) {
270289 return errors .New ("E189AccessToken is empty" )
271290 }
272291 y .Addition .AccessToken = accessTokenResp .E189AccessToken
273- y .Addition .TempUuid = ""
274292 }
275293 }
276294 // 获取SessionKey 和 SessionSecret
@@ -294,6 +312,44 @@ func (y *Cloud189TV) login() (err error) {
294312 return
295313}
296314
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+
297353func (y * Cloud189TV ) RapidUpload (ctx context.Context , dstDir model.Obj , stream model.FileStreamer , isFamily bool , overwrite bool ) (model.Obj , error ) {
298354 fileMd5 := stream .GetHash ().GetHash (utils .MD5 )
299355 if len (fileMd5 ) < utils .MD5 .Width {
0 commit comments