diff --git a/conf/config.go b/conf/config.go index 1ae566c..0ace96d 100644 --- a/conf/config.go +++ b/conf/config.go @@ -1,6 +1,7 @@ package conf import ( + "bytes" _ "embed" "os" "strings" @@ -9,6 +10,7 @@ import ( "github.com/mitchellh/mapstructure" log "github.com/sirupsen/logrus" "github.com/spf13/viper" + "gopkg.in/yaml.v2" ) // Config @@ -38,16 +40,14 @@ type Config struct { CustomApi string `json:"custom_api" yaml:"custom_api" mapstructure:"custom_api"` WhiteList []int64 `json:"white_list" yaml:"white_list" mapstructure:"white_list"` } `json:"tg" yaml:"tg" mapstructure:"tg"` - QQ struct { - } `mapstructure:"qq"` Web struct { Enable bool `json:"enable" yaml:"enable" mapstructure:"enable"` Account string `json:"account" yaml:"account" mapstructure:"account"` Password string `json:"password" yaml:"password" mapstructure:"password"` Host string `json:"host" yaml:"host" mapstructure:"host"` Port int `json:"port" yaml:"port" mapstructure:"port"` - CommonUser map[string]string `json:"common_user" mapstructure:"common_user"` - } `json:"web" mapstructure:"web"` + CommonUser map[string]string `json:"common_user" yaml:"common_user" mapstructure:"common_user"` + } `json:"web" yaml:"web" mapstructure:"web"` Cron string `json:"cron" yaml:"cron" mapstructure:"cron"` CronRandomWait int `json:"cron_random_wait" yaml:"cron_random_wait" mapstructure:"cron_random_wait"` EdgePath string `json:"edge_path" yaml:"edge_path" mapstructure:"edge_path"` @@ -127,6 +127,25 @@ func GetVersion() string { return config.version } +func SetConfig(config2 Config) error { + data, err := yaml.Marshal(&config2) + if err != nil { + log.Errorln("不能正确解析配置文件" + err.Error()) + return err + } + err = viper.ReadConfig(bytes.NewReader(data)) + if err != nil { + log.Errorln("viper不能正确解析配置文件" + err.Error()) + return err + } + err = viper.WriteConfig() + if err != nil { + log.Errorln("保存到文件失败" + err.Error()) + return err + } + return err +} + // InitConfig /* @Description: 初始化配置文件 * @param path diff --git a/conf/config_default.yml b/conf/config_default.yml index cede435..32f1fdc 100644 --- a/conf/config_default.yml +++ b/conf/config_default.yml @@ -127,7 +127,7 @@ github_proxy: https://github.com hot_reload: true # 自定义消息推送,会在定时任务执行之前推送信息 -custom_message: "" +custom_message: "自定义消息$$$https://www.baidu.com" # 自定义消息推送定时 custom_cron: "" diff --git a/main.go b/main.go index d0efd32..06060bf 100644 --- a/main.go +++ b/main.go @@ -247,12 +247,6 @@ func do(m string) { } score, _ = lib.GetUserScore(u.ToCookies()) - content := lib.FormatScore(score) - err = push.PushMessage(u.Nick+"学习情况", u.Nick+"学习情况"+content, "score", u.PushId) - if err != nil { - log.Errorln(err.Error()) - err = nil - } message := fmt.Sprintf("%v 学习完成,用时%.1f分钟
%v", u.Nick, endTime.Sub(startTime).Minutes(), lib.FormatScoreShort(score)) core2.Push(u.PushId, "flush", message) } diff --git a/push/jpush.go b/push/jpush.go deleted file mode 100644 index a89b2fd..0000000 --- a/push/jpush.go +++ /dev/null @@ -1,37 +0,0 @@ -package push - -import ( - "encoding/base64" - "errors" - - "github.com/imroc/req/v3" - - "github.com/johlanse/study_xxqg/conf" -) - -func PushMessage(title, content, message, pushID string) error { - if !conf.GetConfig().JiGuangPush.Enable { - return nil - } - - c := req.C() - response, err := c.R().SetBodyJsonMarshal(map[string]interface{}{ - "platform": "all", - "audience": map[string][]string{ - "registration_id": {pushID}, - }, - "notification": map[string]interface{}{ - "alert": content, - }, - "message": map[string]string{ - "msg_content": message, - }, - }).SetHeader("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(conf.GetConfig().JiGuangPush.AppKey+":"+conf.GetConfig().JiGuangPush.Secret))).Post("https://api.jpush.cn/v3/push") - if err != nil { - return err - } - if response.IsSuccess() { - return nil - } - return errors.New("消息推送失败" + response.Response.Status) -} diff --git a/push/push.go b/push/push.go index b44934b..a8392ac 100644 --- a/push/push.go +++ b/push/push.go @@ -49,11 +49,6 @@ func GetPush(config conf.Config) func(id string, kind string, message string) { log.Infoln("已配置pushDeer推送") pushs = append(pushs, InitPushDeer()) } - if config.JiGuangPush.Enable { - pushs = append(pushs, func(id, kind, message string) { - _ = PushMessage("", message, message, id) - }) - } pushs = append(pushs, func(id, kind, message string) { log.Debugln(fmt.Sprintf("消息id: %v,消息类型:%v,消息内容:%v", id, kind, message)) }) diff --git a/utils/update/update.go b/utils/update/update.go index b56a33a..97b62ce 100644 --- a/utils/update/update.go +++ b/utils/update/update.go @@ -163,6 +163,11 @@ func SelfUpdate(github string, version string) { github = "https://github.com" } + if version == "unknown" { + log.Warningln("测试版本,不更新!") + return + } + log.Infof("正在检查更新.") latest, err := lastVersion() if err != nil { diff --git a/web/configController.go b/web/configController.go new file mode 100644 index 0000000..3c28fdf --- /dev/null +++ b/web/configController.go @@ -0,0 +1,77 @@ +package web + +import ( + "github.com/gin-gonic/gin" + + "github.com/johlanse/study_xxqg/conf" +) + +func configGet() gin.HandlerFunc { + return func(ctx *gin.Context) { + level := ctx.GetInt("level") + if level != 1 { + ctx.JSON(200, Resp{ + Code: 403, + Message: "", + Data: nil, + Success: false, + Error: "", + }) + return + } + ctx.JSON(200, Resp{ + Code: 200, + Message: "获取成功", + Data: conf.GetConfig(), + Success: true, + Error: "", + }) + + } +} + +func configSet() gin.HandlerFunc { + return func(ctx *gin.Context) { + level := ctx.GetInt("level") + if level != 1 { + ctx.JSON(200, Resp{ + Code: 403, + Message: "", + Data: nil, + Success: false, + Error: "", + }) + return + } + c := new(conf.Config) + err := ctx.BindJSON(c) + if err != nil { + ctx.JSON(200, Resp{ + Code: 401, + Message: "", + Data: nil, + Success: false, + Error: err.Error(), + }) + return + } + err = conf.SetConfig(*c) + if err != nil { + ctx.JSON(200, Resp{ + Code: 503, + Message: "", + Data: nil, + Success: false, + Error: err.Error(), + }) + return + } + ctx.JSON(200, Resp{ + Code: 200, + Message: "", + Data: nil, + Success: true, + Error: "", + }) + } +} diff --git a/web/controller.go b/web/controller.go index 5b76b3c..19981fa 100644 --- a/web/controller.go +++ b/web/controller.go @@ -53,7 +53,7 @@ func checkToken() gin.HandlerFunc { ctx.JSON(200, Resp{ Code: 403, Message: "", - Data: nil, + Data: -1, Success: false, Error: "", }) @@ -140,9 +140,7 @@ func addUser() gin.HandlerFunc { }) return } - registerID, _ := ctx.GetQuery("register_id") - log.Infoln("the jpush register id is " + registerID) - _, err = lib.GetToken(p.Code, p.State, registerID) + _, err = lib.GetToken(p.Code, p.State, ctx.GetString("token")) if err != nil { ctx.JSON(403, Resp{ Code: 403, @@ -277,12 +275,6 @@ func study() gin.HandlerFunc { core.RespondDaily(user, "weekly") core.RespondDaily(user, "special") } - score, _ := lib.GetUserScore(user.ToCookies()) - content := lib.FormatScore(score) - err := push.PushMessage(user.Nick+"学习情况", content, "score", user.PushId) - if err != nil { - log.Errorln(err.Error()) - } state.Delete(uid) }() ctx.JSON(200, Resp{ diff --git a/web/router.go b/web/router.go index 32b53f4..38f5ae6 100644 --- a/web/router.go +++ b/web/router.go @@ -11,6 +11,7 @@ import ( "github.com/johlanse/study_xxqg/conf" "github.com/johlanse/study_xxqg/utils" + "github.com/johlanse/study_xxqg/utils/update" ) // 将静态文件嵌入到可执行程序中来 @@ -42,6 +43,49 @@ func RouterInit() *gin.Engine { }) }) + router.POST("/restart", check(), func(ctx *gin.Context) { + if ctx.GetInt("level") == 1 { + ctx.JSON(200, Resp{ + Code: 200, + Message: "", + Data: nil, + Success: true, + Error: "", + }) + utils.Restart() + } else { + ctx.JSON(200, Resp{ + Code: 401, + Message: "", + Data: nil, + Success: false, + Error: "", + }) + } + }) + + router.POST("/update", check(), func(ctx *gin.Context) { + if ctx.GetInt("level") == 1 { + update.SelfUpdate("", conf.GetVersion()) + ctx.JSON(200, Resp{ + Code: 200, + Message: "", + Data: nil, + Success: true, + Error: "", + }) + utils.Restart() + } else { + ctx.JSON(200, Resp{ + Code: 401, + Message: "", + Data: nil, + Success: false, + Error: "", + }) + } + }) + if utils.FileIsExist("./config/flutter_xxqg/") { router.StaticFS("/flutter_xxqg", http.Dir("./config/flutter_xxqg/")) } @@ -57,6 +101,11 @@ func RouterInit() *gin.Engine { router.StaticFS("/dist", http.Dir("./config/dist/")) } + config := router.Group("/config", check()) + + config.GET("", configGet()) + config.POST("", configSet()) + // 对用户管理的组 user := router.Group("/user", check()) // 添加用户