From 3e16a47fd14d0a921b9248b722afc204c8e4669f Mon Sep 17 00:00:00 2001 From: johlanse Date: Fri, 5 Aug 2022 18:08:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=A3=80=E6=9F=A5=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E5=92=8C=E9=87=8D=E5=90=AF=E7=A8=8B=E5=BA=8F=E7=9A=84?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/config.go | 10 +++++++ main.go | 1 + utils/update/update.go | 13 ++++----- utils/util.go | 10 +++++++ web/router.go | 4 +++ web/wx.go | 61 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 93 insertions(+), 6 deletions(-) diff --git a/conf/config.go b/conf/config.go index 4be3232..32d9c9a 100644 --- a/conf/config.go +++ b/conf/config.go @@ -75,6 +75,8 @@ type Config struct { Secret string `json:"secret" yaml:"secret"` AppKey string `json:"app_key" yaml:"app_key"` } `json:"ji_guang_push" yaml:"ji_guang_push"` + + version string } var ( @@ -86,6 +88,14 @@ var ( //go:embed config_default.yml var defaultConfig []byte +func SetVersion(string2 string) { + config.version = string2 +} + +func GetVersion() string { + return config.version +} + // GetConfig /** * @Description: diff --git a/main.go b/main.go index 2bc0d93..b98bd41 100644 --- a/main.go +++ b/main.go @@ -78,6 +78,7 @@ func init() { } func main() { + conf.SetVersion(VERSION) log.Infoln("当前程序运行版本: " + VERSION) if i { core := &lib.Core{} diff --git a/utils/update/update.go b/utils/update/update.go index 2d25f95..5b45c5d 100644 --- a/utils/update/update.go +++ b/utils/update/update.go @@ -20,32 +20,33 @@ import ( ) // CheckUpdate 检查更新 -func CheckUpdate(version string) { +func CheckUpdate(version string) string { log.Infof("正在检查更新.") if version == "(devel)" { log.Warnf("检查更新失败: 使用的 Actions 测试版或自编译版本.") - return + return "" } if version == "unknown" { log.Warnf("检查更新失败: 使用的未知版本.") - return + return "" } if !strings.HasPrefix(version, "v") { log.Warnf("版本格式错误") - return + return "" } latest, err := lastVersion() if err != nil { log.Warnf("检查更新失败: %v", err) - return + return "" } if versionCompare(version, latest) { log.Infof("当前有更新的 study_xxqg 可供更新, 请前往 https://github.com/johlanse/study_xxqg/releases 下载.") log.Infof("当前版本: %v 最新版本: %v", version, latest) - return + return "检测到可用更新,版本号:" + latest } log.Infof("检查更新完成. 当前已运行最新版本.") + return "" } func readLine() (str string) { diff --git a/utils/util.go b/utils/util.go index a3a0388..72854cf 100644 --- a/utils/util.go +++ b/utils/util.go @@ -3,6 +3,9 @@ package utils import ( "os" "os/exec" + + "github.com/johlanse/study_xxqg/conf" + "github.com/johlanse/study_xxqg/utils/update" ) // Restart @@ -16,3 +19,10 @@ func Restart() { }() } + +func GetAbout() string { + msg := "study_xxqg\n程序版本:" + msg += conf.GetVersion() + msg += "\n" + update.CheckUpdate(conf.GetVersion()) + return msg +} diff --git a/web/router.go b/web/router.go index 2c3d3f0..27c8bb9 100644 --- a/web/router.go +++ b/web/router.go @@ -36,6 +36,10 @@ func RouterInit() *gin.Engine { ctx.Redirect(301, "/static/xxqg/build/home.html") }) + router.GET("/about", func(context *gin.Context) { + context.JSON(200, utils.GetAbout()) + }) + router.GET("/new/*file", func(ctx *gin.Context) { if strings.HasSuffix(ctx.Request.URL.Path, "js") { ctx.Header("Content-Type", "application/javascript; charset=utf-8") diff --git a/web/wx.go b/web/wx.go index ae3b0eb..c11c712 100644 --- a/web/wx.go +++ b/web/wx.go @@ -15,6 +15,8 @@ import ( "github.com/johlanse/study_xxqg/conf" "github.com/johlanse/study_xxqg/lib" "github.com/johlanse/study_xxqg/model" + "github.com/johlanse/study_xxqg/utils" + "github.com/johlanse/study_xxqg/utils/update" ) func init() { @@ -32,6 +34,10 @@ const ( StartStudy = "start_study" getUser = "stop_study" SCORE = "score" + + checkUpdate = "check_update" + updateBtn = "updateBtn" + restart = "restart" ) func initWechat() { @@ -83,6 +89,39 @@ func initWechat() { }, }, }, + { + Name: "关于", + Type: "click", + Key: "", + Url: "", + MediaId: "", + SubButtons: []mp.MenuButton{ + { + Name: "检查更新", + Type: "click", + Key: checkUpdate, + Url: "", + MediaId: "", + SubButtons: nil, + }, + { + Name: "重启程序", + Type: "click", + Key: restart, + Url: "", + MediaId: "", + SubButtons: nil, + }, + { + Name: "更新程序", + Type: "click", + Key: updateBtn, + Url: "", + MediaId: "", + SubButtons: nil, + }, + }, + }, }}) if err != nil { log.Errorln("设置自定义菜单出现异常" + err.Error()) @@ -105,10 +144,32 @@ func initWechat() { go handleGetUser(r.FromUserName) case SCORE: go handleScore(r.FromUserName) + case checkUpdate: + handleCheckUpdate(r.FromUserName) + case updateBtn: + handleUpdate(r.FromUserName) + case restart: + handleRestart(r.FromUserName) } }) } +func handleCheckUpdate(id string) { + about := utils.GetAbout() + sendMsg(id, about) +} + +func handleUpdate(id string) { + update.SelfUpdate("", conf.GetVersion()) + sendMsg(id, "检查更新已完成,即将重启程序") + utils.Restart() +} + +func handleRestart(id string) { + sendMsg(id, "即将重启程序") + utils.Restart() +} + func sendMsg(id, message string) { m := map[string]interface{}{ "data": map[string]string{