fix web api client issue

This commit is contained in:
tickstep 2024-03-02 21:22:02 +08:00
parent 4a623b443c
commit 787aa53ce7
9 changed files with 82 additions and 65 deletions

View File

@ -79,7 +79,7 @@ func CmdAlbum() cli.Command {
return nil return nil
} }
if config.Config.ActiveUser().PanClient().WebapiPanClient() == nil { if config.Config.ActiveUser().PanClient().WebapiPanClient() == nil {
fmt.Println("WEB客户端未登录请登录后再使用") fmt.Println("WEB客户端未登录请登录后再使用该命令")
return nil return nil
} }
RunAlbumList() RunAlbumList()
@ -107,7 +107,7 @@ func CmdAlbum() cli.Command {
return nil return nil
} }
if config.Config.ActiveUser().PanClient().WebapiPanClient() == nil { if config.Config.ActiveUser().PanClient().WebapiPanClient() == nil {
fmt.Println("WEB客户端未登录请登录后再使用") fmt.Println("WEB客户端未登录请登录后再使用该命令")
return nil return nil
} }
RunAlbumCreate(c.Args().Get(0), c.Args().Get(1)) RunAlbumCreate(c.Args().Get(0), c.Args().Get(1))
@ -136,7 +136,7 @@ func CmdAlbum() cli.Command {
return nil return nil
} }
if config.Config.ActiveUser().PanClient().WebapiPanClient() == nil { if config.Config.ActiveUser().PanClient().WebapiPanClient() == nil {
fmt.Println("WEB客户端未登录请登录后再使用") fmt.Println("WEB客户端未登录请登录后再使用该命令")
return nil return nil
} }
RunAlbumDelete(c.Args()) RunAlbumDelete(c.Args())
@ -162,7 +162,7 @@ func CmdAlbum() cli.Command {
return nil return nil
} }
if config.Config.ActiveUser().PanClient().WebapiPanClient() == nil { if config.Config.ActiveUser().PanClient().WebapiPanClient() == nil {
fmt.Println("WEB客户端未登录请登录后再使用") fmt.Println("WEB客户端未登录请登录后再使用该命令")
return nil return nil
} }
RunAlbumRename(c.Args().Get(0), c.Args().Get(1)) RunAlbumRename(c.Args().Get(0), c.Args().Get(1))
@ -188,7 +188,7 @@ func CmdAlbum() cli.Command {
return nil return nil
} }
if config.Config.ActiveUser().PanClient().WebapiPanClient() == nil { if config.Config.ActiveUser().PanClient().WebapiPanClient() == nil {
fmt.Println("WEB客户端未登录请登录后再使用") fmt.Println("WEB客户端未登录请登录后再使用该命令")
return nil return nil
} }
RunAlbumListFile(c.Args().Get(0)) RunAlbumListFile(c.Args().Get(0))
@ -214,7 +214,7 @@ func CmdAlbum() cli.Command {
return nil return nil
} }
if config.Config.ActiveUser().PanClient().WebapiPanClient() == nil { if config.Config.ActiveUser().PanClient().WebapiPanClient() == nil {
fmt.Println("WEB客户端未登录请登录后再使用") fmt.Println("WEB客户端未登录请登录后再使用该命令")
return nil return nil
} }
subArgs := c.Args() subArgs := c.Args()
@ -248,7 +248,7 @@ func CmdAlbum() cli.Command {
return nil return nil
} }
if config.Config.ActiveUser().PanClient().WebapiPanClient() == nil { if config.Config.ActiveUser().PanClient().WebapiPanClient() == nil {
fmt.Println("WEB客户端未登录请登录后再使用") fmt.Println("WEB客户端未登录请登录后再使用该命令")
return nil return nil
} }
subArgs := c.Args() subArgs := c.Args()
@ -280,7 +280,7 @@ func CmdAlbum() cli.Command {
return nil return nil
} }
if config.Config.ActiveUser().PanClient().WebapiPanClient() == nil { if config.Config.ActiveUser().PanClient().WebapiPanClient() == nil {
fmt.Println("WEB客户端未登录请登录后再使用") fmt.Println("WEB客户端未登录请登录后再使用该命令")
return nil return nil
} }
subArgs := c.Args() subArgs := c.Args()

