From 050ab4ed4158876c6bbd0541913bfb430e7cf86a Mon Sep 17 00:00:00 2001 From: johlanse Date: Fri, 12 Aug 2022 09:17:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=AF=E8=87=AA=E5=AE=9A=E4=B9=89=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=87=E4=BB=B6=E8=B7=AF=E5=BE=84=EF=BC=8C=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E6=8E=A8=E9=80=81=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/config.go | 37 ++++++++++++++++++++++++++++--------- lib/tg.go | 6 +++--- main.go | 17 ++++++++++------- 3 files changed, 41 insertions(+), 19 deletions(-) diff --git a/conf/config.go b/conf/config.go index e511798..28a310a 100644 --- a/conf/config.go +++ b/conf/config.go @@ -90,28 +90,39 @@ var ( //go:embed config_default.yml var defaultConfig []byte +// SetVersion +/* @Description: 设置应用程序版本号 + * @param string2 + */ func SetVersion(string2 string) { config.version = string2 } +// GetVersion +/* @Description: 获取应用程序版本号 + * @return string + */ func GetVersion() string { return config.version } -// GetConfig -/** - * @Description: - * @return Config +// InitConfig +/* @Description: 初始化配置文件 + * @param path */ -func GetConfig() Config { - file, err := os.ReadFile("./config/config.yml") +func InitConfig(path string) { + if path == "" { + path = "./config/config.yml" + } + log.Infoln("配置文件路径 ==》 " + path) + file, err := os.ReadFile(path) if err != nil { log.Warningln("检测到配置文件可能不存在") - err := os.WriteFile("./config/config.yml", defaultConfig, 0666) + err := os.WriteFile(path, defaultConfig, 0666) if err != nil { log.Errorln("写入到配置文件出现错误") log.Errorln(err.Error()) - return Config{} + return } log.Infoln("成功写入到配置文件,请重启应用") os.Exit(3) @@ -119,7 +130,7 @@ func GetConfig() Config { err = yaml.Unmarshal(file, &config) if err != nil { log.Errorln(err.Error()) - return Config{} + log.Panicln("配置文件解析失败,请检查配置文件") } if config.Scheme == "" { config.Scheme = "https://johlanse.github.io/study_xxqg/scheme.html?" @@ -130,5 +141,13 @@ func GetConfig() Config { if config.TG.CustomApi == "" { config.TG.CustomApi = "https://api.telegram.org" } +} + +// GetConfig +/** + * @Description: 获取配置信息 + * @return Config + */ +func GetConfig() Config { return config } diff --git a/lib/tg.go b/lib/tg.go index d1e623a..0e5796e 100644 --- a/lib/tg.go +++ b/lib/tg.go @@ -273,7 +273,7 @@ func studyAll(bot *Telegram, args []string) { select { case <-timer: { - bot.SendMsg("学习超时,请重新登录或检查日志") + bot.SendMsg("学习超时,请重新学习或检查日志") log.Errorln("学习超时,已自动退出") core.Quit() } @@ -282,7 +282,7 @@ func studyAll(bot *Telegram, args []string) { } } score, _ := GetUserScore(user.ToCookies()) - bot.SendMsg(fmt.Sprintf("当前学习总积分:%v,今日得分:%v", score.TotalScore, score.TodayScore)) + bot.SendMsg(fmt.Sprintf("%v已学习完成\n%v", user.Nick, PrintScore(score))) } s() } @@ -379,7 +379,7 @@ func study(bot *Telegram, args []string) { } } score, _ := GetUserScore(user.ToCookies()) - bot.SendMsg(fmt.Sprintf("当前学习总积分:%v,今日得分:%v", score.TotalScore, score.TodayScore)) + bot.SendMsg(fmt.Sprintf("%v已学习完成\n%v", user.Nick, PrintScore(score))) } func getScores(bot *Telegram, args []string) { diff --git a/main.go b/main.go index dc28118..d089639 100644 --- a/main.go +++ b/main.go @@ -27,17 +27,20 @@ import ( ) var ( - u bool - i bool + u bool + i bool + configPath = "" ) var VERSION = "unknown" func init() { - flag.BoolVar(&u, "u", false, "update the study_xxqg") + flag.BoolVar(&u, "u", false, "更新应用") flag.BoolVar(&i, "init", false, "init the app") + flag.StringVar(&configPath, "config", "./config/config.yml", "设置配置文件路径") flag.Parse() - + // 初始化配置文件 + conf.InitConfig(configPath) config = conf.GetConfig() logFormatter := &easy.Formatter{ TimestampFormat: "2006-01-02 15:04:05", @@ -233,7 +236,7 @@ func do(m string) { log.Debugln(err.Error()) return } - message := u.Nick + " 学习完成:今日得分:" + strconv.Itoa(score.TodayScore) + score, _ = lib.GetUserScore(user.ToCookies()) content := lib.FormatScore(score) err = push.PushMessage(user.Nick+"学习情况", user.Nick+"学习情况"+content, "score", user.PushId) @@ -241,8 +244,8 @@ func do(m string) { log.Errorln(err.Error()) err = nil } - core2.Push("markdown", message) - core2.Push("flush", "") + message := u.Nick + " 学习完成:今日得分:" + lib.PrintScore(score) + core2.Push("flush", message) } // 用户小于1时自动登录