Skip to content

Commit

Permalink
feat(theme):增加用户端获取主题色列表,管理端增加删除修改查找主题色和主题色用户权限的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
qianqianzyk committed Oct 6, 2024
1 parent dde03bf commit 5ca83b3
Show file tree
Hide file tree
Showing 11 changed files with 586 additions and 2 deletions.
177 changes: 177 additions & 0 deletions app/controllers/adminController/theme.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
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
}

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

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)
}
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
13 changes: 13 additions & 0 deletions app/controllers/userController/reg.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"
"wejh-go/app/apiException"
"wejh-go/app/services/sessionServices"
"wejh-go/app/services/themeServices"
"wejh-go/app/services/userServices"
"wejh-go/app/utils"
"wejh-go/config/wechat"
Expand Down Expand Up @@ -57,6 +58,12 @@ func BindOrCreateStudentUserFromWechat(c *gin.Context) {
return
}

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

err = sessionServices.SetUserSession(c, user)
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
Expand Down Expand Up @@ -86,6 +93,12 @@ func CreateStudentUser(c *gin.Context) {
return
}

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

err = sessionServices.SetUserSession(c, user)
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
Expand Down
8 changes: 8 additions & 0 deletions app/models/theme.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package models

type Theme struct {
ID int `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
ThemeConfig string `json:"theme_config"`
}
12 changes: 12 additions & 0 deletions app/models/themePermission.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package models

type ThemePermission struct {
ID int `json:"id"`
StudentID string `json:"student_id"`
CurrentThemeID int `json:"current_theme_id"`
ThemePermission string `json:"theme_permission"`
}

type ThemePermissionData struct {
ThemeIDs []int `json:"theme_id"`
}
Loading

0 comments on commit 5ca83b3

Please sign in to comment.