refactor: 管理后台用户编辑功能 is ready

This commit is contained in:
RockYang
2023-06-19 21:53:07 +08:00
parent 5f812ae649
commit 65a01f4776
7 changed files with 95 additions and 75 deletions

View File

@@ -2,11 +2,13 @@ package admin
import (
"chatplus/core"
"chatplus/core/types"
"chatplus/handler"
"chatplus/store/model"
"chatplus/store/vo"
"chatplus/utils"
"chatplus/utils/resp"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
@@ -49,3 +51,50 @@ func (h *UserHandler) List(c *gin.Context) {
pageVo := vo.NewPage(total, page, pageSize, users)
resp.SUCCESS(c, pageVo)
}
func (h *UserHandler) Update(c *gin.Context) {
var data struct {
Id uint `json:"id"`
Nickname string `json:"nickname"`
Calls int `json:"calls"`
ChatRoles []string `json:"chat_roles"`
ExpiredTime string `json:"expired_time"`
Status bool `json:"status"`
}
if err := c.ShouldBindJSON(&data); err != nil {
resp.ERROR(c, types.InvalidArgs)
return
}
var user = model.User{
Nickname: data.Nickname,
Calls: data.Calls,
Status: data.Status,
ChatRoles: utils.JsonEncode(data.ChatRoles),
ExpiredTime: utils.Str2stamp(data.ExpiredTime),
}
user.Id = data.Id
res := h.db.Updates(&user)
if res.Error != nil {
resp.ERROR(c, "更新数据库失败")
return
}
resp.SUCCESS(c)
}
func (h *UserHandler) InitUser(c *gin.Context) {
var users []model.User
h.db.Find(&users)
for _, u := range users {
var m map[string]int
var roleKeys = make([]string, 0)
utils.JsonDecode(u.ChatRoles, &m)
for k, _ := range m {
roleKeys = append(roleKeys, k)
}
u.ChatRoles = utils.JsonEncode(roleKeys)
h.db.Updates(&u)
}
resp.SUCCESS(c, "SUCCESS")
}