View File

@ -66,7 +66,7 @@ func CmdRecycle() cli.Command {
return nil return nil
} }
if config.Config.ActiveUser().PanClient().WebapiPanClient() == nil { if config.Config.ActiveUser().PanClient().WebapiPanClient() == nil {
fmt.Println("WEB客户端未登录请登录后再使用") fmt.Println("WEB客户端未登录请登录后再使用该命令")
return nil return nil
} }
RunRecycleList(parseDriveId(c)) RunRecycleList(parseDriveId(c))
@ -92,7 +92,7 @@ func CmdRecycle() cli.Command {
return nil return nil
} }
if config.Config.ActiveUser().PanClient().WebapiPanClient() == nil { if config.Config.ActiveUser().PanClient().WebapiPanClient() == nil {
fmt.Println("WEB客户端未登录请登录后再使用") fmt.Println("WEB客户端未登录请登录后再使用该命令")
return nil return nil
} }
if c.NArg() <= 0 { if c.NArg() <= 0 {
@ -122,7 +122,7 @@ func CmdRecycle() cli.Command {
return nil return nil
} }
if config.Config.ActiveUser().PanClient().WebapiPanClient() == nil { if config.Config.ActiveUser().PanClient().WebapiPanClient() == nil {
fmt.Println("WEB客户端未登录请登录后再使用") fmt.Println("WEB客户端未登录请登录后再使用该命令")
return nil return nil
} }
if c.Bool("all") { if c.Bool("all") {

View File

@ -54,7 +54,7 @@ func CmdSave() cli.Command {
return nil return nil
} }
if config.Config.ActiveUser().PanClient().WebapiPanClient() == nil { if config.Config.ActiveUser().PanClient().WebapiPanClient() == nil {
fmt.Println("WEB客户端未登录请登录后再使用") fmt.Println("WEB客户端未登录请登录后再使用该命令")
return nil return nil
} }
RunSave(parseDriveId(c), c.Args()...) RunSave(parseDriveId(c), c.Args()...)

View File

@ -74,7 +74,7 @@ func CmdShare() cli.Command {
return nil return nil
} }
if config.Config.ActiveUser().PanClient().WebapiPanClient() == nil { if config.Config.ActiveUser().PanClient().WebapiPanClient() == nil {
fmt.Println("WEB客户端未登录请登录后再使用") fmt.Println("WEB客户端未登录请登录后再使用该命令")
return nil return nil
} }
et := "" et := ""
@ -152,7 +152,7 @@ func CmdShare() cli.Command {
return nil return nil
} }
if config.Config.ActiveUser().PanClient().WebapiPanClient() == nil { if config.Config.ActiveUser().PanClient().WebapiPanClient() == nil {
fmt.Println("WEB客户端未登录请登录后再使用") fmt.Println("WEB客户端未登录请登录后再使用该命令")
return nil return nil
} }
RunShareList() RunShareList()
@ -172,7 +172,7 @@ func CmdShare() cli.Command {
return nil return nil
} }
if config.Config.ActiveUser().PanClient().WebapiPanClient() == nil { if config.Config.ActiveUser().PanClient().WebapiPanClient() == nil {
fmt.Println("WEB客户端未登录请登录后再使用") fmt.Println("WEB客户端未登录请登录后再使用该命令")
return nil return nil
} }
if c.NArg() < 1 { if c.NArg() < 1 {
@ -203,7 +203,7 @@ func CmdShare() cli.Command {
return nil return nil
} }
if config.Config.ActiveUser().PanClient().WebapiPanClient() == nil { if config.Config.ActiveUser().PanClient().WebapiPanClient() == nil {
fmt.Println("WEB客户端未登录请登录后再使用") fmt.Println("WEB客户端未登录请登录后再使用该命令")
return nil return nil
} }
if c.NArg() < 1 { if c.NArg() < 1 {

View File

@ -146,8 +146,8 @@ func RefreshTokenInNeed(activeUser *config.PanUser, deviceName string) bool {
return false return false
} }
// refresh expired token // refresh expired web token
if activeUser.PanClient() != nil { if activeUser.PanClient().WebapiPanClient() != nil {
if len(activeUser.WebapiToken.AccessToken) > 0 { if len(activeUser.WebapiToken.AccessToken) > 0 {
cz := time.FixedZone("CST", 8*3600) // 东8区 cz := time.FixedZone("CST", 8*3600) // 东8区
expiredTime := time.Unix(activeUser.WebapiToken.Expired, 0).In(cz) expiredTime := time.Unix(activeUser.WebapiToken.Expired, 0).In(cz)
@ -164,7 +164,7 @@ func RefreshTokenInNeed(activeUser *config.PanUser, deviceName string) bool {
} }
// need update refresh token // need update refresh token
logger.Verboseln("access token expired, get new from server") logger.Verboseln("web access token expired, get new from server")
loginHelper := panlogin.NewLoginHelper(config.DefaultTokenServiceWebHost) loginHelper := panlogin.NewLoginHelper(config.DefaultTokenServiceWebHost)
wt, e := loginHelper.GetWebapiNewToken(activeUser.TicketId, activeUser.UserId) wt, e := loginHelper.GetWebapiNewToken(activeUser.TicketId, activeUser.UserId)
if e != nil { if e != nil {

View File

@ -56,7 +56,7 @@ func CmdXcp() cli.Command {
return nil return nil
} }
if config.Config.ActiveUser().PanClient().WebapiPanClient() == nil { if config.Config.ActiveUser().PanClient().WebapiPanClient() == nil {
fmt.Println("WEB客户端未登录请登录后再使用") fmt.Println("WEB客户端未登录请登录后再使用该命令")
return nil return nil
} }
srcDriveId := parseDriveId(c) srcDriveId := parseDriveId(c)

View File

@ -29,3 +29,8 @@ func (p *PanClient) WebapiPanClient() *aliyunpan.PanClient {
func (p *PanClient) OpenapiPanClient() *aliyunpan_open.OpenPanClient { func (p *PanClient) OpenapiPanClient() *aliyunpan_open.OpenPanClient {
return p.openapiPanClient return p.openapiPanClient
} }
func (p *PanClient) UpdateClient(openClient *aliyunpan_open.OpenPanClient, webClient *aliyunpan.PanClient) {
p.webapiPanClient = webClient
p.openapiPanClient = openClient
}

View File

@ -456,6 +456,7 @@ func (c *PanConfig) SetActiveUser(user *PanUser) *PanUser {
u.WebapiToken = user.WebapiToken u.WebapiToken = user.WebapiToken
u.OpenapiToken = user.OpenapiToken u.OpenapiToken = user.OpenapiToken
u.TicketId = user.TicketId u.TicketId = user.TicketId
u.PanClient().UpdateClient(user.PanClient().OpenapiPanClient(), user.PanClient().WebapiPanClient())
needToInsert = false needToInsert = false
break break
} }

View File

@ -153,61 +153,70 @@ doOpenLoginAct:
AccessToken: wt.AccessToken, AccessToken: wt.AccessToken,
Expired: wt.Expired, Expired: wt.Expired,
} }
goto doOpenLoginAct
} }
time.Sleep(time.Duration(1) * time.Second)
goto doOpenLoginAct
} }
return nil, err return nil, err
} }
doWebLoginAct: doWebLoginAct:
// setup webapi client // setup webapi client
appConfig := aliyunpan.AppConfig{ var webUserInfo *aliyunpan.UserInfo
AppId: "25dzX3vbYqktVxyX", var err2 *apierror.ApiError
DeviceId: deviceId, var webPanClient *aliyunpan.PanClient
UserId: userId, if webapiToken != nil && webapiToken.AccessToken != "" {
Nonce: 0, appConfig := aliyunpan.AppConfig{
PublicKey: "", AppId: "25dzX3vbYqktVxyX",
} DeviceId: deviceId,
webPanClient := aliyunpan.NewPanClient(aliyunpan.WebLoginToken{ UserId: userId,
AccessTokenType: "Bearer", Nonce: 0,
AccessToken: webapiToken.AccessToken, PublicKey: "",
RefreshToken: "", }
ExpiresIn: 7200, webPanClient = aliyunpan.NewPanClient(aliyunpan.WebLoginToken{
ExpireTime: webapiToken.GetExpiredTimeCstStr(), AccessTokenType: "Bearer",
}, aliyunpan.AppLoginToken{}, appConfig, aliyunpan.SessionConfig{ AccessToken: webapiToken.AccessToken,
DeviceName: deviceName, RefreshToken: "",
ModelName: "Windows网页版", ExpiresIn: 7200,
}) ExpireTime: webapiToken.GetExpiredTimeCstStr(),
// web api token maybe expired }, aliyunpan.AppLoginToken{}, appConfig, aliyunpan.SessionConfig{
// check & refresh new one DeviceName: deviceName,
webUserInfo, err2 := webPanClient.GetUserInfo() ModelName: "Windows网页版",
if err2 != nil { })
if err2.Code == apierror.ApiCodeTokenExpiredCode && tryRefreshWebToken { // web api token maybe expired
tryRefreshWebToken = false // check & refresh new one
wt, e := loginHelper.GetWebapiNewToken(ticketId, userId) webUserInfo, err2 = webPanClient.GetUserInfo()
if e != nil { if err2 != nil {
logger.Verboseln("get web token from server error: ", e) if err2.Code == apierror.ApiCodeTokenExpiredCode && tryRefreshWebToken {
} tryRefreshWebToken = false
if wt != nil { wt, e := loginHelper.GetWebapiNewToken(ticketId, userId)
webapiToken = &PanClientToken{ if e != nil {
AccessToken: wt.AccessToken, logger.Verboseln("get web token from server error: ", e)
Expired: wt.Expired,
} }
if wt != nil {
webapiToken = &PanClientToken{
AccessToken: wt.AccessToken,
Expired: wt.Expired,
}
}
time.Sleep(time.Duration(1) * time.Second)
goto doWebLoginAct goto doWebLoginAct
} }
webPanClient = nil
//return nil, err2
}
// web create session
if webUserInfo != nil {
appConfig.UserId = webUserInfo.UserId
webPanClient.UpdateAppConfig(appConfig)
r, e := webPanClient.CreateSession(nil)
if e != nil {
logger.Verboseln("call CreateSession error in SetupUserByCookie: " + e.Error())
}
if r != nil && !r.Result {
logger.Verboseln("上传签名秘钥失败,可能是你账号登录的设备已超最大数量")
}
} }
return nil, err2
}
// web create session
appConfig.UserId = webUserInfo.UserId
webPanClient.UpdateAppConfig(appConfig)
r, e := webPanClient.CreateSession(nil)
if e != nil {
logger.Verboseln("call CreateSession error in SetupUserByCookie: " + e.Error())
}
if r != nil && !r.Result {
logger.Verboseln("上传签名秘钥失败,可能是你账号登录的设备已超最大数量")
} }
// //
@ -226,7 +235,9 @@ doWebLoginAct:
if openUserInfo != nil { if openUserInfo != nil {
// fill userId for client // fill userId for client
u.PanClient().OpenapiPanClient().UpdateUserId(openUserInfo.UserId) u.PanClient().OpenapiPanClient().UpdateUserId(openUserInfo.UserId)
u.PanClient().WebapiPanClient().UpdateUserId(openUserInfo.UserId) if u.PanClient().WebapiPanClient() != nil {
u.PanClient().WebapiPanClient().UpdateUserId(openUserInfo.UserId)
}
// update user // update user
if openUserInfo.Nickname != "" { if openUserInfo.Nickname != "" {