File size: 3,537 Bytes
adc1e1c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | // Copyright (c) 2025 Tethys Plex
//
// This file is part of Veloera.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package controller
import (
"encoding/json"
"net/http"
"veloera/model"
"veloera/service"
"github.com/gin-gonic/gin"
)
// GetGlobalModelMapping 获取当前全局模型映射配置
func GetGlobalModelMapping(c *gin.Context) {
mapping := service.GetGlobalModelMapping()
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",
"data": mapping,
})
}
// UpdateGlobalModelMapping 更新全局模型映射配置
func UpdateGlobalModelMapping(c *gin.Context) {
var mapping model.GlobalModelMapping
if err := c.ShouldBindJSON(&mapping); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"success": false,
"message": "请求参数解析失败: " + err.Error(),
})
return
}
if err := service.UpdateGlobalModelMapping(&mapping); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"success": false,
"message": "更新全局模型映射配置失败: " + err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "全局模型映射配置更新成功",
})
}
// GetModelMappingConfig 获取模型映射配置的JSON字符串形式
func GetModelMappingConfig(c *gin.Context) {
configStr := service.GlobalModelMappingToJSONString()
// 尝试解析JSON字符串以验证格式
var config map[string]interface{}
if err := json.Unmarshal([]byte(configStr), &config); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"success": false,
"message": "配置格式错误: " + err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",
"data": config,
})
}
// UpdateModelMappingConfig 通过JSON字符串更新模型映射配置
func UpdateModelMappingConfig(c *gin.Context) {
// 读取请求体
jsonStr, err := c.GetRawData()
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"success": false,
"message": "读取请求体失败: " + err.Error(),
})
return
}
// 如果请求体为空,设置为空字符串
if len(jsonStr) == 0 {
jsonStr = []byte("{}")
}
// 更新配置
if err := service.UpdateGlobalModelMappingFromJSONString(string(jsonStr)); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"success": false,
"message": "更新全局模型映射配置失败: " + err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "全局模型映射配置更新成功",
})
}
// ReloadModelMapping 重新加载模型映射配置
func ReloadModelMapping(c *gin.Context) {
if err := service.ReloadModelMapping(); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"success": false,
"message": "重新加载模型映射配置失败: " + err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "模型映射配置重新加载成功",
})
} |