|
@@ -5,13 +5,17 @@ import (
|
|
|
"encoding/base64"
|
|
|
"encoding/json"
|
|
|
"io/ioutil"
|
|
|
- "log"
|
|
|
"net/http"
|
|
|
+ qu "qfw/util"
|
|
|
"strings"
|
|
|
+ "time"
|
|
|
+
|
|
|
+ "github.com/donnie4w/go-logger/logger"
|
|
|
)
|
|
|
|
|
|
type jgconfig struct {
|
|
|
Pushurl, Appid, AppSecret string
|
|
|
+ Alter map[string]interface{}
|
|
|
Config config
|
|
|
}
|
|
|
type config struct {
|
|
@@ -22,8 +26,16 @@ type config struct {
|
|
|
Options map[string]interface{}
|
|
|
}
|
|
|
|
|
|
+var pushchan chan bool
|
|
|
var Jgconfig jgconfig
|
|
|
|
|
|
+func init() {
|
|
|
+ logger.SetConsole(false)
|
|
|
+ logger.SetRollingDaily("./logs", "jpush.log")
|
|
|
+ qu.ReadConfig("./jgpush.json", &Jgconfig)
|
|
|
+ pushchan = make(chan bool, 20000)
|
|
|
+}
|
|
|
+
|
|
|
/**
|
|
|
*通知推送
|
|
|
alert:通知栏,空默认配置
|
|
@@ -33,12 +45,12 @@ ids:极光id组
|
|
|
extras:自定义字段
|
|
|
broadcast:是否广播,广播ids作废
|
|
|
*/
|
|
|
-func JgpushNc(alert, title, infotype string, ids []string, extras map[string]interface{}, isbroadcast bool) {
|
|
|
+func JgpushNc(title, infotype string, ids []string, extras map[string]interface{}, isbroadcast bool) (bool, map[string]interface{}) {
|
|
|
temp := &Jgconfig.Config
|
|
|
temp.Audience["registration_id"] = ids
|
|
|
android := temp.Notification["android"]
|
|
|
if tmp, ok := android.(map[string]interface{}); ok {
|
|
|
- tmp["alert"] = alert
|
|
|
+ tmp["alert"] = Jgconfig.Alter[infotype]
|
|
|
tmp["title"] = title
|
|
|
tmp["extras"] = map[string]interface{}{
|
|
|
"type": infotype,
|
|
@@ -47,7 +59,7 @@ func JgpushNc(alert, title, infotype string, ids []string, extras map[string]int
|
|
|
}
|
|
|
ios := temp.Notification["ios"]
|
|
|
if tmp, ok := ios.(map[string]interface{}); ok {
|
|
|
- tmp["alert"] = alert
|
|
|
+ tmp["alert"] = Jgconfig.Alter[infotype]
|
|
|
tmp["extras"] = map[string]interface{}{
|
|
|
"type": infotype,
|
|
|
"list": extras,
|
|
@@ -65,17 +77,8 @@ func JgpushNc(alert, title, infotype string, ids []string, extras map[string]int
|
|
|
sendcon["audience"] = "all"
|
|
|
}
|
|
|
bs, _ := json.Marshal(sendcon)
|
|
|
- req, _ := http.NewRequest("POST", Jgconfig.Pushurl, strings.NewReader(string(bs)))
|
|
|
- req.Header.Add("Content-Type", "application/json")
|
|
|
- req.Header.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(Jgconfig.Appid+":"+Jgconfig.AppSecret)))
|
|
|
- resp, err := http.DefaultClient.Do(req)
|
|
|
- if err != nil {
|
|
|
- log.Println("JgpushNc", err.Error())
|
|
|
- } else {
|
|
|
- bs, _ := ioutil.ReadAll(resp.Body)
|
|
|
- resp.Body.Close()
|
|
|
- log.Println("JgpushNc", string(bs))
|
|
|
- }
|
|
|
+ pushchan <- true
|
|
|
+ return push(string(bs))
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -87,7 +90,7 @@ ids:极光id组
|
|
|
extras:自定义字段
|
|
|
broadcast:是否广播,广播ids作废
|
|
|
*/
|
|
|
-func JgpushMsg(msg_title, msg_con, msg_type string, ids []string, extras map[string]interface{}, isbroadcast bool) {
|
|
|
+func JgpushMsg(msg_title, msg_con, msg_type string, ids []string, extras map[string]interface{}, isbroadcast bool) (bool, map[string]interface{}) {
|
|
|
temp := Jgconfig.Config
|
|
|
temp.Audience["registration_id"] = ids
|
|
|
message := temp.Message
|
|
@@ -106,26 +109,17 @@ func JgpushMsg(msg_title, msg_con, msg_type string, ids []string, extras map[str
|
|
|
sendcon["audience"] = "all"
|
|
|
}
|
|
|
bs, _ := json.Marshal(sendcon)
|
|
|
- req, _ := http.NewRequest("POST", Jgconfig.Pushurl, strings.NewReader(string(bs)))
|
|
|
- req.Header.Add("Content-Type", "application/json")
|
|
|
- req.Header.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(Jgconfig.Appid+":"+Jgconfig.AppSecret)))
|
|
|
- resp, err := http.DefaultClient.Do(req)
|
|
|
- if err != nil {
|
|
|
- log.Println("JgpushMsg", err.Error())
|
|
|
- } else {
|
|
|
- bs, _ := ioutil.ReadAll(resp.Body)
|
|
|
- resp.Body.Close()
|
|
|
- log.Println("JgpushMsg", string(bs))
|
|
|
- }
|
|
|
+ pushchan <- true
|
|
|
+ return push(string(bs))
|
|
|
}
|
|
|
|
|
|
//通知、消息推送、广播
|
|
|
-func JgpushNcAndMsg(alert, title, infotype string, msg_title, msg_con, msg_type string, ids []string, extras, msg_extras map[string]interface{}, isbroadcast bool) {
|
|
|
+func JgpushNcAndMsg(title, infotype string, msg_title, msg_con, msg_type string, ids []string, extras, msg_extras map[string]interface{}, isbroadcast bool) (bool, map[string]interface{}) {
|
|
|
temp := Jgconfig.Config
|
|
|
temp.Audience["registration_id"] = ids
|
|
|
android := temp.Notification["android"]
|
|
|
if tmp, ok := android.(map[string]interface{}); ok {
|
|
|
- tmp["alert"] = alert
|
|
|
+ tmp["alert"] = Jgconfig.Alter[infotype]
|
|
|
tmp["title"] = title
|
|
|
tmp["extras"] = map[string]interface{}{
|
|
|
"type": infotype,
|
|
@@ -134,7 +128,7 @@ func JgpushNcAndMsg(alert, title, infotype string, msg_title, msg_con, msg_type
|
|
|
}
|
|
|
ios := temp.Notification["ios"]
|
|
|
if tmp, ok := ios.(map[string]interface{}); ok {
|
|
|
- tmp["alert"] = alert
|
|
|
+ tmp["alert"] = Jgconfig.Alter[infotype]
|
|
|
tmp["extras"] = map[string]interface{}{
|
|
|
"type": infotype,
|
|
|
"list": extras,
|
|
@@ -158,15 +152,37 @@ func JgpushNcAndMsg(alert, title, infotype string, msg_title, msg_con, msg_type
|
|
|
sendcon["audience"] = "all"
|
|
|
}
|
|
|
bs, _ := json.Marshal(sendcon)
|
|
|
- req, _ := http.NewRequest("POST", Jgconfig.Pushurl, strings.NewReader(string(bs)))
|
|
|
+ pushchan <- true
|
|
|
+ return push(string(bs))
|
|
|
+}
|
|
|
+
|
|
|
+func push(content string) (bool, map[string]interface{}) {
|
|
|
+ defer func() {
|
|
|
+ <-pushchan
|
|
|
+ }()
|
|
|
+ req, _ := http.NewRequest("POST", Jgconfig.Pushurl, strings.NewReader(content))
|
|
|
req.Header.Add("Content-Type", "application/json")
|
|
|
req.Header.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(Jgconfig.Appid+":"+Jgconfig.AppSecret)))
|
|
|
- resp, err := http.DefaultClient.Do(req)
|
|
|
- if err != nil {
|
|
|
- log.Println("JgpushNcAndMsg", err.Error())
|
|
|
- } else {
|
|
|
- bs, _ := ioutil.ReadAll(resp.Body)
|
|
|
- resp.Body.Close()
|
|
|
- log.Println("JgpushNcAndMsg", string(bs))
|
|
|
+ b := false
|
|
|
+ tmp := map[string]interface{}{}
|
|
|
+ for i := 0; i < 10; i++ {
|
|
|
+ resp, err := http.DefaultClient.Do(req)
|
|
|
+ defer resp.Body.Close()
|
|
|
+ if err != nil {
|
|
|
+ tmp["err"] = err.Error()
|
|
|
+ logger.Error(content)
|
|
|
+ } else {
|
|
|
+ bs, _ := ioutil.ReadAll(resp.Body)
|
|
|
+ json.Unmarshal(bs, &tmp)
|
|
|
+ }
|
|
|
+ if qu.Int64All(tmp["sendno"]) == 0 {
|
|
|
+ logger.Info(tmp)
|
|
|
+ b = true
|
|
|
+ break
|
|
|
+ } else {
|
|
|
+ logger.Warn(tmp)
|
|
|
+ }
|
|
|
+ time.Sleep(5 * time.Second)
|
|
|
}
|
|
|
+ return b, tmp
|
|
|
}
|