conversationlogic.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package conversation
  2. import (
  3. "context"
  4. "slowwildws/internal/types"
  5. "slowwildws/internal/utils"
  6. "time"
  7. msgChat "git.banshen.xyz/huangguangrong/slow_wild_queue/types"
  8. "github.com/mitchellh/mapstructure"
  9. "github.com/zeromicro/go-zero/core/logx"
  10. )
  11. type ConversationLogic struct {
  12. logx.Logger
  13. }
  14. func NewConversationLogic(ctx context.Context) *ConversationLogic {
  15. return &ConversationLogic{
  16. Logger: logx.WithContext(ctx),
  17. }
  18. }
  19. func (l *ConversationLogic) ChatHandler(msg *types.Message, uid int64) (*msgChat.MsgChatTransfer, error) {
  20. var data msgChat.Chat
  21. if err := mapstructure.Decode(msg.Data, &data); err != nil {
  22. return nil, err
  23. }
  24. // 设置发送者id
  25. data.SendID = msg.UserID
  26. // 设置接收者id
  27. data.RecvID = msg.FormID
  28. // 设置消息id
  29. data.Msg.MsgID = msg.Id
  30. // 设置消息发送时间
  31. data.SendTime = time.Now().Unix()
  32. switch data.ChatType {
  33. case msgChat.SingleChatType:
  34. data.ConversationId = utils.CombineUserID(uid, data.RecvID)
  35. case msgChat.GroupChatType:
  36. data.ConversationId = utils.UserIdToHex(data.RecvID)
  37. }
  38. msgData := &msgChat.MsgChatTransfer{
  39. MsgId: msg.Id,
  40. ConversationId: data.ConversationId,
  41. ChatType: msgChat.ChatType(data.ChatType),
  42. SendID: uid,
  43. RecvID: data.RecvID,
  44. SendTime: time.Now().Unix(),
  45. MType: msgChat.MType(data.MType),
  46. Content: data.Content,
  47. }
  48. return msgData, nil
  49. }