From 755392e9995b1656d751d1870f40f7682215bc6d Mon Sep 17 00:00:00 2001 From: lafay <2021211506@stu.hit.edu.cn> Date: Thu, 18 Jun 2026 00:02:00 +0800 Subject: [PATCH] refactor(jpush): move vendor field handling into ThirdPartyChannel struct Move the vendor switch logic into the ThirdPartyChannel struct as Set/IsEmpty/Normalize methods to improve encapsulation and eliminate code duplication in the vendor params builder. Also simplify messageToParams to reuse buildMessagePayload results and remove unused notification_type extras from push notifications. --- internal/pkg/jpush/client.go | 36 +++++++++++++++++++++++++++++ internal/pkg/jpush/vendor/params.go | 25 ++------------------ internal/service/push_service.go | 17 +++++++------- 3 files changed, 46 insertions(+), 32 deletions(-) diff --git a/internal/pkg/jpush/client.go b/internal/pkg/jpush/client.go index d36049b..563456a 100644 --- a/internal/pkg/jpush/client.go +++ b/internal/pkg/jpush/client.go @@ -78,6 +78,42 @@ type ThirdPartyChannel struct { Honor map[string]any `json:"honor,omitempty"` } +// Set 按厂商标识写入对应厂商的私有字段 +// vendor 对应 third_party_channel 的 key(xiaomi/huawei/oppo/vivo/meizu/fcm/honor) +// 未知厂商将被忽略 +func (t *ThirdPartyChannel) Set(vendor string, m map[string]any) { + switch vendor { + case "xiaomi": + t.Xiaomi = m + case "huawei": + t.Huawei = m + case "oppo": + t.OPPO = m + case "vivo": + t.VIVO = m + case "meizu": + t.Meizu = m + case "fcm": + t.FCM = m + case "honor": + t.Honor = m + } +} + +// IsEmpty 判断是否所有厂商字段均为空 +func (t *ThirdPartyChannel) IsEmpty() bool { + return t.Xiaomi == nil && t.Huawei == nil && t.OPPO == nil && + t.VIVO == nil && t.Meizu == nil && t.Honor == nil && t.FCM == nil +} + +// Normalize 当所有厂商字段为空时返回 nil,避免序列化出空的 third_party_channel 对象 +func (t *ThirdPartyChannel) Normalize() *ThirdPartyChannel { + if t == nil || t.IsEmpty() { + return nil + } + return t +} + type AndroidNotif struct { Alert string `json:"alert"` Title string `json:"title,omitempty"` diff --git a/internal/pkg/jpush/vendor/params.go b/internal/pkg/jpush/vendor/params.go index 8075352..4ee3ee7 100644 --- a/internal/pkg/jpush/vendor/params.go +++ b/internal/pkg/jpush/vendor/params.go @@ -120,32 +120,11 @@ func (f *Factory) Build(params MessageParams, cfg config.ChannelConfig) *PushPay if m == nil { continue } - switch c.VendorName() { - case "xiaomi": - tpc.Xiaomi = m - case "huawei": - tpc.Huawei = m - case "oppo": - tpc.OPPO = m - case "vivo": - tpc.VIVO = m - case "meizu": - tpc.Meizu = m - case "honor": - tpc.Honor = m - case "fcm": - tpc.FCM = m - } - } - - // 全部厂商字段为空则置 nil,避免序列化出空的 third_party_channel - if tpc.Xiaomi == nil && tpc.Huawei == nil && tpc.OPPO == nil && - tpc.VIVO == nil && tpc.Meizu == nil && tpc.Honor == nil && tpc.FCM == nil { - tpc = nil + tpc.Set(c.VendorName(), m) } return &PushPayload{ Notification: notification, - TPC: tpc, + TPC: tpc.Normalize(), } } diff --git a/internal/service/push_service.go b/internal/service/push_service.go index be8f50d..02139aa 100644 --- a/internal/service/push_service.go +++ b/internal/service/push_service.go @@ -617,17 +617,18 @@ func (s *pushServiceImpl) buildPayload(params vendor.MessageParams) *vendor.Push // messageToParams 从 model.Message 提取厂商无关的统一参数 // 用于 PushToUser / worker / retry 等通用路径 +// 复用 buildMessagePayload 的计算结果,避免重复解析 message func (s *pushServiceImpl) messageToParams(message *model.Message) vendor.MessageParams { - _ = message.Decrypt() payload := buildMessagePayload(message) - scene := s.resolveChannelScene(message) - + // senderName 复用 payload.Extras 中已提取的 actor_name,无需再次访问 ExtraData senderName := "" - if message.ExtraData != nil { - senderName = message.ExtraData.ActorName + if name, ok := payload.Extras["actor_name"]; ok { + if n, ok := name.(string); ok { + senderName = n + } } return vendor.MessageParams{ - Scene: scene, + Scene: s.resolveChannelScene(message), Title: payload.Title, Body: payload.Content, SenderName: senderName, @@ -787,7 +788,6 @@ func (s *pushServiceImpl) PushChatMessage(ctx context.Context, userID string, co content = "[非文本消息]" } extras := map[string]any{ - "notification_type": "chat_message", "message_id": message.ID, "conversation_id": message.ConversationID, "conversation_type": string(convType), @@ -900,8 +900,7 @@ func (s *pushServiceImpl) PushSystemNotification(ctx context.Context, userID str title = string(notification.Type) } extras := map[string]any{ - "notification_id": fmt.Sprintf("%d", notification.ID), - "notification_type": string(notification.Type), + "notification_id": fmt.Sprintf("%d", notification.ID), } if notification.ExtraData != nil { extras["actor_id"] = notification.ExtraData.ActorIDStr