Skip to content

Commit 5ca83b3

Browse files
committed
feat(theme):增加用户端获取主题色列表,管理端增加删除修改查找主题色和主题色用户权限的功能
1 parent dde03bf commit 5ca83b3

File tree

11 files changed

+586
-2
lines changed

11 files changed

+586
-2
lines changed
+177
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
package adminController
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
"wejh-go/app/apiException"
6+
"wejh-go/app/models"
7+
"wejh-go/app/services/themeServices"
8+
"wejh-go/app/utils"
9+
)
10+
11+
type CreateThemeData struct {
12+
Name string `json:"name" binding:"required"`
13+
Type string `json:"type" binding:"required"`
14+
ThemeConfig string `json:"theme_config"`
15+
}
16+
17+
// 管理员创建主题色
18+
func CreateTheme(c *gin.Context) {
19+
var data CreateThemeData
20+
err := c.ShouldBindJSON(&data)
21+
if err != nil {
22+
_ = c.AbortWithError(200, apiException.ParamError)
23+
return
24+
}
25+
26+
record := models.Theme{
27+
Name: data.Name,
28+
Type: data.Type,
29+
ThemeConfig: data.ThemeConfig,
30+
}
31+
themeID, err := themeServices.CreateTheme(record)
32+
if err != nil {
33+
_ = c.AbortWithError(200, apiException.ServerError)
34+
return
35+
}
36+
if data.Type == "all" {
37+
studentIDs, err := themeServices.GetAllStudentIDs()
38+
if err != nil {
39+
_ = c.AbortWithError(200, apiException.ServerError)
40+
return
41+
}
42+
43+
err = themeServices.AddThemePermission(themeID, studentIDs)
44+
if err != nil {
45+
_ = c.AbortWithError(200, apiException.ServerError)
46+
return
47+
}
48+
}
49+
50+
utils.JsonSuccessResponse(c, nil)
51+
}
52+
53+
type UpdateThemeData struct {
54+
ID int `json:"id" binding:"required"`
55+
Name string `json:"name" binding:"required"`
56+
ThemeConfig string `json:"theme_config" binding:"required"`
57+
}
58+
59+
// 管理员更新主题色
60+
func UpdateTheme(c *gin.Context) {
61+
var data UpdateThemeData
62+
err := c.ShouldBindJSON(&data)
63+
if err != nil {
64+
_ = c.AbortWithError(200, apiException.ParamError)
65+
return
66+
}
67+
68+
record := models.Theme{
69+
Name: data.Name,
70+
ThemeConfig: data.ThemeConfig,
71+
}
72+
err = themeServices.UpdateTheme(data.ID, record)
73+
if err != nil {
74+
_ = c.AbortWithError(200, apiException.ServerError)
75+
return
76+
}
77+
78+
utils.JsonSuccessResponse(c, nil)
79+
}
80+
81+
// 管理员获取主题色列表
82+
func GetThemes(c *gin.Context) {
83+
themes, err := themeServices.GetThemes()
84+
if err != nil {
85+
_ = c.AbortWithError(200, apiException.ServerError)
86+
return
87+
}
88+
89+
utils.JsonSuccessResponse(c, gin.H{
90+
"theme_list": themes,
91+
})
92+
}
93+
94+
type DeleteThemeData struct {
95+
ID int `form:"id" binding:"required"`
96+
}
97+
98+
// 管理员根据id删除主题色
99+
func DeleteTheme(c *gin.Context) {
100+
var data DeleteThemeData
101+
err := c.ShouldBindQuery(&data)
102+
if err != nil {
103+
_ = c.AbortWithError(200, apiException.ParamError)
104+
return
105+
}
106+
107+
err = themeServices.CheckThemeExist(data.ID)
108+
if err != nil {
109+
_ = c.AbortWithError(200, apiException.ServerError)
110+
return
111+
}
112+
113+
err = themeServices.DeleteTheme(data.ID)
114+
if err != nil {
115+
_ = c.AbortWithError(200, apiException.ServerError)
116+
return
117+
}
118+
utils.JsonSuccessResponse(c, nil)
119+
}
120+
121+
type AddThemePermissionData struct {
122+
ThemeID int `json:"theme_id" binding:"required"`
123+
StudentID []string `json:"student_id" binding:"required"`
124+
}
125+
126+
// 管理员添加用户主题色权限
127+
func AddThemePermission(c *gin.Context) {
128+
var data AddThemePermissionData
129+
err := c.ShouldBindJSON(&data)
130+
if err != nil {
131+
_ = c.AbortWithError(200, apiException.ParamError)
132+
return
133+
}
134+
135+
err = themeServices.CheckThemeExist(data.ThemeID)
136+
if err != nil {
137+
_ = c.AbortWithError(200, apiException.ServerError)
138+
return
139+
}
140+
141+
err = themeServices.AddThemePermission(data.ThemeID, data.StudentID)
142+
if err != nil {
143+
_ = c.AbortWithError(200, apiException.ServerError)
144+
return
145+
}
146+
utils.JsonSuccessResponse(c, nil)
147+
}
148+
149+
type GetThemePermissionData struct {
150+
StudentID string `form:"student_id" binding:"required"`
151+
}
152+
153+
// 管理员根据学号查询用户主题色权限
154+
func GetThemePermission(c *gin.Context) {
155+
var data GetThemePermissionData
156+
err := c.ShouldBindQuery(&data)
157+
if err != nil {
158+
_ = c.AbortWithError(200, apiException.ParamError)
159+
return
160+
}
161+
162+
themePermission, err := themeServices.GetThemePermissionByStudentID(data.StudentID)
163+
if err != nil {
164+
_ = c.AbortWithError(200, apiException.ServerError)
165+
return
166+
}
167+
168+
themeNames, err := themeServices.GetThemeNameByID(themePermission)
169+
if err != nil {
170+
_ = c.AbortWithError(200, apiException.ServerError)
171+
return
172+
}
173+
174+
utils.JsonSuccessResponse(c, gin.H{
175+
"theme_name": themeNames,
176+
})
177+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package themeController
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
"wejh-go/app/apiException"
6+
"wejh-go/app/services/sessionServices"
7+
"wejh-go/app/services/themeServices"
8+
"wejh-go/app/utils"
9+
)
10+
11+
func GetThemeList(c *gin.Context) {
12+
user, err := sessionServices.GetUserSession(c)
13+
if err != nil {
14+
_ = c.AbortWithError(200, apiException.NotLogin)
15+
return
16+
}
17+
18+
themePermission, err := themeServices.GetThemePermissionByStudentID(user.StudentID)
19+
if err != nil {
20+
_ = c.AbortWithError(200, apiException.ServerError)
21+
return
22+
}
23+
24+
themes, err := themeServices.GetThemesByID(themePermission)
25+
if err != nil {
26+
_ = c.AbortWithError(200, apiException.ServerError)
27+
return
28+
}
29+
30+
utils.JsonSuccessResponse(c, gin.H{
31+
"theme_list": themes,
32+
"current_theme_id": themePermission.CurrentThemeID,
33+
})
34+
}
35+
36+
type ChooseCurrentThemeData struct {
37+
ID int `json:"id" binding:"required"`
38+
}
39+
40+
func ChooseCurrentTheme(c *gin.Context) {
41+
var data ChooseCurrentThemeData
42+
err := c.ShouldBindJSON(&data)
43+
if err != nil {
44+
_ = c.AbortWithError(200, apiException.ParamError)
45+
return
46+
}
47+
48+
user, err := sessionServices.GetUserSession(c)
49+
if err != nil {
50+
_ = c.AbortWithError(200, apiException.NotLogin)
51+
return
52+
}
53+
54+
err = themeServices.CheckThemeExist(data.ID)
55+
if err != nil {
56+
_ = c.AbortWithError(200, apiException.ServerError)
57+
return
58+
}
59+
60+
err = themeServices.UpdateCurrentTheme(data.ID, user.StudentID)
61+
if err != nil {
62+
_ = c.AbortWithError(200, apiException.ServerError)
63+
return
64+
}
65+
utils.JsonSuccessResponse(c, nil)
66+
}

