|
@@ -1,15 +1,19 @@
|
|
|
package rag
|
|
|
|
|
|
import (
|
|
|
+ "encoding/json"
|
|
|
"eta/eta_api/controllers"
|
|
|
"eta/eta_api/models"
|
|
|
+ "eta/eta_api/models/llm"
|
|
|
"eta/eta_api/models/system"
|
|
|
"eta/eta_api/services/llm/facade"
|
|
|
"eta/eta_api/utils"
|
|
|
"eta/eta_api/utils/ws"
|
|
|
+ "fmt"
|
|
|
"github.com/gorilla/websocket"
|
|
|
"net"
|
|
|
"net/http"
|
|
|
+ "strings"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
@@ -18,16 +22,128 @@ type ChatController struct {
|
|
|
}
|
|
|
|
|
|
func (cc *ChatController) Prepare() {
|
|
|
- cc.SysUser = cc.Ctx.Input.GetData("admin").(*system.Admin)
|
|
|
- if cc.SysUser == nil || cc.SysUser.AdminId == 0 {
|
|
|
- utils.FileLog.Error("用户信息不存在")
|
|
|
+ method := cc.Ctx.Input.Method()
|
|
|
+ uri := cc.Ctx.Input.URI()
|
|
|
+ if method == "GET" {
|
|
|
+ authorization := cc.Ctx.Input.Header("authorization")
|
|
|
+ if authorization == "" {
|
|
|
+ authorization = cc.Ctx.Input.Header("Authorization")
|
|
|
+ }
|
|
|
+ if strings.Contains(authorization, ";") {
|
|
|
+ authorization = strings.Replace(authorization, ";", "$", 1)
|
|
|
+ }
|
|
|
+ if authorization == "" {
|
|
|
+ strArr := strings.Split(uri, "?")
|
|
|
+ for k, v := range strArr {
|
|
|
+ fmt.Println(k, v)
|
|
|
+ }
|
|
|
+ if len(strArr) > 1 {
|
|
|
+ authorization = strArr[1]
|
|
|
+ authorization = strings.Replace(authorization, "Authorization", "authorization", -1)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if authorization == "" {
|
|
|
+ utils.FileLog.Error("authorization为空,未授权")
|
|
|
+ cc.Ctx.ResponseWriter.WriteHeader(http.StatusUnauthorized)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ tokenStr := authorization
|
|
|
+ tokenArr := strings.Split(tokenStr, "=")
|
|
|
+ token := tokenArr[1]
|
|
|
+
|
|
|
+ session, err := system.GetSysSessionByToken(token)
|
|
|
+ if err != nil {
|
|
|
+ if utils.IsErrNoRow(err) {
|
|
|
+ utils.FileLog.Error("authorization已过期")
|
|
|
+ cc.Ctx.ResponseWriter.WriteHeader(http.StatusUnauthorized)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ utils.FileLog.Error("authorization查询用户信息失败")
|
|
|
+ cc.Ctx.ResponseWriter.WriteHeader(http.StatusBadRequest)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if session == nil {
|
|
|
+ utils.FileLog.Error("会话不存在")
|
|
|
+ cc.Ctx.ResponseWriter.WriteHeader(http.StatusBadRequest)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ account := utils.MD5(session.UserName)
|
|
|
+ if !utils.CheckToken(account, token) {
|
|
|
+ utils.FileLog.Error("authorization校验不合法")
|
|
|
+ cc.Ctx.ResponseWriter.WriteHeader(http.StatusUnauthorized)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if time.Now().After(session.ExpiredTime) {
|
|
|
+ utils.FileLog.Error("authorization过期法")
|
|
|
+ cc.Ctx.ResponseWriter.WriteHeader(http.StatusUnauthorized)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ admin, err := system.GetSysUserById(session.SysUserId)
|
|
|
+ if err != nil {
|
|
|
+ if utils.IsErrNoRow(err) {
|
|
|
+ utils.FileLog.Error("权限不够")
|
|
|
+ cc.Ctx.ResponseWriter.WriteHeader(http.StatusForbidden)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ utils.FileLog.Error("获取用户信息失败")
|
|
|
+ cc.Ctx.ResponseWriter.WriteHeader(http.StatusBadRequest)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if admin == nil {
|
|
|
+ utils.FileLog.Error("权限不够")
|
|
|
+ cc.Ctx.ResponseWriter.WriteHeader(http.StatusForbidden)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if admin.Enabled != 1 {
|
|
|
+ utils.FileLog.Error("用户被禁用")
|
|
|
+ cc.Ctx.ResponseWriter.WriteHeader(http.StatusForbidden)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ roleId := admin.RoleId
|
|
|
+ list, e := system.GetMenuButtonApisByRoleId(roleId)
|
|
|
+ if e != nil {
|
|
|
+ utils.FileLog.Error("接口权限查询出错", e)
|
|
|
+ cc.Ctx.ResponseWriter.WriteHeader(http.StatusForbidden)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var api string
|
|
|
+ for _, v := range list {
|
|
|
+ if v.Api != "" {
|
|
|
+ api += v.Api + "&"
|
|
|
+ }
|
|
|
+ }
|
|
|
+ api += "&" + models.BusinessConfMap["PublicApi"]
|
|
|
+
|
|
|
+ api = strings.TrimRight(api, "&")
|
|
|
+ uri = strings.Replace(uri, "/adminapi", "", 1)
|
|
|
+ uris := strings.Split(uri, "?")
|
|
|
+ uri = uris[0]
|
|
|
+
|
|
|
+ apis := strings.Split(api, "&")
|
|
|
+ apiMap := make(map[string]bool, 0)
|
|
|
+ for _, s := range apis {
|
|
|
+ apiMap[s] = true
|
|
|
+ }
|
|
|
+ if !apiMap[uri] {
|
|
|
+ utils.FileLog.Error("用户无权访问")
|
|
|
+ cc.Ctx.ResponseWriter.WriteHeader(http.StatusForbidden)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ cc.SysUser = admin
|
|
|
+ } else {
|
|
|
+ utils.FileLog.Error("请求方法类型错误")
|
|
|
cc.Ctx.ResponseWriter.WriteHeader(http.StatusBadRequest)
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
|
|
|
func (kbctrl *KbController) NewChat() {
|
|
@@ -36,6 +152,13 @@ func (kbctrl *KbController) NewChat() {
|
|
|
kbctrl.Data["json"] = br
|
|
|
kbctrl.ServeJSON()
|
|
|
}()
|
|
|
+ var req facade.LLMKnowledgeSearch
|
|
|
+ err := json.Unmarshal(kbctrl.Ctx.Input.RequestBody, &req)
|
|
|
+ if err != nil {
|
|
|
+ br.Msg = "参数解析异常!"
|
|
|
+ br.ErrMsg = "参数解析失败,Err:" + err.Error()
|
|
|
+ return
|
|
|
+ }
|
|
|
sysUser := kbctrl.SysUser
|
|
|
if sysUser == nil {
|
|
|
br.Msg = "请登录"
|
|
@@ -43,12 +166,17 @@ func (kbctrl *KbController) NewChat() {
|
|
|
br.Ret = 408
|
|
|
return
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+ session := llm.UserLlmChat{
|
|
|
+ UserId: sysUser.AdminId,
|
|
|
+ CreatedTime: time.Now(),
|
|
|
+ ChatTitle: "新会话",
|
|
|
+ }
|
|
|
+ err = session.CreateChatSession()
|
|
|
+ if err != nil {
|
|
|
+ br.Msg = "创建失败"
|
|
|
+ br.ErrMsg = "创建失败,Err:" + err.Error()
|
|
|
+ return
|
|
|
+ }
|
|
|
br.Ret = 200
|
|
|
br.Success = true
|
|
|
br.Msg = "创建成功"
|