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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -29,3 +29,8 @@ func (p *PanClient) WebapiPanClient() *aliyunpan.PanClient {
func (p *PanClient) OpenapiPanClient() *aliyunpan_open.OpenPanClient {
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.OpenapiToken = user.OpenapiToken
u.TicketId = user.TicketId
u.PanClient().UpdateClient(user.PanClient().OpenapiPanClient(), user.PanClient().WebapiPanClient())
needToInsert = false
break
}

View File

@ -153,61 +153,70 @@ doOpenLoginAct:
AccessToken: wt.AccessToken,
Expired: wt.Expired,
}
goto doOpenLoginAct
}
time.Sleep(time.Duration(1) * time.Second)
goto doOpenLoginAct
}
return nil, err
}
doWebLoginAct:
// setup webapi client
appConfig := aliyunpan.AppConfig{
AppId: "25dzX3vbYqktVxyX",
DeviceId: deviceId,
UserId: userId,
Nonce: 0,
PublicKey: "",
}
webPanClient := aliyunpan.NewPanClient(aliyunpan.WebLoginToken{
AccessTokenType: "Bearer",
AccessToken: webapiToken.AccessToken,
RefreshToken: "",
ExpiresIn: 7200,
ExpireTime: webapiToken.GetExpiredTimeCstStr(),
}, aliyunpan.AppLoginToken{}, appConfig, aliyunpan.SessionConfig{
DeviceName: deviceName,
ModelName: "Windows网页版",
})
// web api token maybe expired
// check & refresh new one
webUserInfo, err2 := webPanClient.GetUserInfo()
if err2 != nil {
if err2.Code == apierror.ApiCodeTokenExpiredCode && tryRefreshWebToken {
tryRefreshWebToken = false
wt, e := loginHelper.GetWebapiNewToken(ticketId, userId)
if e != nil {
logger.Verboseln("get web token from server error: ", e)
}
if wt != nil {
webapiToken = &PanClientToken{
AccessToken: wt.AccessToken,
Expired: wt.Expired,
var webUserInfo *aliyunpan.UserInfo
var err2 *apierror.ApiError
var webPanClient *aliyunpan.PanClient
if webapiToken != nil && webapiToken.AccessToken != "" {
appConfig := aliyunpan.AppConfig{
AppId: "25dzX3vbYqktVxyX",
DeviceId: deviceId,
UserId: userId,
Nonce: 0,
PublicKey: "",
}
webPanClient = aliyunpan.NewPanClient(aliyunpan.WebLoginToken{
AccessTokenType: "Bearer",
AccessToken: webapiToken.AccessToken,
RefreshToken: "",
ExpiresIn: 7200,
ExpireTime: webapiToken.GetExpiredTimeCstStr(),
}, aliyunpan.AppLoginToken{}, appConfig, aliyunpan.SessionConfig{
DeviceName: deviceName,
ModelName: "Windows网页版",
})
// web api token maybe expired
// check & refresh new one
webUserInfo, err2 = webPanClient.GetUserInfo()
if err2 != nil {
if err2.Code == apierror.ApiCodeTokenExpiredCode && tryRefreshWebToken {
tryRefreshWebToken = false
wt, e := loginHelper.GetWebapiNewToken(ticketId, userId)
if e != nil {
logger.Verboseln("get web token from server error: ", e)
}
if wt != nil {
webapiToken = &PanClientToken{
AccessToken: wt.AccessToken,
Expired: wt.Expired,
}
}
time.Sleep(time.Duration(1) * time.Second)
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 {
// fill userId for client
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
if openUserInfo.Nickname != "" {