Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:新增新生提醒功能并调整修改系统信息接口 #74

Merged
merged 1 commit into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package config

import (
"context"
"errors"
"gorm.io/gorm"
"time"
"wejh-go/app/models"
"wejh-go/config/database"
Expand All @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions app/config/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const webpUrlKey = "jpgUrlKey"

const fileUrlKey = "fileUrlKey"

const registerTipsKey = "registerTipsKey"

func GetSchoolBusUrl() string {
return getConfig(schoolBusUrlKey)
}
Expand All @@ -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) }
21 changes: 20 additions & 1 deletion app/config/term.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down
12 changes: 8 additions & 4 deletions app/controllers/adminController/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions app/controllers/systemController/controlInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)

}
2 changes: 1 addition & 1 deletion config/router/adminRouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
{
Expand Down
2 changes: 1 addition & 1 deletion config/router/initRouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Loading