mirror of
https://gitee.com/zhangsken/aliyunpan.git
synced 2026-10-16 10:29:42 +08:00
optimize login routine logic
This commit is contained in:
@@ -70,6 +70,7 @@ func CmdLs() cli.Command {
|
||||
`,
|
||||
Category: "阿里云盘",
|
||||
Before: ReloadConfigFunc,
|
||||
After: SaveConfigFunc,
|
||||
Action: func(c *cli.Context) error {
|
||||
if config.Config.ActiveUser() == nil {
|
||||
fmt.Println("未登录账号")
|
||||
|
||||
@@ -308,6 +308,9 @@ func RefreshOpenTokenInNeed(activeUser *config.PanUser) *panlogin.CommonResultEr
|
||||
// AutomaticallyRefreshTokenTask 自动刷新Token的后台任务
|
||||
func AutomaticallyRefreshTokenTask() {
|
||||
go func() {
|
||||
// 延迟启动自动刷新Token任务,因为主程序启动的时候默认会进行Token的初始化和刷新,避免无效的并行初始化Token
|
||||
time.Sleep(time.Duration(15) * time.Minute)
|
||||
|
||||
// 检测延迟时间(分钟)
|
||||
delayMinuteTime := time.Duration(1) * time.Minute
|
||||
//delayMinuteTime := time.Duration(10) * time.Second // for test
|
||||
@@ -329,7 +332,7 @@ func AutomaticallyRefreshTokenTask() {
|
||||
attempt = 1
|
||||
delayMinuteTime = time.Duration(1) * time.Minute
|
||||
|
||||
// 刷新Web端Token
|
||||
// 刷新Webapi端Token
|
||||
RefreshWebTokenInNeed(activeUser)
|
||||
|
||||
// 保存新token到配置文件
|
||||
|
||||
@@ -43,8 +43,6 @@ func CmdLocalCd() cli.Command {
|
||||
切换根目录:
|
||||
aliyunpan lcd /
|
||||
`,
|
||||
Before: command.ReloadConfigFunc,
|
||||
After: command.SaveConfigFunc,
|
||||
Action: func(c *cli.Context) error {
|
||||
if c.NArg() == 0 {
|
||||
cli.ShowCommandHelp(c, c.Command.Name)
|
||||
|
||||
@@ -18,7 +18,6 @@ import (
|
||||
"github.com/olekukonko/tablewriter"
|
||||
"github.com/tickstep/aliyunpan-api/aliyunpan"
|
||||
"github.com/tickstep/aliyunpan/cmder/cmdtable"
|
||||
"github.com/tickstep/aliyunpan/internal/command"
|
||||
"github.com/tickstep/library-go/converter"
|
||||
"github.com/urfave/cli"
|
||||
"log"
|
||||
@@ -47,7 +46,6 @@ func CmdLocalLs() cli.Command {
|
||||
详细列出 我的资源 内的文件和目录
|
||||
aliyunpan lls /我的资源
|
||||
`,
|
||||
Before: command.ReloadConfigFunc,
|
||||
Action: func(c *cli.Context) error {
|
||||
var (
|
||||
orderBy aliyunpan.FileOrderBy = aliyunpan.FileOrderByUpdatedAt
|
||||
|
||||
@@ -152,16 +152,23 @@ doOpenLoginAct:
|
||||
AccessToken: openapiToken.AccessToken,
|
||||
ExpiredAt: openapiToken.Expired,
|
||||
}, nil)
|
||||
openPanClient.SetAccessTokenRefreshCallback(func(userId string, newToken openapi.ApiToken) error {
|
||||
// api层自动刷新Token,回调通知程序
|
||||
logger.Verboseln("openapi token refresh, use new token")
|
||||
openapiToken.AccessToken = newToken.AccessToken
|
||||
openapiToken.Expired = newToken.ExpiredAt
|
||||
return nil
|
||||
})
|
||||
|
||||
// open api token maybe expired
|
||||
// check & refresh new one
|
||||
openUserInfo, err := openPanClient.GetUserInfo()
|
||||
if err != nil {
|
||||
if err.Code == apierror.ApiCodeTokenExpiredCode && tryRefreshOpenToken {
|
||||
if err.Code == apierror.ApiCodeTokenExpiredCode && tryRefreshOpenToken { // 如果是token过期,则尝试刷新
|
||||
tryRefreshOpenToken = false
|
||||
wt, e := loginHelper.GetOpenapiNewToken(ticketId, userId, openapiToken.AccessToken)
|
||||
if e != nil {
|
||||
logger.Verboseln("get openapi token from server error: ", e.Msg)
|
||||
wt, re := loginHelper.GetOpenapiNewToken(ticketId, userId, openapiToken.AccessToken)
|
||||
if re != nil {
|
||||
logger.Verboseln("get openapi token from server error: ", re.Msg)
|
||||
return nil, apierror.NewFailedApiError("get new openapi token error, try login again")
|
||||
}
|
||||
if wt != nil {
|
||||
@@ -170,7 +177,7 @@ doOpenLoginAct:
|
||||
Expired: wt.Expired,
|
||||
}
|
||||
|
||||
// 检查token有效期
|
||||
// TODO: 检查token有效期
|
||||
}
|
||||
|
||||
// 回到token检测流程,必须确保token是有效
|
||||
@@ -250,6 +257,7 @@ doWebLoginAct:
|
||||
WorkdirFileEntity: *aliyunpan.NewFileEntityForRootDir(),
|
||||
}
|
||||
u.PanClient().OpenapiPanClient().SetAccessTokenRefreshCallback(func(userId string, newToken openapi.ApiToken) error {
|
||||
// api层自动刷新Token,回调通知程序
|
||||
logger.Verboseln("openapi token refresh, update for user")
|
||||
u.OpenapiToken = &PanClientToken{
|
||||
AccessToken: newToken.AccessToken,
|
||||
|
||||
@@ -84,16 +84,14 @@ func init() {
|
||||
|
||||
func checkLoginExpiredAndRelogin() {
|
||||
command.ReloadConfigFunc(nil)
|
||||
activeUser := config.Config.ActiveUser()
|
||||
if activeUser == nil || activeUser.UserId == "" {
|
||||
// maybe expired, try to login
|
||||
command.TryLogin()
|
||||
} else {
|
||||
// 尝试登录
|
||||
activeUser := command.TryLogin()
|
||||
if activeUser != nil && activeUser.UserId != "" {
|
||||
// 刷新过期Token并保存到配置文件
|
||||
command.RefreshWebTokenInNeed(activeUser, config.Config.DeviceName)
|
||||
command.RefreshWebTokenInNeed(activeUser)
|
||||
command.RefreshOpenTokenInNeed(activeUser)
|
||||
}
|
||||
command.SaveConfigFunc(nil)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
@@ -103,20 +101,7 @@ func main() {
|
||||
checkLoginExpiredAndRelogin()
|
||||
|
||||
// check token expired task
|
||||
go func() {
|
||||
for {
|
||||
// Token刷新进程,不管是CLI命令行模式,还是直接命令模式,本刷新任务都会执行
|
||||
time.Sleep(time.Duration(1) * time.Minute)
|
||||
//time.Sleep(time.Duration(5) * time.Second)
|
||||
for _, u := range config.Config.UserList {
|
||||
if u.UserId == config.Config.ActiveUID {
|
||||
if u.PanClient() != nil {
|
||||
checkLoginExpiredAndRelogin()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
command.AutomaticallyRefreshTokenTask() // Token刷新进程,不管是CLI命令行模式,还是直接命令模式,本刷新任务都会执行
|
||||
|
||||
app := cli.NewApp()
|
||||
cmder.SetApp(app)
|
||||
@@ -253,8 +238,10 @@ func main() {
|
||||
fmt.Printf("提示: Ctrl + A / E 跳转命令 首 / 尾.\n")
|
||||
fmt.Printf("提示: 输入 help 获取帮助.\n")
|
||||
|
||||
// check update
|
||||
// 刷新配置文件
|
||||
command.ReloadConfigFunc(c)
|
||||
|
||||
// check update
|
||||
if config.Config.UpdateCheckInfo.LatestVer != "" {
|
||||
if utils.ParseVersionNum(config.Config.UpdateCheckInfo.LatestVer) > utils.ParseVersionNum(global.AppVersion) {
|
||||
fmt.Printf("\n当前的软件版本为:%s, 现在有新版本 %s 可供更新,强烈推荐进行更新!(可以输入 update 命令进行更新)\n\n",
|
||||
@@ -262,6 +249,7 @@ func main() {
|
||||
}
|
||||
}
|
||||
go func() {
|
||||
// 新版本检测异步任务
|
||||
latestCheckTime := config.Config.UpdateCheckInfo.CheckTime
|
||||
nowTime := time.Now().Unix()
|
||||
secsOf12Hour := int64(43200)
|
||||
@@ -279,14 +267,22 @@ func main() {
|
||||
}
|
||||
}()
|
||||
|
||||
for {
|
||||
for { // 命令行交互进程,每一次命令执行都会进入到这个for循环
|
||||
var (
|
||||
prompt string
|
||||
activeUser = config.Config.ActiveUser()
|
||||
)
|
||||
|
||||
if activeUser == nil {
|
||||
// 尝试重新登录并获取新的token
|
||||
activeUser = command.TryLogin()
|
||||
|
||||
// 自动登录成功
|
||||
if activeUser != nil {
|
||||
logger.Verboseln("自动重新登录成功")
|
||||
// 保存新token到配置文件
|
||||
command.SaveConfigFunc(c)
|
||||
}
|
||||
}
|
||||
|
||||
if activeUser != nil && activeUser.Nickname != "" {
|
||||
@@ -309,14 +305,14 @@ func main() {
|
||||
prompt = app.Name + " > "
|
||||
}
|
||||
|
||||
commandLine, err := line.State.Prompt(prompt)
|
||||
switch err {
|
||||
commandLine, err1 := line.State.Prompt(prompt)
|
||||
switch err1 {
|
||||
case liner.ErrPromptAborted:
|
||||
return
|
||||
case nil:
|
||||
// continue
|
||||
default:
|
||||
fmt.Println(err)
|
||||
fmt.Println(err1)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -332,9 +328,9 @@ func main() {
|
||||
|
||||
// 恢复原始终端状态
|
||||
// 防止运行命令时程序被结束, 终端出现异常
|
||||
line.Pause()
|
||||
//line.Pause()
|
||||
c.App.Run(s)
|
||||
line.Resume()
|
||||
//line.Resume()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user