chatHistory.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package controller
  2. import (
  3. v1 "aiChat/api/aiChat/v1"
  4. "aiChat/internal/model"
  5. "app.yhyue.com/moapp/jybase/encrypt"
  6. "context"
  7. "fmt"
  8. "github.com/gogf/gf/v2/frame/g"
  9. )
  10. var (
  11. ChatHistory = cChatHistory{}
  12. )
  13. type cChatHistory struct{}
  14. // Method 获取聊天记录
  15. func (c *cChatHistory) Method(ctx context.Context, req *v1.ChatHistoryReq) (res *v1.ChatHistoryRes, err error) {
  16. session := model.SessionCtx.Get(ctx).JSession
  17. if session.PositionId <= 0 {
  18. return nil, fmt.Errorf("请登录")
  19. }
  20. res = &v1.ChatHistoryRes{}
  21. if req.PrevId != "" {
  22. req.PrevId = encrypt.SE.Decode4Hex(req.PrevId) //id解密
  23. }
  24. history, hasNext, err := model.ChatHistory.GetMessage(session.PositionId, req.PageNum, req.PageSize, req.PrevId)
  25. if err != nil {
  26. g.Log().Error(ctx, "%d查询聊天记录异常,error:%s", session.PositionId, err)
  27. res.ErrorCode = -1
  28. res.ErrorMsg = "数据查询异常"
  29. return
  30. }
  31. res.Data.HasMore = hasNext
  32. for _, v := range history {
  33. res.Data.List = append(res.Data.List, v1.History{
  34. Id: encrypt.SE.Encode2Hex(v.Id),
  35. Content: v.Content,
  36. Type: v.Type,
  37. Useful: v.Useful,
  38. Actions: v.Actions,
  39. CreateTime: v.CreateTime.Unix(),
  40. })
  41. }
  42. return
  43. }