chatHistory.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package controller
  2. import (
  3. v1 "aiChat/api/v1"
  4. "aiChat/internal/model"
  5. "app.yhyue.com/moapp/jybase/encrypt"
  6. "context"
  7. "fmt"
  8. "github.com/gogf/gf/v2/os/glog"
  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.AccountId <= 0 {
  18. return nil, fmt.Errorf("无用户身份")
  19. }
  20. res = &v1.ChatHistoryRes{}
  21. history, err := model.ChatHistroy.GetMessage(model.SessionCtx.Get(ctx).JSession.AccountId, req.PageNum, req.PageSize)
  22. if err != nil {
  23. glog.Error(ctx, "%d查询聊天记录异常,error:%s", model.SessionCtx.Get(ctx).JSession.AccountId, err)
  24. res.ErrorCode = -1
  25. res.ErrorMsg = "数据查询异常"
  26. return
  27. }
  28. for _, v := range history {
  29. res.Data = append(res.Data, v1.History{
  30. Id: encrypt.SE.Encode2Hex(v.Id),
  31. Content: v.Content,
  32. Type: v.Type,
  33. Useful: v.Useful,
  34. CreateTime: v.CreateTime.Unix(),
  35. })
  36. }
  37. return
  38. }