增加检查更新和重启程序的功能

This commit is contained in:
johlanse 2022-08-05 18:08:05 +08:00
parent c18594ad46
commit 3e16a47fd1
6 changed files with 93 additions and 6 deletions

View File

@ -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:

View File

@ -78,6 +78,7 @@ func init() {
}
func main() {
conf.SetVersion(VERSION)
log.Infoln("当前程序运行版本: " + VERSION)
if i {
core := &lib.Core{}

View File

@ -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) {

View File

@ -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
}

View File

@ -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")

View File

@ -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{