更新密码找回uniapp
This commit is contained in:
1 parent
7f9a66da0f
commit
3fdda89833
1304 files changed
+3503
-26132
No files matched your search
@@ -19,12 +19,10 @@ import (
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
)
|
||||
|
||||
// PlatformAgentApiController 智能体API管理(yz_platform_agent_api,平台端)
|
||||
type PlatformAgentApiController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
// agentApiHTTPClient 用于探测上游接口连通性
|
||||
var agentApiHTTPClient = &http.Client{
|
||||
Timeout: 30 * time.Second,
|
||||
Transport: &http.Transport{
|
||||
@@ -32,7 +30,6 @@ var agentApiHTTPClient = &http.Client{
|
||||
},
|
||||
}
|
||||
|
||||
// 支持的请求协议白名单,与前端 constants.js 的 PROTOCOLS 保持一致
|
||||
var agentApiProtocols = map[string]bool{
|
||||
models.AgentProtocolChatCompletions: true,
|
||||
models.AgentProtocolResponses: true,
|
||||
@@ -41,16 +38,12 @@ var agentApiProtocols = map[string]bool{
|
||||
models.AgentProtocolCustom: true,
|
||||
}
|
||||
|
||||
// agentApiEndpointPath 各协议在根地址之后要追加的路径
|
||||
// custom 与 gemini 不在此表中:custom 不追加任何路径,gemini 需要嵌入模型名
|
||||
var agentApiEndpointPath = map[string]string{
|
||||
models.AgentProtocolChatCompletions: "/chat/completions",
|
||||
models.AgentProtocolResponses: "/responses",
|
||||
models.AgentProtocolAnthropic: "/messages",
|
||||
}
|
||||
|
||||
// normalizeProtocol 缺省或非法值统一回落到 Chat Completions
|
||||
// 该协议被绝大多数官方接口、国产网关与自建中转支持,作为兜底最安全
|
||||
func normalizeProtocol(p string) string {
|
||||
p = strings.TrimSpace(strings.ToLower(p))
|
||||
if p == "" || !agentApiProtocols[p] {
|
||||
@@ -78,8 +71,6 @@ func (c *PlatformAgentApiController) platformClaims() (*jwtutil.Claims, error) {
|
||||
return identity.Claims(c.Ctx.Request.Header.Get("Authorization"), identity.EndPlatform)
|
||||
}
|
||||
|
||||
// agentApiPayload 新增/更新请求体
|
||||
// ApiKeys 用指针:编辑时不传该字段表示不修改已存密钥列表
|
||||
type agentApiPayload struct {
|
||||
Provider string `json:"provider"`
|
||||
Protocol string `json:"protocol"`
|
||||
@@ -92,8 +83,6 @@ type agentApiPayload struct {
|
||||
Remark string `json:"remark"`
|
||||
}
|
||||
|
||||
// apiKeysToJSON 把密钥列表序列化为 JSON 字符串入库
|
||||
// 去掉空密钥、按 key 去重,备注允许为空
|
||||
func apiKeysToJSON(list []models.AgentApiKey) string {
|
||||
cleaned := make([]models.AgentApiKey, 0, len(list))
|
||||
seen := make(map[string]bool, len(list))
|
||||
@@ -115,8 +104,6 @@ func apiKeysToJSON(list []models.AgentApiKey) string {
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// apiKeysFromJSON 把库里的 JSON 字符串反序列化为密钥列表
|
||||
// 兼容三种历史/异常格式:对象数组、纯字符串数组、单个裸密钥字符串
|
||||
func apiKeysFromJSON(raw string) []models.AgentApiKey {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
@@ -124,7 +111,6 @@ func apiKeysFromJSON(raw string) []models.AgentApiKey {
|
||||
}
|
||||
|
||||
if strings.HasPrefix(raw, "[") {
|
||||
// 标准格式:[{"key":"sk-x","remark":"mimo198"}]
|
||||
var objs []models.AgentApiKey
|
||||
if err := json.Unmarshal([]byte(raw), &objs); err == nil {
|
||||
out := make([]models.AgentApiKey, 0, len(objs))
|
||||
@@ -135,7 +121,6 @@ func apiKeysFromJSON(raw string) []models.AgentApiKey {
|
||||
}
|
||||
return out
|
||||
}
|
||||
// 退化格式:["sk-x","sk-y"]
|
||||
var strs []string
|
||||
if err := json.Unmarshal([]byte(raw), &strs); err == nil {
|
||||
out := make([]models.AgentApiKey, 0, len(strs))
|
||||
@@ -149,11 +134,9 @@ func apiKeysFromJSON(raw string) []models.AgentApiKey {
|
||||
return []models.AgentApiKey{}
|
||||
}
|
||||
|
||||
// 迁移遗漏时的兜底:整个字段就是一个裸密钥
|
||||
return []models.AgentApiKey{{Key: raw}}
|
||||
}
|
||||
|
||||
// modelsToJSON 把模型数组序列化为 JSON 字符串入库
|
||||
func modelsToJSON(list []string) string {
|
||||
cleaned := make([]string, 0, len(list))
|
||||
seen := make(map[string]bool, len(list))
|
||||
@@ -172,8 +155,6 @@ func modelsToJSON(list []string) string {
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// modelsFromJSON 把库里的 JSON 字符串反序列化为模型数组
|
||||
// 兼容历史数据可能存的逗号分隔格式
|
||||
func modelsFromJSON(raw string) []string {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
@@ -196,8 +177,6 @@ func modelsFromJSON(raw string) []string {
|
||||
return out
|
||||
}
|
||||
|
||||
// agentApiToMap 统一输出结构
|
||||
// 密钥以明文返回,前端卡片默认脱敏、可切换显示;接口本身已受平台端鉴权保护
|
||||
func agentApiToMap(row *models.PlatformAgentApi) map[string]interface{} {
|
||||
keys := apiKeysFromJSON(row.ApiKeys)
|
||||
protocol := normalizeProtocol(row.Protocol)
|
||||
@@ -224,9 +203,7 @@ func agentApiToMap(row *models.PlatformAgentApi) map[string]interface{} {
|
||||
return out
|
||||
}
|
||||
|
||||
// validateAgentApiPayload 校验必填与地址格式
|
||||
func validateAgentApiPayload(p *agentApiPayload, isCreate bool) error {
|
||||
// 上游接口为用户自由填写的文本,不做枚举校验
|
||||
p.Provider = strings.TrimSpace(p.Provider)
|
||||
if p.Provider == "" {
|
||||
return fmt.Errorf("请输入上游接口名称")
|
||||
@@ -235,7 +212,6 @@ func validateAgentApiPayload(p *agentApiPayload, isCreate bool) error {
|
||||
return fmt.Errorf("上游接口名称过长")
|
||||
}
|
||||
|
||||
// 请求协议必须是白名单内的值,缺省回落到 Chat Completions
|
||||
p.Protocol = strings.TrimSpace(strings.ToLower(p.Protocol))
|
||||
if p.Protocol == "" {
|
||||
p.Protocol = models.AgentProtocolChatCompletions
|
||||
@@ -268,7 +244,6 @@ func validateAgentApiPayload(p *agentApiPayload, isCreate bool) error {
|
||||
return fmt.Errorf("请至少添加一个模型")
|
||||
}
|
||||
|
||||
// 新增时必须带密钥;编辑时不传表示沿用已存密钥列表
|
||||
if p.ApiKeys != nil {
|
||||
valid := 0
|
||||
for _, k := range *p.ApiKeys {
|
||||
@@ -294,9 +269,6 @@ func isHTTPURL(s string) bool {
|
||||
return strings.HasPrefix(low, "http://") || strings.HasPrefix(low, "https://")
|
||||
}
|
||||
|
||||
// buildAgentEndpoint 依据协议算出实际请求地址
|
||||
// custom 协议直接把填写的地址当完整端点,不做任何追加;
|
||||
// 其余协议在根地址后追加各自路径,若地址已以该路径结尾则不重复追加
|
||||
func buildAgentEndpoint(protocol, base, model, apiKey string) string {
|
||||
base = strings.TrimRight(base, "/")
|
||||
|
||||
@@ -305,7 +277,6 @@ func buildAgentEndpoint(protocol, base, model, apiKey string) string {
|
||||
return base
|
||||
|
||||
case models.AgentProtocolGemini:
|
||||
// 模型名嵌在路径中间,无固定后缀可判重
|
||||
return fmt.Sprintf("%s/models/%s:generateContent?key=%s", base, model, apiKey)
|
||||
|
||||
default:
|
||||
@@ -313,7 +284,6 @@ func buildAgentEndpoint(protocol, base, model, apiKey string) string {
|
||||
if path == "" {
|
||||
path = "/chat/completions"
|
||||
}
|
||||
// 用户可能填根地址,也可能直接填完整端点,后者不再追加
|
||||
if strings.HasSuffix(strings.ToLower(base), strings.ToLower(path)) {
|
||||
return base
|
||||
}
|
||||
@@ -321,7 +291,6 @@ func buildAgentEndpoint(protocol, base, model, apiKey string) string {
|
||||
}
|
||||
}
|
||||
|
||||
// List GET /platform/agentApi/list?page=1&pageSize=24&keyword=&provider=&status=
|
||||
func (c *PlatformAgentApiController) List() {
|
||||
if _, err := c.platformClaims(); err != nil {
|
||||
code := identity.Status(err)
|
||||
@@ -350,7 +319,6 @@ func (c *PlatformAgentApiController) List() {
|
||||
cond := orm.NewCondition()
|
||||
needCond := false
|
||||
|
||||
// 上游名称为自由文本,用模糊匹配而非精确相等
|
||||
if provider != "" {
|
||||
cond = cond.And("provider__icontains", provider)
|
||||
needCond = true
|
||||
@@ -399,7 +367,6 @@ func (c *PlatformAgentApiController) List() {
|
||||
})
|
||||
}
|
||||
|
||||
// Detail GET /platform/agentApi/:id
|
||||
func (c *PlatformAgentApiController) Detail() {
|
||||
if _, err := c.platformClaims(); err != nil {
|
||||
code := identity.Status(err)
|
||||
@@ -415,7 +382,6 @@ func (c *PlatformAgentApiController) Detail() {
|
||||
c.ok("success", agentApiToMap(row))
|
||||
}
|
||||
|
||||
// findByID 读取路径参数并查询记录,出错时已写入响应
|
||||
func (c *PlatformAgentApiController) findByID() (*models.PlatformAgentApi, error) {
|
||||
id, err := strconv.ParseUint(c.Ctx.Input.Param(":id"), 10, 64)
|
||||
if err != nil || id == 0 {
|
||||
@@ -439,7 +405,6 @@ func (c *PlatformAgentApiController) findByID() (*models.PlatformAgentApi, error
|
||||
return &row, nil
|
||||
}
|
||||
|
||||
// Create POST /platform/agentApi
|
||||
func (c *PlatformAgentApiController) Create() {
|
||||
claims, err := c.platformClaims()
|
||||
if err != nil {
|
||||
@@ -496,7 +461,6 @@ func (c *PlatformAgentApiController) Create() {
|
||||
c.ok("创建成功", agentApiToMap(row))
|
||||
}
|
||||
|
||||
// Update PUT /platform/agentApi/:id
|
||||
func (c *PlatformAgentApiController) Update() {
|
||||
if _, err := c.platformClaims(); err != nil {
|
||||
code := identity.Status(err)
|
||||
@@ -540,7 +504,6 @@ func (c *PlatformAgentApiController) Update() {
|
||||
if p.Status != nil {
|
||||
updates["status"] = *p.Status
|
||||
}
|
||||
// 未上送 api_keys 表示不修改已存密钥列表
|
||||
if p.ApiKeys != nil {
|
||||
updates["api_keys"] = apiKeysToJSON(*p.ApiKeys)
|
||||
}
|
||||
@@ -556,7 +519,6 @@ func (c *PlatformAgentApiController) Update() {
|
||||
c.ok("更新成功", nil)
|
||||
}
|
||||
|
||||
// Delete DELETE /platform/agentApi/:id
|
||||
func (c *PlatformAgentApiController) Delete() {
|
||||
if _, err := c.platformClaims(); err != nil {
|
||||
code := identity.Status(err)
|
||||
@@ -584,7 +546,6 @@ func (c *PlatformAgentApiController) Delete() {
|
||||
c.ok("删除成功", nil)
|
||||
}
|
||||
|
||||
// BatchDelete POST /platform/agentApi/batchDelete
|
||||
func (c *PlatformAgentApiController) BatchDelete() {
|
||||
if _, err := c.platformClaims(); err != nil {
|
||||
code := identity.Status(err)
|
||||
@@ -626,7 +587,6 @@ func (c *PlatformAgentApiController) BatchDelete() {
|
||||
c.ok("批量删除成功", nil)
|
||||
}
|
||||
|
||||
// ToggleStatus POST /platform/agentApi/:id/status
|
||||
func (c *PlatformAgentApiController) ToggleStatus() {
|
||||
if _, err := c.platformClaims(); err != nil {
|
||||
code := identity.Status(err)
|
||||
@@ -672,8 +632,6 @@ func (c *PlatformAgentApiController) ToggleStatus() {
|
||||
c.ok("操作成功", nil)
|
||||
}
|
||||
|
||||
// agentApiTestResult 测试结果统一结构,与前端 test.vue 字段对应
|
||||
// Endpoint 回显实际请求的完整地址,便于排查路径拼接类问题(如上游根地址少了版本前缀)
|
||||
type agentApiTestResult struct {
|
||||
Success bool `json:"success"`
|
||||
Message string `json:"message"`
|
||||
@@ -686,8 +644,6 @@ type agentApiTestResult struct {
|
||||
Detail string `json:"detail,omitempty"`
|
||||
}
|
||||
|
||||
// redactKeyInURL 隐去 URL 查询串里的密钥
|
||||
// Gemini 协议把 key 放在 query 中,回显 endpoint 前必须脱敏
|
||||
func redactKeyInURL(rawURL, key string) string {
|
||||
if key == "" {
|
||||
return rawURL
|
||||
@@ -695,9 +651,6 @@ func redactKeyInURL(rawURL, key string) string {
|
||||
return strings.ReplaceAll(rawURL, key, "***")
|
||||
}
|
||||
|
||||
// Test POST /platform/agentApi/test
|
||||
// 请求体:{ "id": 1, "model": "gpt-4o", "key_index": 0, "prompt": "你好" }
|
||||
// key_index 指定用密钥列表中的第几个密钥,缺省用第一个
|
||||
func (c *PlatformAgentApiController) Test() {
|
||||
if _, err := c.platformClaims(); err != nil {
|
||||
code := identity.Status(err)
|
||||
@@ -746,7 +699,6 @@ func (c *PlatformAgentApiController) Test() {
|
||||
model = list[0]
|
||||
}
|
||||
|
||||
// 挑选待测密钥
|
||||
keys := apiKeysFromJSON(row.ApiKeys)
|
||||
if len(keys) == 0 {
|
||||
c.jsonErr(400, 400, "该配置未添加 API Key")
|
||||
@@ -771,7 +723,6 @@ func (c *PlatformAgentApiController) Test() {
|
||||
c.ok("success", result)
|
||||
}
|
||||
|
||||
// probeAgentApi 用指定密钥按配置的请求协议发起一次最小化对话请求
|
||||
func probeAgentApi(
|
||||
row *models.PlatformAgentApi,
|
||||
apiKey models.AgentApiKey,
|
||||
@@ -785,7 +736,6 @@ func probeAgentApi(
|
||||
}
|
||||
}
|
||||
|
||||
// 协议由配置显式指定,不再根据上游名称猜测
|
||||
protocol := normalizeProtocol(row.Protocol)
|
||||
endpoint := buildAgentEndpoint(protocol, base, model, apiKey.Key)
|
||||
|
||||
@@ -808,7 +758,6 @@ func probeAgentApi(
|
||||
})
|
||||
|
||||
case models.AgentProtocolGemini:
|
||||
// 密钥已放在 query 中,无需鉴权头
|
||||
body, err = json.Marshal(map[string]interface{}{
|
||||
"contents": []map[string]interface{}{
|
||||
{"parts": []map[string]string{{"text": prompt}}},
|
||||
@@ -816,7 +765,6 @@ func probeAgentApi(
|
||||
})
|
||||
|
||||
case models.AgentProtocolResponses:
|
||||
// Responses API 用 input 字段替代 messages
|
||||
headers["Authorization"] = "Bearer " + apiKey.Key
|
||||
body, err = json.Marshal(map[string]interface{}{
|
||||
"model": model,
|
||||
@@ -825,7 +773,6 @@ func probeAgentApi(
|
||||
})
|
||||
|
||||
default:
|
||||
// Chat Completions 与 custom 均按 OpenAI 标准报文发送
|
||||
headers["Authorization"] = "Bearer " + apiKey.Key
|
||||
body, err = json.Marshal(map[string]interface{}{
|
||||
"model": model,
|
||||
@@ -874,13 +821,11 @@ func probeAgentApi(
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// 限制读取长度,避免超长响应占满内存
|
||||
respBody, _ := io.ReadAll(io.LimitReader(resp.Body, 8*1024))
|
||||
preview := string(respBody)
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
msg := fmt.Sprintf("上游返回 HTTP %d", resp.StatusCode)
|
||||
// 404 多半是接口地址少了版本前缀,给出可操作的提示
|
||||
if resp.StatusCode == 404 {
|
||||
msg += ":接口地址可能不正确,请检查是否缺少版本路径(如 /v1、/compatible-mode/v2)"
|
||||
}
|
||||
@@ -908,7 +853,6 @@ func probeAgentApi(
|
||||
}
|
||||
}
|
||||
|
||||
// extractAgentReply 从上游响应里提取模型回复文本,解析失败则回退为原始预览
|
||||
func extractAgentReply(protocol string, respBody []byte, fallback string) string {
|
||||
switch protocol {
|
||||
case models.AgentProtocolAnthropic:
|
||||
@@ -937,7 +881,6 @@ func extractAgentReply(protocol string, respBody []byte, fallback string) string
|
||||
}
|
||||
|
||||
case models.AgentProtocolResponses:
|
||||
// Responses API 优先取聚合字段 output_text,回退到 output 数组
|
||||
var r struct {
|
||||
OutputText string `json:"output_text"`
|
||||
Output []struct {
|
||||
|
||||
Reference in new issue
Block a user