🎉 初始化项目
This commit is contained in:
6
server/api/v1/system/enter.go
Normal file
6
server/api/v1/system/enter.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package system
|
||||
|
||||
type ApiGroup struct {
|
||||
UserApi UserApi
|
||||
ApiApi ApiApi
|
||||
}
|
||||
149
server/api/v1/system/sys_api.go
Normal file
149
server/api/v1/system/sys_api.go
Normal file
@@ -0,0 +1,149 @@
|
||||
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 ApiApi struct{}
|
||||
|
||||
var apiService = service.ServiceGroupApp.SystemServiceGroup.ApiService
|
||||
|
||||
// CreateApi 创建API
|
||||
// @Tags System
|
||||
// @Summary 创建API
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body request.CreateApiRequest true "API信息"
|
||||
// @Success 200 {object} response.Response{msg=string} "创建成功"
|
||||
// @Router /v1/system/api [post]
|
||||
func (a *ApiApi) CreateApi(c *gin.Context) {
|
||||
var req request.CreateApiRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
err := apiService.CreateApi(&req)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("创建API失败!", zap.Error(err))
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("创建成功", c)
|
||||
}
|
||||
|
||||
// UpdateApi 更新API
|
||||
// @Tags System
|
||||
// @Summary 更新API
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body request.UpdateApiRequest true "API信息"
|
||||
// @Success 200 {object} response.Response{msg=string} "更新成功"
|
||||
// @Router /v1/system/api [put]
|
||||
func (a *ApiApi) UpdateApi(c *gin.Context) {
|
||||
var req request.UpdateApiRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
err := apiService.UpdateApi(&req)
|
||||
if err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("更新成功", c)
|
||||
}
|
||||
|
||||
// DeleteApi 删除API
|
||||
// @Tags System
|
||||
// @Summary 删除API
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param id path uint true "API ID"
|
||||
// @Success 200 {object} response.Response{msg=string} "删除成功"
|
||||
// @Router /v1/system/api/:id [delete]
|
||||
func (a *ApiApi) DeleteApi(c *gin.Context) {
|
||||
id := utils.GetUintParam(c, "id")
|
||||
|
||||
err := apiService.DeleteApi(id)
|
||||
if err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("删除成功", c)
|
||||
}
|
||||
|
||||
// GetApiList 获取API列表
|
||||
// @Tags System
|
||||
// @Summary 获取API列表
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param page query int false "页码"
|
||||
// @Param pageSize query int false "每页数量"
|
||||
// @Param path query string false "API路径"
|
||||
// @Param apiGroup query string false "API分组"
|
||||
// @Param method query string false "请求方法"
|
||||
// @Success 200 {object} response.Response{data=response.PageResult} "获取成功"
|
||||
// @Router /v1/system/api/list [get]
|
||||
func (a *ApiApi) GetApiList(c *gin.Context) {
|
||||
var req request.GetApiListRequest
|
||||
if err := c.ShouldBindQuery(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
if req.Page == 0 {
|
||||
req.Page = 1
|
||||
}
|
||||
if req.PageSize == 0 {
|
||||
req.PageSize = 10
|
||||
}
|
||||
|
||||
list, total, err := apiService.GetApiList(&req)
|
||||
if err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithDetailed(response.PageResult{
|
||||
List: list,
|
||||
Total: total,
|
||||
Page: req.Page,
|
||||
PageSize: req.PageSize,
|
||||
}, "获取成功", c)
|
||||
}
|
||||
|
||||
// GetApiById 根据ID获取API
|
||||
// @Tags System
|
||||
// @Summary 根据ID获取API
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param id path uint true "API ID"
|
||||
// @Success 200 {object} response.Response{data=response.ApiInfo} "获取成功"
|
||||
// @Router /v1/system/api/:id [get]
|
||||
func (a *ApiApi) GetApiById(c *gin.Context) {
|
||||
id := utils.GetUintParam(c, "id")
|
||||
|
||||
info, err := apiService.GetApiById(id)
|
||||
if err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithData(info, c)
|
||||
}
|
||||
199
server/api/v1/system/sys_user.go
Normal file
199
server/api/v1/system/sys_user.go
Normal file
@@ -0,0 +1,199 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user