conversationlogic.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package conversation
  2. import (
  3. "context"
  4. "slowwildws/internal/types"
  5. "time"
  6. msgChat "git.banshen.xyz/huangguangrong/slow_wild_queue/types"
  7. "github.com/mitchellh/mapstructure"
  8. "github.com/zeromicro/go-zero/core/logx"
  9. )
  10. type ConversationLogic struct {
  11. logx.Logger
  12. }
  13. func NewConversationLogic(ctx context.Context) *ConversationLogic {
  14. return &ConversationLogic{
  15. Logger: logx.WithContext(ctx),
  16. }
  17. }
  18. func (l *ConversationLogic) ChatHandler(msg *types.Message, uid int64) (*msgChat.MsgChatTransfer, error) {
  19. var data msgChat.Chat
  20. if err := mapstructure.Decode(msg.Data, &data); err != nil {
  21. return nil, err
  22. }
  23. // 设置发送者id
  24. data.SendID = msg.UserID
  25. // 设置接收者id
  26. data.RecvID = msg.FormID
  27. // 设置消息id
  28. data.Msg.MsgID = msg.Id
  29. // 设置消息发送时间
  30. data.SendTime = time.Now().Unix()
  31. // 使用服务端下发的会话id,所以注释这里
  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.Msg.MType),
  46. Content: data.Msg.Content,
  47. }
  48. return msgData, nil
  49. }