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(theme):增加用户端获取主题色列表,管理端增加删除修改查找主题色和主题色用户权限的功能 #78

Merged
merged 1 commit into from
Oct 12, 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
6 changes: 6 additions & 0 deletions app/config/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const fileUrlKey = "fileUrlKey"

const registerTipsKey = "registerTipsKey"

const defaultThemeKey = "defaultThemeKey"

func GetSchoolBusUrl() string {
return getConfig(schoolBusUrlKey)
}
Expand Down Expand Up @@ -35,3 +37,7 @@ func SetFileUrlKey(url string) error {
func GetRegisterTipsKey() string { return getConfig(registerTipsKey) }

func SetRegisterTipsKey(url string) error { return setConfig(registerTipsKey, url) }

func GetDefaultThemeKey() string { return getConfig(defaultThemeKey) }

func SetDefaultThemeKey(url string) error { return setConfig(defaultThemeKey, url) }
6 changes: 5 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 SetSystemInfo(yearValue, termValue, termStartDateValue, scoreYearValue, scoreTermValue, jpgUrlValue, fileUrlValue, registerTipsValue, schoolBusUrlValue string) error {
func SetSystemInfo(yearValue, termValue, termStartDateValue, scoreYearValue, scoreTermValue, jpgUrlValue, fileUrlValue, registerTipsValue, schoolBusUrlValue, defaultThemeValue string) error {
err := setConfig(termYearKey, yearValue)
if err != nil {
return err
Expand Down Expand Up @@ -43,6 +43,10 @@ func SetSystemInfo(yearValue, termValue, termStartDateValue, scoreYearValue, sco
if err != nil {
return err
}
err = setConfig(defaultThemeKey, defaultThemeValue)
if err != nil {
return err
}
return err
}

Expand Down
3 changes: 2 additions & 1 deletion app/controllers/adminController/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type SystemInfoForm struct {
JpgUrlValue string `json:"jpgUrlValue"`
FileUrlValue string `json:"fileUrlValue"`
RegisterTips string `json:"registerTips"`
DefaultThemeValue string `json:"defaultThemeValue"`
}

type encryptForm struct {
Expand Down Expand Up @@ -71,7 +72,7 @@ func SetSystemInfo(c *gin.Context) {
return
}

err = config.SetSystemInfo(postForm.YearValue, postForm.TermValue, postForm.TermStartDateValue, postForm.ScoreYearValue, postForm.ScoreTermValue, postForm.JpgUrlValue, postForm.FileUrlValue, postForm.RegisterTips, postForm.SchoolBusUrlValue)
err = config.SetSystemInfo(postForm.YearValue, postForm.TermValue, postForm.TermStartDateValue, postForm.ScoreYearValue, postForm.ScoreTermValue, postForm.JpgUrlValue, postForm.FileUrlValue, postForm.RegisterTips, postForm.SchoolBusUrlValue, postForm.DefaultThemeValue)
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
Expand Down
180 changes: 180 additions & 0 deletions app/controllers/adminController/theme.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package adminController

import (
"github.com/gin-gonic/gin"
"wejh-go/app/apiException"
"wejh-go/app/models"
"wejh-go/app/services/themeServices"
"wejh-go/app/utils"
)

type CreateThemeData struct {
Name string `json:"name" binding:"required"`
Type string `json:"type" binding:"required"`
ThemeConfig string `json:"theme_config"`
}

// 管理员创建主题色
func CreateTheme(c *gin.Context) {
var data CreateThemeData
err := c.ShouldBindJSON(&data)
if err != nil {
_ = c.AbortWithError(200, apiException.ParamError)
return
}

record := models.Theme{
Name: data.Name,
Type: data.Type,
ThemeConfig: data.ThemeConfig,
}
themeID, err := themeServices.CreateTheme(record)
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}
if data.Type == "all" {
studentIDs, err := themeServices.GetAllStudentIDs()
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}

_, err = themeServices.AddThemePermission(themeID, studentIDs)
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}
}

utils.JsonSuccessResponse(c, nil)
}

type UpdateThemeData struct {
ID int `json:"id" binding:"required"`
Name string `json:"name" binding:"required"`
ThemeConfig string `json:"theme_config" binding:"required"`
}

// 管理员更新主题色
func UpdateTheme(c *gin.Context) {
var data UpdateThemeData
err := c.ShouldBindJSON(&data)
if err != nil {
_ = c.AbortWithError(200, apiException.ParamError)
return
}

record := models.Theme{
Name: data.Name,
ThemeConfig: data.ThemeConfig,
}
err = themeServices.UpdateTheme(data.ID, record)
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}

utils.JsonSuccessResponse(c, nil)
}

// 管理员获取主题色列表
func GetThemes(c *gin.Context) {
themes, err := themeServices.GetThemes()
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}

utils.JsonSuccessResponse(c, gin.H{
"theme_list": themes,
})
}

type DeleteThemeData struct {
ID int `form:"id" binding:"required"`
}

// 管理员根据id删除主题色
func DeleteTheme(c *gin.Context) {
var data DeleteThemeData
err := c.ShouldBindQuery(&data)
if err != nil {
_ = c.AbortWithError(200, apiException.ParamError)
return
}

err = themeServices.CheckThemeExist(data.ID)
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}

err = themeServices.DeleteTheme(data.ID)
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}
utils.JsonSuccessResponse(c, nil)
}

