218
server/api/v1/app/ai_config.go
Normal file
218
server/api/v1/app/ai_config.go
Normal file
@@ -0,0 +1,218 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"git.echol.cn/loser/st/server/global"
|
||||
"git.echol.cn/loser/st/server/model/app/request"
|
||||
commonResponse "git.echol.cn/loser/st/server/model/common/response"
|
||||
"git.echol.cn/loser/st/server/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type AIConfigApi struct{}
|
||||
|
||||
// CreateAIConfig
|
||||
// @Tags AppAIConfig
|
||||
// @Summary 创建AI配置
|
||||
// @Produce application/json
|
||||
// @Param data body request.CreateAIConfigRequest true "AI配置信息"
|
||||
// @Success 200 {object} commonResponse.Response{data=response.AIConfigResponse} "创建成功"
|
||||
// @Router /app/ai-config [post]
|
||||
// @Security ApiKeyAuth
|
||||
func (a *AIConfigApi) CreateAIConfig(c *gin.Context) {
|
||||
var req request.CreateAIConfigRequest
|
||||
err := c.ShouldBindJSON(&req)
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := service.ServiceGroupApp.AppServiceGroup.AIConfigService.CreateAIConfig(&req)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("创建AI配置失败", zap.Error(err))
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
commonResponse.OkWithData(resp, c)
|
||||
}
|
||||
|
||||
// GetAIConfigList
|
||||
// @Tags AppAIConfig
|
||||
// @Summary 获取AI配置列表
|
||||
// @Produce application/json
|
||||
// @Success 200 {object} commonResponse.Response{data=response.AIConfigListResponse} "获取成功"
|
||||
// @Router /app/ai-config [get]
|
||||
// @Security ApiKeyAuth
|
||||
func (a *AIConfigApi) GetAIConfigList(c *gin.Context) {
|
||||
resp, err := service.ServiceGroupApp.AppServiceGroup.AIConfigService.GetAIConfigList()
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("获取AI配置列表失败", zap.Error(err))
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
commonResponse.OkWithData(resp, c)
|
||||
}
|
||||
|
||||
// UpdateAIConfig
|
||||
// @Tags AppAIConfig
|
||||
// @Summary 更新AI配置
|
||||
// @Produce application/json
|
||||
// @Param id path int true "配置ID"
|
||||
// @Param data body request.UpdateAIConfigRequest true "AI配置信息"
|
||||
// @Success 200 {object} commonResponse.Response{msg=string} "更新成功"
|
||||
// @Router /app/ai-config/:id [put]
|
||||
// @Security ApiKeyAuth
|
||||
func (a *AIConfigApi) UpdateAIConfig(c *gin.Context) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage("无效的配置ID", c)
|
||||
return
|
||||
}
|
||||
|
||||
var req request.UpdateAIConfigRequest
|
||||
err = c.ShouldBindJSON(&req)
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
err = service.ServiceGroupApp.AppServiceGroup.AIConfigService.UpdateAIConfig(uint(id), &req)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("更新AI配置失败", zap.Error(err))
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
commonResponse.OkWithMessage("更新成功", c)
|
||||
}
|
||||
|
||||
// DeleteAIConfig
|
||||
// @Tags AppAIConfig
|
||||
// @Summary 删除AI配置
|
||||
// @Produce application/json
|
||||
// @Param id path int true "配置ID"
|
||||
// @Success 200 {object} commonResponse.Response{msg=string} "删除成功"
|
||||
// @Router /app/ai-config/:id [delete]
|
||||
// @Security ApiKeyAuth
|
||||
func (a *AIConfigApi) DeleteAIConfig(c *gin.Context) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage("无效的配置ID", c)
|
||||
return
|
||||
}
|
||||
|
||||
err = service.ServiceGroupApp.AppServiceGroup.AIConfigService.DeleteAIConfig(uint(id))
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("删除AI配置失败", zap.Error(err))
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
commonResponse.OkWithMessage("删除成功", c)
|
||||
}
|
||||
|
||||
// GetModels
|
||||
// @Tags AppAIConfig
|
||||
// @Summary 获取可用模型列表
|
||||
// @Produce application/json
|
||||
// @Param data body request.GetModelsRequest true "获取模型请求"
|
||||
// @Success 200 {object} commonResponse.Response{data=response.GetModelsResponse} "获取成功"
|
||||
// @Router /app/ai-config/models [post]
|
||||
// @Security ApiKeyAuth
|
||||
func (a *AIConfigApi) GetModels(c *gin.Context) {
|
||||
var req request.GetModelsRequest
|
||||
err := c.ShouldBindJSON(&req)
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := service.ServiceGroupApp.AppServiceGroup.AIConfigService.GetModels(&req)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("获取模型列表失败", zap.Error(err))
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
commonResponse.OkWithData(resp, c)
|
||||
}
|
||||
|
||||
// GetModelsByID
|
||||
// @Tags AppAIConfig
|
||||
// @Summary 通过配置ID获取可用模型列表
|
||||
// @Produce application/json
|
||||
// @Param id path int true "配置ID"
|
||||
// @Success 200 {object} commonResponse.Response{data=response.GetModelsResponse} "获取成功"
|
||||
// @Router /app/ai-config/:id/models [get]
|
||||
// @Security ApiKeyAuth
|
||||
func (a *AIConfigApi) GetModelsByID(c *gin.Context) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage("无效的配置ID", c)
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := service.ServiceGroupApp.AppServiceGroup.AIConfigService.GetModelsByID(uint(id))
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("获取模型列表失败", zap.Error(err))
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
commonResponse.OkWithData(resp, c)
|
||||
}
|
||||
|
||||
// TestAIConfig
|
||||
// @Tags AppAIConfig
|
||||
// @Summary 测试AI配置
|
||||
// @Produce application/json
|
||||
// @Param data body request.TestAIConfigRequest true "测试配置请求"
|
||||
// @Success 200 {object} commonResponse.Response{data=response.TestAIConfigResponse} "测试完成"
|
||||
// @Router /app/ai-config/test [post]
|
||||
// @Security ApiKeyAuth
|
||||
func (a *AIConfigApi) TestAIConfig(c *gin.Context) {
|
||||
var req request.TestAIConfigRequest
|
||||
err := c.ShouldBindJSON(&req)
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := service.ServiceGroupApp.AppServiceGroup.AIConfigService.TestAIConfig(&req)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("测试AI配置失败", zap.Error(err))
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
commonResponse.OkWithData(resp, c)
|
||||
}
|
||||
|
||||
// TestAIConfigByID
|
||||
// @Tags AppAIConfig
|
||||
// @Summary 通过ID测试AI配置
|
||||
// @Produce application/json
|
||||
// @Param id path int true "配置ID"
|
||||
// @Success 200 {object} commonResponse.Response{data=response.TestAIConfigResponse} "测试完成"
|
||||
// @Router /app/ai-config/:id/test [post]
|
||||
// @Security ApiKeyAuth
|
||||
func (a *AIConfigApi) TestAIConfigByID(c *gin.Context) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage("无效的配置ID", c)
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := service.ServiceGroupApp.AppServiceGroup.AIConfigService.TestAIConfigByID(uint(id))
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("测试AI配置失败", zap.Error(err))
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
commonResponse.OkWithData(resp, c)
|
||||
}
|
||||
189
server/api/v1/app/auth.go
Normal file
189
server/api/v1/app/auth.go
Normal file
@@ -0,0 +1,189 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"git.echol.cn/loser/st/server/global"
|
||||
"git.echol.cn/loser/st/server/model/app/request"
|
||||
"git.echol.cn/loser/st/server/model/common"
|
||||
commonResponse "git.echol.cn/loser/st/server/model/common/response"
|
||||
"git.echol.cn/loser/st/server/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type AuthApi struct{}
|
||||
|
||||
// Register
|
||||
// @Tags AppAuth
|
||||
// @Summary 用户注册
|
||||
// @Produce application/json
|
||||
// @Param data body request.RegisterRequest true "用户注册信息"
|
||||
// @Success 200 {object} commonResponse.Response{msg=string} "注册成功"
|
||||
// @Router /app/auth/register [post]
|
||||
func (a *AuthApi) Register(c *gin.Context) {
|
||||
var req request.RegisterRequest
|
||||
err := c.ShouldBindJSON(&req)
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
err = service.ServiceGroupApp.AppServiceGroup.AuthService.Register(&req)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("注册失败", zap.Error(err))
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
commonResponse.OkWithMessage("注册成功", c)
|
||||
}
|
||||
|
||||
// Login
|
||||
// @Tags AppAuth
|
||||
// @Summary 用户登录
|
||||
// @Produce application/json
|
||||
// @Param data body request.LoginRequest true "用户登录信息"
|
||||
// @Success 200 {object} commonResponse.Response{data=response.LoginResponse} "登录成功"
|
||||
// @Router /app/auth/login [post]
|
||||
func (a *AuthApi) Login(c *gin.Context) {
|
||||
var req request.LoginRequest
|
||||
err := c.ShouldBindJSON(&req)
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
ip := c.ClientIP()
|
||||
resp, err := service.ServiceGroupApp.AppServiceGroup.AuthService.Login(&req, ip)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("登录失败", zap.Error(err))
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
commonResponse.OkWithData(resp, c)
|
||||
}
|
||||
|
||||
// RefreshToken
|
||||
// @Tags AppAuth
|
||||
// @Summary 刷新Token
|
||||
// @Produce application/json
|
||||
// @Param data body request.RefreshTokenRequest true "刷新Token"
|
||||
// @Success 200 {object} commonResponse.Response{data=response.LoginResponse} "刷新成功"
|
||||
// @Router /app/auth/refresh [post]
|
||||
func (a *AuthApi) RefreshToken(c *gin.Context) {
|
||||
var req request.RefreshTokenRequest
|
||||
err := c.ShouldBindJSON(&req)
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := service.ServiceGroupApp.AppServiceGroup.AuthService.RefreshToken(&req)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("刷新Token失败", zap.Error(err))
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
commonResponse.OkWithData(resp, c)
|
||||
}
|
||||
|
||||
// Logout
|
||||
// @Tags AppAuth
|
||||
// @Summary 用户登出
|
||||
// @Produce application/json
|
||||
// @Success 200 {object} commonResponse.Response{msg=string} "登出成功"
|
||||
// @Router /app/auth/logout [post]
|
||||
// @Security ApiKeyAuth
|
||||
func (a *AuthApi) Logout(c *gin.Context) {
|
||||
userID := common.GetAppUserID(c)
|
||||
token := c.GetHeader("Authorization")
|
||||
if len(token) > 7 {
|
||||
token = token[7:] // 移除 "Bearer " 前缀
|
||||
}
|
||||
|
||||
err := service.ServiceGroupApp.AppServiceGroup.AuthService.Logout(userID, token)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("登出失败", zap.Error(err))
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
commonResponse.OkWithMessage("登出成功", c)
|
||||
}
|
||||
|
||||
// GetUserInfo
|
||||
// @Tags AppAuth
|
||||
// @Summary 获取用户信息
|
||||
// @Produce application/json
|
||||
// @Success 200 {object} commonResponse.Response{data=response.AppUserResponse} "获取成功"
|
||||
// @Router /app/auth/userinfo [get]
|
||||
// @Security ApiKeyAuth
|
||||
func (a *AuthApi) GetUserInfo(c *gin.Context) {
|
||||
userID := common.GetAppUserID(c)
|
||||
|
||||
resp, err := service.ServiceGroupApp.AppServiceGroup.AuthService.GetUserInfo(userID)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("获取用户信息失败", zap.Error(err))
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
commonResponse.OkWithData(resp, c)
|
||||
}
|
||||
|
||||
// UpdateProfile
|
||||
// @Tags AppAuth
|
||||
// @Summary 更新用户信息
|
||||
// @Produce application/json
|
||||
// @Param data body request.UpdateProfileRequest true "用户信息"
|
||||
// @Success 200 {object} commonResponse.Response{msg=string} "更新成功"
|
||||
// @Router /app/user/profile [put]
|
||||
// @Security ApiKeyAuth
|
||||
func (a *AuthApi) UpdateProfile(c *gin.Context) {
|
||||
userID := common.GetAppUserID(c)
|
||||
|
||||
var req request.UpdateProfileRequest
|
||||
err := c.ShouldBindJSON(&req)
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
err = service.ServiceGroupApp.AppServiceGroup.AuthService.UpdateProfile(userID, &req)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("更新用户信息失败", zap.Error(err))
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
commonResponse.OkWithMessage("更新成功", c)
|
||||
}
|
||||
|
||||
// ChangePassword
|
||||
// @Tags AppAuth
|
||||
// @Summary 修改密码
|
||||
// @Produce application/json
|
||||
// @Param data body request.ChangePasswordRequest true "密码信息"
|
||||
// @Success 200 {object} commonResponse.Response{msg=string} "修改成功"
|
||||
// @Router /app/user/change-password [post]
|
||||
// @Security ApiKeyAuth
|
||||
func (a *AuthApi) ChangePassword(c *gin.Context) {
|
||||
userID := common.GetAppUserID(c)
|
||||
|
||||
var req request.ChangePasswordRequest
|
||||
err := c.ShouldBindJSON(&req)
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
err = service.ServiceGroupApp.AppServiceGroup.AuthService.ChangePassword(userID, &req)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("修改密码失败", zap.Error(err))
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
commonResponse.OkWithMessage("修改成功", c)
|
||||
}
|
||||
246
server/api/v1/app/character.go
Normal file
246
server/api/v1/app/character.go
Normal file
@@ -0,0 +1,246 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
|
||||
"git.echol.cn/loser/st/server/global"
|
||||
"git.echol.cn/loser/st/server/model/app/request"
|
||||
"git.echol.cn/loser/st/server/model/common"
|
||||
commonResponse "git.echol.cn/loser/st/server/model/common/response"
|
||||
"git.echol.cn/loser/st/server/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type CharacterApi struct{}
|
||||
|
||||
// CreateCharacter
|
||||
// @Tags AppCharacter
|
||||
// @Summary 创建角色卡
|
||||
// @Produce application/json
|
||||
// @Param data body request.CreateCharacterRequest true "角色卡信息"
|
||||
// @Success 200 {object} commonResponse.Response{data=response.CharacterResponse} "创建成功"
|
||||
// @Router /app/character [post]
|
||||
// @Security ApiKeyAuth
|
||||
func (a *CharacterApi) CreateCharacter(c *gin.Context) {
|
||||
userID := common.GetAppUserID(c)
|
||||
|
||||
var req request.CreateCharacterRequest
|
||||
err := c.ShouldBindJSON(&req)
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := service.ServiceGroupApp.AppServiceGroup.CharacterService.CreateCharacter(userID, &req)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("创建角色卡失败", zap.Error(err))
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
commonResponse.OkWithData(resp, c)
|
||||
}
|
||||
|
||||
// GetCharacterList
|
||||
// @Tags AppCharacter
|
||||
// @Summary 获取角色卡列表
|
||||
// @Produce application/json
|
||||
// @Param page query int false "页码"
|
||||
// @Param pageSize query int false "每页数量"
|
||||
// @Param keyword query string false "关键词"
|
||||
// @Param tag query string false "标签"
|
||||
// @Param isPublic query bool false "是否公开"
|
||||
// @Success 200 {object} commonResponse.Response{data=response.CharacterListResponse} "获取成功"
|
||||
// @Router /app/character [get]
|
||||
// @Security ApiKeyAuth
|
||||
func (a *CharacterApi) GetCharacterList(c *gin.Context) {
|
||||
userID := common.GetAppUserID(c)
|
||||
|
||||
var req request.GetCharacterListRequest
|
||||
req.Page, _ = strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
req.PageSize, _ = strconv.Atoi(c.DefaultQuery("pageSize", "20"))
|
||||
req.Keyword = c.Query("keyword")
|
||||
req.Tag = c.Query("tag")
|
||||
|
||||
if isPublicStr := c.Query("isPublic"); isPublicStr != "" {
|
||||
isPublic := isPublicStr == "true"
|
||||
req.IsPublic = &isPublic
|
||||
}
|
||||
|
||||
// 参数验证
|
||||
if req.Page < 1 {
|
||||
req.Page = 1
|
||||
}
|
||||
if req.PageSize < 1 || req.PageSize > 100 {
|
||||
req.PageSize = 20
|
||||
}
|
||||
|
||||
resp, err := service.ServiceGroupApp.AppServiceGroup.CharacterService.GetCharacterList(userID, &req)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("获取角色卡列表失败", zap.Error(err))
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
commonResponse.OkWithData(resp, c)
|
||||
}
|
||||
|
||||
// GetCharacterByID
|
||||
// @Tags AppCharacter
|
||||
// @Summary 获取角色卡详情
|
||||
// @Produce application/json
|
||||
// @Param id path int true "角色卡ID"
|
||||
// @Success 200 {object} commonResponse.Response{data=response.CharacterResponse} "获取成功"
|
||||
// @Router /app/character/:id [get]
|
||||
// @Security ApiKeyAuth
|
||||
func (a *CharacterApi) GetCharacterByID(c *gin.Context) {
|
||||
userID := common.GetAppUserID(c)
|
||||
characterID, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage("无效的角色卡ID", c)
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := service.ServiceGroupApp.AppServiceGroup.CharacterService.GetCharacterByID(userID, uint(characterID))
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("获取角色卡详情失败", zap.Error(err))
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
commonResponse.OkWithData(resp, c)
|
||||
}
|
||||
|
||||
// UpdateCharacter
|
||||
// @Tags AppCharacter
|
||||
// @Summary 更新角色卡
|
||||
// @Produce application/json
|
||||
// @Param id path int true "角色卡ID"
|
||||
// @Param data body request.UpdateCharacterRequest true "角色卡信息"
|
||||
// @Success 200 {object} commonResponse.Response{msg=string} "更新成功"
|
||||
// @Router /app/character/:id [put]
|
||||
// @Security ApiKeyAuth
|
||||
func (a *CharacterApi) UpdateCharacter(c *gin.Context) {
|
||||
userID := common.GetAppUserID(c)
|
||||
characterID, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage("无效的角色卡ID", c)
|
||||
return
|
||||
}
|
||||
|
||||
var req request.UpdateCharacterRequest
|
||||
err = c.ShouldBindJSON(&req)
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
err = service.ServiceGroupApp.AppServiceGroup.CharacterService.UpdateCharacter(userID, uint(characterID), &req)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("更新角色卡失败", zap.Error(err))
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
commonResponse.OkWithMessage("更新成功", c)
|
||||
}
|
||||
|
||||
// DeleteCharacter
|
||||
// @Tags AppCharacter
|
||||
// @Summary 删除角色卡
|
||||
// @Produce application/json
|
||||
// @Param id path int true "角色卡ID"
|
||||
// @Success 200 {object} commonResponse.Response{msg=string} "删除成功"
|
||||
// @Router /app/character/:id [delete]
|
||||
// @Security ApiKeyAuth
|
||||
func (a *CharacterApi) DeleteCharacter(c *gin.Context) {
|
||||
userID := common.GetAppUserID(c)
|
||||
characterID, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage("无效的角色卡ID", c)
|
||||
return
|
||||
}
|
||||
|
||||
err = service.ServiceGroupApp.AppServiceGroup.CharacterService.DeleteCharacter(userID, uint(characterID))
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("删除角色卡失败", zap.Error(err))
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
commonResponse.OkWithMessage("删除成功", c)
|
||||
}
|
||||
|
||||
// UploadCharacter
|
||||
// @Tags AppCharacter
|
||||
// @Summary 上传角色卡文件(PNG/JSON)
|
||||
// @Accept multipart/form-data
|
||||
// @Produce application/json
|
||||
// @Param file formData file true "角色卡文件"
|
||||
// @Success 200 {object} commonResponse.Response{data=response.CharacterResponse} "上传成功"
|
||||
// @Router /app/character/upload [post]
|
||||
// @Security ApiKeyAuth
|
||||
func (a *CharacterApi) UploadCharacter(c *gin.Context) {
|
||||
userID := common.GetAppUserID(c)
|
||||
|
||||
file, err := c.FormFile("file")
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage("请选择文件", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 根据文件类型选择导入方式
|
||||
contentType := file.Header.Get("Content-Type")
|
||||
var resp interface{}
|
||||
|
||||
if contentType == "image/png" || contentType == "image/x-png" {
|
||||
resp, err = service.ServiceGroupApp.AppServiceGroup.CharacterService.ImportCharacterFromPNG(userID, file)
|
||||
} else if contentType == "application/json" {
|
||||
resp, err = service.ServiceGroupApp.AppServiceGroup.CharacterService.ImportCharacterFromJSON(userID, file)
|
||||
} else {
|
||||
commonResponse.FailWithMessage("不支持的文件类型,仅支持 PNG 和 JSON", c)
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("上传角色卡失败", zap.Error(err))
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
commonResponse.OkWithData(resp, c)
|
||||
}
|
||||
|
||||
// ExportCharacter
|
||||
// @Tags AppCharacter
|
||||
// @Summary 导出角色卡为 JSON
|
||||
// @Produce application/json
|
||||
// @Param id path int true "角色卡ID"
|
||||
// @Success 200 {object} utils.CharacterCardV2 "导出成功"
|
||||
// @Router /app/character/:id/export [get]
|
||||
// @Security ApiKeyAuth
|
||||
func (a *CharacterApi) ExportCharacter(c *gin.Context) {
|
||||
userID := common.GetAppUserID(c)
|
||||
characterID, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage("无效的角色卡ID", c)
|
||||
return
|
||||
}
|
||||
|
||||
card, err := service.ServiceGroupApp.AppServiceGroup.CharacterService.ExportCharacterToJSON(userID, uint(characterID))
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("导出角色卡失败", zap.Error(err))
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 设置下载响应头
|
||||
c.Header("Content-Type", "application/json")
|
||||
c.Header("Content-Disposition", "attachment; filename=character_"+c.Param("id")+".json")
|
||||
|
||||
// 直接返回 JSON
|
||||
jsonData, _ := json.MarshalIndent(card, "", " ")
|
||||
c.Data(200, "application/json", jsonData)
|
||||
}
|
||||
301
server/api/v1/app/conversation.go
Normal file
301
server/api/v1/app/conversation.go
Normal file
@@ -0,0 +1,301 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"git.echol.cn/loser/st/server/global"
|
||||
"git.echol.cn/loser/st/server/model/app/request"
|
||||
"git.echol.cn/loser/st/server/model/common"
|
||||
commonResponse "git.echol.cn/loser/st/server/model/common/response"
|
||||
"git.echol.cn/loser/st/server/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type ConversationApi struct{}
|
||||
|
||||
// CreateConversation
|
||||
// @Tags AppConversation
|
||||
// @Summary 创建对话
|
||||
// @Produce application/json
|
||||
// @Param data body request.CreateConversationRequest true "对话信息"
|
||||
// @Success 200 {object} commonResponse.Response{data=response.ConversationResponse} "创建成功"
|
||||
// @Router /app/conversation [post]
|
||||
// @Security ApiKeyAuth
|
||||
func (a *ConversationApi) CreateConversation(c *gin.Context) {
|
||||
userID := common.GetAppUserID(c)
|
||||
|
||||
var req request.CreateConversationRequest
|
||||
err := c.ShouldBindJSON(&req)
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := service.ServiceGroupApp.AppServiceGroup.ConversationService.CreateConversation(userID, &req)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("创建对话失败", zap.Error(err))
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
commonResponse.OkWithData(resp, c)
|
||||
}
|
||||
|
||||
// GetConversationList
|
||||
// @Tags AppConversation
|
||||
// @Summary 获取对话列表
|
||||
// @Produce application/json
|
||||
// @Param page query int false "页码"
|
||||
// @Param pageSize query int false "每页数量"
|
||||
// @Success 200 {object} commonResponse.Response{data=response.ConversationListResponse} "获取成功"
|
||||
// @Router /app/conversation [get]
|
||||
// @Security ApiKeyAuth
|
||||
func (a *ConversationApi) GetConversationList(c *gin.Context) {
|
||||
userID := common.GetAppUserID(c)
|
||||
|
||||
var req request.GetConversationListRequest
|
||||
req.Page, _ = strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
req.PageSize, _ = strconv.Atoi(c.DefaultQuery("pageSize", "20"))
|
||||
|
||||
if req.Page < 1 {
|
||||
req.Page = 1
|
||||
}
|
||||
if req.PageSize < 1 || req.PageSize > 100 {
|
||||
req.PageSize = 20
|
||||
}
|
||||
|
||||
resp, err := service.ServiceGroupApp.AppServiceGroup.ConversationService.GetConversationList(userID, &req)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("获取对话列表失败", zap.Error(err))
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
commonResponse.OkWithData(resp, c)
|
||||
}
|
||||
|
||||
// GetConversationByID
|
||||
// @Tags AppConversation
|
||||
// @Summary 获取对话详情
|
||||
// @Produce application/json
|
||||
// @Param id path int true "对话ID"
|
||||
// @Success 200 {object} commonResponse.Response{data=response.ConversationResponse} "获取成功"
|
||||
// @Router /app/conversation/:id [get]
|
||||
// @Security ApiKeyAuth
|
||||
func (a *ConversationApi) GetConversationByID(c *gin.Context) {
|
||||
userID := common.GetAppUserID(c)
|
||||
conversationID, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage("无效的对话ID", c)
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := service.ServiceGroupApp.AppServiceGroup.ConversationService.GetConversationByID(userID, uint(conversationID))
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("获取对话详情失败", zap.Error(err))
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
commonResponse.OkWithData(resp, c)
|
||||
}
|
||||
|
||||
// UpdateConversationSettings
|
||||
// @Tags AppConversation
|
||||
// @Summary 更新对话设置
|
||||
// @Produce application/json
|
||||
// @Param id path int true "对话ID"
|
||||
// @Param data body request.UpdateConversationSettingsRequest true "设置信息"
|
||||
// @Success 200 {object} commonResponse.Response{msg=string} "更新成功"
|
||||
// @Router /app/conversation/:id/settings [put]
|
||||
// @Security ApiKeyAuth
|
||||
func (a *ConversationApi) UpdateConversationSettings(c *gin.Context) {
|
||||
userID := common.GetAppUserID(c)
|
||||
conversationID, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage("无效的对话ID", c)
|
||||
return
|
||||
}
|
||||
|
||||
var req request.UpdateConversationSettingsRequest
|
||||
err = c.ShouldBindJSON(&req)
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
err = service.ServiceGroupApp.AppServiceGroup.ConversationService.UpdateConversationSettings(userID, uint(conversationID), req.Settings)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("更新对话设置失败", zap.Error(err))
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
commonResponse.OkWithMessage("更新成功", c)
|
||||
}
|
||||
|
||||
// DeleteConversation
|
||||
// @Tags AppConversation
|
||||
// @Summary 删除对话
|
||||
// @Produce application/json
|
||||
// @Param id path int true "对话ID"
|
||||
// @Success 200 {object} commonResponse.Response{msg=string} "删除成功"
|
||||
// @Router /app/conversation/:id [delete]
|
||||
// @Security ApiKeyAuth
|
||||
func (a *ConversationApi) DeleteConversation(c *gin.Context) {
|
||||
userID := common.GetAppUserID(c)
|
||||
conversationID, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage("无效的对话ID", c)
|
||||
return
|
||||
}
|
||||
|
||||
err = service.ServiceGroupApp.AppServiceGroup.ConversationService.DeleteConversation(userID, uint(conversationID))
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("删除对话失败", zap.Error(err))
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
commonResponse.OkWithMessage("删除成功", c)
|
||||
}
|
||||
|
||||
// GetMessageList
|
||||
// @Tags AppConversation
|
||||
// @Summary 获取消息列表
|
||||
// @Produce application/json
|
||||
// @Param id path int true "对话ID"
|
||||
// @Param page query int false "页码"
|
||||
// @Param pageSize query int false "每页数量"
|
||||
// @Success 200 {object} commonResponse.Response{data=response.MessageListResponse} "获取成功"
|
||||
// @Router /app/conversation/:id/messages [get]
|
||||
// @Security ApiKeyAuth
|
||||
func (a *ConversationApi) GetMessageList(c *gin.Context) {
|
||||
userID := common.GetAppUserID(c)
|
||||
conversationID, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage("无效的对话ID", c)
|
||||
return
|
||||
}
|
||||
|
||||
var req request.GetMessageListRequest
|
||||
req.Page, _ = strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
req.PageSize, _ = strconv.Atoi(c.DefaultQuery("pageSize", "50"))
|
||||
|
||||
if req.Page < 1 {
|
||||
req.Page = 1
|
||||
}
|
||||
if req.PageSize < 1 || req.PageSize > 100 {
|
||||
req.PageSize = 50
|
||||
}
|
||||
|
||||
resp, err := service.ServiceGroupApp.AppServiceGroup.ConversationService.GetMessageList(userID, uint(conversationID), &req)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("获取消息列表失败", zap.Error(err))
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
commonResponse.OkWithData(resp, c)
|
||||
}
|
||||
|
||||
// SendMessage
|
||||
// @Tags AppConversation
|
||||
// @Summary 发送消息
|
||||
// @Produce application/json
|
||||
// @Param id path int true "对话ID"
|
||||
// @Param data body request.SendMessageRequest true "消息内容"
|
||||
// @Success 200 {object} commonResponse.Response{data=response.MessageResponse} "发送成功"
|
||||
// @Router /app/conversation/:id/message [post]
|
||||
// @Security ApiKeyAuth
|
||||
func (a *ConversationApi) SendMessage(c *gin.Context) {
|
||||
userID := common.GetAppUserID(c)
|
||||
conversationID, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage("无效的对话ID", c)
|
||||
return
|
||||
}
|
||||
|
||||
var req request.SendMessageRequest
|
||||
err = c.ShouldBindJSON(&req)
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否启用流式传输
|
||||
stream := c.Query("stream") == "true"
|
||||
|
||||
if stream {
|
||||
// 流式传输
|
||||
a.SendMessageStream(c, userID, uint(conversationID), &req)
|
||||
} else {
|
||||
// 普通传输
|
||||
resp, err := service.ServiceGroupApp.AppServiceGroup.ConversationService.SendMessage(userID, uint(conversationID), &req)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("发送消息失败", zap.Error(err))
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
commonResponse.OkWithData(resp, c)
|
||||
}
|
||||
}
|
||||
|
||||
// SendMessageStream 流式传输消息
|
||||
func (a *ConversationApi) SendMessageStream(c *gin.Context, userID, conversationID uint, req *request.SendMessageRequest) {
|
||||
// 设置SSE响应头
|
||||
c.Header("Content-Type", "text/event-stream")
|
||||
c.Header("Cache-Control", "no-cache")
|
||||
c.Header("Connection", "keep-alive")
|
||||
c.Header("X-Accel-Buffering", "no")
|
||||
|
||||
// 创建流式传输通道
|
||||
streamChan := make(chan string, 100)
|
||||
errorChan := make(chan error, 1)
|
||||
doneChan := make(chan bool, 1)
|
||||
|
||||
// 启动流式传输
|
||||
go func() {
|
||||
err := service.ServiceGroupApp.AppServiceGroup.ConversationService.SendMessageStream(
|
||||
userID, conversationID, req, streamChan, doneChan,
|
||||
)
|
||||
if err != nil {
|
||||
errorChan <- err
|
||||
}
|
||||
}()
|
||||
|
||||
// 发送流式数据
|
||||
flusher, ok := c.Writer.(http.Flusher)
|
||||
if !ok {
|
||||
commonResponse.FailWithMessage("不支持流式传输", c)
|
||||
return
|
||||
}
|
||||
|
||||
for {
|
||||
select {
|
||||
case chunk := <-streamChan:
|
||||
// 手动写入 SSE 格式,避免 Gin 的 SSEvent 进行 JSON 序列化
|
||||
c.Writer.Write([]byte("event: message\n"))
|
||||
c.Writer.Write([]byte(fmt.Sprintf("data: %s\n\n", chunk)))
|
||||
flusher.Flush()
|
||||
case err := <-errorChan:
|
||||
// 发送错误
|
||||
c.Writer.Write([]byte("event: error\n"))
|
||||
c.Writer.Write([]byte(fmt.Sprintf("data: %s\n\n", err.Error())))
|
||||
flusher.Flush()
|
||||
return
|
||||
case <-doneChan:
|
||||
// 发送完成信号
|
||||
c.Writer.Write([]byte("event: done\n"))
|
||||
c.Writer.Write([]byte("data: \n\n"))
|
||||
flusher.Flush()
|
||||
return
|
||||
case <-c.Request.Context().Done():
|
||||
// 客户端断开连接
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
10
server/api/v1/app/enter.go
Normal file
10
server/api/v1/app/enter.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package app
|
||||
|
||||
type ApiGroup struct {
|
||||
AuthApi
|
||||
CharacterApi
|
||||
ConversationApi
|
||||
AIConfigApi
|
||||
PresetApi
|
||||
UploadApi
|
||||
}
|
||||
265
server/api/v1/app/preset.go
Normal file
265
server/api/v1/app/preset.go
Normal file
@@ -0,0 +1,265 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"io"
|
||||
"strconv"
|
||||
|
||||
"git.echol.cn/loser/st/server/model/app/request"
|
||||
"git.echol.cn/loser/st/server/model/app/response"
|
||||
"git.echol.cn/loser/st/server/model/common"
|
||||
commonResponse "git.echol.cn/loser/st/server/model/common/response"
|
||||
"git.echol.cn/loser/st/server/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type PresetApi struct{}
|
||||
|
||||
// CreatePreset 创建预设
|
||||
// @Summary 创建预设
|
||||
// @Tags Preset
|
||||
// @Accept JSON
|
||||
// @Produce JSON
|
||||
// @Param data body request.CreatePresetRequest true "预设信息"
|
||||
// @Success 200 {object} response.PresetResponse
|
||||
// @Router /app/preset [post]
|
||||
func (a *PresetApi) CreatePreset(c *gin.Context) {
|
||||
var req request.CreatePresetRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
userID := common.GetAppUserID(c)
|
||||
preset, err := service.ServiceGroupApp.AppServiceGroup.PresetService.CreatePreset(userID, &req)
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
resp := response.ToPresetResponse(preset)
|
||||
commonResponse.OkWithData(resp, c)
|
||||
}
|
||||
|
||||
// GetPresetList 获取预设列表
|
||||
// @Summary 获取预设列表
|
||||
// @Tags Preset
|
||||
// @Accept JSON
|
||||
// @Produce JSON
|
||||
// @Param page query int false "页码"
|
||||
// @Param pageSize query int false "每页数量"
|
||||
// @Param keyword query string false "关键词"
|
||||
// @Param isPublic query bool false "是否公开"
|
||||
// @Success 200 {object} response.PresetListResponse
|
||||
// @Router /app/preset [get]
|
||||
func (a *PresetApi) GetPresetList(c *gin.Context) {
|
||||
var req request.GetPresetListRequest
|
||||
if err := c.ShouldBindQuery(&req); err != nil {
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 设置默认值
|
||||
if req.Page == 0 {
|
||||
req.Page = 1
|
||||
}
|
||||
if req.PageSize == 0 {
|
||||
req.PageSize = 20
|
||||
}
|
||||
|
||||
userID := common.GetAppUserID(c)
|
||||
presets, total, err := service.ServiceGroupApp.AppServiceGroup.PresetService.GetPresetList(userID, &req)
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 转换为响应格式
|
||||
list := make([]response.PresetResponse, 0, len(presets))
|
||||
for _, preset := range presets {
|
||||
list = append(list, response.ToPresetResponse(&preset))
|
||||
}
|
||||
|
||||
resp := response.PresetListResponse{
|
||||
List: list,
|
||||
Total: total,
|
||||
Page: req.Page,
|
||||
PageSize: req.PageSize,
|
||||
}
|
||||
|
||||
commonResponse.OkWithData(resp, c)
|
||||
}
|
||||
|
||||
// GetPresetByID 根据ID获取预设
|
||||
// @Summary 根据ID获取预设
|
||||
// @Tags Preset
|
||||
// @Accept JSON
|
||||
// @Produce JSON
|
||||
// @Param id path int true "预设ID"
|
||||
// @Success 200 {object} response.PresetResponse
|
||||
// @Router /app/preset/:id [get]
|
||||
func (a *PresetApi) GetPresetByID(c *gin.Context) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage("无效的预设ID", c)
|
||||
return
|
||||
}
|
||||
|
||||
userID := common.GetAppUserID(c)
|
||||
preset, err := service.ServiceGroupApp.AppServiceGroup.PresetService.GetPresetByID(userID, uint(id))
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
resp := response.ToPresetResponse(preset)
|
||||
commonResponse.OkWithData(resp, c)
|
||||
}
|
||||
|
||||
// UpdatePreset 更新预设
|
||||
// @Summary 更新预设
|
||||
// @Tags Preset
|
||||
// @Accept JSON
|
||||
// @Produce JSON
|
||||
// @Param id path int true "预设ID"
|
||||
// @Param data body request.UpdatePresetRequest true "预设信息"
|
||||
// @Success 200 {string} string "更新成功"
|
||||
// @Router /app/preset/:id [put]
|
||||
func (a *PresetApi) UpdatePreset(c *gin.Context) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage("无效的预设ID", c)
|
||||
return
|
||||
}
|
||||
|
||||
var req request.UpdatePresetRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
userID := common.GetAppUserID(c)
|
||||
if err := service.ServiceGroupApp.AppServiceGroup.PresetService.UpdatePreset(userID, uint(id), &req); err != nil {
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
commonResponse.OkWithMessage("更新成功", c)
|
||||
}
|
||||
|
||||
// DeletePreset 删除预设
|
||||
// @Summary 删除预设
|
||||
// @Tags Preset
|
||||
// @Accept JSON
|
||||
// @Produce JSON
|
||||
// @Param id path int true "预设ID"
|
||||
// @Success 200 {string} string "删除成功"
|
||||
// @Router /app/preset/:id [delete]
|
||||
func (a *PresetApi) DeletePreset(c *gin.Context) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage("无效的预设ID", c)
|
||||
return
|
||||
}
|
||||
|
||||
userID := common.GetAppUserID(c)
|
||||
if err := service.ServiceGroupApp.AppServiceGroup.PresetService.DeletePreset(userID, uint(id)); err != nil {
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
commonResponse.OkWithMessage("删除成功", c)
|
||||
}
|
||||
|
||||
// SetDefaultPreset 设置默认预设
|
||||
// @Summary 设置默认预设
|
||||
// @Tags Preset
|
||||
// @Accept JSON
|
||||
// @Produce JSON
|
||||
// @Param id path int true "预设ID"
|
||||
// @Success 200 {string} string "设置成功"
|
||||
// @Router /app/preset/:id/default [post]
|
||||
func (a *PresetApi) SetDefaultPreset(c *gin.Context) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage("无效的预设ID", c)
|
||||
return
|
||||
}
|
||||
|
||||
userID := common.GetAppUserID(c)
|
||||
if err := service.ServiceGroupApp.AppServiceGroup.PresetService.SetDefaultPreset(userID, uint(id)); err != nil {
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
commonResponse.OkWithMessage("设置成功", c)
|
||||
}
|
||||
|
||||
// ImportPreset 导入预设
|
||||
// @Summary 导入预设
|
||||
// @Tags Preset
|
||||
// @Accept multipart/form-data
|
||||
// @Produce JSON
|
||||
// @Param file formData file true "预设JSON文件"
|
||||
// @Success 200 {object} response.PresetResponse
|
||||
// @Router /app/preset/import [post]
|
||||
func (a *PresetApi) ImportPreset(c *gin.Context) {
|
||||
file, err := c.FormFile("file")
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage("请上传文件", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 读取文件内容
|
||||
f, err := file.Open()
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage("读取文件失败", c)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
jsonData, err := io.ReadAll(f)
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage("读取文件内容失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
userID := common.GetAppUserID(c)
|
||||
preset, err := service.ServiceGroupApp.AppServiceGroup.PresetService.ImportPresetFromJSON(userID, jsonData, file.Filename)
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
resp := response.ToPresetResponse(preset)
|
||||
commonResponse.OkWithData(resp, c)
|
||||
}
|
||||
|
||||
// ExportPreset 导出预设
|
||||
// @Summary 导出预设
|
||||
// @Tags Preset
|
||||
// @Accept JSON
|
||||
// @Produce JSON
|
||||
// @Param id path int true "预设ID"
|
||||
// @Success 200 {file} file "预设JSON文件"
|
||||
// @Router /app/preset/:id/export [get]
|
||||
func (a *PresetApi) ExportPreset(c *gin.Context) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage("无效的预设ID", c)
|
||||
return
|
||||
}
|
||||
|
||||
userID := common.GetAppUserID(c)
|
||||
jsonData, err := service.ServiceGroupApp.AppServiceGroup.PresetService.ExportPresetToJSON(userID, uint(id))
|
||||
if err != nil {
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 设置响应头
|
||||
c.Header("Content-Type", "application/json")
|
||||
c.Header("Content-Disposition", "attachment; filename=preset-"+c.Param("id")+".json")
|
||||
|
||||
// 返回JSON数据
|
||||
c.Data(200, "application/json", jsonData)
|
||||
}
|
||||
41
server/api/v1/app/upload.go
Normal file
41
server/api/v1/app/upload.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"git.echol.cn/loser/st/server/global"
|
||||
commonResponse "git.echol.cn/loser/st/server/model/common/response"
|
||||
"git.echol.cn/loser/st/server/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type UploadApi struct{}
|
||||
|
||||
// UploadImage 上传图片
|
||||
// @Summary 上传图片
|
||||
// @Tags Upload
|
||||
// @Accept multipart/form-data
|
||||
// @Produce json
|
||||
// @Param file formData file true "图片文件"
|
||||
// @Success 200 {object} map[string]string "返回图片URL"
|
||||
// @Router /app/upload/image [post]
|
||||
func (a *UploadApi) UploadImage(c *gin.Context) {
|
||||
_, header, err := c.Request.FormFile("file")
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("接收文件失败", zap.Error(err))
|
||||
commonResponse.FailWithMessage("请上传图片文件", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 上传图片到 OSS
|
||||
imageURL, err := service.ServiceGroupApp.AppServiceGroup.UploadService.UploadImage(header)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("上传图片失败", zap.Error(err))
|
||||
commonResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 返回图片 URL
|
||||
commonResponse.OkWithData(gin.H{
|
||||
"url": imageURL,
|
||||
}, c)
|
||||
}
|
||||
Reference in New Issue
Block a user