✨ 新增正则和扩展模块
This commit is contained in:
@@ -6,6 +6,8 @@ type ApiGroup struct {
|
||||
AuthApi
|
||||
CharacterApi
|
||||
WorldInfoApi
|
||||
ExtensionApi
|
||||
RegexScriptApi
|
||||
}
|
||||
|
||||
var (
|
||||
|
||||
565
server/api/v1/app/extension.go
Normal file
565
server/api/v1/app/extension.go
Normal file
@@ -0,0 +1,565 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"git.echol.cn/loser/st/server/global"
|
||||
"git.echol.cn/loser/st/server/middleware"
|
||||
"git.echol.cn/loser/st/server/model/app/request"
|
||||
"git.echol.cn/loser/st/server/model/app/response"
|
||||
sysResponse "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 ExtensionApi struct{}
|
||||
|
||||
var extensionService = service.ServiceGroupApp.AppServiceGroup.ExtensionService
|
||||
|
||||
// CreateExtension 创建/安装扩展
|
||||
// @Summary 创建/安装扩展
|
||||
// @Description 创建一个新的扩展或安装扩展
|
||||
// @Tags 扩展管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param data body request.CreateExtensionRequest true "扩展信息"
|
||||
// @Success 200 {object} response.Response{data=response.ExtensionResponse}
|
||||
// @Router /app/extension [post]
|
||||
func (a *ExtensionApi) CreateExtension(c *gin.Context) {
|
||||
userID := middleware.GetAppUserID(c)
|
||||
|
||||
var req request.CreateExtensionRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
sysResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
extension, err := extensionService.CreateExtension(userID, &req)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("创建扩展失败", zap.Error(err))
|
||||
sysResponse.FailWithMessage("创建失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
sysResponse.OkWithData(response.ToExtensionResponse(extension), c)
|
||||
}
|
||||
|
||||
// UpdateExtension 更新扩展
|
||||
// @Summary 更新扩展
|
||||
// @Description 更新扩展信息
|
||||
// @Tags 扩展管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "扩展ID"
|
||||
// @Param data body request.UpdateExtensionRequest true "扩展信息"
|
||||
// @Success 200 {object} response.Response
|
||||
// @Router /app/extension/:id [put]
|
||||
func (a *ExtensionApi) UpdateExtension(c *gin.Context) {
|
||||
userID := middleware.GetAppUserID(c)
|
||||
|
||||
// 从路径参数获取 ID
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err != nil {
|
||||
sysResponse.FailWithMessage("无效的扩展ID", c)
|
||||
return
|
||||
}
|
||||
extensionID := uint(id)
|
||||
|
||||
var req request.UpdateExtensionRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
sysResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
if err := extensionService.UpdateExtension(userID, extensionID, &req); err != nil {
|
||||
global.GVA_LOG.Error("更新扩展失败", zap.Error(err))
|
||||
sysResponse.FailWithMessage("更新失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
sysResponse.OkWithMessage("更新成功", c)
|
||||
}
|
||||
|
||||
// DeleteExtension 删除/卸载扩展
|
||||
// @Summary 删除/卸载扩展
|
||||
// @Description 删除扩展
|
||||
// @Tags 扩展管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "扩展ID"
|
||||
// @Param deleteFiles query bool false "是否删除文件"
|
||||
// @Success 200 {object} response.Response
|
||||
// @Router /app/extension/:id [delete]
|
||||
func (a *ExtensionApi) DeleteExtension(c *gin.Context) {
|
||||
userID := middleware.GetAppUserID(c)
|
||||
|
||||
// 从路径参数获取 ID
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err != nil {
|
||||
sysResponse.FailWithMessage("无效的扩展ID", c)
|
||||
return
|
||||
}
|
||||
extensionID := uint(id)
|
||||
deleteFiles := c.Query("deleteFiles") == "true"
|
||||
|
||||
if err := extensionService.DeleteExtension(userID, extensionID, deleteFiles); err != nil {
|
||||
global.GVA_LOG.Error("删除扩展失败", zap.Error(err))
|
||||
sysResponse.FailWithMessage("删除失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
sysResponse.OkWithMessage("删除成功", c)
|
||||
}
|
||||
|
||||
// GetExtension 获取扩展详情
|
||||
// @Summary 获取扩展详情
|
||||
// @Description 获取扩展详细信息
|
||||
// @Tags 扩展管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "扩展ID"
|
||||
// @Success 200 {object} response.Response{data=response.ExtensionResponse}
|
||||
// @Router /app/extension/:id [get]
|
||||
func (a *ExtensionApi) GetExtension(c *gin.Context) {
|
||||
userID := middleware.GetAppUserID(c)
|
||||
|
||||
// 从路径参数获取 ID
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err != nil {
|
||||
sysResponse.FailWithMessage("无效的扩展ID", c)
|
||||
return
|
||||
}
|
||||
extensionID := uint(id)
|
||||
|
||||
extension, err := extensionService.GetExtension(userID, extensionID)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("获取扩展失败", zap.Error(err))
|
||||
sysResponse.FailWithMessage("获取失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
sysResponse.OkWithData(response.ToExtensionResponse(extension), c)
|
||||
}
|
||||
|
||||
// GetExtensionList 获取扩展列表
|
||||
// @Summary 获取扩展列表
|
||||
// @Description 获取扩展列表
|
||||
// @Tags 扩展管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param data query request.ExtensionListRequest true "查询参数"
|
||||
// @Success 200 {object} response.Response{data=response.ExtensionListResponse}
|
||||
// @Router /app/extension/list [get]
|
||||
func (a *ExtensionApi) GetExtensionList(c *gin.Context) {
|
||||
userID := middleware.GetAppUserID(c)
|
||||
|
||||
var req request.ExtensionListRequest
|
||||
if err := c.ShouldBindQuery(&req); err != nil {
|
||||
sysResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 默认分页
|
||||
if req.Page < 1 {
|
||||
req.Page = 1
|
||||
}
|
||||
if req.PageSize < 1 {
|
||||
req.PageSize = 20
|
||||
}
|
||||
|
||||
result, err := extensionService.GetExtensionList(userID, &req)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("获取扩展列表失败", zap.Error(err))
|
||||
sysResponse.FailWithMessage("获取失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
sysResponse.OkWithData(result, c)
|
||||
}
|
||||
|
||||
// ToggleExtension 启用/禁用扩展
|
||||
// @Summary 启用/禁用扩展
|
||||
// @Description 切换扩展的启用状态
|
||||
// @Tags 扩展管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "扩展ID"
|
||||
// @Param data body request.ToggleExtensionRequest true "启用状态"
|
||||
// @Success 200 {object} response.Response
|
||||
// @Router /app/extension/:id/toggle [post]
|
||||
func (a *ExtensionApi) ToggleExtension(c *gin.Context) {
|
||||
userID := middleware.GetAppUserID(c)
|
||||
|
||||
// 从路径参数获取 ID
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err != nil {
|
||||
sysResponse.FailWithMessage("无效的扩展ID", c)
|
||||
return
|
||||
}
|
||||
extensionID := uint(id)
|
||||
|
||||
var req request.ToggleExtensionRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
sysResponse.FailWithMessage("请求参数错误", c)
|
||||
return
|
||||
}
|
||||
|
||||
if req.IsEnabled == nil {
|
||||
sysResponse.FailWithMessage("isEnabled 参数不能为空", c)
|
||||
return
|
||||
}
|
||||
|
||||
if err := extensionService.ToggleExtension(userID, extensionID, *req.IsEnabled); err != nil {
|
||||
global.GVA_LOG.Error("切换扩展状态失败", zap.Error(err))
|
||||
sysResponse.FailWithMessage("操作失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
sysResponse.OkWithMessage("操作成功", c)
|
||||
}
|
||||
|
||||
// UpdateExtensionSettings 更新扩展配置
|
||||
// @Summary 更新扩展配置
|
||||
// @Description 更新扩展的用户配置
|
||||
// @Tags 扩展管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "扩展ID"
|
||||
// @Param data body request.UpdateExtensionSettingsRequest true "配置信息"
|
||||
// @Success 200 {object} response.Response
|
||||
// @Router /app/extension/:id/settings [put]
|
||||
func (a *ExtensionApi) UpdateExtensionSettings(c *gin.Context) {
|
||||
userID := middleware.GetAppUserID(c)
|
||||
|
||||
// 从路径参数获取 ID
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err != nil {
|
||||
sysResponse.FailWithMessage("无效的扩展ID", c)
|
||||
return
|
||||
}
|
||||
extensionID := uint(id)
|
||||
|
||||
var req request.UpdateExtensionSettingsRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
sysResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
if err := extensionService.UpdateExtensionSettings(userID, extensionID, req.Settings); err != nil {
|
||||
global.GVA_LOG.Error("更新扩展配置失败", zap.Error(err))
|
||||
sysResponse.FailWithMessage("更新失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
sysResponse.OkWithMessage("更新成功", c)
|
||||
}
|
||||
|
||||
// GetExtensionSettings 获取扩展配置
|
||||
// @Summary 获取扩展配置
|
||||
// @Description 获取扩展的用户配置
|
||||
// @Tags 扩展管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "扩展ID"
|
||||
// @Success 200 {object} response.Response{data=map[string]interface{}}
|
||||
// @Router /app/extension/:id/settings [get]
|
||||
func (a *ExtensionApi) GetExtensionSettings(c *gin.Context) {
|
||||
userID := middleware.GetAppUserID(c)
|
||||
|
||||
// 从路径参数获取 ID
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err != nil {
|
||||
sysResponse.FailWithMessage("无效的扩展ID", c)
|
||||
return
|
||||
}
|
||||
extensionID := uint(id)
|
||||
|
||||
settings, err := extensionService.GetExtensionSettings(userID, extensionID)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("获取扩展配置失败", zap.Error(err))
|
||||
sysResponse.FailWithMessage("获取失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
sysResponse.OkWithData(settings, c)
|
||||
}
|
||||
|
||||
// GetExtensionManifest 获取扩展 manifest
|
||||
// @Summary 获取扩展 manifest
|
||||
// @Description 获取扩展的 manifest.json
|
||||
// @Tags 扩展管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "扩展ID"
|
||||
// @Success 200 {object} response.Response{data=response.ExtensionManifestResponse}
|
||||
// @Router /app/extension/:id/manifest [get]
|
||||
func (a *ExtensionApi) GetExtensionManifest(c *gin.Context) {
|
||||
userID := middleware.GetAppUserID(c)
|
||||
|
||||
// 从路径参数获取 ID
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err != nil {
|
||||
sysResponse.FailWithMessage("无效的扩展ID", c)
|
||||
return
|
||||
}
|
||||
extensionID := uint(id)
|
||||
|
||||
manifest, err := extensionService.GetExtensionManifest(userID, extensionID)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("获取扩展 manifest 失败", zap.Error(err))
|
||||
sysResponse.FailWithMessage("获取失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
sysResponse.OkWithData(manifest, c)
|
||||
}
|
||||
|
||||
// ImportExtension 导入扩展
|
||||
// @Summary 导入扩展
|
||||
// @Description 从文件导入扩展
|
||||
// @Tags 扩展管理
|
||||
// @Accept multipart/form-data
|
||||
// @Produce json
|
||||
// @Param file formData file true "扩展文件(manifest.json)"
|
||||
// @Success 200 {object} response.Response{data=response.ExtensionResponse}
|
||||
// @Router /app/extension/import [post]
|
||||
func (a *ExtensionApi) ImportExtension(c *gin.Context) {
|
||||
userID := middleware.GetAppUserID(c)
|
||||
|
||||
// 获取文件
|
||||
file, err := c.FormFile("file")
|
||||
if err != nil {
|
||||
sysResponse.FailWithMessage("请上传扩展文件", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 文件大小限制(5MB)
|
||||
if file.Size > 5<<20 {
|
||||
sysResponse.FailWithMessage("文件大小不能超过 5MB", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 读取文件内容
|
||||
src, err := file.Open()
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("打开文件失败", zap.Error(err))
|
||||
sysResponse.FailWithMessage("文件读取失败", c)
|
||||
return
|
||||
}
|
||||
defer src.Close()
|
||||
|
||||
fileData, err := io.ReadAll(src)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("读取文件内容失败", zap.Error(err))
|
||||
sysResponse.FailWithMessage("文件读取失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 导入扩展
|
||||
extension, err := extensionService.ImportExtension(userID, fileData)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("导入扩展失败", zap.Error(err))
|
||||
sysResponse.FailWithMessage("导入失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
sysResponse.OkWithData(response.ToExtensionResponse(extension), c)
|
||||
}
|
||||
|
||||
// ExportExtension 导出扩展
|
||||
// @Summary 导出扩展
|
||||
// @Description 导出扩展为 manifest.json 文件
|
||||
// @Tags 扩展管理
|
||||
// @Accept json
|
||||
// @Produce application/json
|
||||
// @Param id path int true "扩展ID"
|
||||
// @Success 200 {object} response.ExtensionManifestResponse
|
||||
// @Router /app/extension/:id/export [get]
|
||||
func (a *ExtensionApi) ExportExtension(c *gin.Context) {
|
||||
userID := middleware.GetAppUserID(c)
|
||||
|
||||
// 从路径参数获取 ID
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err != nil {
|
||||
sysResponse.FailWithMessage("无效的扩展ID", c)
|
||||
return
|
||||
}
|
||||
extensionID := uint(id)
|
||||
|
||||
exportData, err := extensionService.ExportExtension(userID, extensionID)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("导出扩展失败", zap.Error(err))
|
||||
sysResponse.FailWithMessage("导出失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 设置响应头
|
||||
filename := fmt.Sprintf("extension_%d_manifest.json", extensionID)
|
||||
c.Header("Content-Type", "application/json")
|
||||
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filename))
|
||||
|
||||
// 直接返回 JSON 数据
|
||||
c.Data(http.StatusOK, "application/json", exportData)
|
||||
}
|
||||
|
||||
// UpdateExtensionStats 更新扩展统计
|
||||
// @Summary 更新扩展统计
|
||||
// @Description 更新扩展的使用统计
|
||||
// @Tags 扩展管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param data body request.ExtensionStatsRequest true "统计信息"
|
||||
// @Success 200 {object} response.Response
|
||||
// @Router /app/extension/stats [post]
|
||||
func (a *ExtensionApi) UpdateExtensionStats(c *gin.Context) {
|
||||
userID := middleware.GetAppUserID(c)
|
||||
|
||||
var req request.ExtensionStatsRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
sysResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
if err := extensionService.UpdateExtensionStats(userID, req.ExtensionID, req.Action, req.Value); err != nil {
|
||||
global.GVA_LOG.Error("更新扩展统计失败", zap.Error(err))
|
||||
sysResponse.FailWithMessage("更新失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
sysResponse.OkWithMessage("更新成功", c)
|
||||
}
|
||||
|
||||
// GetEnabledExtensions 获取启用的扩展列表
|
||||
// @Summary 获取启用的扩展列表
|
||||
// @Description 获取用户启用的所有扩展(用于前端加载)
|
||||
// @Tags 扩展管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} response.Response{data=[]response.ExtensionResponse}
|
||||
// @Router /app/extension/enabled [get]
|
||||
func (a *ExtensionApi) GetEnabledExtensions(c *gin.Context) {
|
||||
userID := middleware.GetAppUserID(c)
|
||||
|
||||
extensions, err := extensionService.GetEnabledExtensions(userID)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("获取启用扩展列表失败", zap.Error(err))
|
||||
sysResponse.FailWithMessage("获取失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
sysResponse.OkWithData(extensions, c)
|
||||
}
|
||||
|
||||
// InstallExtensionFromURL 智能安装扩展(自动识别 Git URL 或 Manifest URL)
|
||||
// @Summary 智能安装扩展
|
||||
// @Description 自动识别 Git 仓库 URL 或 Manifest.json URL 并安装扩展(兼容 SillyTavern)
|
||||
// @Tags 扩展管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param data body request.InstallExtensionFromURLRequest true "安装 URL 信息"
|
||||
// @Success 200 {object} response.Response{data=response.ExtensionResponse}
|
||||
// @Router /app/extension/install/url [post]
|
||||
func (a *ExtensionApi) InstallExtensionFromURL(c *gin.Context) {
|
||||
userID := middleware.GetAppUserID(c)
|
||||
|
||||
var req request.InstallExtensionFromURLRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
sysResponse.FailWithMessage("请求参数错误: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 设置默认分支
|
||||
if req.Branch == "" {
|
||||
req.Branch = "main"
|
||||
}
|
||||
|
||||
extension, err := extensionService.InstallExtensionFromURL(userID, req.URL, req.Branch)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("从 URL 安装扩展失败", zap.Error(err), zap.String("url", req.URL))
|
||||
sysResponse.FailWithMessage("安装失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
sysResponse.OkWithData(response.ToExtensionResponse(extension), c)
|
||||
}
|
||||
|
||||
// UpgradeExtension 升级扩展版本
|
||||
// @Summary 升级扩展版本
|
||||
// @Description 根据扩展的安装来源自动选择更新方式(Git pull 或重新下载)
|
||||
// @Tags 扩展管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "扩展ID"
|
||||
// @Param data body request.UpdateExtensionRequest false "更新选项"
|
||||
// @Success 200 {object} response.Response{data=response.ExtensionResponse}
|
||||
// @Router /app/extension/:id/update [post]
|
||||
func (a *ExtensionApi) UpgradeExtension(c *gin.Context) {
|
||||
userID := middleware.GetAppUserID(c)
|
||||
|
||||
// 从路径参数获取 ID
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err != nil {
|
||||
sysResponse.FailWithMessage("无效的扩展ID", c)
|
||||
return
|
||||
}
|
||||
extensionID := uint(id)
|
||||
|
||||
var req request.UpdateExtensionRequest
|
||||
// 允许不传 body(使用默认值)
|
||||
_ = c.ShouldBindJSON(&req)
|
||||
|
||||
extension, err := extensionService.UpgradeExtension(userID, extensionID, req.Force)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("升级扩展失败", zap.Error(err), zap.Uint("extensionID", extensionID))
|
||||
sysResponse.FailWithMessage("升级失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
sysResponse.OkWithData(response.ToExtensionResponse(extension), c)
|
||||
}
|
||||
|
||||
// InstallExtensionFromGit 从 Git URL 安装扩展
|
||||
// @Summary 从 Git URL 安装扩展
|
||||
// @Description 从 Git 仓库 URL 克隆并安装扩展
|
||||
// @Tags 扩展管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param data body request.InstallExtensionFromGitRequest true "Git URL 信息"
|
||||
// @Success 200 {object} response.Response{data=response.ExtensionResponse}
|
||||
// @Router /app/extension/install/git [post]
|
||||
func (a *ExtensionApi) InstallExtensionFromGit(c *gin.Context) {
|
||||
userID := middleware.GetAppUserID(c)
|
||||
|
||||
var req request.InstallExtensionFromGitRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
sysResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 设置默认分支
|
||||
if req.Branch == "" {
|
||||
req.Branch = "main"
|
||||
}
|
||||
|
||||
extension, err := extensionService.InstallExtensionFromGit(userID, req.GitUrl, req.Branch)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("从 Git 安装扩展失败", zap.Error(err))
|
||||
sysResponse.FailWithMessage("安装失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
sysResponse.OkWithData(response.ToExtensionResponse(extension), c)
|
||||
}
|
||||
479
server/api/v1/app/regex_script.go
Normal file
479
server/api/v1/app/regex_script.go
Normal file
@@ -0,0 +1,479 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.echol.cn/loser/st/server/global"
|
||||
"git.echol.cn/loser/st/server/middleware"
|
||||
"git.echol.cn/loser/st/server/model/app"
|
||||
"git.echol.cn/loser/st/server/model/app/request"
|
||||
"git.echol.cn/loser/st/server/model/app/response"
|
||||
sysResponse "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 RegexScriptApi struct{}
|
||||
|
||||
var regexScriptService = service.ServiceGroupApp.AppServiceGroup.RegexScriptService
|
||||
|
||||
// CreateRegexScript 创建正则脚本
|
||||
// @Summary 创建正则脚本
|
||||
// @Description 创建一个新的正则表达式脚本
|
||||
// @Tags 正则脚本管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param data body request.CreateRegexScriptRequest true "脚本信息"
|
||||
// @Success 200 {object} response.Response{data=app.AIRegexScript}
|
||||
// @Router /app/regex [post]
|
||||
func (a *RegexScriptApi) CreateRegexScript(c *gin.Context) {
|
||||
userID := middleware.GetAppUserID(c)
|
||||
|
||||
var req request.CreateRegexScriptRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
sysResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
script, err := regexScriptService.CreateRegexScript(userID, &req)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("创建正则脚本失败", zap.Error(err))
|
||||
sysResponse.FailWithMessage("创建失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
sysResponse.OkWithData(response.ToRegexScriptResponse(script), c)
|
||||
}
|
||||
|
||||
// UpdateRegexScript 更新正则脚本
|
||||
// @Summary 更新正则脚本
|
||||
// @Description 更新正则脚本信息
|
||||
// @Tags 正则脚本管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "脚本ID"
|
||||
// @Param data body request.UpdateRegexScriptRequest true "脚本信息"
|
||||
// @Success 200 {object} response.Response
|
||||
// @Router /app/regex/:id [put]
|
||||
func (a *RegexScriptApi) UpdateRegexScript(c *gin.Context) {
|
||||
userID := middleware.GetAppUserID(c)
|
||||
|
||||
// 从路径参数获取 ID
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err != nil {
|
||||
sysResponse.FailWithMessage("无效的脚本ID", c)
|
||||
return
|
||||
}
|
||||
scriptID := uint(id)
|
||||
|
||||
var req request.UpdateRegexScriptRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
sysResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
if err := regexScriptService.UpdateRegexScript(userID, scriptID, &req); err != nil {
|
||||
global.GVA_LOG.Error("更新正则脚本失败", zap.Error(err))
|
||||
sysResponse.FailWithMessage("更新失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
sysResponse.OkWithMessage("更新成功", c)
|
||||
}
|
||||
|
||||
// DeleteRegexScript 删除正则脚本
|
||||
// @Summary 删除正则脚本
|
||||
// @Description 删除正则脚本
|
||||
// @Tags 正则脚本管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "脚本ID"
|
||||
// @Success 200 {object} response.Response
|
||||
// @Router /app/regex/:id [delete]
|
||||
func (a *RegexScriptApi) DeleteRegexScript(c *gin.Context) {
|
||||
userID := middleware.GetAppUserID(c)
|
||||
|
||||
// 从路径参数获取 ID
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err != nil {
|
||||
sysResponse.FailWithMessage("无效的脚本ID", c)
|
||||
return
|
||||
}
|
||||
scriptID := uint(id)
|
||||
|
||||
if err := regexScriptService.DeleteRegexScript(userID, scriptID); err != nil {
|
||||
global.GVA_LOG.Error("删除正则脚本失败", zap.Error(err))
|
||||
sysResponse.FailWithMessage("删除失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
sysResponse.OkWithMessage("删除成功", c)
|
||||
}
|
||||
|
||||
// GetRegexScript 获取正则脚本详情
|
||||
// @Summary 获取正则脚本详情
|
||||
// @Description 获取正则脚本详细信息
|
||||
// @Tags 正则脚本管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "脚本ID"
|
||||
// @Success 200 {object} response.Response{data=response.RegexScriptResponse}
|
||||
// @Router /app/regex/:id [get]
|
||||
func (a *RegexScriptApi) GetRegexScript(c *gin.Context) {
|
||||
userID := middleware.GetAppUserID(c)
|
||||
|
||||
// 从路径参数获取 ID
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err != nil {
|
||||
sysResponse.FailWithMessage("无效的脚本ID", c)
|
||||
return
|
||||
}
|
||||
scriptID := uint(id)
|
||||
|
||||
script, err := regexScriptService.GetRegexScript(userID, scriptID)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("获取正则脚本失败", zap.Error(err))
|
||||
sysResponse.FailWithMessage("获取失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
sysResponse.OkWithData(response.ToRegexScriptResponse(script), c)
|
||||
}
|
||||
|
||||
// GetRegexScriptList 获取正则脚本列表
|
||||
// @Summary 获取正则脚本列表
|
||||
// @Description 获取正则脚本列表
|
||||
// @Tags 正则脚本管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param scriptName query string false "脚本名称"
|
||||
// @Param isGlobal query boolean false "是否全局"
|
||||
// @Param enabled query boolean false "是否启用"
|
||||
// @Param characterId query int false "关联角色ID"
|
||||
// @Param page query int false "页码"
|
||||
// @Param pageSize query int false "每页大小"
|
||||
// @Success 200 {object} response.Response{data=response.RegexScriptListResponse}
|
||||
// @Router /app/regex [get]
|
||||
func (a *RegexScriptApi) GetRegexScriptList(c *gin.Context) {
|
||||
userID := middleware.GetAppUserID(c)
|
||||
|
||||
var req request.RegexScriptListRequest
|
||||
if err := c.ShouldBindQuery(&req); err != nil {
|
||||
sysResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 设置默认值
|
||||
if req.Page <= 0 {
|
||||
req.Page = 1
|
||||
}
|
||||
if req.PageSize <= 0 {
|
||||
req.PageSize = 20
|
||||
}
|
||||
|
||||
scripts, total, err := regexScriptService.GetRegexScriptList(userID, &req)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("获取正则脚本列表失败", zap.Error(err))
|
||||
sysResponse.FailWithMessage("获取失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 转换为响应格式
|
||||
responses := make([]response.RegexScriptResponse, len(scripts))
|
||||
for i, script := range scripts {
|
||||
responses[i] = response.ToRegexScriptResponse(&script)
|
||||
}
|
||||
|
||||
sysResponse.OkWithData(response.RegexScriptListResponse{
|
||||
List: responses,
|
||||
Total: total,
|
||||
Page: req.Page,
|
||||
PageSize: req.PageSize,
|
||||
}, c)
|
||||
}
|
||||
|
||||
// LinkCharactersToRegex 关联角色到正则脚本
|
||||
// @Summary 关联角色到正则脚本
|
||||
// @Description 将角色关联到指定的正则脚本
|
||||
// @Tags 正则脚本管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "脚本ID"
|
||||
// @Param data body request.LinkCharacterToRegexRequest true "角色ID列表"
|
||||
// @Success 200 {object} response.Response
|
||||
// @Router /app/regex/:id/link [post]
|
||||
func (a *RegexScriptApi) LinkCharactersToRegex(c *gin.Context) {
|
||||
userID := middleware.GetAppUserID(c)
|
||||
|
||||
// 从路径参数获取 ID
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err != nil {
|
||||
sysResponse.FailWithMessage("无效的脚本ID", c)
|
||||
return
|
||||
}
|
||||
scriptID := uint(id)
|
||||
|
||||
var req request.LinkCharacterToRegexRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
sysResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
if err := regexScriptService.LinkCharactersToRegex(userID, scriptID, req.CharacterIDs); err != nil {
|
||||
global.GVA_LOG.Error("关联角色失败", zap.Error(err))
|
||||
sysResponse.FailWithMessage("关联失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
sysResponse.OkWithMessage("关联成功", c)
|
||||
}
|
||||
|
||||
// GetCharacterRegexScripts 获取角色关联的正则脚本
|
||||
// @Summary 获取角色关联的正则脚本
|
||||
// @Description 获取特定角色关联的所有正则脚本
|
||||
// @Tags 正则脚本管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param characterId path int true "角色ID"
|
||||
// @Success 200 {object} response.Response{data=[]response.RegexScriptResponse}
|
||||
// @Router /app/regex/character/:characterId [get]
|
||||
func (a *RegexScriptApi) GetCharacterRegexScripts(c *gin.Context) {
|
||||
userID := middleware.GetAppUserID(c)
|
||||
|
||||
// 从路径参数获取 ID
|
||||
idStr := c.Param("characterId")
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err != nil {
|
||||
sysResponse.FailWithMessage("无效的角色ID", c)
|
||||
return
|
||||
}
|
||||
characterID := uint(id)
|
||||
|
||||
scripts, err := regexScriptService.GetCharacterRegexScripts(userID, characterID)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("获取角色正则脚本失败", zap.Error(err))
|
||||
sysResponse.FailWithMessage("获取失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 转换为响应格式
|
||||
responses := make([]response.RegexScriptResponse, len(scripts))
|
||||
for i, script := range scripts {
|
||||
responses[i] = response.ToRegexScriptResponse(&script)
|
||||
}
|
||||
|
||||
sysResponse.OkWithData(responses, c)
|
||||
}
|
||||
|
||||
// DuplicateRegexScript 复制正则脚本
|
||||
// @Summary 复制正则脚本
|
||||
// @Description 创建正则脚本的副本
|
||||
// @Tags 正则脚本管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "脚本ID"
|
||||
// @Success 200 {object} response.Response{data=app.AIRegexScript}
|
||||
// @Router /app/regex/:id/duplicate [post]
|
||||
func (a *RegexScriptApi) DuplicateRegexScript(c *gin.Context) {
|
||||
userID := middleware.GetAppUserID(c)
|
||||
|
||||
// 从路径参数获取 ID
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err != nil {
|
||||
sysResponse.FailWithMessage("无效的脚本ID", c)
|
||||
return
|
||||
}
|
||||
scriptID := uint(id)
|
||||
|
||||
newScript, err := regexScriptService.DuplicateRegexScript(userID, scriptID)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("复制正则脚本失败", zap.Error(err))
|
||||
sysResponse.FailWithMessage("复制失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
sysResponse.OkWithData(response.ToRegexScriptResponse(newScript), c)
|
||||
}
|
||||
|
||||
// TestRegexScript 测试正则脚本
|
||||
// @Summary 测试正则脚本
|
||||
// @Description 测试正则表达式的匹配和替换效果
|
||||
// @Tags 正则脚本管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param data body request.TestRegexScriptRequest true "测试数据"
|
||||
// @Success 200 {object} response.Response{data=response.TestRegexScriptResponse}
|
||||
// @Router /app/regex/test [post]
|
||||
func (a *RegexScriptApi) TestRegexScript(c *gin.Context) {
|
||||
var req request.TestRegexScriptRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
sysResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := regexScriptService.TestRegexScript(&req)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("测试正则脚本失败", zap.Error(err))
|
||||
sysResponse.FailWithMessage("测试失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
sysResponse.OkWithData(result, c)
|
||||
}
|
||||
|
||||
// ApplyRegexScripts 应用正则脚本
|
||||
// @Summary 应用正则脚本
|
||||
// @Description 对文本应用正则脚本进行处理
|
||||
// @Tags 正则脚本管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param data body request.ApplyRegexScriptsRequest true "应用参数"
|
||||
// @Success 200 {object} response.Response{data=response.ApplyRegexScriptsResponse}
|
||||
// @Router /app/regex/apply [post]
|
||||
func (a *RegexScriptApi) ApplyRegexScripts(c *gin.Context) {
|
||||
userID := middleware.GetAppUserID(c)
|
||||
|
||||
var req request.ApplyRegexScriptsRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
sysResponse.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := regexScriptService.ApplyRegexScripts(userID, &req)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("应用正则脚本失败", zap.Error(err))
|
||||
sysResponse.FailWithMessage("应用失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
sysResponse.OkWithData(result, c)
|
||||
}
|
||||
|
||||
// ImportRegexScripts 导入正则脚本
|
||||
// @Summary 导入正则脚本
|
||||
// @Description 从 JSON 文件导入正则脚本
|
||||
// @Tags 正则脚本管理
|
||||
// @Accept multipart/form-data
|
||||
// @Produce json
|
||||
// @Param file formData file true "JSON 文件"
|
||||
// @Param overwriteMode formData string false "覆盖模式: skip, overwrite, merge"
|
||||
// @Success 200 {object} response.Response
|
||||
// @Router /app/regex/import [post]
|
||||
func (a *RegexScriptApi) ImportRegexScripts(c *gin.Context) {
|
||||
userID := middleware.GetAppUserID(c)
|
||||
|
||||
// 获取文件
|
||||
file, err := c.FormFile("file")
|
||||
if err != nil {
|
||||
sysResponse.FailWithMessage("获取文件失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 获取覆盖模式
|
||||
overwriteMode := c.DefaultPostForm("overwriteMode", "skip")
|
||||
|
||||
// 读取文件内容
|
||||
openedFile, err := file.Open()
|
||||
if err != nil {
|
||||
sysResponse.FailWithMessage("打开文件失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
defer openedFile.Close()
|
||||
|
||||
content, err := io.ReadAll(openedFile)
|
||||
if err != nil {
|
||||
sysResponse.FailWithMessage("读取文件失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 解析 JSON
|
||||
var exportData response.RegexScriptExportData
|
||||
if err := json.Unmarshal(content, &exportData); err != nil {
|
||||
sysResponse.FailWithMessage("解析JSON失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 转换为 AIRegexScript
|
||||
scripts := make([]app.AIRegexScript, len(exportData.Scripts))
|
||||
for i, resp := range exportData.Scripts {
|
||||
scripts[i] = app.AIRegexScript{
|
||||
ScriptName: resp.ScriptName,
|
||||
Description: resp.Description,
|
||||
FindRegex: resp.FindRegex,
|
||||
ReplaceString: resp.ReplaceString,
|
||||
Enabled: resp.Enabled,
|
||||
IsGlobal: resp.IsGlobal,
|
||||
TrimStrings: resp.TrimStrings,
|
||||
OnlyFormat: resp.OnlyFormat,
|
||||
RunOnEdit: resp.RunOnEdit,
|
||||
SubstituteRegex: resp.SubstituteRegex,
|
||||
MinDepth: resp.MinDepth,
|
||||
MaxDepth: resp.MaxDepth,
|
||||
Placement: resp.Placement,
|
||||
AffectMinDepth: resp.AffectMinDepth,
|
||||
AffectMaxDepth: resp.AffectMaxDepth,
|
||||
LinkedChars: resp.LinkedChars,
|
||||
}
|
||||
}
|
||||
|
||||
// 导入
|
||||
imported, err := regexScriptService.ImportRegexScripts(userID, scripts, overwriteMode)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("导入正则脚本失败", zap.Error(err))
|
||||
sysResponse.FailWithMessage("导入失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
sysResponse.OkWithMessage(fmt.Sprintf("成功导入 %d 个脚本", imported), c)
|
||||
}
|
||||
|
||||
// ExportRegexScripts 导出正则脚本
|
||||
// @Summary 导出正则脚本
|
||||
// @Description 导出正则脚本为 JSON 文件
|
||||
// @Tags 正则脚本管理
|
||||
// @Accept json
|
||||
// @Produce application/json
|
||||
// @Param scriptIds query string false "脚本ID列表(逗号分隔)"
|
||||
// @Success 200 {object} response.RegexScriptExportData
|
||||
// @Router /app/regex/export [get]
|
||||
func (a *RegexScriptApi) ExportRegexScripts(c *gin.Context) {
|
||||
userID := middleware.GetAppUserID(c)
|
||||
|
||||
// 获取脚本ID列表
|
||||
scriptIDsStr := c.Query("scriptIds")
|
||||
var scriptIDs []uint
|
||||
if scriptIDsStr != "" {
|
||||
var ids []uint
|
||||
for _, idStr := range strings.Split(scriptIDsStr, ",") {
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err == nil {
|
||||
ids = append(ids, uint(id))
|
||||
}
|
||||
}
|
||||
scriptIDs = ids
|
||||
}
|
||||
|
||||
exportData, err := regexScriptService.ExportRegexScripts(userID, scriptIDs)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("导出正则脚本失败", zap.Error(err))
|
||||
sysResponse.FailWithMessage("导出失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 设置下载响应头
|
||||
c.Header("Content-Type", "application/json")
|
||||
c.Header("Content-Disposition", "attachment; filename=regex_scripts_export.json")
|
||||
|
||||
c.JSON(http.StatusOK, exportData)
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"git.echol.cn/loser/st/server/global"
|
||||
"git.echol.cn/loser/st/server/middleware"
|
||||
@@ -61,7 +62,15 @@ func (a *WorldInfoApi) CreateWorldBook(c *gin.Context) {
|
||||
// @Router /app/worldbook/:id [put]
|
||||
func (a *WorldInfoApi) UpdateWorldBook(c *gin.Context) {
|
||||
userID := middleware.GetAppUserID(c)
|
||||
bookID := c.GetUint("id")
|
||||
|
||||
// 从路径参数获取 ID
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err != nil {
|
||||
sysResponse.FailWithMessage("无效的世界书ID", c)
|
||||
return
|
||||
}
|
||||
bookID := uint(id)
|
||||
|
||||
var req request.UpdateWorldBookRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
@@ -89,7 +98,15 @@ func (a *WorldInfoApi) UpdateWorldBook(c *gin.Context) {
|
||||
// @Router /app/worldbook/:id [delete]
|
||||
func (a *WorldInfoApi) DeleteWorldBook(c *gin.Context) {
|
||||
userID := middleware.GetAppUserID(c)
|
||||
bookID := c.GetUint("id")
|
||||
|
||||
// 从路径参数获取 ID
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err != nil {
|
||||
sysResponse.FailWithMessage("无效的世界书ID", c)
|
||||
return
|
||||
}
|
||||
bookID := uint(id)
|
||||
|
||||
if err := worldInfoService.DeleteWorldBook(userID, bookID); err != nil {
|
||||
global.GVA_LOG.Error("删除世界书失败", zap.Error(err))
|
||||
@@ -111,7 +128,15 @@ func (a *WorldInfoApi) DeleteWorldBook(c *gin.Context) {
|
||||
// @Router /app/worldbook/:id [get]
|
||||
func (a *WorldInfoApi) GetWorldBook(c *gin.Context) {
|
||||
userID := middleware.GetAppUserID(c)
|
||||
bookID := c.GetUint("id")
|
||||
|
||||
// 从路径参数获取 ID
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err != nil {
|
||||
sysResponse.FailWithMessage("无效的世界书ID", c)
|
||||
return
|
||||
}
|
||||
bookID := uint(id)
|
||||
|
||||
book, err := worldInfoService.GetWorldBook(userID, bookID)
|
||||
if err != nil {
|
||||
@@ -334,7 +359,15 @@ func (a *WorldInfoApi) ImportWorldBook(c *gin.Context) {
|
||||
// @Router /app/worldbook/:id/export [get]
|
||||
func (a *WorldInfoApi) ExportWorldBook(c *gin.Context) {
|
||||
userID := middleware.GetAppUserID(c)
|
||||
bookID := c.GetUint("id")
|
||||
|
||||
// 从路径参数获取 ID
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err != nil {
|
||||
sysResponse.FailWithMessage("无效的世界书ID", c)
|
||||
return
|
||||
}
|
||||
bookID := uint(id)
|
||||
|
||||
exportData, err := worldInfoService.ExportWorldBook(userID, bookID)
|
||||
if err != nil {
|
||||
@@ -399,10 +432,18 @@ func (a *WorldInfoApi) MatchWorldInfo(c *gin.Context) {
|
||||
// @Router /app/worldbook/character/:characterId [get]
|
||||
func (a *WorldInfoApi) GetCharacterWorldBooks(c *gin.Context) {
|
||||
userID := middleware.GetAppUserID(c)
|
||||
characterID := c.GetUint("characterId")
|
||||
|
||||
// 从路径参数获取 ID
|
||||
idStr := c.Param("characterId")
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err != nil {
|
||||
sysResponse.FailWithMessage("无效的角色ID", c)
|
||||
return
|
||||
}
|
||||
characterID := uint(id)
|
||||
|
||||
var books []app.AIWorldInfo
|
||||
err := global.GVA_DB.
|
||||
err = global.GVA_DB.
|
||||
Where("user_id = ? AND (is_global = true OR ? = ANY(linked_chars))", userID, fmt.Sprintf("%d", characterID)).
|
||||
Find(&books).Error
|
||||
|
||||
@@ -431,7 +472,15 @@ func (a *WorldInfoApi) GetCharacterWorldBooks(c *gin.Context) {
|
||||
// @Router /app/worldbook/:id/duplicate [post]
|
||||
func (a *WorldInfoApi) DuplicateWorldBook(c *gin.Context) {
|
||||
userID := middleware.GetAppUserID(c)
|
||||
bookID := c.GetUint("id")
|
||||
|
||||
// 从路径参数获取 ID
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err != nil {
|
||||
sysResponse.FailWithMessage("无效的世界书ID", c)
|
||||
return
|
||||
}
|
||||
bookID := uint(id)
|
||||
|
||||
// 获取原世界书
|
||||
book, err := worldInfoService.GetWorldBook(userID, bookID)
|
||||
|
||||
Reference in New Issue
Block a user