mirror of
https://github.com/yangjian102621/geekai.git
synced 2025-11-05 16:53:46 +08:00
refactor: 管理后台用户编辑功能 is ready
This commit is contained in:
@@ -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")
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"chatplus/store/vo"
|
||||
"chatplus/utils"
|
||||
"chatplus/utils/resp"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -33,11 +34,11 @@ func (h *ChatRoleHandler) List(c *gin.Context) {
|
||||
if userId > 0 {
|
||||
var user model.User
|
||||
h.db.First(&user, userId)
|
||||
var roleMap map[string]int
|
||||
err := utils.JsonDecode(user.ChatRoles, &roleMap)
|
||||
var roleKeys []string
|
||||
err := utils.JsonDecode(user.ChatRoles, &roleKeys)
|
||||
if err == nil {
|
||||
for index, r := range roles {
|
||||
if _, ok := roleMap[r.Key]; !ok {
|
||||
if utils.ContainsStr(roleKeys, r.Key) {
|
||||
roles = append(roles[:index], roles[index+1:]...)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,9 +62,9 @@ func (h *UserHandler) Register(c *gin.Context) {
|
||||
// 默认订阅所有角色
|
||||
var chatRoles []model.ChatRole
|
||||
h.db.Find(&chatRoles)
|
||||
var roleMap = make(map[string]int)
|
||||
var roleKeys = make([]string, 0)
|
||||
for _, r := range chatRoles {
|
||||
roleMap[r.Key] = 1
|
||||
roleKeys = append(roleKeys, r.Key)
|
||||
}
|
||||
|
||||
salt := utils.RandString(8)
|
||||
@@ -75,7 +75,7 @@ func (h *UserHandler) Register(c *gin.Context) {
|
||||
Avatar: "images/avatar/user.png",
|
||||
Salt: salt,
|
||||
Status: true,
|
||||
ChatRoles: utils.JsonEncode(roleMap),
|
||||
ChatRoles: utils.JsonEncode(roleKeys),
|
||||
ChatConfig: utils.JsonEncode(types.ChatConfig{
|
||||
Temperature: h.App.ChatConfig.Temperature,
|
||||
MaxTokens: h.App.ChatConfig.MaxTokens,
|
||||
@@ -145,14 +145,10 @@ func (h *UserHandler) Login(c *gin.Context) {
|
||||
h.App.ChatSession.Put(sessionId, types.ChatSession{ClientIP: c.ClientIP(), UserId: user.Id, Username: data.Username, SessionId: sessionId})
|
||||
|
||||
// 加载用户订阅的聊天角色
|
||||
var roleMap map[string]int
|
||||
err = utils.JsonDecode(user.ChatRoles, &roleMap)
|
||||
var roleKeys []string
|
||||
err = utils.JsonDecode(user.ChatRoles, &roleKeys)
|
||||
var chatRoles interface{}
|
||||
if err == nil {
|
||||
roleKeys := make([]string, 0)
|
||||
for key := range roleMap {
|
||||
roleKeys = append(roleKeys, key)
|
||||
}
|
||||
var roles []model.ChatRole
|
||||
res = h.db.Where("marker IN ?", roleKeys).Find(&roles)
|
||||
if res.Error == err {
|
||||
|
||||
@@ -134,6 +134,8 @@ func main() {
|
||||
fx.Invoke(func(s *core.AppServer, h *admin.UserHandler) {
|
||||
group := s.Engine.Group("/api/admin/user/")
|
||||
group.GET("list", h.List)
|
||||
group.POST("update", h.Update)
|
||||
group.GET("test", h.InitUser)
|
||||
}),
|
||||
fx.Invoke(func(s *core.AppServer, h *admin.ChatRoleHandler) {
|
||||
group := s.Engine.Group("/api/admin/role/")
|
||||
|
||||
@@ -11,7 +11,7 @@ type User struct {
|
||||
Tokens int64 `json:"tokens"` // 剩余tokens
|
||||
Calls int `json:"calls"` // 剩余对话次数
|
||||
ChatConfig types.ChatConfig `json:"chat_config"` // 聊天配置
|
||||
ChatRoles map[string]int `json:"chat_roles"` // 聊天角色集合
|
||||
ChatRoles []string `json:"chat_roles"` // 聊天角色集合
|
||||
ExpiredTime int64 `json:"expired_time"` // 账户到期时间
|
||||
Status bool `json:"status"` // 当前状态
|
||||
LastLoginAt int64 `json:"last_login_at"` // 最后登录时间
|
||||
|
||||
Reference in New Issue
Block a user