feat: complete settings

This commit is contained in:
engigu
2023-12-31 21:31:56 +08:00
parent 77af985b76
commit 66b3a30a62
25 changed files with 595 additions and 37 deletions
+63 -4
View File
@@ -9,7 +9,30 @@ import (
"net/http"
)
type EditUserPasswd struct {
type GetSettingReq struct {
Section string `json:"section" validate:"required,max=50" label:"节点"`
}
// GetUserSetting 用户重设密码
func GetUserSetting(c *gin.Context) {
var (
appG = app.Gin{C: c}
)
section := c.Query("section")
settingService := settings_service.UserSettings{}
settings, err := settingService.GetUserSetting(section)
if err != nil {
appG.CResponse(http.StatusBadRequest, fmt.Sprintf("获取失败!错误原因:%s", err), nil)
return
}
appG.CResponse(http.StatusOK, "获取成功!", settings)
}
// EditUserPasswdReq 编辑设置请求
type EditUserPasswdReq struct {
OldPassword string `json:"old_passwd" validate:"required,max=50" label:"旧密码"`
NewPassword string `json:"new_passwd" validate:"required,max=50" label:"新密码"`
}
@@ -18,7 +41,7 @@ type EditUserPasswd struct {
func EditPasswd(c *gin.Context) {
var (
appG = app.Gin{C: c}
req EditUserPasswd
req EditUserPasswdReq
)
currentUser := app.GetCurrentUserName(c)
@@ -28,12 +51,12 @@ func EditPasswd(c *gin.Context) {
return
}
sendTaskService := settings_service.UserSettings{
settingService := settings_service.UserSettings{
UserName: currentUser,
OldPassword: req.OldPassword,
NewPassword: req.NewPassword,
}
err := sendTaskService.EditUserPasswd()
err := settingService.EditUserPasswd()
if err != nil {
appG.CResponse(http.StatusBadRequest, fmt.Sprintf("修改密码失败!错误原因:%s", err), nil)
return
@@ -42,3 +65,39 @@ func EditPasswd(c *gin.Context) {
appG.CResponse(http.StatusOK, "修改成功!", nil)
}
type SettingReq struct {
Section string `json:"section" validate:"required,max=50" label:"一级分类"`
Data map[string]string
}
// EditSettings 编辑设置
func EditSettings(c *gin.Context) {
var (
appG = app.Gin{C: c}
req SettingReq
)
errCode, errStr := app.BindJsonAndPlayValid(c, &req)
if errCode != e.SUCCESS {
appG.CResponse(errCode, errStr, nil)
return
}
currentUser := app.GetCurrentUserName(c)
settingService := settings_service.UserSettings{}
diffStr := settingService.ValidateDiffSetting(req.Section, req.Data)
if diffStr != "" {
appG.CResponse(http.StatusBadRequest, diffStr, nil)
return
}
for key, value := range req.Data {
err := settingService.EditSettings(req.Section, key, value, currentUser)
if err != nil {
appG.CResponse(http.StatusBadRequest, fmt.Sprintf("修改密码失败!错误原因:%s", err), nil)
return
}
}
appG.CResponse(http.StatusOK, "修改成功!", nil)
}
+2
View File
@@ -46,6 +46,8 @@ func InitRouter() *gin.Engine {
// settings
apiv1.POST("/settings/setpasswd", v1.EditPasswd)
apiv1.POST("/settings/set", v1.EditSettings)
apiv1.GET("/settings/getsetting", v1.GetUserSetting)
}