|
|
|
@ -28,6 +28,8 @@ var (
|
|
|
|
|
const (
|
|
|
|
|
PushURL = "/push/single/alias"
|
|
|
|
|
AuthURL = "/auth"
|
|
|
|
|
TaskURL = "/push/list/message"
|
|
|
|
|
BatchPushURL = "/push/list/alias"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
@ -53,8 +55,15 @@ type AuthResp struct {
|
|
|
|
|
Token string `json:"token"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type TaskResp struct {
|
|
|
|
|
TaskID string `json:"taskID"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type PushReq struct {
|
|
|
|
|
RequestID string `json:"request_id"`
|
|
|
|
|
Settings struct {
|
|
|
|
|
TTL int32 `json:"ttl"`
|
|
|
|
|
} `json:"settings"`
|
|
|
|
|
Audience struct {
|
|
|
|
|
Alias []string `json:"alias"`
|
|
|
|
|
} `json:"audience"`
|
|
|
|
@ -66,6 +75,8 @@ type PushReq struct {
|
|
|
|
|
Ios Ios `json:"ios"`
|
|
|
|
|
Android Android `json:"android"`
|
|
|
|
|
} `json:"push_channel"`
|
|
|
|
|
IsAsync bool `json:"is_async"`
|
|
|
|
|
Taskid string `json:"taskid"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Ios struct {
|
|
|
|
@ -121,7 +132,7 @@ func (g *Getui) Push(userIDList []string, title, detailContent, operationID stri
|
|
|
|
|
token, err := db.DB.GetGetuiToken()
|
|
|
|
|
log.NewDebug(operationID, utils.GetSelfFuncName(), "token:", token)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.NewError(operationID, utils.OperationIDGenerator(), "GetGetuiToken failed", err.Error())
|
|
|
|
|
log.NewError(operationID, utils.GetSelfFuncName(), "GetGetuiToken failed", err.Error())
|
|
|
|
|
}
|
|
|
|
|
if token == "" || err != nil {
|
|
|
|
|
token, err = g.getTokenAndSave2Redis(operationID)
|
|
|
|
@ -130,12 +141,9 @@ func (g *Getui) Push(userIDList []string, title, detailContent, operationID stri
|
|
|
|
|
return "", utils.Wrap(err, "")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pushReq := PushReq{
|
|
|
|
|
RequestID: utils.OperationIDGenerator(),
|
|
|
|
|
Audience: struct {
|
|
|
|
|
Alias []string `json:"alias"`
|
|
|
|
|
}{Alias: []string{userIDList[0]}},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var pushReq PushReq
|
|
|
|
|
pushResp := PushResp{}
|
|
|
|
|
pushReq.PushMessage.Notification = Notification{
|
|
|
|
|
Title: title,
|
|
|
|
|
Body: detailContent,
|
|
|
|
@ -143,6 +151,29 @@ func (g *Getui) Push(userIDList []string, title, detailContent, operationID stri
|
|
|
|
|
ChannelID: config.Config.Push.Getui.ChannelID,
|
|
|
|
|
ChannelName: config.Config.Push.Getui.ChannelName,
|
|
|
|
|
}
|
|
|
|
|
if len(userIDList) > 1 {
|
|
|
|
|
taskID, err := db.DB.GetGetuiTaskID()
|
|
|
|
|
log.NewDebug(operationID, utils.GetSelfFuncName(), "token:", token)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.NewError(operationID, utils.GetSelfFuncName(), "GetGetuiTaskID failed", err.Error())
|
|
|
|
|
}
|
|
|
|
|
if taskID == "" || err != nil {
|
|
|
|
|
taskID, err = g.GetTaskIDAndSave2Redis(operationID, token, pushReq)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.NewError(operationID, utils.GetSelfFuncName(), "GetTaskIDAndSave2Redis failed", err.Error())
|
|
|
|
|
return "", utils.Wrap(err, "")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pushReq.IsAsync = true
|
|
|
|
|
pushReq.Taskid = taskID
|
|
|
|
|
err = g.request(BatchPushURL, pushReq, token, &pushResp, operationID)
|
|
|
|
|
} else {
|
|
|
|
|
pushReq := PushReq{
|
|
|
|
|
RequestID: utils.OperationIDGenerator(),
|
|
|
|
|
Audience: struct {
|
|
|
|
|
Alias []string `json:"alias"`
|
|
|
|
|
}{Alias: []string{userIDList[0]}},
|
|
|
|
|
}
|
|
|
|
|
pushReq.PushChannel.Ios.Aps.Sound = "default"
|
|
|
|
|
pushReq.PushChannel.Ios.Aps.Alert = Alert{
|
|
|
|
|
Title: title,
|
|
|
|
@ -169,8 +200,8 @@ func (g *Getui) Push(userIDList []string, title, detailContent, operationID stri
|
|
|
|
|
Classification: 1,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
pushResp := PushResp{}
|
|
|
|
|
err = g.request(PushURL, pushReq, token, &pushResp, operationID)
|
|
|
|
|
}
|
|
|
|
|
switch err {
|
|
|
|
|
case TokenExpireError:
|
|
|
|
|
token, err = g.getTokenAndSave2Redis(operationID)
|
|
|
|
@ -209,6 +240,15 @@ func (g *Getui) Auth(operationID string, timeStamp int64) (token string, expireT
|
|
|
|
|
return respAuth.Token, int64(expire), err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *Getui) GetTaskID(operationID, token string, pushReq PushReq) (string, error) {
|
|
|
|
|
respTask := TaskResp{}
|
|
|
|
|
err := g.request(TaskURL, pushReq, token, &respTask, operationID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", utils.Wrap(err, "")
|
|
|
|
|
}
|
|
|
|
|
return respTask.TaskID, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *Getui) request(url string, content interface{}, token string, returnStruct interface{}, operationID string) error {
|
|
|
|
|
con, err := json.Marshal(content)
|
|
|
|
|
if err != nil {
|
|
|
|
@ -257,3 +297,16 @@ func (g *Getui) getTokenAndSave2Redis(operationID string) (token string, err err
|
|
|
|
|
}
|
|
|
|
|
return token, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *Getui) GetTaskIDAndSave2Redis(operationID, token string, pushReq PushReq) (taskID string, err error) {
|
|
|
|
|
pushReq.Settings.TTL = 1000 * 60 * 60 * 24
|
|
|
|
|
taskID, err = g.GetTaskID(operationID, token, pushReq)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", utils.Wrap(err, "GetTaskIDAndSave2Redis failed")
|
|
|
|
|
}
|
|
|
|
|
err = db.DB.SetGetuiTaskID(taskID, 60*60*23)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", utils.Wrap(err, "Auth failed")
|
|
|
|
|
}
|
|
|
|
|
return token, nil
|
|
|
|
|
}
|
|
|
|
|