From e000dc18aaa40b71facd50c8b8552c36a2def19d Mon Sep 17 00:00:00 2001 From: liu ming <740921760@qq.com> Date: Fri, 23 Sep 2022 17:40:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=A2=A4=E5=8D=9A=E6=8E=A8?= =?UTF-8?q?=E9=80=81=20(#284)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: liuming --- config/config.yaml | 6 ++ internal/push/logic/init.go | 4 + internal/push/mobpush/common/getSign.go | 18 +++++ internal/push/mobpush/push.go | 75 +++++++++++++++++++ .../push/mobpush/requestParams/pushForward.go | 14 ++++ .../push/mobpush/requestParams/pushNotify.go | 26 +++++++ .../push/mobpush/requestParams/pushObj.go | 28 +++++++ .../push/mobpush/requestParams/pushTarget.go | 13 ++++ pkg/common/config/config.go | 7 ++ 9 files changed, 191 insertions(+) create mode 100644 internal/push/mobpush/common/getSign.go create mode 100644 internal/push/mobpush/push.go create mode 100644 internal/push/mobpush/requestParams/pushForward.go create mode 100644 internal/push/mobpush/requestParams/pushNotify.go create mode 100644 internal/push/mobpush/requestParams/pushObj.go create mode 100644 internal/push/mobpush/requestParams/pushTarget.go diff --git a/config/config.yaml b/config/config.yaml index 0e8748c03..f66a84dc3 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -239,6 +239,12 @@ push: fcm: #firebase cloud message 消息推送 serviceAccount: "openim-5c6c0-firebase-adminsdk-ppwol-8765884a78.json" #帐号文件,此处需要改修配置,并且这个文件放在 config目录下 enable: false + mob: #袤博推送 + appKey: "3377f689a25" #帐号文件,此处需要改修配置,并且这个文件放在 config目录下 + pushUrl: "https://api.push.mob.com/v3/push/createPush" + scheme: "dianzhijiaunilinks://dianzhijia.com?page=rent" + appSecret: "77b4e20e94db3a776b87d8693be23e" + enable: false diff --git a/internal/push/logic/init.go b/internal/push/logic/init.go index 81ed48087..b73d0bd53 100644 --- a/internal/push/logic/init.go +++ b/internal/push/logic/init.go @@ -45,6 +45,10 @@ func init() { if config.Config.Push.Fcm.Enable { offlinePusher = fcm.NewFcm() } + + if config.Config.Push.Mob.Enable { + offlinePusher = mobpush.MobPushClient + } } func initPrometheus() { diff --git a/internal/push/mobpush/common/getSign.go b/internal/push/mobpush/common/getSign.go new file mode 100644 index 000000000..0ee7b4a60 --- /dev/null +++ b/internal/push/mobpush/common/getSign.go @@ -0,0 +1,18 @@ +package common + +import ( + "Open_IM/pkg/common/config" + "crypto/md5" + "encoding/hex" + "fmt" + "io" +) + +func GetSign(paramsStr string) string { + h := md5.New() + io.WriteString(h, paramsStr) + io.WriteString(h, config.Config.Push.Mob.AppSecret) + fmt.Printf("%x", h.Sum(nil)) + + return hex.EncodeToString(h.Sum(nil)) +} diff --git a/internal/push/mobpush/push.go b/internal/push/mobpush/push.go new file mode 100644 index 000000000..e8ac4ecfb --- /dev/null +++ b/internal/push/mobpush/push.go @@ -0,0 +1,75 @@ +package mobpush + +import ( + "Open_IM/internal/push" + "Open_IM/internal/push/mobpush/common" + "Open_IM/internal/push/mobpush/requestParams" + "Open_IM/pkg/common/config" + "encoding/json" + "io/ioutil" + "net/http" + "strings" +) + +var ( + MobPushClient *MobPush +) + +func init() { + MobPushClient = newGetuiClient() +} + +type MobPush struct{} + +func newGetuiClient() *MobPush { + return &MobPush{} +} + +func (j *MobPush) Push(accounts []string, alert, detailContent, operationID string, opts push.PushOpts) (string, error) { + + var target requestParams.PushTarget + + target.SetAlias(accounts) + target.SetTarget(2) + + var no requestParams.PushNotify + no.SetType(1) + no.SetIosProduction(1) + no.SetPlats([]int{1, 2}) + no.SetContent(alert) + + var forward requestParams.PushForward + forward.SetNextType(2) + forward.SetScheme(config.Config.Push.Mob.Scheme) + + var po requestParams.PushObj + po.SetSource("webapi") + po.SetAppkey(config.Config.Push.Mob.AppKey) + po.SetPushTarget(&target) + po.SetPushNotify(&no) + po.SetPushForward(&forward) + + con, err := json.Marshal(po) + if err != nil { + return "", err + } + client := &http.Client{} + req, err := http.NewRequest("POST", config.Config.Push.Mob.PushUrl, strings.NewReader(string(con))) + if err != nil { + return "", err + } + req.Header.Set("Content-Type", "application/json") + req.Header.Set("key", config.Config.Push.Mob.AppKey) + req.Header.Set("sign", common.GetSign(string(con))) + + resp, err := client.Do(req) + if err != nil { + return "", err + } + defer resp.Body.Close() + result, err := ioutil.ReadAll(resp.Body) + if err != nil { + return "", err + } + return string(result), nil +} diff --git a/internal/push/mobpush/requestParams/pushForward.go b/internal/push/mobpush/requestParams/pushForward.go new file mode 100644 index 000000000..ffda49459 --- /dev/null +++ b/internal/push/mobpush/requestParams/pushForward.go @@ -0,0 +1,14 @@ +package requestParams + +type PushForward struct { + NextType int `json:"nextType"` + Scheme string `json:"scheme,omitempty"` +} + +func (m *PushForward) SetNextType(c int) { + m.NextType = c +} + +func (m *PushForward) SetScheme(t string) { + m.Scheme = t +} diff --git a/internal/push/mobpush/requestParams/pushNotify.go b/internal/push/mobpush/requestParams/pushNotify.go new file mode 100644 index 000000000..0a6c92899 --- /dev/null +++ b/internal/push/mobpush/requestParams/pushNotify.go @@ -0,0 +1,26 @@ +package requestParams + +type PushNotify struct { + Plats []int `json:"plats,omitempty"` + IosProduction int `json:"iosProduction,omitempty"` + Content string `json:"content,omitempty"` + Type int `json:"type,omitempty"` +} + +func (n *PushNotify) SetPlats(plats []int) { + n.Plats = plats + +} + +func (n *PushNotify) SetIosProduction(iosProduction int) { + n.IosProduction = iosProduction + +} + +func (n *PushNotify) SetContent(content string) { + n.Content = content +} + +func (n *PushNotify) SetType(Type int) { + n.Type = Type +} diff --git a/internal/push/mobpush/requestParams/pushObj.go b/internal/push/mobpush/requestParams/pushObj.go new file mode 100644 index 000000000..5fa9223da --- /dev/null +++ b/internal/push/mobpush/requestParams/pushObj.go @@ -0,0 +1,28 @@ +package requestParams + +type PushObj struct { + Source interface{} `json:"source"` + Appkey interface{} `json:"appkey"` + PushTarget interface{} `json:"pushTarget,omitempty"` + PushNotify interface{} `json:"pushNotify,omitempty"` + PushForward interface{} `json:"pushForward,omitempty"` +} + +func (p *PushObj) SetSource(source string) { + p.Source = source +} + +func (p *PushObj) SetAppkey(appkey string) { + p.Appkey = appkey +} + +func (p *PushObj) SetPushTarget(no *PushTarget) { + p.PushTarget = no +} + +func (p *PushObj) SetPushNotify(m *PushNotify) { + p.PushNotify = m +} +func (p *PushObj) SetPushForward(o *PushForward) { + p.PushForward = o +} diff --git a/internal/push/mobpush/requestParams/pushTarget.go b/internal/push/mobpush/requestParams/pushTarget.go new file mode 100644 index 000000000..3650d3a40 --- /dev/null +++ b/internal/push/mobpush/requestParams/pushTarget.go @@ -0,0 +1,13 @@ +package requestParams + +type PushTarget struct { + Target interface{} `json:"target,omitempty"` + Alias []string `json:"alias,omitempty"` +} + +func (p *PushTarget) SetTarget(target int) { + p.Target = target +} +func (p *PushTarget) SetAlias(alias []string) { + p.Alias = alias +} diff --git a/pkg/common/config/config.go b/pkg/common/config/config.go index d28362808..71e717a3a 100644 --- a/pkg/common/config/config.go +++ b/pkg/common/config/config.go @@ -215,6 +215,13 @@ type config struct { ServiceAccount string `yaml:"serviceAccount"` Enable bool `yaml:"enable"` } + Mob struct { + AppKey string `yaml:"appKey"` + PushUrl string `yaml:"pushUrl"` + Scheme string `yaml:"scheme"` + AppSecret string `yaml:"appSecret"` + Enable bool `yaml:"enable"` + } } Manager struct { AppManagerUid []string `yaml:"appManagerUid"`