type AddThemePermissionData struct {
ThemeID int `json:"theme_id" binding:"required"`
StudentID []string `json:"student_id" binding:"required"`
}

// 管理员添加用户主题色权限
func AddThemePermission(c *gin.Context) {
var data AddThemePermissionData
err := c.ShouldBindJSON(&data)
if err != nil {
_ = c.AbortWithError(200, apiException.ParamError)
return
}

err = themeServices.CheckThemeExist(data.ThemeID)
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}

invalidStudentIDs, err := themeServices.AddThemePermission(data.ThemeID, data.StudentID)
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}

utils.JsonSuccessResponse(c, gin.H{
"invalid_student_ids": invalidStudentIDs,
})
}

type GetThemePermissionData struct {
StudentID string `form:"student_id" binding:"required"`
}

// 管理员根据学号查询用户主题色权限
func GetThemePermission(c *gin.Context) {
var data GetThemePermissionData
err := c.ShouldBindQuery(&data)
if err != nil {
_ = c.AbortWithError(200, apiException.ParamError)
return
}

themePermission, err := themeServices.GetThemePermissionByStudentID(data.StudentID)
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}

themeNames, err := themeServices.GetThemeNameByID(themePermission)
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}

utils.JsonSuccessResponse(c, gin.H{
"theme_name": themeNames,
})
}
66 changes: 66 additions & 0 deletions app/controllers/funcControllers/themeController/themeController.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package themeController

import (
"github.com/gin-gonic/gin"
"wejh-go/app/apiException"
"wejh-go/app/services/sessionServices"
"wejh-go/app/services/themeServices"
"wejh-go/app/utils"
)

func GetThemeList(c *gin.Context) {
user, err := sessionServices.GetUserSession(c)
if err != nil {
_ = c.AbortWithError(200, apiException.NotLogin)
return
}

themePermission, err := themeServices.GetThemePermissionByStudentID(user.StudentID)
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}

themes, err := themeServices.GetThemesByID(themePermission)
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}

utils.JsonSuccessResponse(c, gin.H{
"theme_list": themes,
"current_theme_id": themePermission.CurrentThemeID,
})
}

type ChooseCurrentThemeData struct {
ID int `json:"id" binding:"required"`
}

func ChooseCurrentTheme(c *gin.Context) {
var data ChooseCurrentThemeData
err := c.ShouldBindJSON(&data)
if err != nil {
_ = c.AbortWithError(200, apiException.ParamError)
return
}

user, err := sessionServices.GetUserSession(c)
if err != nil {
_ = c.AbortWithError(200, apiException.NotLogin)
return
}

err = themeServices.CheckThemeExist(data.ID)
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}

err = themeServices.UpdateCurrentTheme(data.ID, user.StudentID)
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}
utils.JsonSuccessResponse(c, nil)
}
23 changes: 23 additions & 0 deletions app/controllers/systemController/controlInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package systemController

import (
"github.com/gin-gonic/gin"
"strconv"
"time"
"wejh-go/app/apiException"
"wejh-go/app/config"
"wejh-go/app/models"
"wejh-go/app/services/themeServices"
"wejh-go/app/utils"
)

Expand All @@ -15,11 +19,29 @@ func Info(c *gin.Context) {
jpgUrl := config.GetWebpUrlKey()
fileUrl := config.GetFileUrlKey()
registerTips := config.GetRegisterTipsKey()
defaultThemeIDStr := config.GetDefaultThemeKey()

week := ((currentTime.Unix()-startTime.Unix())/3600+8)/24/7 + 1
if currentTime.Unix() < startTime.Unix()-8*3600 {
week = -1
}

var defaultTheme models.Theme
if defaultThemeIDStr != "" {
defaultThemeID, err := strconv.Atoi(defaultThemeIDStr)
if err == nil {
defaultTheme, err = themeServices.GetThemeByID(defaultThemeID)
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}
if defaultTheme.Type != "all" {
_ = c.AbortWithError(200, apiException.ServerError)
return
}
}
}

response := gin.H{
"time": time.Now(),
"is_begin": week > 0,
Expand All @@ -33,6 +55,7 @@ func Info(c *gin.Context) {
"jpgUrl": jpgUrl,
"fileUrl": fileUrl,
"registerTips": registerTips,
"defaultTheme": defaultTheme,
}
utils.JsonSuccessResponse(c, response)

Expand Down
1 change: 0 additions & 1 deletion app/controllers/userController/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ func AuthByPassword(c *gin.Context) {
"createTime": user.CreateTime,
},
})

}

func AuthBySession(c *gin.Context) {
Expand Down
7 changes: 7 additions & 0 deletions app/controllers/userController/del.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/gin-gonic/gin"
"wejh-go/app/apiException"
"wejh-go/app/services/sessionServices"
"wejh-go/app/services/themeServices"
"wejh-go/app/services/userServices"
"wejh-go/app/utils"
)
Expand Down Expand Up @@ -32,6 +33,12 @@ func DelAccount(c *gin.Context) {
return
}

err = themeServices.DeleteThemePermission(user.StudentID)
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}

if err = userServices.DelAccount(user, postForm.IDCard); err != nil {
if err == apiException.StudentNumAndIidError {
_ = c.AbortWithError(200, apiException.StudentNumAndIidError)
Expand Down
Loading
Loading