feat: API Key manage is ready

This commit is contained in:
RockYang
2023-06-20 18:05:33 +08:00
parent 469844d97b
commit e490e15bc4
8 changed files with 248 additions and 37 deletions

View File

@@ -8,10 +8,9 @@ import (
"chatplus/store/vo"
"chatplus/utils"
"chatplus/utils/resp"
"net/http"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"time"
)
type ApiKeyHandler struct {
@@ -25,38 +24,50 @@ func NewApiKeyHandler(app *core.AppServer, db *gorm.DB) *ApiKeyHandler {
return &h
}
func (h *ApiKeyHandler) Add(c *gin.Context) {
func (h *ApiKeyHandler) Save(c *gin.Context) {
var data struct {
Key string
Id uint `json:"id"`
UserId uint `json:"user_id"`
Value string `json:"value"`
LastUsedAt string `json:"last_used_at"`
CreatedAt int64 `json:"created_at"`
}
if err := c.ShouldBindJSON(&data); err != nil {
resp.ERROR(c, types.InvalidArgs)
return
}
// 获取当前登录用户
var userId uint = 0
user, err := utils.GetLoginUser(c, h.db)
if err == nil {
userId = user.Id
apiKey := model.ApiKey{Value: data.Value, UserId: data.UserId, LastUsedAt: utils.Str2stamp(data.LastUsedAt)}
apiKey.Id = data.Id
if apiKey.Id > 0 {
apiKey.CreatedAt = time.Unix(data.CreatedAt, 0)
}
var key = model.ApiKey{Value: data.Key, UserId: userId}
res := h.db.Create(&key)
res := h.db.Save(&apiKey)
if res.Error != nil {
c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "操作失败"})
resp.ERROR(c, "更新数据库失败!")
return
}
resp.SUCCESS(c, key)
var keyVo vo.ApiKey
err := utils.CopyObject(apiKey, &keyVo)
if err != nil {
resp.ERROR(c, "数据拷贝失败!")
return
}
keyVo.Id = apiKey.Id
keyVo.CreatedAt = apiKey.CreatedAt.Unix()
resp.SUCCESS(c, keyVo)
}
func (h *ApiKeyHandler) List(c *gin.Context) {
page := h.GetInt(c, "page", 1)
pageSize := h.GetInt(c, "page_size", 20)
offset := (page - 1) * pageSize
userId := h.GetInt(c, "user_id", -1)
query := h.db.Debug().Session(&gorm.Session{})
if userId >= 0 {
query = query.Where("user_id", userId)
}
var items []model.ApiKey
var keys = make([]vo.ApiKey, 0)
var total int64
h.db.Model(&model.ApiKey{}).Count(&total)
res := h.db.Offset(offset).Limit(pageSize).Find(&items)
res := query.Find(&items)
if res.Error == nil {
for _, item := range items {
var key vo.ApiKey
@@ -71,6 +82,18 @@ func (h *ApiKeyHandler) List(c *gin.Context) {
}
}
}
pageVo := vo.NewPage(total, page, pageSize, keys)
resp.SUCCESS(c, pageVo)
resp.SUCCESS(c, keys)
}
func (h *ApiKeyHandler) Remove(c *gin.Context) {
id := h.GetInt(c, "id", 0)
if id > 0 {
res := h.db.Where("id = ?", id).Delete(&model.ApiKey{})
if res.Error != nil {
resp.ERROR(c, "更新数据库失败!")
return
}
}
resp.SUCCESS(c)
}

View File

@@ -24,8 +24,8 @@ func NewChatRoleHandler(app *core.AppServer, db *gorm.DB) *ChatRoleHandler {
return &h
}
// Update 更新某个聊天角色信息,这里只允许更改名称以及启用和禁用角色操作
func (h *ChatRoleHandler) Update(c *gin.Context) {
// Save 创建或者更新某个角色
func (h *ChatRoleHandler) Save(c *gin.Context) {
var data vo.ChatRole
if err := c.ShouldBindJSON(&data); err != nil {
resp.ERROR(c, types.InvalidArgs)

View File

@@ -128,8 +128,9 @@ func main() {
}),
fx.Invoke(func(s *core.AppServer, h *admin.ApiKeyHandler) {
group := s.Engine.Group("/api/admin/apikey/")
group.POST("add", h.Add)
group.POST("save", h.Save)
group.GET("list", h.List)
group.GET("remove", h.Remove)
}),
fx.Invoke(func(s *core.AppServer, h *admin.UserHandler) {
group := s.Engine.Group("/api/admin/user/")
@@ -141,7 +142,7 @@ func main() {
fx.Invoke(func(s *core.AppServer, h *admin.ChatRoleHandler) {
group := s.Engine.Group("/api/admin/role/")
group.GET("list", h.List)
group.POST("update", h.Update)
group.POST("save", h.Save)
group.POST("sort", h.SetSort)
group.GET("remove", h.Remove)
}),