新增前端配置管理

This commit is contained in:
johlanse 2022-09-06 22:56:18 +08:00
parent eca573fe05
commit 60d86cdb06
9 changed files with 157 additions and 63 deletions

View File

@ -1,6 +1,7 @@
package conf package conf
import ( import (
"bytes"
_ "embed" _ "embed"
"os" "os"
"strings" "strings"
@ -9,6 +10,7 @@ import (
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/viper" "github.com/spf13/viper"
"gopkg.in/yaml.v2"
) )
// Config // Config
@ -38,16 +40,14 @@ type Config struct {
CustomApi string `json:"custom_api" yaml:"custom_api" mapstructure:"custom_api"` CustomApi string `json:"custom_api" yaml:"custom_api" mapstructure:"custom_api"`
WhiteList []int64 `json:"white_list" yaml:"white_list" mapstructure:"white_list"` WhiteList []int64 `json:"white_list" yaml:"white_list" mapstructure:"white_list"`
} `json:"tg" yaml:"tg" mapstructure:"tg"` } `json:"tg" yaml:"tg" mapstructure:"tg"`
QQ struct {
} `mapstructure:"qq"`
Web struct { Web struct {
Enable bool `json:"enable" yaml:"enable" mapstructure:"enable"` Enable bool `json:"enable" yaml:"enable" mapstructure:"enable"`
Account string `json:"account" yaml:"account" mapstructure:"account"` Account string `json:"account" yaml:"account" mapstructure:"account"`
Password string `json:"password" yaml:"password" mapstructure:"password"` Password string `json:"password" yaml:"password" mapstructure:"password"`
Host string `json:"host" yaml:"host" mapstructure:"host"` Host string `json:"host" yaml:"host" mapstructure:"host"`
Port int `json:"port" yaml:"port" mapstructure:"port"` Port int `json:"port" yaml:"port" mapstructure:"port"`
CommonUser map[string]string `json:"common_user" mapstructure:"common_user"` CommonUser map[string]string `json:"common_user" yaml:"common_user" mapstructure:"common_user"`
} `json:"web" mapstructure:"web"` } `json:"web" yaml:"web" mapstructure:"web"`
Cron string `json:"cron" yaml:"cron" mapstructure:"cron"` Cron string `json:"cron" yaml:"cron" mapstructure:"cron"`
CronRandomWait int `json:"cron_random_wait" yaml:"cron_random_wait" mapstructure:"cron_random_wait"` 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"` EdgePath string `json:"edge_path" yaml:"edge_path" mapstructure:"edge_path"`
@ -127,6 +127,25 @@ func GetVersion() string {
return config.version 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 // InitConfig
/* @Description: 初始化配置文件 /* @Description: 初始化配置文件
* @param path * @param path

View File

@ -127,7 +127,7 @@ github_proxy: https://github.com
hot_reload: true hot_reload: true
# 自定义消息推送,会在定时任务执行之前推送信息 # 自定义消息推送,会在定时任务执行之前推送信息
custom_message: "" custom_message: "自定义消息$$$https://www.baidu.com"
# 自定义消息推送定时 # 自定义消息推送定时
custom_cron: "" custom_cron: ""

View File

@ -247,12 +247,6 @@ func do(m string) {
} }
score, _ = lib.GetUserScore(u.ToCookies()) 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分钟</br>%v", u.Nick, endTime.Sub(startTime).Minutes(), lib.FormatScoreShort(score)) message := fmt.Sprintf("%v 学习完成,用时%.1f分钟</br>%v", u.Nick, endTime.Sub(startTime).Minutes(), lib.FormatScoreShort(score))
core2.Push(u.PushId, "flush", message) core2.Push(u.PushId, "flush", message)
} }

View File

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

View File

@ -49,11 +49,6 @@ func GetPush(config conf.Config) func(id string, kind string, message string) {
log.Infoln("已配置pushDeer推送") log.Infoln("已配置pushDeer推送")
pushs = append(pushs, InitPushDeer()) 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) { pushs = append(pushs, func(id, kind, message string) {
log.Debugln(fmt.Sprintf("消息id: %v消息类型%v,消息内容:%v", id, kind, message)) log.Debugln(fmt.Sprintf("消息id: %v消息类型%v,消息内容:%v", id, kind, message))
}) })

View File

@ -163,6 +163,11 @@ func SelfUpdate(github string, version string) {
github = "https://github.com" github = "https://github.com"
} }
if version == "unknown" {
log.Warningln("测试版本,不更新!")
return
}
log.Infof("正在检查更新.") log.Infof("正在检查更新.")
latest, err := lastVersion() latest, err := lastVersion()
if err != nil { if err != nil {

77
web/configController.go Normal file
View File

@ -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: "",
})
}
}

View File

@ -53,7 +53,7 @@ func checkToken() gin.HandlerFunc {
ctx.JSON(200, Resp{ ctx.JSON(200, Resp{
Code: 403, Code: 403,
Message: "", Message: "",
Data: nil, Data: -1,
Success: false, Success: false,
Error: "", Error: "",
}) })
@ -140,9 +140,7 @@ func addUser() gin.HandlerFunc {
}) })
return return
} }
registerID, _ := ctx.GetQuery("register_id") _, err = lib.GetToken(p.Code, p.State, ctx.GetString("token"))
log.Infoln("the jpush register id is " + registerID)
_, err = lib.GetToken(p.Code, p.State, registerID)
if err != nil { if err != nil {
ctx.JSON(403, Resp{ ctx.JSON(403, Resp{
Code: 403, Code: 403,
@ -277,12 +275,6 @@ func study() gin.HandlerFunc {
core.RespondDaily(user, "weekly") core.RespondDaily(user, "weekly")
core.RespondDaily(user, "special") 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) state.Delete(uid)
}() }()
ctx.JSON(200, Resp{ ctx.JSON(200, Resp{

View File

@ -11,6 +11,7 @@ import (
"github.com/johlanse/study_xxqg/conf" "github.com/johlanse/study_xxqg/conf"
"github.com/johlanse/study_xxqg/utils" "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/") { if utils.FileIsExist("./config/flutter_xxqg/") {
router.StaticFS("/flutter_xxqg", http.Dir("./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/")) router.StaticFS("/dist", http.Dir("./config/dist/"))
} }
config := router.Group("/config", check())
config.GET("", configGet())
config.POST("", configSet())
// 对用户管理的组 // 对用户管理的组
user := router.Group("/user", check()) user := router.Group("/user", check())
// 添加用户 // 添加用户