chatHistory.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. if req.PrevId != "" {
  22. req.PrevId = encrypt.SE.Decode4Hex(req.PrevId) //id解密
  23. }
  24. history, err := model.ChatHistroy.GetMessage(model.SessionCtx.Get(ctx).JSession.AccountId, req.PageNum, req.PageSize, req.PrevId)
  25. if err != nil {
  26. glog.Error(ctx, "%d查询聊天记录异常,error:%s", model.SessionCtx.Get(ctx).JSession.AccountId, err)
  27. res.ErrorCode = -1
  28. res.ErrorMsg = "数据查询异常"
  29. return
  30. }
  31. for _, v := range history {
  32. res.Data = append(res.Data, v1.History{
  33. Id: encrypt.SE.Encode2Hex(v.Id),
  34. Content: v.Content,
  35. Type: v.Type,
  36. Useful: v.Useful,
  37. CreateTime: v.CreateTime.Unix(),
  38. })
  39. }
  40. return
  41. }