更新密码找回uniapp
This commit is contained in:
1 parent
7f9a66da0f
commit
3fdda89833
1304 files changed
+3503
-26132
No files matched your search
@@ -17,8 +17,6 @@ import (
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
)
|
||||
|
||||
// PlatformAiController 平台�?AI 管理(platform「AI管理」页�?
|
||||
// 服务商配置存 yz_platform_ai_provider;OCR 配置暂存 yz_platform_site_setting 标签�?
|
||||
type PlatformAiController struct {
|
||||
beego.Controller
|
||||
}
|
||||
@@ -42,7 +40,6 @@ func maskKey(key string) string {
|
||||
|
||||
func fillProvider(m *models.PlatformAiProvider) {
|
||||
if m.Models != "" {
|
||||
// 兼容两种存储格式:[{"id","name"}] 对象数组 �?["model-id"] 字符串数�?
|
||||
var raw []json.RawMessage
|
||||
if err := json.Unmarshal([]byte(m.Models), &raw); err == nil {
|
||||
for _, r := range raw {
|
||||
@@ -62,7 +59,6 @@ func fillProvider(m *models.PlatformAiProvider) {
|
||||
m.ApiKey = ""
|
||||
}
|
||||
|
||||
// List GET /platform/ai/provider/list?provider_type=
|
||||
func (c *PlatformAiController) List() {
|
||||
if _, err := requirePlatform(&c.Controller); err != nil {
|
||||
code := identity.Status(err)
|
||||
@@ -138,7 +134,6 @@ func (p *platformAiProviderPayload) toModel() *models.PlatformAiProvider {
|
||||
}
|
||||
}
|
||||
|
||||
// Create POST /platform/ai/provider
|
||||
func (c *PlatformAiController) Create() {
|
||||
if _, err := requirePlatform(&c.Controller); err != nil {
|
||||
code := identity.Status(err)
|
||||
@@ -166,7 +161,6 @@ func (c *PlatformAiController) Create() {
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
// Update PUT /platform/ai/provider/:id
|
||||
func (c *PlatformAiController) Update() {
|
||||
if _, err := requirePlatform(&c.Controller); err != nil {
|
||||
code := identity.Status(err)
|
||||
@@ -183,7 +177,6 @@ func (c *PlatformAiController) Update() {
|
||||
return
|
||||
}
|
||||
m := p.toModel()
|
||||
// api_key 传空表示不修�?
|
||||
update := map[string]any{
|
||||
"provider_type": m.ProviderType, "api_type": m.ApiType, "name": m.Name, "api_base": m.ApiBase,
|
||||
"models": m.Models, "default_model": m.DefaultModel,
|
||||
@@ -211,7 +204,6 @@ func (c *PlatformAiController) Update() {
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
// Delete DELETE /platform/ai/provider/:id
|
||||
func (c *PlatformAiController) Delete() {
|
||||
if _, err := requirePlatform(&c.Controller); err != nil {
|
||||
code := identity.Status(err)
|
||||
@@ -234,7 +226,6 @@ func (c *PlatformAiController) Delete() {
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
// loadProvider 取一条启用中的服务商配置(优先指�?id,否则平台默认)
|
||||
func loadPlatformProvider(id uint64) (*models.PlatformAiProvider, error) {
|
||||
var m models.PlatformAiProvider
|
||||
qs := models.Orm.QueryTable(new(models.PlatformAiProvider)).
|
||||
@@ -252,7 +243,6 @@ func loadPlatformProvider(id uint64) (*models.PlatformAiProvider, error) {
|
||||
return &m, err
|
||||
}
|
||||
|
||||
// chatCompletion 按服务商协议类型分发调用,返回助手回复文�?
|
||||
func chatCompletion(provider *models.PlatformAiProvider, model string, messages []map[string]string) (string, error) {
|
||||
switch provider.ApiType {
|
||||
case "responses":
|
||||
@@ -277,7 +267,6 @@ func doAIRequest(req *http.Request, apiKey string) ([]byte, int, error) {
|
||||
return body, resp.StatusCode, nil
|
||||
}
|
||||
|
||||
// chatCompletions OpenAI Chat Completions:POST {api_base}/chat/completions
|
||||
func chatCompletions(provider *models.PlatformAiProvider, model string, messages []map[string]string) (string, error) {
|
||||
payload := map[string]any{"model": model, "messages": messages, "stream": false}
|
||||
body, _ := json.Marshal(payload)
|
||||
@@ -314,8 +303,6 @@ func chatCompletions(provider *models.PlatformAiProvider, model string, messages
|
||||
return out.Choices[0].Message.Content, nil
|
||||
}
|
||||
|
||||
// chatResponses OpenAI Responses:POST {api_base}/responses
|
||||
// input 直接复用 messages 数组(role/content 结构兼容�?
|
||||
func chatResponses(provider *models.PlatformAiProvider, model string, messages []map[string]string) (string, error) {
|
||||
payload := map[string]any{"model": model, "input": messages}
|
||||
body, _ := json.Marshal(payload)
|
||||
@@ -362,8 +349,6 @@ func chatResponses(provider *models.PlatformAiProvider, model string, messages [
|
||||
return "", errUpstream("上游返回为空")
|
||||
}
|
||||
|
||||
// chatAnthropic Anthropic Messages:POST {api_base}/v1/messages
|
||||
// system 消息提取到顶�?system 字段;x-api-key + anthropic-version 鉴权
|
||||
func chatAnthropic(provider *models.PlatformAiProvider, model string, messages []map[string]string) (string, error) {
|
||||
var systemParts []string
|
||||
var msgs []map[string]string
|
||||
@@ -440,8 +425,6 @@ type upstreamError string
|
||||
func (e upstreamError) Error() string { return "AI服务调用失败: " + string(e) }
|
||||
func errUpstream(msg string) error { return upstreamError(msg) }
|
||||
|
||||
// Test POST /platform/ai/provider/test { id } �?{ provider_id, model }
|
||||
// 用一�?ping 验证连通�?
|
||||
func (c *PlatformAiController) Test() {
|
||||
if _, err := requirePlatform(&c.Controller); err != nil {
|
||||
code := identity.Status(err)
|
||||
@@ -494,8 +477,6 @@ func (c *PlatformAiController) Test() {
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
// Chat POST /platform/ai/chat { provider_id, model, messages:[{role,content}] }
|
||||
// Playground 对话代理:平台管理员�?AI管理页测试各服务�?
|
||||
func (c *PlatformAiController) Chat() {
|
||||
if _, err := requirePlatform(&c.Controller); err != nil {
|
||||
code := identity.Status(err)
|
||||
@@ -525,7 +506,6 @@ func (c *PlatformAiController) Chat() {
|
||||
c.jsonErr(400, 400, "未选择模型")
|
||||
return
|
||||
}
|
||||
// 只保�?role/content,避免把多余字段透传给上�?
|
||||
clean := make([]map[string]string, 0, len(req.Messages))
|
||||
for _, m := range req.Messages {
|
||||
role := strings.TrimSpace(m["role"])
|
||||
@@ -550,10 +530,8 @@ func (c *PlatformAiController) Chat() {
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
// OCR 配置标签(存 yz_platform_site_setting,OCR 识别实现后续接入�?
|
||||
var ocrConfigTags = []string{"ocr_enabled", "ocr_provider_id", "ocr_model", "ocr_prompt"}
|
||||
|
||||
// OcrConfig GET /platform/ai/ocrConfig
|
||||
func (c *PlatformAiController) OcrConfig() {
|
||||
if _, err := requirePlatform(&c.Controller); err != nil {
|
||||
code := identity.Status(err)
|
||||
@@ -569,7 +547,6 @@ func (c *PlatformAiController) OcrConfig() {
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
// SaveOcrConfig POST /platform/ai/ocrConfig { ocr_enabled, ocr_provider_id, ocr_model, ocr_prompt }
|
||||
func (c *PlatformAiController) SaveOcrConfig() {
|
||||
if _, err := requirePlatform(&c.Controller); err != nil {
|
||||
code := identity.Status(err)
|
||||
@@ -604,8 +581,6 @@ func (c *PlatformAiController) SaveOcrConfig() {
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
// RecognizeOcr POST /platform/ai/ocr/recognize —�?OCR 识别占位接口
|
||||
// 具体实现(视觉模�?/ 专用 OCR 服务)后续接入;当前返回配置状态供前端联调�?
|
||||
func (c *PlatformAiController) RecognizeOcr() {
|
||||
if _, err := requirePlatform(&c.Controller); err != nil {
|
||||
code := identity.Status(err)
|
||||
@@ -629,9 +604,6 @@ func (c *PlatformAiController) RecognizeOcr() {
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
// ==================== 流式对话(Playground 打字机效果)====================
|
||||
|
||||
// 归一�?SSE 事件:{"t":"delta"|"think"|"done"|"error","c":"..."}
|
||||
func writeStreamEvent(w http.ResponseWriter, flusher http.Flusher, ev map[string]any) {
|
||||
b, _ := json.Marshal(ev)
|
||||
fmt.Fprintf(w, "data: %s\n\n", b)
|
||||
@@ -640,8 +612,6 @@ func writeStreamEvent(w http.ResponseWriter, flusher http.Flusher, ev map[string
|
||||
}
|
||||
}
|
||||
|
||||
// streamHTTPRequest 构造带 stream:true 的上游请求,返回 *http.Request 与鉴权头已设置的 client 请求
|
||||
// messages 的 content 支持字符串或多模态内容块数组(附件直传大模型,不落库)
|
||||
func buildStreamRequest(provider *models.PlatformAiProvider, model string, messages []map[string]any) (*http.Request, error) {
|
||||
var url string
|
||||
var payload map[string]any
|
||||
@@ -695,10 +665,6 @@ func buildStreamRequest(provider *models.PlatformAiProvider, model string, messa
|
||||
return req, nil
|
||||
}
|
||||
|
||||
// normalizeStreamData 把上�?SSE data 负载转成归一化事件列�?
|
||||
// 返回 (events, isDone, isError)
|
||||
// anthropicContent 把 OpenAI 风格的 message content 转成 Anthropic content blocks
|
||||
// 字符串 → [{type:text}];数组 → text 块 + base64 图片块(data URL 拆分)
|
||||
func anthropicContent(content any) []map[string]any {
|
||||
if s, ok := content.(string); ok {
|
||||
return []map[string]any{{"type": "text", "text": s}}
|
||||
@@ -741,7 +707,6 @@ func anthropicContent(content any) []map[string]any {
|
||||
return blocks
|
||||
}
|
||||
|
||||
// parseDataURL 解析 data:[mediatype];base64,xxxx
|
||||
func parseDataURL(u string) (media, data string, ok bool) {
|
||||
if !strings.HasPrefix(u, "data:") {
|
||||
return "", "", false
|
||||
@@ -822,7 +787,7 @@ func normalizeStreamData(apiType string, data string) ([]map[string]any, bool) {
|
||||
}
|
||||
return []map[string]any{{"t": "error", "c": msg}}, true
|
||||
}
|
||||
default: // chat_completions
|
||||
default:
|
||||
choices, _ := raw["choices"].([]any)
|
||||
if len(choices) > 0 {
|
||||
if ch, ok := choices[0].(map[string]any); ok {
|
||||
@@ -847,10 +812,8 @@ func normalizeStreamData(apiType string, data string) ([]map[string]any, bool) {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// ChatStream POST /platform/ai/chat/stream
|
||||
// SSE 代理:把三种上游协议的流式响应归一化为 {"t":"delta|think|done|error","c":"..."} 转发给前�?
|
||||
func (c *PlatformAiController) ChatStream() {
|
||||
defer c.StopRun() // 阻止 Beego 在 handler 返回后继续模板渲染
|
||||
defer c.StopRun()
|
||||
if _, err := requirePlatform(&c.Controller); err != nil {
|
||||
code := identity.Status(err)
|
||||
c.jsonErr(code, code, err.Error())
|
||||
@@ -867,7 +830,6 @@ func (c *PlatformAiController) ChatStream() {
|
||||
c.jsonErr(400, 400, "参数错误")
|
||||
return
|
||||
}
|
||||
// 落库用户消息(最后一条)
|
||||
if req.SessionID > 0 {
|
||||
persistUserMessage(req.SessionID, req.Messages[len(req.Messages)-1]["content"])
|
||||
}
|
||||
@@ -911,7 +873,6 @@ func (c *PlatformAiController) ChatStream() {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// 切换为 SSE 响应(Beego ResponseWriter 是包装类型,取底层 http.ResponseWriter)
|
||||
rw := c.Ctx.ResponseWriter.ResponseWriter
|
||||
rw.Header().Set("Content-Type", "text/event-stream; charset=utf-8")
|
||||
rw.Header().Set("Cache-Control", "no-cache")
|
||||
@@ -957,7 +918,6 @@ func (c *PlatformAiController) ChatStream() {
|
||||
persistStreamResult(req.SessionID, accContent.String(), accThink.String())
|
||||
}
|
||||
|
||||
// persistStreamResult 流式结束后落库:assistant 消息 + 会话更新时间/标题
|
||||
func persistStreamResult(sessionID uint64, content, think string) {
|
||||
if sessionID == 0 {
|
||||
return
|
||||
@@ -978,7 +938,6 @@ func persistStreamResult(sessionID uint64, content, think string) {
|
||||
Update(orm.Params{"update_time": time.Now()})
|
||||
}
|
||||
|
||||
// persistUserMessage 流式开始前落库 user 消息(content 存原始 JSON,含附件)
|
||||
func persistUserMessage(sessionID uint64, content any) {
|
||||
if sessionID == 0 {
|
||||
return
|
||||
@@ -994,7 +953,6 @@ func persistUserMessage(sessionID uint64, content any) {
|
||||
Seq: last.Seq + 1,
|
||||
CreateTime: time.Now(),
|
||||
})
|
||||
// 首条消息时回填会话标题
|
||||
var cnt int64
|
||||
_, _ = models.Orm.QueryTable(new(models.PlatformAiChatMessage)).
|
||||
Filter("session_id", sessionID).Count()
|
||||
@@ -1012,7 +970,6 @@ func persistUserMessage(sessionID uint64, content any) {
|
||||
Update(orm.Params{"update_time": time.Now()})
|
||||
}
|
||||
|
||||
// userContentText 从 user content(字符串或多模态数组)提取纯文本用于标题
|
||||
func userContentText(content any) string {
|
||||
switch v := content.(type) {
|
||||
case string:
|
||||
@@ -1031,9 +988,6 @@ func userContentText(content any) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// ==================== 会话管理 ====================
|
||||
|
||||
// ListChatSessions GET /platform/ai/chat/sessions
|
||||
func (c *PlatformAiController) ListChatSessions() {
|
||||
if _, err := requirePlatform(&c.Controller); err != nil {
|
||||
c.jsonErr(403, 403, "无权限")
|
||||
@@ -1051,7 +1005,6 @@ func (c *PlatformAiController) ListChatSessions() {
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
// CreateChatSession POST /platform/ai/chat/session {provider_id, model}
|
||||
func (c *PlatformAiController) CreateChatSession() {
|
||||
if _, err := requirePlatform(&c.Controller); err != nil {
|
||||
c.jsonErr(403, 403, "无权限")
|
||||
@@ -1078,7 +1031,6 @@ func (c *PlatformAiController) CreateChatSession() {
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
// GetChatSessionMessages GET /platform/ai/chat/session/:id/messages
|
||||
func (c *PlatformAiController) GetChatSessionMessages() {
|
||||
if _, err := requirePlatform(&c.Controller); err != nil {
|
||||
c.jsonErr(403, 403, "无权限")
|
||||
@@ -1092,7 +1044,6 @@ func (c *PlatformAiController) GetChatSessionMessages() {
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
// UpdateChatSession PUT /platform/ai/chat/session/:id {title?, is_top?}
|
||||
func (c *PlatformAiController) UpdateChatSession() {
|
||||
if _, err := requirePlatform(&c.Controller); err != nil {
|
||||
c.jsonErr(403, 403, "无权限")
|
||||
@@ -1132,7 +1083,6 @@ func (c *PlatformAiController) UpdateChatSession() {
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
// DeleteChatSession DELETE /platform/ai/chat/session/:id
|
||||
func (c *PlatformAiController) DeleteChatSession() {
|
||||
if _, err := requirePlatform(&c.Controller); err != nil {
|
||||
c.jsonErr(403, 403, "无权限")
|
||||
|
||||
Reference in new issue
Block a user