app/controllers/userController/auth.go

-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ func AuthByPassword(c *gin.Context) {
7070
"createTime": user.CreateTime,
7171
},
7272
})
73-
7473
}
7574

7675
func AuthBySession(c *gin.Context) {

app/controllers/userController/del.go

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/gin-gonic/gin"
55
"wejh-go/app/apiException"
66
"wejh-go/app/services/sessionServices"
7+
"wejh-go/app/services/themeServices"
78
"wejh-go/app/services/userServices"
89
"wejh-go/app/utils"
910
)
@@ -32,6 +33,12 @@ func DelAccount(c *gin.Context) {
3233
return
3334
}
3435

36+
err = themeServices.DeleteThemePermission(user.StudentID)
37+
if err != nil {
38+
_ = c.AbortWithError(200, apiException.ServerError)
39+
return
40+
}
41+
3542
if err = userServices.DelAccount(user, postForm.IDCard); err != nil {
3643
if err == apiException.StudentNumAndIidError {
3744
_ = c.AbortWithError(200, apiException.StudentNumAndIidError)

app/controllers/userController/reg.go

+13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strings"
66
"wejh-go/app/apiException"
77
"wejh-go/app/services/sessionServices"
8+
"wejh-go/app/services/themeServices"
89
"wejh-go/app/services/userServices"
910
"wejh-go/app/utils"
1011
"wejh-go/config/wechat"
@@ -57,6 +58,12 @@ func BindOrCreateStudentUserFromWechat(c *gin.Context) {
5758
return
5859
}
5960

61+
err = themeServices.AddDefaultThemePermission(postForm.StudentID)
62+
if err != nil {
63+
_ = c.AbortWithError(200, apiException.ServerError)
64+
return
65+
}
66+
6067
err = sessionServices.SetUserSession(c, user)
6168
if err != nil {
6269
_ = c.AbortWithError(200, apiException.ServerError)
@@ -86,6 +93,12 @@ func CreateStudentUser(c *gin.Context) {
8693
return
8794
}
8895

96+
err = themeServices.AddDefaultThemePermission(postForm.StudentID)
97+
if err != nil {
98+
_ = c.AbortWithError(200, apiException.ServerError)
99+
return
100+
}
101+
89102
err = sessionServices.SetUserSession(c, user)
90103
if err != nil {
91104
_ = c.AbortWithError(200, apiException.ServerError)

app/models/theme.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package models
2+
3+
type Theme struct {
4+
ID int `json:"id"`
5+
Name string `json:"name"`
6+
Type string `json:"type"`
7+
ThemeConfig string `json:"theme_config"`
8+
}

app/models/themePermission.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package models
2+
3+
type ThemePermission struct {
4+
ID int `json:"id"`
5+
StudentID string `json:"student_id"`
6+
CurrentThemeID int `json:"current_theme_id"`
7+
ThemePermission string `json:"theme_permission"`
8+
}
9+
10+
type ThemePermissionData struct {
11+
ThemeIDs []int `json:"theme_id"`
12+
}

0 commit comments

Comments
 (0)