From 429550d1e08e7798290e827199091ceb132b30ae Mon Sep 17 00:00:00 2001 From: qianqianzyk <3265569512@qq.com> Date: Tue, 30 Jul 2024 21:09:51 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=96=B0=E5=A2=9E=E6=96=B0=E7=94=9F?= =?UTF-8?q?=E6=8F=90=E9=86=92=E5=8A=9F=E8=83=BD=E5=B9=B6=E4=B8=94=E8=B0=83?= =?UTF-8?q?=E6=95=B4=E4=BF=AE=E6=94=B9=E7=B3=BB=E7=BB=9F=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E4=BD=BF=E6=94=AF=E6=8C=81=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E6=A0=A1=E5=9B=ADbus=E3=80=81=E5=9B=BE=E7=89=87=E3=80=81?= =?UTF-8?q?=E6=96=87=E4=BB=B6url?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/config/config.go | 28 +++++++++++-------- app/config/key.go | 6 ++++ app/config/term.go | 21 +++++++++++++- app/controllers/adminController/init.go | 12 +++++--- .../systemController/controlInfo.go | 11 ++++++-- config/router/adminRouter.go | 2 +- config/router/initRouter.go | 2 +- 7 files changed, 61 insertions(+), 21 deletions(-) diff --git a/app/config/config.go b/app/config/config.go index fb24923..30c085d 100644 --- a/app/config/config.go +++ b/app/config/config.go @@ -2,6 +2,8 @@ package config import ( "context" + "errors" + "gorm.io/gorm" "time" "wejh-go/app/models" "wejh-go/config/database" @@ -28,22 +30,24 @@ func getConfig(key string) string { func setConfig(key, value string) error { redis.RedisClient.Set(ctx, key, value, 0) - res := database.DB.Model(models.Config{}).Where( - &models.Config{ - Key: key, - }).Updates(&models.Config{ - Key: key, - Value: value, - }) - if res.RowsAffected == 0 { - rc := database.DB.Create(&models.Config{ + var config models.Config + result := database.DB.Where("`key` = ?", key).First(&config) + if result.Error != nil && !errors.Is(result.Error, gorm.ErrRecordNotFound) { + return result.Error + } + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + config = models.Config{ Key: key, Value: value, UpdateTime: time.Now(), - }) - return rc.Error + } + result = database.DB.Create(&config) + } else { + config.Value = value + config.UpdateTime = time.Now() + result = database.DB.Updates(&config) } - return res.Error + return result.Error } func checkConfig(key string) bool { diff --git a/app/config/key.go b/app/config/key.go index b78bb0a..cb22bf4 100644 --- a/app/config/key.go +++ b/app/config/key.go @@ -6,6 +6,8 @@ const webpUrlKey = "jpgUrlKey" const fileUrlKey = "fileUrlKey" +const registerTipsKey = "registerTipsKey" + func GetSchoolBusUrl() string { return getConfig(schoolBusUrlKey) } @@ -29,3 +31,7 @@ func GetFileUrlKey() string { func SetFileUrlKey(url string) error { return setConfig(fileUrlKey, url) } + +func GetRegisterTipsKey() string { return getConfig(registerTipsKey) } + +func SetRegisterTipsKey(url string) error { return setConfig(registerTipsKey, url) } diff --git a/app/config/term.go b/app/config/term.go index ed9a178..75559d5 100644 --- a/app/config/term.go +++ b/app/config/term.go @@ -6,7 +6,7 @@ const termStartDate = "termStartDate" const scoreTermYearKey = "scoreTermYearKey" const scoreTermKey = "scoreTermKey" -func SetTermInfo(yearValue, termValue, termStartDateValue, scoreYearValue, scoreTermValue string) error { +func SetSystemInfo(yearValue, termValue, termStartDateValue, scoreYearValue, scoreTermValue, jpgUrlValue, fileUrlValue, registerTipsValue, schoolBusUrlValue string) error { err := setConfig(termYearKey, yearValue) if err != nil { return err @@ -24,6 +24,25 @@ func SetTermInfo(yearValue, termValue, termStartDateValue, scoreYearValue, score return err } err = setConfig(scoreTermKey, scoreTermValue) + if err != nil { + return err + } + err = setConfig(schoolBusUrlKey, schoolBusUrlValue) + if err != nil { + return err + } + err = setConfig(webpUrlKey, jpgUrlValue) + if err != nil { + return err + } + err = setConfig(fileUrlKey, fileUrlValue) + if err != nil { + return err + } + err = setConfig(registerTipsKey, registerTipsValue) + if err != nil { + return err + } return err } diff --git a/app/controllers/adminController/init.go b/app/controllers/adminController/init.go index eeb8a07..0f77024 100644 --- a/app/controllers/adminController/init.go +++ b/app/controllers/adminController/init.go @@ -8,12 +8,16 @@ import ( "wejh-go/app/utils" ) -type termInfoForm struct { +type SystemInfoForm struct { YearValue string `json:"yearValue"` TermValue string `json:"termValue"` TermStartDateValue string `json:"termStartDateValue"` ScoreYearValue string `json:"scoreYearValue"` ScoreTermValue string `json:"scoreTermValue"` + SchoolBusUrlValue string `json:"schoolBusUrlValue"` + JpgUrlValue string `json:"jpgUrlValue"` + FileUrlValue string `json:"fileUrlValue"` + RegisterTips string `json:"registerTips"` } type encryptForm struct { @@ -59,15 +63,15 @@ func ResetInit(c *gin.Context) { utils.JsonSuccessResponse(c, nil) } -func SetTermInfo(c *gin.Context) { - var postForm termInfoForm +func SetSystemInfo(c *gin.Context) { + var postForm SystemInfoForm err := c.ShouldBindJSON(&postForm) if err != nil { _ = c.AbortWithError(200, apiException.ParamError) return } - err = config.SetTermInfo(postForm.YearValue, postForm.TermValue, postForm.TermStartDateValue, postForm.ScoreYearValue, postForm.ScoreTermValue) + err = config.SetSystemInfo(postForm.YearValue, postForm.TermValue, postForm.TermStartDateValue, postForm.ScoreYearValue, postForm.ScoreTermValue, postForm.JpgUrlValue, postForm.FileUrlValue, postForm.RegisterTips, postForm.SchoolBusUrlValue) if err != nil { _ = c.AbortWithError(200, apiException.ServerError) return diff --git a/app/controllers/systemController/controlInfo.go b/app/controllers/systemController/controlInfo.go index 56224ae..3ce3a02 100644 --- a/app/controllers/systemController/controlInfo.go +++ b/app/controllers/systemController/controlInfo.go @@ -12,12 +12,15 @@ func Info(c *gin.Context) { startTime, _ := time.Parse("2006-01-02", startTimeString) // 学期开始的时间 currentTime := time.Now() schoolBusUrl := config.GetSchoolBusUrl() + jpgUrl := config.GetWebpUrlKey() + fileUrl := config.GetFileUrlKey() + registerTips := config.GetRegisterTipsKey() week := ((currentTime.Unix()-startTime.Unix())/3600+8)/24/7 + 1 if currentTime.Unix() < startTime.Unix()-8*3600 { week = -1 } - utils.JsonSuccessResponse(c, gin.H{ + response := gin.H{ "time": time.Now(), "is_begin": week > 0, "termStartDate": startTimeString, @@ -27,6 +30,10 @@ func Info(c *gin.Context) { "scoreTerm": scoreTerm, "week": week, "schoolBusUrl": schoolBusUrl, - }) + "jpgUrl": jpgUrl, + "fileUrl": fileUrl, + "registerTips": registerTips, + } + utils.JsonSuccessResponse(c, response) } diff --git a/config/router/adminRouter.go b/config/router/adminRouter.go index 5d3fccc..17e9e3a 100755 --- a/config/router/adminRouter.go +++ b/config/router/adminRouter.go @@ -38,7 +38,7 @@ func adminRouterInit(r *gin.RouterGroup) { { set.GET("/reset", adminController.ResetInit) set.POST("/encrypt", adminController.SetEncryptKey) - set.POST("/terminfo", adminController.SetTermInfo) + set.POST("/systeminfo", adminController.SetSystemInfo) } user := admin.Group("/user") { diff --git a/config/router/initRouter.go b/config/router/initRouter.go index 0b66356..0155d3a 100644 --- a/config/router/initRouter.go +++ b/config/router/initRouter.go @@ -8,5 +8,5 @@ import ( func initRouterInit(r *gin.RouterGroup) { r.GET("/init", adminController.SetInit) r.POST("/encrypt", adminController.SetEncryptKey) - r.POST("/terminfo", adminController.SetTermInfo) + r.POST("/systeminfo", adminController.SetSystemInfo) }