This commit is contained in:
wangchuxiao
2022-04-08 15:40:07 +08:00
parent 59cc45eaab
commit 4efdabc1ff
14 changed files with 240 additions and 43 deletions
+141 -5
View File
@@ -1,12 +1,148 @@
package getui
type Getui struct {
import (
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/db"
"Open_IM/pkg/common/log"
"Open_IM/pkg/utils"
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"strconv"
"time"
)
var (
GetuiClient *Getui
)
func init() {
GetuiClient = newGetuiClient()
}
func (g *Getui) Push(userIDList []string, alert, detailContent, platform string) (resp string, err error) {
return "", nil
type Getui struct{}
type GetuiCommonResp struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
}
func (g *Getui) Auth(apiKey, secretKey string, timeStamp int64) (token string, err error) {
return "", nil
type AuthReq struct {
Sign string `json:"sign"`
Timestamp string `json:"timestamp"`
Appkey string `json:"appkey"`
}
type AuthResp struct {
ExpireTime string `json:"expire_time"`
Token string `json:"token"`
}
type PushReq struct {
RequestID string `json:"request_id"`
Audience struct {
Cid []string `json:"cid"`
} `json:"audience"`
PushMssage struct {
Notification Notification `json:"notification"`
} `json:"push_message"`
}
type Notification struct {
Title string `json:"title"`
Body string `json:"body"`
ClickType string `json:"click_type"`
Intent string `json:"intent"`
Url string `json:"url"`
}
type PushResp struct {
}
func newGetuiClient() *Getui {
return &Getui{}
}
func (g *Getui) Push(userIDList []string, alert, detailContent, platform, operationID string) (resp string, err error) {
token, err := db.DB.GetGetuiToken()
if err != nil {
log.NewError(operationID, utils.OperationIDGenerator(), "GetGetuiToken", err.Error())
}
if token == "" || err != nil {
token, expireTime, err := g.Auth(config.Config.Push.Getui.AppKey, config.Config.Push.Getui.Sign, operationID, time.Now().Unix())
if err != nil {
return "", utils.Wrap(err, "Auth failed")
}
log.NewInfo(operationID, "getui", utils.GetSelfFuncName(), token, expireTime, err)
err = db.DB.SetGetuiToken(token, expireTime-time.Now().Unix()-20)
if err != nil {
return "", utils.Wrap(err, "Auth failed")
}
}
pushReq := PushReq{
RequestID: utils.OperationIDGenerator(),
Audience: struct {
Cid []string `json:"cid"`
}{Cid: []string{userIDList[0]}},
}
pushReq.PushMssage.Notification = Notification{
Title: alert,
Body: alert,
ClickType: "none",
}
if config.Config.Push.Getui.Intent != "" {
pushReq.PushMssage.Notification.Intent = config.Config.Push.Getui.Intent
pushReq.PushMssage.Notification.ClickType = "intent"
}
pushResp := PushResp{}
err = g.request(pushReq, token, &pushResp, operationID)
if err != nil {
return "", utils.Wrap(err, "")
}
respBytes, err := json.Marshal(pushResp)
return string(respBytes), err
}
func (g *Getui) Auth(appKey, sign, operationID string, timeStamp int64) (token string, expireTime int64, err error) {
log.NewInfo(operationID, utils.GetSelfFuncName(), appKey, sign, timeStamp)
reqAuth := AuthReq{
Sign: sign,
Timestamp: strconv.Itoa(int(timeStamp)),
Appkey: appKey,
}
respAuth := AuthResp{}
err = g.request(reqAuth, "", &respAuth, operationID)
expire, err := strconv.Atoi(respAuth.ExpireTime)
return respAuth.Token, int64(expire), err
}
func (g *Getui) request(content interface{}, token string, returnStruct interface{}, operationID string) error {
con, err := json.Marshal(content)
if err != nil {
return err
}
client := &http.Client{}
req, err := http.NewRequest("POST", config.Config.Push.Getui.PushUrl, bytes.NewBuffer(con))
if err != nil {
return err
}
req.Header.Set(token, token)
req.Header.Set("content-type", "application/json")
req.Header.Set("content-type", "charset=utf-8")
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
}
log.NewInfo(operationID, "getui", utils.GetSelfFuncName(), string(result))
if err := json.Unmarshal(result, returnStruct); err != nil {
return err
}
return nil
}
+23 -10
View File
@@ -10,14 +10,29 @@ import (
"net/http"
)
type JPushResp struct {
var (
JPushClient *JPush
)
func init() {
JPushClient = newGetuiClient()
}
type JPush struct {
type JPush struct{}
func newGetuiClient() *JPush {
return &JPush{}
}
func JGAccountListPush(accounts []string, alert, detailContent, platform string) ([]byte, error) {
func (j *JPush) Auth(apiKey, secretKey string, timeStamp int64) (token string, err error) {
return token, nil
}
func (j *JPush) SetAlias(cid, alias string) (resp string, err error) {
return resp, nil
}
func (j *JPush) Push(accounts []string, alert, detailContent, platform, operationID string) (string, error) {
var pf requestBody.Platform
_ = pf.SetPlatform(platform)
var au requestBody.Audience
@@ -37,25 +52,23 @@ func JGAccountListPush(accounts []string, alert, detailContent, platform string)
con, err := json.Marshal(po)
if err != nil {
return nil, err
return "", err
}
client := &http.Client{}
req, err := http.NewRequest("POST", config.Config.Push.Jpns.PushUrl, bytes.NewBuffer(con))
if err != nil {
return nil, err
return "", err
}
req.Header.Set("Authorization", common.GetAuthorization(config.Config.Push.Jpns.AppKey, config.Config.Push.Jpns.MasterSecret))
resp, err := client.Do(req)
if err != nil {
return nil, err
return "", err
}
defer resp.Body.Close()
result, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
return "", err
}
return result, nil
return string(result), nil
}
+15 -4
View File
@@ -7,7 +7,9 @@
package logic
import (
push "Open_IM/internal/push/jpush"
pusher "Open_IM/internal/push"
"Open_IM/internal/push/getui"
jpush "Open_IM/internal/push/jpush"
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/constant"
"Open_IM/pkg/common/log"
@@ -99,12 +101,21 @@ func MsgToUser(pushMsg *pbPush.PushMsgReq) {
content = constant.ContentType2PushContent[constant.Common]
}
}
pushResult, err := push.JGAccountListPush(UIDList, content, jsonCustomContent, constant.PlatformIDToName(t))
var offlinePusher pusher.OfflinePusher
if config.Config.Push.Getui.Enable {
offlinePusher = getui.GetuiClient
}
if config.Config.Push.Jpns.Enable {
offlinePusher = jpush.JPushClient
}
if offlinePusher == nil {
offlinePusher = jpush.JPushClient
}
pushResult, err := offlinePusher.Push(UIDList, content, jsonCustomContent, constant.PlatformIDToName(t), pushMsg.OperationID)
if err != nil {
log.NewError(pushMsg.OperationID, "offline push error", pushMsg.String(), err.Error(), constant.PlatformIDToName(t))
} else {
log.NewDebug(pushMsg.OperationID, "offline push return result is ", string(pushResult), pushMsg.MsgData, constant.PlatformIDToName(t))
log.NewDebug(pushMsg.OperationID, "offline push return result is ", pushResult, pushMsg.MsgData, constant.PlatformIDToName(t))
}
}
+2 -3
View File
@@ -1,6 +1,5 @@
package push
type offlinePusher interface {
auth(apiKey, secretKey string, timeStamp int64) (token string, err error)
push(userIDList []string, alert, detailContent, platform string) (resp string, err error)
type OfflinePusher interface {
Push(userIDList []string, alert, detailContent, platform, operationID string) (resp string, err error)
}