Skip to content

Commit

Permalink
fix: fix login unreload config bug
Browse files Browse the repository at this point in the history
  • Loading branch information
wearzdk committed Nov 17, 2022
1 parent 3d1a9ea commit 15f5322
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 40 deletions.
1 change: 1 addition & 0 deletions app/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ func Login() {
}
SaveUserConfig(userInfoConf)
log.Info("用户信息已保存至userInfo.json")
ReloadUserConfig()
}

// CheckLoginExpire 检查登录是否过期
Expand Down
15 changes: 12 additions & 3 deletions app/reservation.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,20 @@ func WaitForReservation() {
go func(areaId int, seatId int) {
reservationResultChan <- Reserve(areaId, seatId)
}(reservation.AreaId, seatId)
// 随机等待2-7秒
time.Sleep(time.Duration(rand.Intn(5)+2) * time.Second)
// 随机等待1-2秒
time.Sleep(time.Duration(rand.Intn(1)+1) * time.Second)
if isSuccess {
break
}
}
if isSuccess {
break
}
// 如果已经预约30分钟,则停止预约
if time.Now().Sub(ReserveTime).Minutes() > 30 {
log.Info("已经预约30分钟,停止预约")
break
}
}
log.Info("今日预约已完成")
}
Expand Down Expand Up @@ -247,7 +252,11 @@ func Reserve(areaId, seatId int) bool {
config.SaveConfig()
return true
} else {
color.Cyan(reserveResponse.Msg)
log.Warning(reserveResponse.Msg)
if reserveResponse.Msg == "没有登录或登录已超时" {
// 重新登录
Login()
}
return false
}
}
Expand Down
4 changes: 2 additions & 2 deletions task/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func DailyTask() {
// 预约时间更新
app.ReserveTime = time.Now().AddDate(0, 0, 1)
// 登录
app.Login()
go app.Login()

}

Expand All @@ -48,7 +48,7 @@ func LoginCheck() {
isExpire := app.CheckLoginExpire()
if isExpire {
log.Warning("登录已过期 尝试重新登陆...")
app.Login()
go app.Login()
}
}

Expand Down
53 changes: 18 additions & 35 deletions utils/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,70 +9,53 @@ import (

// 日志

type log struct {
Info string
Type string
}

var logs []log
var logFile *os.File

func init() {
logs = []log{}
WriteLog()
// 每隔一分钟写入一次日志
go func() {
for {
WriteLog()
time.Sleep(time.Minute)
}
}()

// 打开文件
var err error
logFile, err = os.OpenFile("log.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Println(err)
return
}
}

// Info 日志

func Info(format string, a ...interface{}) {
out := "INFO: " + fmt.Sprintf(format, a...)
logs = append(logs, log{Info: out, Type: "info"})
color.Green(out)
WriteLog(out, "INFO: ")

}

// Error 日志
func Error(err error, info ...string) {
out := "ERROR: " + fmt.Sprint(info) + " " + err.Error()
logs = append(logs, log{Info: out, Type: "error"})
color.Red(out)
WriteLog(out, "ERROR: ")
}

// Warning 日志
func Warning(info string) {
out := "WARNING: " + info
logs = append(logs, log{Info: out, Type: "warning"})
color.Yellow(out)
WriteLog(out, "WARNING: ")
}

func Debug(info ...interface{}) {
out := "DEBUG: " + fmt.Sprint(info)
logs = append(logs, log{Info: out, Type: "debug"})
color.Blue(out)
WriteLog(out, "DEBUG: ")
}

// WriteLog 定期写入日志文件
func WriteLog() {
logfile, err := os.OpenFile("log.txt", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
fmt.Println("打开文件失败")
return
}
for _, logItem := range logs {
_, err = logfile.WriteString(logItem.Info + "\n")
if err != nil {
return
}
}
err = logfile.Close()
// WriteLog 写入日志文件
func WriteLog(log, logType string) {
// 写入文件
_, err := logFile.WriteString(time.Now().Format("2006-01-02 15:04:05") + log + "\n")
if err != nil {
fmt.Println(err)
return
}
logs = []log{}
}

0 comments on commit 15f5322

Please sign in to comment.