添加推送登录超时时等待一定时间后重新推送

This commit is contained in:
johlanse 2022-05-03 19:35:14 +08:00
parent 326585bcdb
commit 71f049daf6
6 changed files with 41 additions and 20 deletions

View File

@ -46,6 +46,14 @@ type Config struct {
EdgePath string `json:"edge_path" yaml:"edge_path"` EdgePath string `json:"edge_path" yaml:"edge_path"`
QrCOde bool `json:"qr_code" yaml:"qr_code"` QrCOde bool `json:"qr_code" yaml:"qr_code"`
StartWait int `json:"start_wait" yaml:"start_wait"` StartWait int `json:"start_wait" yaml:"start_wait"`
// cookie强制过期时间单位为h
ForceExpiration int `json:"force_expiration" yaml:"force_expiration"`
Retry struct {
// 重试次数
Times int `json:"times" yaml:"times"`
// 重试时间
Intervals int `json:"intervals" yaml:"intervals"`
} `json:"retry" yaml:"retry"`
} }
var ( var (

View File

@ -51,6 +51,15 @@ web:
# 网页端登录密码 # 网页端登录密码
password: admin password: admin
# 登录重试配置
retry:
# 重试次数
times: 0
# 重试之间的时间间隔,单位为分钟
intervals: 5
# 设置是否定时执行学习程序格式为cron格式 # 设置是否定时执行学习程序格式为cron格式
# "9 19 * * *" 每天19点9分执行一次 # "9 19 * * *" 每天19点9分执行一次
# "* 10 * * *” 每天早上十点执行一次 # "* 10 * * *” 每天早上十点执行一次

View File

@ -84,7 +84,7 @@ type checkQrCodeResp struct {
*/ */
func (c *Core) Init() { func (c *Core) Init() {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
c.initWondows() c.initWindows()
} else { } else {
c.initNotWindows() c.initNotWindows()
} }
@ -117,7 +117,7 @@ func GetToken(code, sign string) (bool, error) {
Nick: nick, Nick: nick,
UID: uid, UID: uid,
Token: token, Token: token,
LoginTime: time.Now().UnixNano(), LoginTime: time.Now().Unix(),
} }
err = model.AddUser(user) err = model.AddUser(user)
if err != nil { if err != nil {
@ -134,7 +134,7 @@ func GetToken(code, sign string) (bool, error) {
* @return *model.User * @return *model.User
* @return error * @return error
*/ */
func (c *Core) L() (*model.User, error) { func (c *Core) L(retryTimes int) (*model.User, error) {
client := req.C() client := req.C()
client.OnAfterResponse(func(client *req.Client, response *req.Response) error { client.OnAfterResponse(func(client *req.Client, response *req.Response) error {
return nil return nil
@ -183,9 +183,8 @@ func (c *Core) L() (*model.User, error) {
} }
if res.Success { if res.Success {
return true, res.Data return true, res.Data
} else {
return false, ""
} }
return false, ""
} }
for i := 0; i < 150; i++ { for i := 0; i < 150; i++ {
code, data := checkQrCode() code, data := checkQrCode()
@ -207,29 +206,28 @@ func (c *Core) L() (*model.User, error) {
Nick: nick, Nick: nick,
UID: uid, UID: uid,
Token: response.Cookies()[0].Value, Token: response.Cookies()[0].Value,
LoginTime: time.Now().UnixNano(), LoginTime: time.Now().Unix(),
} }
err = model.AddUser(user) err = model.AddUser(user)
if err != nil { if err != nil {
return nil, err return nil, err
} }
c.Push("text", "登录成功,用户名:"+nick) c.Push("text", "登录成功,用户名:"+nick)
// model.AddUser(&model.User{
// Nick: nick,
// UID: info,
// Token: resp.Cookies()[],
// LoginTime: 0,
// })
// if err != nil {
// return cos, err
//}
return user, err return user, err
} }
} }
return nil, errors.New("time out") if retryTimes == 0 {
return nil, errors.New("time out")
} else {
// 等待几分钟后重新执行
time.Sleep(time.Duration(GetConfig().Retry.Intervals) * time.Minute)
c.Push("text", fmt.Sprintf("登录超时,将进行第%d重新次登录", retryTimes))
return c.L(retryTimes - 1)
}
} }
func (c *Core) initWondows() { func (c *Core) initWindows() {
dir, err := os.Getwd() dir, err := os.Getwd()
if err != nil { if err != nil {
return return

View File

@ -178,7 +178,7 @@ func login(bot *Telegram, args []string) {
} }
core.Init() core.Init()
defer core.Quit() defer core.Quit()
_, err := core.L() _, err := core.L(config.Retry.Times)
if err != nil { if err != nil {
bot.SendMsg(err.Error()) bot.SendMsg(err.Error())
return return

View File

@ -159,7 +159,7 @@ func do() {
switch { switch {
case len(users) < 1: case len(users) < 1:
log.Infoln("未检测到有效用户信息,将采用登录模式") log.Infoln("未检测到有效用户信息,将采用登录模式")
u, err := core.L() u, err := core.L(config.Retry.Times)
if err != nil { if err != nil {
log.Errorln(err.Error()) log.Errorln(err.Error())
return return

View File

@ -55,7 +55,13 @@ func Query() ([]*User, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
// login := time.Unix(u.LoginTime, 0)
// sub := time.Now().Sub(login)
if CheckUserCookie(u) { if CheckUserCookie(u) {
// if lib.GetConfig().ForceExpiration != 0 && sub.Hours() > float64(lib.GetConfig().ForceExpiration) {
// log.Infoln("用户" + u.Nick + "cookie已强制失效")
// continue
// }
users = append(users, u) users = append(users, u)
} else { } else {
log.Infoln("用户" + u.Nick + "cookie已失效") log.Infoln("用户" + u.Nick + "cookie已失效")
@ -113,7 +119,7 @@ func AddUser(user *User) error {
*/ */
func UpdateUser(user *User) error { func UpdateUser(user *User) error {
ping() ping()
_, err := db.Exec("update user set token=?,login_time=? where uid = ?", user.Token, time.Now().Unix(), user.UID) _, err := db.Exec("update user set token=? where uid = ?", user.Token, user.UID)
if err != nil { if err != nil {
log.Errorln("更新数据失败") log.Errorln("更新数据失败")
log.Errorln(err.Error()) log.Errorln(err.Error())