feat: finish adding chat role to user function

This commit is contained in:
RockYang
2023-10-16 06:56:42 +08:00
parent 80e2b34abc
commit d2a8d655c8
5 changed files with 178 additions and 12 deletions

View File

@@ -2,6 +2,7 @@ package handler
import (
"chatplus/core"
"chatplus/core/types"
"chatplus/store/model"
"chatplus/store/vo"
"chatplus/utils"
@@ -76,3 +77,29 @@ func (h *ChatRoleHandler) List(c *gin.Context) {
}
resp.SUCCESS(c, roleVos)
}
// AddRole 为用户添加角色
func (h *ChatRoleHandler) AddRole(c *gin.Context) {
user, err := utils.GetLoginUser(c, h.db)
if err != nil {
resp.NotAuth(c)
return
}
var data struct {
Keys []string `json:"keys"`
}
if err = c.ShouldBindJSON(&data); err != nil {
resp.ERROR(c, types.InvalidArgs)
return
}
res := h.db.Model(&model.User{}).Where("id = ?", user.Id).UpdateColumn("chat_roles_json", utils.JsonEncode(data.Keys))
if res.Error != nil {
logger.Error("添加应用失败:", err)
resp.ERROR(c, "更新数据库失败!")
return
}
resp.SUCCESS(c)
}

View File

@@ -185,6 +185,7 @@ func main() {
fx.Invoke(func(s *core.AppServer, h *handler.ChatRoleHandler) {
group := s.Engine.Group("/api/role/")
group.GET("list", h.List)
group.POST("add", h.AddRole)
}),
fx.Invoke(func(s *core.AppServer, h *handler.UserHandler) {
group := s.Engine.Group("/api/user/")

View File

@@ -36,7 +36,7 @@ func CopyObject(src interface{}, dst interface{}) error {
pType := reflect.New(value.Type())
v2 := pType.Interface()
err := json.Unmarshal([]byte(v.String()), &v2)
if err == nil {
if err == nil && v2 != nil {
value.Set(reflect.ValueOf(v2).Elem())
}
// map, struct, slice to string