|
@@ -1,55 +0,0 @@
|
|
|
-package MessageHandlers
|
|
|
-
|
|
|
-import (
|
|
|
- "context"
|
|
|
- "github.com/zeromicro/go-zero/core/logx"
|
|
|
- "wechat-api/internal/pkg/wechat_ws"
|
|
|
- "wechat-api/internal/svc"
|
|
|
-)
|
|
|
-
|
|
|
-// MessageHandlerStrategy 消息处理策略接口
|
|
|
-type MessageHandlerStrategy interface {
|
|
|
- Handle(ctx context.Context, msg *wechat_ws.MsgJsonObject, svcCtx *svc.ServiceContext) error
|
|
|
-}
|
|
|
-
|
|
|
-type MessageHandler struct {
|
|
|
- strategies map[string]MessageHandlerStrategy
|
|
|
- svcCtx *svc.ServiceContext
|
|
|
-}
|
|
|
-
|
|
|
-func NewMessageHandler(svcCtx *svc.ServiceContext) *MessageHandler {
|
|
|
- return &MessageHandler{
|
|
|
- strategies: make(map[string]MessageHandlerStrategy),
|
|
|
- svcCtx: svcCtx,
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-// RegisterStrategy 注册消息处理策略
|
|
|
-func (h *MessageHandler) RegisterStrategy(msgType string, strategy MessageHandlerStrategy) {
|
|
|
- h.strategies[msgType] = strategy
|
|
|
-}
|
|
|
-
|
|
|
-// Handle 根据 msgType 处理消息
|
|
|
-func (h *MessageHandler) Handle(ctx context.Context, msg *wechat_ws.MsgJsonObject) error {
|
|
|
- strategy, exists := h.strategies[msg.MsgType]
|
|
|
- if !exists {
|
|
|
- logx.Error("No handler registered for msgType: %s", msg.MsgType)
|
|
|
- return nil
|
|
|
- }
|
|
|
- return strategy.Handle(ctx, msg, h.svcCtx)
|
|
|
-}
|
|
|
-
|
|
|
-// RegisterStrategies 使用映射表注册所有策略
|
|
|
-func (h *MessageHandler) RegisterStrategies() {
|
|
|
- strategyMap := map[string]func(*svc.ServiceContext) MessageHandlerStrategy{
|
|
|
- "FriendPushNotice": func(svcCtx *svc.ServiceContext) MessageHandlerStrategy {
|
|
|
- return NewFriendPushNoticeTypeHandler(svcCtx)
|
|
|
- },
|
|
|
- "ChatroomPushNotice": func(svcCtx *svc.ServiceContext) MessageHandlerStrategy {
|
|
|
- return NewChatroomPushNoticeTypeHandler(svcCtx)
|
|
|
- },
|
|
|
- }
|
|
|
- for msgType, strategyFunc := range strategyMap {
|
|
|
- h.RegisterStrategy(msgType, strategyFunc(h.svcCtx))
|
|
|
- }
|
|
|
-}
|