فهرست منبع

feat:聊天记录

wangshan 2 سال پیش
والد
کامیت
cf90390118
5فایلهای تغییر یافته به همراه17 افزوده شده و 9 حذف شده
  1. 3 2
      api/v1/aiChatApi.go
  2. 4 1
      internal/controller/chatHistory.go
  3. 6 2
      internal/model/chatHistory.go
  4. 2 2
      internal/model/question.go
  5. 2 2
      internal/model/ws.go

+ 3 - 2
api/v1/aiChatApi.go

@@ -7,8 +7,9 @@ import (
 // ChatHistoryReq 历史记录
 type ChatHistoryReq struct {
 	g.Meta   `path:"/chatHistory" tags:"AiChat" method:"post" summary:"查询聊天历史记录"`
-	PageSize int `json:"pageSize" v:"between:1,20" dc:"每页数据量"`
-	PageNum  int `json:"pageNum" v:"between:0,20" dc:"页码,从0开始"`
+	PageSize int    `json:"pageSize" v:"between:1,20" dc:"每页数据量"`
+	PageNum  int    `json:"pageNum" v:"between:0,20" dc:"页码,从0开始"`
+	PrevId   string `json:"prevId" dc:"当前记录id"`
 }
 
 type History struct {

+ 4 - 1
internal/controller/chatHistory.go

@@ -22,7 +22,10 @@ func (c *cChatHistory) Method(ctx context.Context, req *v1.ChatHistoryReq) (res
 		return nil, fmt.Errorf("无用户身份")
 	}
 	res = &v1.ChatHistoryRes{}
-	history, err := model.ChatHistroy.GetMessage(model.SessionCtx.Get(ctx).JSession.AccountId, req.PageNum, req.PageSize)
+	if req.PrevId != "" {
+		req.PrevId = encrypt.SE.Decode4Hex(req.PrevId) //id解密
+	}
+	history, err := model.ChatHistroy.GetMessage(model.SessionCtx.Get(ctx).JSession.AccountId, req.PageNum, req.PageSize, req.PrevId)
 	if err != nil {
 		glog.Error(ctx, "%d查询聊天记录异常,error:%s", model.SessionCtx.Get(ctx).JSession.AccountId, err)
 		res.ErrorCode = -1

+ 6 - 2
internal/model/chatHistory.go

@@ -79,8 +79,12 @@ func (m *cChatHistroy) Save(ctx context.Context, msgs ...*ChatRecord) (id int64)
 }
 
 // GetMessage 查询聊天信息
-func (m *cChatHistroy) GetMessage(userId int64, pageNum, pageSize int) (h []ResHistory, err error) {
-	err = g.Model("ai_message_history").Where("person_id = ?", userId).OrderDesc("create_time").OrderDesc("id").Limit(pageNum, (pageNum+1)*pageSize).Scan(&h)
+func (m *cChatHistroy) GetMessage(userId int64, pageNum, pageSize int, prevId string) (h []ResHistory, err error) {
+	if prevId != "" {
+		err = g.Model("ai_message_history").Where("person_id = ? and id > ? ", userId, prevId).OrderAsc("id").Limit(0, pageSize).Scan(&h)
+	} else {
+		err = g.Model("ai_message_history").Where("person_id = ?", userId).OrderDesc("create_time").OrderDesc("id").Limit(pageNum*pageSize, (pageNum+1)*pageSize).Scan(&h)
+	}
 	if err != nil {
 		return
 	}

+ 2 - 2
internal/model/question.go

@@ -26,8 +26,8 @@ type cQuestion struct {
 }
 
 type BaseQuestion struct {
-	Prompt  string     `json:"prompt"`
-	History [][]string `json:"history"`
+	Prompt  string                   `json:"prompt"`
+	History []map[string]interface{} `json:"history"`
 }
 
 type QuestionReq struct {

+ 2 - 2
internal/model/ws.go

@@ -6,8 +6,8 @@ import (
 	"app.yhyue.com/moapp/jybase/date"
 	"app.yhyue.com/moapp/jybase/fsw"
 	"context"
+	"encoding/json"
 	"fmt"
-	"github.com/gogf/gf/v2/encoding/gjson"
 	"github.com/gogf/gf/v2/frame/g"
 	"github.com/gogf/gf/v2/net/ghttp"
 	"github.com/gogf/gf/v2/os/glog"
@@ -30,7 +30,7 @@ func (m *WsChat) Handle(ws *ghttp.WebSocket, msg []byte) {
 	jSession := SessionCtx.Get(m.Ctx).JSession
 
 	req := &QuestionReq{}
-	if err := gjson.Unmarshal(msg, req); err != nil {
+	if err := json.Unmarshal(msg, &req); err != nil {
 		glog.Errorf(m.Ctx, "%d 接收消息Unmarshal出错:%v", jSession.AccountId, err)
 		return
 	}