正在显示
16 个修改的文件
包含
234 行增加
和
40 行删除
| @@ -4,7 +4,7 @@ Port: 8080 | @@ -4,7 +4,7 @@ Port: 8080 | ||
| 4 | 4 | ||
| 5 | Verbose: false | 5 | Verbose: false |
| 6 | Migrate: true | 6 | Migrate: true |
| 7 | -Timeout: 30000 | 7 | +Timeout: 60000 |
| 8 | LogRequest: true # 记录详细请求日志 | 8 | LogRequest: true # 记录详细请求日志 |
| 9 | 9 | ||
| 10 | Log: | 10 | Log: |
| @@ -25,5 +25,5 @@ Redis: | @@ -25,5 +25,5 @@ Redis: | ||
| 25 | Type: node | 25 | Type: node |
| 26 | Pass: | 26 | Pass: |
| 27 | DB: | 27 | DB: |
| 28 | - DataSource: host=114.55.200.59 user=postgres password=eagle1010 dbname=su_enterprise_platform_preonline port=31543 sslmode=disable TimeZone=Asia/Shanghai | 28 | + DataSource: host=114.55.200.59 user=postgres password=eagle1010 dbname=su_enterprise_platform port=31543 sslmode=disable TimeZone=Asia/Shanghai |
| 29 | 29 |
| 1 | package chat | 1 | package chat |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | + "context" | ||
| 5 | + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/contextdata" | ||
| 6 | + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr" | ||
| 4 | "net/http" | 7 | "net/http" |
| 5 | 8 | ||
| 6 | "github.com/zeromicro/go-zero/rest/httpx" | 9 | "github.com/zeromicro/go-zero/rest/httpx" |
| @@ -16,8 +19,15 @@ func ChatSessionConversationWsHandler(svcCtx *svc.ServiceContext) http.HandlerFu | @@ -16,8 +19,15 @@ func ChatSessionConversationWsHandler(svcCtx *svc.ServiceContext) http.HandlerFu | ||
| 16 | httpx.ErrorCtx(r.Context(), w, err) | 19 | httpx.ErrorCtx(r.Context(), w, err) |
| 17 | return | 20 | return |
| 18 | } | 21 | } |
| 19 | - | ||
| 20 | - l := chat.NewChatSessionConversationWsLogic(r.Context(), svcCtx) | 22 | + var ( |
| 23 | + ctx context.Context | ||
| 24 | + err error | ||
| 25 | + ) | ||
| 26 | + if ctx, err = contextdata.ParseToken(r.Context(), svcCtx.Config.SystemAuth.AccessSecret, req.Token); err != nil { | ||
| 27 | + httpx.WriteJson(w, http.StatusUnauthorized, xerr.Error(xerr.TokenExpireError, err.Error())) | ||
| 28 | + return | ||
| 29 | + } | ||
| 30 | + l := chat.NewChatSessionConversationWsLogic(ctx, svcCtx) | ||
| 21 | resp, err := l.ChatSessionConversationWs(w, r, &req) | 31 | resp, err := l.ChatSessionConversationWs(w, r, &req) |
| 22 | if err != nil { | 32 | if err != nil { |
| 23 | httpx.ErrorCtx(r.Context(), w, err) | 33 | httpx.ErrorCtx(r.Context(), w, err) |
| @@ -53,11 +53,6 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { | @@ -53,11 +53,6 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { | ||
| 53 | Handler: chat.ChatSessionConversationHandler(serverCtx), | 53 | Handler: chat.ChatSessionConversationHandler(serverCtx), |
| 54 | }, | 54 | }, |
| 55 | { | 55 | { |
| 56 | - Method: http.MethodGet, | ||
| 57 | - Path: "/chat/session/conversation", | ||
| 58 | - Handler: chat.ChatSessionConversationWsHandler(serverCtx), | ||
| 59 | - }, | ||
| 60 | - { | ||
| 61 | Method: http.MethodPost, | 56 | Method: http.MethodPost, |
| 62 | Path: "/chat/session/add_files", | 57 | Path: "/chat/session/add_files", |
| 63 | Handler: chat.ChatSessionAddFilesHandler(serverCtx), | 58 | Handler: chat.ChatSessionAddFilesHandler(serverCtx), |
| @@ -98,6 +93,20 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { | @@ -98,6 +93,20 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { | ||
| 98 | []rest.Middleware{serverCtx.LogRequest}, | 93 | []rest.Middleware{serverCtx.LogRequest}, |
| 99 | []rest.Route{ | 94 | []rest.Route{ |
| 100 | { | 95 | { |
| 96 | + Method: http.MethodGet, | ||
| 97 | + Path: "/chat/session/conversation", | ||
| 98 | + Handler: chat.ChatSessionConversationWsHandler(serverCtx), | ||
| 99 | + }, | ||
| 100 | + }..., | ||
| 101 | + ), | ||
| 102 | + rest.WithPrefix("/v1"), | ||
| 103 | + ) | ||
| 104 | + | ||
| 105 | + server.AddRoutes( | ||
| 106 | + rest.WithMiddlewares( | ||
| 107 | + []rest.Middleware{serverCtx.LogRequest}, | ||
| 108 | + []rest.Route{ | ||
| 109 | + { | ||
| 101 | Method: http.MethodPost, | 110 | Method: http.MethodPost, |
| 102 | Path: "/chat/data/session/search", | 111 | Path: "/chat/data/session/search", |
| 103 | Handler: chat.ChatDataSessionSearchHandler(serverCtx), | 112 | Handler: chat.ChatDataSessionSearchHandler(serverCtx), |
| @@ -56,6 +56,7 @@ func (l *ChatSessionConversationLogic) ChatSessionConversation(w http.ResponseWr | @@ -56,6 +56,7 @@ func (l *ChatSessionConversationLogic) ChatSessionConversation(w http.ResponseWr | ||
| 56 | if session.Module == domain.ModuleSparkChat { | 56 | if session.Module == domain.ModuleSparkChat { |
| 57 | req.ModelId = domain.SparkChatDocModelId | 57 | req.ModelId = domain.SparkChatDocModelId |
| 58 | } | 58 | } |
| 59 | + cs := &ConversationService{} | ||
| 59 | dm = &domain.ChatSessionRecord{ | 60 | dm = &domain.ChatSessionRecord{ |
| 60 | CompanyId: token.CompanyId, | 61 | CompanyId: token.CompanyId, |
| 61 | UserId: token.UserId, | 62 | UserId: token.UserId, |
| @@ -72,7 +73,7 @@ func (l *ChatSessionConversationLogic) ChatSessionConversation(w http.ResponseWr | @@ -72,7 +73,7 @@ func (l *ChatSessionConversationLogic) ChatSessionConversation(w http.ResponseWr | ||
| 72 | var channel = make(chan string, 5) | 73 | var channel = make(chan string, 5) |
| 73 | // 异步访问AI接口 | 74 | // 异步访问AI接口 |
| 74 | fx.Parallel(func() { | 75 | fx.Parallel(func() { |
| 75 | - answer, err = Conversation(l.ctx, l.svcCtx, session, req.ModelId, req.Text, channel) | 76 | + answer, err = cs.Conversation(l.ctx, l.svcCtx, session, req.ModelId, req.Text, channel) |
| 76 | }, func() { | 77 | }, func() { |
| 77 | for { | 78 | for { |
| 78 | if _, ok = <-channel; !ok { | 79 | if _, ok = <-channel; !ok { |
| @@ -104,8 +105,11 @@ func (l *ChatSessionConversationLogic) ChatSessionConversation(w http.ResponseWr | @@ -104,8 +105,11 @@ func (l *ChatSessionConversationLogic) ChatSessionConversation(w http.ResponseWr | ||
| 104 | return | 105 | return |
| 105 | } | 106 | } |
| 106 | 107 | ||
| 108 | +type ConversationService struct { | ||
| 109 | +} | ||
| 110 | + | ||
| 107 | // Conversation 普通对话 | 111 | // Conversation 普通对话 |
| 108 | -func Conversation(ctx context.Context, svc *svc.ServiceContext, session *domain.ChatSession, modelId int64, text string, channel chan string) (answer string, err error) { | 112 | +func (c *ConversationService) Conversation(ctx context.Context, svc *svc.ServiceContext, session *domain.ChatSession, modelId int64, text string, channel chan string, messages ...ai.Message) (answer string, err error) { |
| 109 | var ( | 113 | var ( |
| 110 | m *domain.ChatModel | 114 | m *domain.ChatModel |
| 111 | ok bool | 115 | ok bool |
| @@ -121,9 +125,9 @@ func Conversation(ctx context.Context, svc *svc.ServiceContext, session *domain. | @@ -121,9 +125,9 @@ func Conversation(ctx context.Context, svc *svc.ServiceContext, session *domain. | ||
| 121 | switch m.Id { | 125 | switch m.Id { |
| 122 | // 星火3.5 | 126 | // 星火3.5 |
| 123 | case 1, 2, 3: | 127 | case 1, 2, 3: |
| 124 | - answer, err = ai.ChatGPT(m.Code, m.Config.AppKey, text, channel) | 128 | + answer, err = ai.ChatGPT(m.Code, m.Config.AppKey, text, channel, messages...) |
| 125 | case 4: | 129 | case 4: |
| 126 | - answer, err = ai.ChatSpark(m.Config.AppId, m.Config.AppKey, m.Config.AppSecret, text, channel) | 130 | + answer, err = ai.ChatSpark(m.Config.AppId, m.Config.AppKey, m.Config.AppSecret, text, channel, messages...) |
| 127 | } | 131 | } |
| 128 | if err != nil { | 132 | if err != nil { |
| 129 | return "", err | 133 | return "", err |
| @@ -2,11 +2,13 @@ package chat | @@ -2,11 +2,13 @@ package chat | ||
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "context" | 4 | "context" |
| 5 | + "errors" | ||
| 5 | "github.com/gorilla/websocket" | 6 | "github.com/gorilla/websocket" |
| 6 | "github.com/zeromicro/go-zero/core/fx" | 7 | "github.com/zeromicro/go-zero/core/fx" |
| 7 | "github.com/zeromicro/go-zero/rest/httpx" | 8 | "github.com/zeromicro/go-zero/rest/httpx" |
| 8 | "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain" | 9 | "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain" |
| 9 | "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/open" | 10 | "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/open" |
| 11 | + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/ai" | ||
| 10 | "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/contextdata" | 12 | "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/contextdata" |
| 11 | "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction" | 13 | "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction" |
| 12 | "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr" | 14 | "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr" |
| @@ -54,6 +56,7 @@ func (l *ChatSessionConversationWsLogic) ChatSessionConversationWs(w http.Respon | @@ -54,6 +56,7 @@ func (l *ChatSessionConversationWsLogic) ChatSessionConversationWs(w http.Respon | ||
| 54 | if session.Module == domain.ModuleSparkChat { | 56 | if session.Module == domain.ModuleSparkChat { |
| 55 | req.ModelId = domain.SparkChatDocModelId | 57 | req.ModelId = domain.SparkChatDocModelId |
| 56 | } | 58 | } |
| 59 | + var cs = &ConversationService{} | ||
| 57 | var answer string | 60 | var answer string |
| 58 | var upgrader = websocket.Upgrader{ | 61 | var upgrader = websocket.Upgrader{ |
| 59 | ReadBufferSize: 1024, | 62 | ReadBufferSize: 1024, |
| @@ -64,6 +67,7 @@ func (l *ChatSessionConversationWsLogic) ChatSessionConversationWs(w http.Respon | @@ -64,6 +67,7 @@ func (l *ChatSessionConversationWsLogic) ChatSessionConversationWs(w http.Respon | ||
| 64 | } | 67 | } |
| 65 | var wsconn *websocket.Conn | 68 | var wsconn *websocket.Conn |
| 66 | wsconn, err = upgrader.Upgrade(w, r, nil) | 69 | wsconn, err = upgrader.Upgrade(w, r, nil) |
| 70 | + wsconn.SetPingHandler(nil) | ||
| 67 | if err != nil { | 71 | if err != nil { |
| 68 | httpx.ErrorCtx(r.Context(), w, err) | 72 | httpx.ErrorCtx(r.Context(), w, err) |
| 69 | return | 73 | return |
| @@ -71,12 +75,33 @@ func (l *ChatSessionConversationWsLogic) ChatSessionConversationWs(w http.Respon | @@ -71,12 +75,33 @@ func (l *ChatSessionConversationWsLogic) ChatSessionConversationWs(w http.Respon | ||
| 71 | defer func() { | 75 | defer func() { |
| 72 | wsconn.Close() | 76 | wsconn.Close() |
| 73 | }() | 77 | }() |
| 78 | + | ||
| 79 | + // 心跳 | ||
| 80 | + //go func() { | ||
| 81 | + // ticker := time.NewTicker(30 * time.Second) | ||
| 82 | + // defer ticker.Stop() | ||
| 83 | + // for { | ||
| 84 | + // select { | ||
| 85 | + // case <-ticker.C: | ||
| 86 | + // if err := wsconn.WriteMessage(websocket.PingMessage, nil); err != nil { | ||
| 87 | + // logx.Error("Ping error:", err) | ||
| 88 | + // return | ||
| 89 | + // } | ||
| 90 | + // } | ||
| 91 | + // } | ||
| 92 | + //}() | ||
| 93 | + history := make([]ai.Message, 0) | ||
| 74 | for { | 94 | for { |
| 75 | var text []byte | 95 | var text []byte |
| 76 | _, text, err = wsconn.ReadMessage() | 96 | _, text, err = wsconn.ReadMessage() |
| 77 | if err != nil { | 97 | if err != nil { |
| 78 | break | 98 | break |
| 79 | } | 99 | } |
| 100 | + if string(text) == "ping" { | ||
| 101 | + logx.Infof("->> 收到心跳 用户:%s 会话:%s %s", user.Name, session.Title, string(text)) | ||
| 102 | + wsconn.WriteMessage(websocket.PongMessage, text) | ||
| 103 | + continue | ||
| 104 | + } | ||
| 80 | var beginUnix = time.Now().UnixMilli() | 105 | var beginUnix = time.Now().UnixMilli() |
| 81 | var channel = make(chan string, 5) | 106 | var channel = make(chan string, 5) |
| 82 | dm = &domain.ChatSessionRecord{ | 107 | dm = &domain.ChatSessionRecord{ |
| @@ -93,13 +118,12 @@ func (l *ChatSessionConversationWsLogic) ChatSessionConversationWs(w http.Respon | @@ -93,13 +118,12 @@ func (l *ChatSessionConversationWsLogic) ChatSessionConversationWs(w http.Respon | ||
| 93 | } | 118 | } |
| 94 | fx.Parallel(func() { | 119 | fx.Parallel(func() { |
| 95 | // 异步访问AI接口 | 120 | // 异步访问AI接口 |
| 96 | - answer, err = Conversation(l.ctx, l.svcCtx, session, req.ModelId, string(text), channel) | 121 | + answer, err = cs.Conversation(l.ctx, l.svcCtx, session, req.ModelId, string(text), channel, history...) |
| 97 | }, func() { | 122 | }, func() { |
| 98 | for { | 123 | for { |
| 99 | var v string | 124 | var v string |
| 100 | if v, ok = <-channel; ok { | 125 | if v, ok = <-channel; ok { |
| 101 | if err = wsconn.WriteJSON(types.ChatSessionConversationResponse{Parts: []string{v}, Finished: false}); err != nil { | 126 | if err = wsconn.WriteJSON(types.ChatSessionConversationResponse{Parts: []string{v}, Finished: false}); err != nil { |
| 102 | - //httpx.ErrorCtx(r.Context(), w, err) | ||
| 103 | dm.Status = domain.FinishedFail | 127 | dm.Status = domain.FinishedFail |
| 104 | break | 128 | break |
| 105 | } | 129 | } |
| @@ -110,7 +134,14 @@ func (l *ChatSessionConversationWsLogic) ChatSessionConversationWs(w http.Respon | @@ -110,7 +134,14 @@ func (l *ChatSessionConversationWsLogic) ChatSessionConversationWs(w http.Respon | ||
| 110 | } | 134 | } |
| 111 | return | 135 | return |
| 112 | }) | 136 | }) |
| 113 | - | 137 | + history = append(history, ai.Message{ |
| 138 | + Role: "user", | ||
| 139 | + Content: string(text), | ||
| 140 | + }) | ||
| 141 | + //, ai.Message{ | ||
| 142 | + // Role: "assistant", | ||
| 143 | + // Content: string(answer), | ||
| 144 | + // } | ||
| 114 | // 记录 | 145 | // 记录 |
| 115 | dm.AnswerText = answer | 146 | dm.AnswerText = answer |
| 116 | dm.Cost = time.Now().UnixMilli() - beginUnix | 147 | dm.Cost = time.Now().UnixMilli() - beginUnix |
| @@ -127,6 +158,11 @@ func (l *ChatSessionConversationWsLogic) ChatSessionConversationWs(w http.Respon | @@ -127,6 +158,11 @@ func (l *ChatSessionConversationWsLogic) ChatSessionConversationWs(w http.Respon | ||
| 127 | } | 158 | } |
| 128 | 159 | ||
| 129 | if err != nil { | 160 | if err != nil { |
| 161 | + var closeErr *websocket.CloseError | ||
| 162 | + if ok = errors.As(err, &closeErr); ok { | ||
| 163 | + logx.Info("Close connect->", err.Error()) | ||
| 164 | + return nil, nil | ||
| 165 | + } | ||
| 130 | return nil, xerr.NewErrMsgErr("AI模型异常,稍后再试", err) | 166 | return nil, xerr.NewErrMsgErr("AI模型异常,稍后再试", err) |
| 131 | } | 167 | } |
| 132 | return | 168 | return |
| @@ -110,6 +110,7 @@ type ChatSessionConversationResponse struct { | @@ -110,6 +110,7 @@ type ChatSessionConversationResponse struct { | ||
| 110 | } | 110 | } |
| 111 | 111 | ||
| 112 | type ChatSessionConversationRequestWs struct { | 112 | type ChatSessionConversationRequestWs struct { |
| 113 | + Token string `form:"token,optional"` // 授权的token | ||
| 113 | SessionId int64 `form:"sessionId"` // 会话ID | 114 | SessionId int64 `form:"sessionId"` // 会话ID |
| 114 | ModelId int64 `form:"modelId"` // 模型ID | 115 | ModelId int64 `form:"modelId"` // 模型ID |
| 115 | ContentType string `form:"contentType"` // 内容类型 文本:text (图片:image 文档:document) | 116 | ContentType string `form:"contentType"` // 内容类型 文本:text (图片:image 文档:document) |
| @@ -29,9 +29,9 @@ service Core { | @@ -29,9 +29,9 @@ service Core { | ||
| 29 | @doc "聊天会话-对话" | 29 | @doc "聊天会话-对话" |
| 30 | @handler chatSessionConversation | 30 | @handler chatSessionConversation |
| 31 | post /chat/session/conversation (ChatSessionConversationRequest) returns (ChatSessionConversationResponse) | 31 | post /chat/session/conversation (ChatSessionConversationRequest) returns (ChatSessionConversationResponse) |
| 32 | - @doc "聊天会话-对话" | ||
| 33 | - @handler chatSessionConversationWs | ||
| 34 | - get /chat/session/conversation (ChatSessionConversationRequestWs) returns (ChatSessionConversationResponse) | 32 | +// @doc "聊天会话-对话" |
| 33 | +// @handler chatSessionConversationWs | ||
| 34 | +// get /chat/session/conversation (ChatSessionConversationRequestWs) returns (ChatSessionConversationResponse) | ||
| 35 | 35 | ||
| 36 | @doc "聊天会话-添加文件" | 36 | @doc "聊天会话-添加文件" |
| 37 | @handler chatSessionAddFiles | 37 | @handler chatSessionAddFiles |
| @@ -56,6 +56,18 @@ service Core { | @@ -56,6 +56,18 @@ service Core { | ||
| 56 | get /chat/models (ChatModelsRequest) returns (ChatModelsResponse) | 56 | get /chat/models (ChatModelsRequest) returns (ChatModelsResponse) |
| 57 | } | 57 | } |
| 58 | 58 | ||
| 59 | +// 后台接口 | ||
| 60 | +@server( | ||
| 61 | + prefix: v1 | ||
| 62 | + group: chat | ||
| 63 | + middleware: LogRequest | ||
| 64 | +) | ||
| 65 | +service Core { | ||
| 66 | + @doc "聊天会话-对话" | ||
| 67 | + @handler chatSessionConversationWs | ||
| 68 | + get /chat/session/conversation (ChatSessionConversationRequestWs) returns (ChatSessionConversationResponse) | ||
| 69 | +} | ||
| 70 | + | ||
| 59 | 71 | ||
| 60 | // 数据管理后台接口 | 72 | // 数据管理后台接口 |
| 61 | @server( | 73 | @server( |
| @@ -183,6 +195,7 @@ type( | @@ -183,6 +195,7 @@ type( | ||
| 183 | Finished bool `json:"finished"` | 195 | Finished bool `json:"finished"` |
| 184 | } | 196 | } |
| 185 | ChatSessionConversationRequestWs{ | 197 | ChatSessionConversationRequestWs{ |
| 198 | + Token string `form:"token,optional"` // 授权的token | ||
| 186 | SessionId int64 `form:"sessionId"` // 会话ID | 199 | SessionId int64 `form:"sessionId"` // 会话ID |
| 187 | ModelId int64 `form:"modelId"` // 模型ID | 200 | ModelId int64 `form:"modelId"` // 模型ID |
| 188 | ContentType string `form:"contentType"` // 内容类型 文本:text (图片:image 文档:document) | 201 | ContentType string `form:"contentType"` // 内容类型 文本:text (图片:image 文档:document) |
| @@ -2,6 +2,7 @@ package repository | @@ -2,6 +2,7 @@ package repository | ||
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "context" | 4 | "context" |
| 5 | + "fmt" | ||
| 5 | "github.com/jinzhu/copier" | 6 | "github.com/jinzhu/copier" |
| 6 | "github.com/pkg/errors" | 7 | "github.com/pkg/errors" |
| 7 | "github.com/tiptok/gocomm/pkg/cache" | 8 | "github.com/tiptok/gocomm/pkg/cache" |
| @@ -187,7 +188,12 @@ func (repository *ChatSessionRecordRepository) FindByCompanyUser(ctx context.Con | @@ -187,7 +188,12 @@ func (repository *ChatSessionRecordRepository) FindByCompanyUser(ctx context.Con | ||
| 187 | total int64 | 188 | total int64 |
| 188 | ) | 189 | ) |
| 189 | queryFunc := func() (interface{}, error) { | 190 | queryFunc := func() (interface{}, error) { |
| 190 | - tx = tx.Model(&ms).Order("id asc") | 191 | + tx = tx.Model(&ms) |
| 192 | + if v, ok := queryOptions["orderById"]; ok { | ||
| 193 | + tx.Order(fmt.Sprintf("id %v", v)) | ||
| 194 | + } else { | ||
| 195 | + tx.Order("id asc") | ||
| 196 | + } | ||
| 191 | tx.Where("company_id = ?", companyId).Where("user_id = ?", userId) | 197 | tx.Where("company_id = ?", companyId).Where("user_id = ?", userId) |
| 192 | if v, ok := queryOptions["sessionId"]; ok { | 198 | if v, ok := queryOptions["sessionId"]; ok { |
| 193 | tx.Where("session_id =?", v) | 199 | tx.Where("session_id =?", v) |
| @@ -50,13 +50,14 @@ func (l *SystemEmployeeUpdateLogic) SystemEmployeeUpdate(req *types.EmployeeUpda | @@ -50,13 +50,14 @@ func (l *SystemEmployeeUpdateLogic) SystemEmployeeUpdate(req *types.EmployeeUpda | ||
| 50 | if employee.Code != req.Employee.Code { | 50 | if employee.Code != req.Employee.Code { |
| 51 | // 员工编码唯一 | 51 | // 员工编码唯一 |
| 52 | if req.Employee.Code != "" { | 52 | if req.Employee.Code != "" { |
| 53 | - if employee, err = l.svcCtx.EmployeeRepository.FindOneByCode(l.ctx, conn, token.CompanyId, req.Employee.Code); err == nil { | 53 | + var foundEmployee *domain.SysEmployee |
| 54 | + if foundEmployee, err = l.svcCtx.EmployeeRepository.FindOneByCode(l.ctx, conn, token.CompanyId, req.Employee.Code); foundEmployee != nil && err == nil { | ||
| 54 | return nil, xerr.NewErrMsgErr("工号重复", err) | 55 | return nil, xerr.NewErrMsgErr("工号重复", err) |
| 55 | } | 56 | } |
| 56 | } | 57 | } |
| 57 | } | 58 | } |
| 58 | if err = transaction.MustUseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error { | 59 | if err = transaction.MustUseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error { |
| 59 | - // 修改姓名 | 60 | + // 修改用户姓名 |
| 60 | if user.Name != req.Employee.Name && req.Employee.Phone == user.Phone { | 61 | if user.Name != req.Employee.Name && req.Employee.Phone == user.Phone { |
| 61 | user.Name = req.Employee.Name | 62 | user.Name = req.Employee.Name |
| 62 | if user, err = l.svcCtx.UserRepository.UpdateWithVersion(l.ctx, conn, user); err != nil { | 63 | if user, err = l.svcCtx.UserRepository.UpdateWithVersion(l.ctx, conn, user); err != nil { |
| @@ -7,3 +7,24 @@ update user_department | @@ -7,3 +7,24 @@ update user_department | ||
| 7 | set employee_id = employee.id | 7 | set employee_id = employee.id |
| 8 | from employee | 8 | from employee |
| 9 | where employee.user_id = user_department.user_id; | 9 | where employee.user_id = user_department.user_id; |
| 10 | + | ||
| 11 | + | ||
| 12 | +/* | ||
| 13 | +-- suplus_enterprise | ||
| 14 | +-- 用户表 | ||
| 15 | +select user_id id,name,phone,avatar,UNIX_TIMESTAMP() created_at,UNIX_TIMESTAMP() updated_at,0 deleted_at,0 version,0 is_del from user_info | ||
| 16 | +where company_id = 1 and user_id <>0 and name ='林忠' | ||
| 17 | + | ||
| 18 | +-- 职员表 | ||
| 19 | +select uid id,user_id,company_id,'' code,(case when status=1 then 1 else 2 end) account_status,'正式' employee_type,'{}' base_info,'{}' work_info,UNIX_TIMESTAMP() created_at,UNIX_TIMESTAMP() updated_at,0 deleted_at,0 version,0 is_del from user_info | ||
| 20 | +where company_id = 1 and user_id <>0 | ||
| 21 | + | ||
| 22 | + | ||
| 23 | +-- suplus_business_admin | ||
| 24 | +-- 部门表 | ||
| 25 | +select id,company_id,name,'' code,parent_id,'[]' department_heads,false is_root,1 sort,UNIX_TIMESTAMP() created_at,UNIX_TIMESTAMP() updated_at,0 deleted_at,0 version,0 is_del from departments where company_id = 1 | ||
| 26 | + | ||
| 27 | +-- 用户部门表 | ||
| 28 | +select company_id,department_id,user_id,0 employee_id,UNIX_TIMESTAMP() created_at from user_departments where company_id = 1 and deleted_at is null | ||
| 29 | +-- 关联职员表更新职员id | ||
| 30 | +*/ |
此 diff 太大无法显示。
| 1 | +apiVersion: v1 | ||
| 2 | +kind: ConfigMap | ||
| 3 | +metadata: | ||
| 4 | + name: sumicro-chat-config-prd | ||
| 5 | +data: | ||
| 6 | + config.yml: | | ||
| 7 | + Name: sumicro-chat-prd | ||
| 8 | + Host: 0.0.0.0 | ||
| 9 | + Port: 8080 | ||
| 10 | + | ||
| 11 | + Verbose: false | ||
| 12 | + Migrate: true | ||
| 13 | + Timeout: 30000 | ||
| 14 | + LogRequest: true # 记录详细请求日志 | ||
| 15 | + | ||
| 16 | + Log: | ||
| 17 | + Mode: file | ||
| 18 | + Encoding: plain | ||
| 19 | + Level: debug # info | ||
| 20 | + MaxSize: 1 # 2MB | ||
| 21 | + TimeFormat: 2006-01-02 15:04:05 | ||
| 22 | + Rotation: size | ||
| 23 | + MaxContentLength: 10240 | ||
| 24 | + | ||
| 25 | + SystemAuth: | ||
| 26 | + AccessSecret: su-platform | ||
| 27 | + AccessExpire: 360000 | ||
| 28 | + | ||
| 29 | + Redis: | ||
| 30 | + Host: 192.168.0.243:6379 | ||
| 31 | + Type: node | ||
| 32 | + Pass: | ||
| 33 | + DB: | ||
| 34 | + DataSource: host=114.55.200.59 user=postgres password=eagle1010 dbname=su_enterprise_platform port=31544 sslmode=disable TimeZone=Asia/Shanghai | ||
| 35 | +--- | ||
| 36 | +apiVersion: v1 | ||
| 37 | +kind: ConfigMap | ||
| 38 | +metadata: | ||
| 39 | + name: sumicro-system-config-prd | ||
| 40 | +data: | ||
| 41 | + config.yml: | | ||
| 42 | + Name: sumicro-system-prd | ||
| 43 | + Host: 0.0.0.0 | ||
| 44 | + Port: 8080 | ||
| 45 | + | ||
| 46 | + Verbose: false | ||
| 47 | + Migrate: true | ||
| 48 | + Timeout: 30000 | ||
| 49 | + LogRequest: true | ||
| 50 | + | ||
| 51 | + Log: | ||
| 52 | + Mode: file | ||
| 53 | + Encoding: plain | ||
| 54 | + Level: debug | ||
| 55 | + MaxSize: 1 | ||
| 56 | + TimeFormat: 2006-01-02 15:04:05 | ||
| 57 | + Rotation: size | ||
| 58 | + MaxContentLength: 10240 | ||
| 59 | + | ||
| 60 | + SystemAuth: | ||
| 61 | + AccessSecret: su-platform | ||
| 62 | + AccessExpire: 360000 | ||
| 63 | + | ||
| 64 | + Redis: | ||
| 65 | + Host: 192.168.0.243:6379 | ||
| 66 | + Type: node | ||
| 67 | + Pass: | ||
| 68 | + DB: | ||
| 69 | + DataSource: host=114.55.200.59 user=postgres password=eagle1010 dbname=su_enterprise_platform port=31544 sslmode=disable TimeZone=Asia/Shanghai |
| @@ -9,7 +9,7 @@ import ( | @@ -9,7 +9,7 @@ import ( | ||
| 9 | "io" | 9 | "io" |
| 10 | ) | 10 | ) |
| 11 | 11 | ||
| 12 | -func ChatGPT(gptModelCode, key string, question string, channel chan string) (answer string, err error) { | 12 | +func ChatGPT(gptModelCode, key string, question string, channel chan string, messages ...Message) (answer string, err error) { |
| 13 | config := openai.DefaultConfig(key) | 13 | config := openai.DefaultConfig(key) |
| 14 | config.BaseURL = "http://47.251.84.160:8080/v1" // "https://api.openai.com/v1" // | 14 | config.BaseURL = "http://47.251.84.160:8080/v1" // "https://api.openai.com/v1" // |
| 15 | c := openai.NewClientWithConfig(config) | 15 | c := openai.NewClientWithConfig(config) |
| @@ -18,14 +18,19 @@ func ChatGPT(gptModelCode, key string, question string, channel chan string) (an | @@ -18,14 +18,19 @@ func ChatGPT(gptModelCode, key string, question string, channel chan string) (an | ||
| 18 | req := openai.ChatCompletionRequest{ | 18 | req := openai.ChatCompletionRequest{ |
| 19 | Model: gptModelCode, | 19 | Model: gptModelCode, |
| 20 | MaxTokens: 2048, | 20 | MaxTokens: 2048, |
| 21 | - Messages: []openai.ChatCompletionMessage{ | ||
| 22 | - { | ||
| 23 | - Role: openai.ChatMessageRoleUser, | ||
| 24 | - Content: question, | ||
| 25 | - }, | ||
| 26 | - }, | 21 | + Messages: []openai.ChatCompletionMessage{}, |
| 27 | Stream: true, | 22 | Stream: true, |
| 28 | } | 23 | } |
| 24 | + for _, m := range messages { | ||
| 25 | + req.Messages = append(req.Messages, openai.ChatCompletionMessage{ | ||
| 26 | + Role: m.Role, | ||
| 27 | + Content: m.Content, | ||
| 28 | + }) | ||
| 29 | + } | ||
| 30 | + req.Messages = append(req.Messages, openai.ChatCompletionMessage{ | ||
| 31 | + Role: openai.ChatMessageRoleUser, | ||
| 32 | + Content: question, | ||
| 33 | + }) | ||
| 29 | stream, err := c.CreateChatCompletionStream(ctx, req) | 34 | stream, err := c.CreateChatCompletionStream(ctx, req) |
| 30 | if err != nil { | 35 | if err != nil { |
| 31 | fmt.Printf("ChatCompletionStream error: %v\n", err) | 36 | fmt.Printf("ChatCompletionStream error: %v\n", err) |
| @@ -29,7 +29,7 @@ var ( | @@ -29,7 +29,7 @@ var ( | ||
| 29 | hostChatDocumentUrl = "wss://chatdoc.xfyun.cn/openapi/chat" | 29 | hostChatDocumentUrl = "wss://chatdoc.xfyun.cn/openapi/chat" |
| 30 | ) | 30 | ) |
| 31 | 31 | ||
| 32 | -func ChatSpark(appid string, apiKey string, apiSecret string, question string, channel chan string) (answer string, err error) { | 32 | +func ChatSpark(appid string, apiKey string, apiSecret string, question string, channel chan string, messages ...Message) (answer string, err error) { |
| 33 | // fmt.Println(HmacWithShaTobase64("hmac-sha256", "hello\nhello", "hello")) | 33 | // fmt.Println(HmacWithShaTobase64("hmac-sha256", "hello\nhello", "hello")) |
| 34 | // st := time.Now() | 34 | // st := time.Now() |
| 35 | d := websocket.Dialer{ | 35 | d := websocket.Dialer{ |
| @@ -45,7 +45,7 @@ func ChatSpark(appid string, apiKey string, apiSecret string, question string, c | @@ -45,7 +45,7 @@ func ChatSpark(appid string, apiKey string, apiSecret string, question string, c | ||
| 45 | } | 45 | } |
| 46 | 46 | ||
| 47 | go func() { | 47 | go func() { |
| 48 | - data := genParams1(appid, question) | 48 | + data := genParams(appid, question, messages...) |
| 49 | conn.WriteJSON(data) | 49 | conn.WriteJSON(data) |
| 50 | 50 | ||
| 51 | }() | 51 | }() |
| @@ -119,11 +119,11 @@ type SparkChatMessage struct { | @@ -119,11 +119,11 @@ type SparkChatMessage struct { | ||
| 119 | } | 119 | } |
| 120 | 120 | ||
| 121 | // 生成参数 | 121 | // 生成参数 |
| 122 | -func genParams1(appid, question string) map[string]interface{} { // 根据实际情况修改返回的数据结构和字段名 | ||
| 123 | - | ||
| 124 | - messages := []Message{ | ||
| 125 | - {Role: "user", Content: question}, | ||
| 126 | - } | 122 | +func genParams(appid, question string, list ...Message) map[string]interface{} { // 根据实际情况修改返回的数据结构和字段名 |
| 123 | + var messages = list | ||
| 124 | + messages = append(messages, Message{ | ||
| 125 | + Role: "user", Content: question, | ||
| 126 | + }) | ||
| 127 | 127 | ||
| 128 | data := map[string]interface{}{ // 根据实际情况修改返回的数据结构和字段名 | 128 | data := map[string]interface{}{ // 根据实际情况修改返回的数据结构和字段名 |
| 129 | "header": map[string]interface{}{ // 根据实际情况修改返回的数据结构和字段名 | 129 | "header": map[string]interface{}{ // 根据实际情况修改返回的数据结构和字段名 |
| 1 | package contextdata | 1 | package contextdata |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | + "context" | ||
| 4 | "github.com/golang-jwt/jwt/v4" | 5 | "github.com/golang-jwt/jwt/v4" |
| 5 | "time" | 6 | "time" |
| 6 | ) | 7 | ) |
| @@ -24,3 +25,16 @@ func (tk UserToken) GenerateToken(secret string, expire int64) (string, error) { | @@ -24,3 +25,16 @@ func (tk UserToken) GenerateToken(secret string, expire int64) (string, error) { | ||
| 24 | 25 | ||
| 25 | return token.SignedString([]byte(secret)) | 26 | return token.SignedString([]byte(secret)) |
| 26 | } | 27 | } |
| 28 | + | ||
| 29 | +func ParseToken(ctx context.Context, secret string, tokenStr string) (context.Context, error) { | ||
| 30 | + token, err := jwt.Parse(tokenStr, func(token *jwt.Token) (interface{}, error) { | ||
| 31 | + return []byte(secret), nil | ||
| 32 | + }) | ||
| 33 | + if err != nil { | ||
| 34 | + return ctx, err | ||
| 35 | + } | ||
| 36 | + for k, v := range token.Claims.(jwt.MapClaims) { | ||
| 37 | + ctx = context.WithValue(ctx, k, v) | ||
| 38 | + } | ||
| 39 | + return ctx, nil | ||
| 40 | +} |
| @@ -3,7 +3,10 @@ package contextdata | @@ -3,7 +3,10 @@ package contextdata | ||
| 3 | import ( | 3 | import ( |
| 4 | "context" | 4 | "context" |
| 5 | "encoding/json" | 5 | "encoding/json" |
| 6 | + "fmt" | ||
| 6 | "github.com/zeromicro/go-zero/core/logx" | 7 | "github.com/zeromicro/go-zero/core/logx" |
| 8 | + "strconv" | ||
| 9 | + "strings" | ||
| 7 | ) | 10 | ) |
| 8 | 11 | ||
| 9 | var ( | 12 | var ( |
| @@ -14,12 +17,14 @@ var ( | @@ -14,12 +17,14 @@ var ( | ||
| 14 | 17 | ||
| 15 | func GetInt64FromCtx(ctx context.Context, key string) int64 { | 18 | func GetInt64FromCtx(ctx context.Context, key string) int64 { |
| 16 | var uid int64 | 19 | var uid int64 |
| 17 | - if jsonUid, ok := ctx.Value(key).(json.Number); ok { | ||
| 18 | - if int64Uid, err := jsonUid.Int64(); err == nil { | ||
| 19 | - uid = int64Uid | ||
| 20 | - } else { | 20 | + if value := ctx.Value(key); value != nil { |
| 21 | + valueStr := strings.Trim(fmt.Sprintf("%v", value), `"`) | ||
| 22 | + i64, err := strconv.ParseInt(valueStr, 10, 64) | ||
| 23 | + if err != nil { | ||
| 21 | logx.WithContext(ctx).Errorf("GetUidFromCtx err : %+v", err) | 24 | logx.WithContext(ctx).Errorf("GetUidFromCtx err : %+v", err) |
| 22 | } | 25 | } |
| 26 | + uid = i64 | ||
| 27 | + return uid | ||
| 23 | } | 28 | } |
| 24 | return uid | 29 | return uid |
| 25 | } | 30 | } |
-
请 注册 或 登录 后发表评论