chatHistory.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package controller
  2. import (
  3. v1 "aiChat/api/v1"
  4. "aiChat/internal/model"
  5. "aiChat/utility"
  6. "app.yhyue.com/moapp/jybase/encrypt"
  7. "github.com/gogf/gf/v2/net/ghttp"
  8. "github.com/gogf/gf/v2/os/glog"
  9. )
  10. // ChatHistory 获取聊天记录
  11. func ChatHistory(r *ghttp.Request) {
  12. final := func() (res v1.ChatHistoryRes) {
  13. res = v1.ChatHistoryRes{}
  14. session, _ := utility.GetSession(r)
  15. if session.AccountId == 0 {
  16. res.ErrorCode = -1
  17. res.ErrorMsg = "未登陆"
  18. return
  19. }
  20. pageSize := r.Get("PageSize", 5).Int()
  21. pageNum := r.Get("PageNum", 0).Int()
  22. history, err := model.Message.GetMessage(session.AccountId, pageNum, pageSize)
  23. if err != nil {
  24. glog.Error(r.Context(), "%d查询聊天记录异常,error:%s", session.AccountId, err)
  25. res.ErrorCode = -1
  26. res.ErrorMsg = "数据查询异常"
  27. return
  28. }
  29. for _, v := range history {
  30. res.Data = append(res.Data, v1.History{
  31. Id: encrypt.SE.Encode2Hex(v.Id),
  32. Content: v.Content,
  33. Type: v.Type,
  34. Useful: v.Useful,
  35. CreateTime: v.CreateTime.Unix(),
  36. })
  37. }
  38. return
  39. }()
  40. r.Response.Write(final)
  41. }