200 lines
5.3 KiB
Go
200 lines
5.3 KiB
Go
package system
|
|
|
|
import (
|
|
"git.echol.cn/loser/ai_proxy/server/global"
|
|
"git.echol.cn/loser/ai_proxy/server/model/common/response"
|
|
"git.echol.cn/loser/ai_proxy/server/model/system/request"
|
|
"git.echol.cn/loser/ai_proxy/server/service"
|
|
"git.echol.cn/loser/ai_proxy/server/utils"
|
|
"github.com/gin-gonic/gin"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
type UserApi struct{}
|
|
|
|
var userService = service.ServiceGroupApp.SystemServiceGroup.UserService
|
|
|
|
// Login 用户登录
|
|
// @Tags System
|
|
// @Summary 用户登录
|
|
// @accept application/json
|
|
// @Produce application/json
|
|
// @Param data body request.LoginRequest true "用户名, 密码"
|
|
// @Success 200 {object} response.Response{data=response.LoginResponse} "登录成功"
|
|
// @Router /v1/system/user/login [post]
|
|
func (u *UserApi) Login(c *gin.Context) {
|
|
var req request.LoginRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
resp, err := userService.Login(&req)
|
|
if err != nil {
|
|
global.GVA_LOG.Error("登录失败!", zap.Error(err))
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
response.OkWithData(resp, c)
|
|
}
|
|
|
|
// Register 用户注册
|
|
// @Tags System
|
|
// @Summary 用户注册
|
|
// @accept application/json
|
|
// @Produce application/json
|
|
// @Param data body request.RegisterRequest true "用户信息"
|
|
// @Success 200 {object} response.Response{msg=string} "注册成功"
|
|
// @Router /v1/system/user/register [post]
|
|
func (u *UserApi) Register(c *gin.Context) {
|
|
var req request.RegisterRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
_, err := userService.Register(&req)
|
|
if err != nil {
|
|
global.GVA_LOG.Error("注册失败!", zap.Error(err))
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
response.OkWithMessage("注册成功", c)
|
|
}
|
|
|
|
// GetUserInfo 获取用户信息
|
|
// @Tags System
|
|
// @Summary 获取用户信息
|
|
// @Security ApiKeyAuth
|
|
// @accept application/json
|
|
// @Produce application/json
|
|
// @Success 200 {object} response.Response{data=response.UserInfo} "获取成功"
|
|
// @Router /v1/system/user/info [get]
|
|
func (u *UserApi) GetUserInfo(c *gin.Context) {
|
|
userID := utils.GetUserID(c)
|
|
|
|
info, err := userService.GetUserInfo(userID)
|
|
if err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
response.OkWithData(info, c)
|
|
}
|
|
|
|
// GetUserList 获取用户列表
|
|
// @Tags System
|
|
// @Summary 获取用户列表
|
|
// @Security ApiKeyAuth
|
|
// @accept application/json
|
|
// @Produce application/json
|
|
// @Param page query int false "页码"
|
|
// @Param pageSize query int false "每页数量"
|
|
// @Success 200 {object} response.Response{data=response.PageResult} "获取成功"
|
|
// @Router /v1/system/user/list [get]
|
|
func (u *UserApi) GetUserList(c *gin.Context) {
|
|
page := utils.GetIntQuery(c, "page", 1)
|
|
pageSize := utils.GetIntQuery(c, "pageSize", 10)
|
|
|
|
list, total, err := userService.GetUserList(page, pageSize)
|
|
if err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
response.OkWithDetailed(response.PageResult{
|
|
List: list,
|
|
Total: total,
|
|
Page: page,
|
|
PageSize: pageSize,
|
|
}, "获取成功", c)
|
|
}
|
|
|
|
// UpdateUser 更新用户
|
|
// @Tags System
|
|
// @Summary 更新用户
|
|
// @Security ApiKeyAuth
|
|
// @accept application/json
|
|
// @Produce application/json
|
|
// @Param data body request.UpdateUserRequest true "用户信息"
|
|
// @Success 200 {object} response.Response{msg=string} "更新成功"
|
|
// @Router /v1/system/user [put]
|
|
func (u *UserApi) UpdateUser(c *gin.Context) {
|
|
var req request.UpdateUserRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
err := userService.UpdateUser(&req)
|
|
if err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
response.OkWithMessage("更新成功", c)
|
|
}
|
|
|
|
// DeleteUser 删除用户
|
|
// @Tags System
|
|
// @Summary 删除用户
|
|
// @Security ApiKeyAuth
|
|
// @accept application/json
|
|
// @Produce application/json
|
|
// @Param id path uint true "用户ID"
|
|
// @Success 200 {object} response.Response{msg=string} "删除成功"
|
|
// @Router /v1/system/user/:id [delete]
|
|
func (u *UserApi) DeleteUser(c *gin.Context) {
|
|
userID := utils.GetUintParam(c, "id")
|
|
|
|
err := userService.DeleteUser(userID)
|
|
if err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
response.OkWithMessage("删除成功", c)
|
|
}
|
|
|
|
// GetAPIKey 获取API密钥
|
|
// @Tags System
|
|
// @Summary 获取API密钥
|
|
// @Security ApiKeyAuth
|
|
// @accept application/json
|
|
// @Produce application/json
|
|
// @Success 200 {object} response.Response{data=string} "获取成功"
|
|
// @Router /v1/system/user/apikey [get]
|
|
func (u *UserApi) GetAPIKey(c *gin.Context) {
|
|
userID := utils.GetUserID(c)
|
|
|
|
apiKey, err := userService.GetAPIKey(userID)
|
|
if err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
response.OkWithData(gin.H{"apiKey": apiKey}, c)
|
|
}
|
|
|
|
// RegenerateAPIKey 重新生成API密钥
|
|
// @Tags System
|
|
// @Summary 重新生成API密钥
|
|
// @Security ApiKeyAuth
|
|
// @accept application/json
|
|
// @Produce application/json
|
|
// @Success 200 {object} response.Response{data=string} "生成成功"
|
|
// @Router /v1/system/user/apikey/regenerate [post]
|
|
func (u *UserApi) RegenerateAPIKey(c *gin.Context) {
|
|
userID := utils.GetUserID(c)
|
|
|
|
apiKey, err := userService.RegenerateAPIKey(userID)
|
|
if err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
response.OkWithData(gin.H{"apiKey": apiKey}, c)
|
|
}
|