zhangjinkun 7 anni fa
parent
commit
33e51fe573
4 ha cambiato i file con 72 aggiunte e 51 eliminazioni
  1. 8 3
      golang/src/jgpush.json
  2. 54 38
      golang/src/jgpush/jgpush.go
  3. 3 6
      golang/src/main.go
  4. 7 4
      golang/src/main_test.go

+ 8 - 3
golang/src/jgpush.json

@@ -2,6 +2,11 @@
     "pushurl": "https://bjapi.push.jiguang.cn/v3/push",
     "appid": "5efa1257867cf5d77d007ce6",
     "appSecret": "d7a4e79978cd73cd61a5c7f7",
+    "alter": {
+        "bid": "剑鱼推送消息,最新的订阅内容",
+        "project": "剑鱼推送消息,我关注的项目",
+        "entname": "剑鱼推送消息,我关注的企业"
+    },
     "config": {
         "platform": "all",
         "audience": {
@@ -22,9 +27,9 @@
             }
         },
         "message": {
-            "msg_content": "Hi,JPush",
-            "content_type": "text",
-            "title": "msg",
+            "msg_content": "剑鱼推送消息,最新的订阅内容",
+            "content_type": "剑鱼招标订阅,最新的订阅内容",
+            "title": "剑鱼招标订阅",
             "extras": {}
         },
         "options": {

+ 54 - 38
golang/src/jgpush/jgpush.go

@@ -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
 }

+ 3 - 6
golang/src/main.go

@@ -2,14 +2,11 @@ package main
 
 import (
 	"jgpush"
-	qu "qfw/util"
+	"log"
 )
 
-func init() {
-	qu.ReadConfig("./jgpush.json", &jgpush.Jgconfig)
-}
-
 //13065ffa4e027b9d39e
 func main() {
-	jgpush.JgpushNc("剑鱼推送消息,最新的订阅内容", "招标公告", "bid", []string{"13065ffa4e027b9d39e"}, nil, false)
+	b, tmp := jgpush.JgpushNc("招标公告", "bid", []string{"13065ffa4e027b9d39e"}, nil, false)
+	log.Println(b, tmp)
 }

+ 7 - 4
golang/src/main_test.go

@@ -3,20 +3,23 @@ package main
 
 import (
 	"jgpush"
+	"log"
 	"testing"
 )
 
 func Test_jgpush(t *testing.T) {
 	ids := []string{"13065ffa4e027b9d39e"}
 	//通知
-	jgpush.JgpushNc("剑鱼推送消息,最新的订阅内容", "招标公告", "bid", ids, map[string]interface{}{
+	b, tmp := jgpush.JgpushNc("招标公告", "bid", ids, map[string]interface{}{
 		"name": "张三", //信息以及其他自定义属性
 	}, false)
+	log.Println("JgpushNc", b, tmp)
 	//消息
-	jgpush.JgpushMsg("msg_title", "msg_con", "text", ids, map[string]interface{}{
+	b, tmp = jgpush.JgpushMsg("msg_title", "msg_con", "text", ids, map[string]interface{}{
 		"name": "张三",
 	}, false)
+	log.Println("JgpushMsg", b, tmp)
 	//通知、消息
-	jgpush.JgpushNcAndMsg("alert", "title", "bid", "msg_title", "msg_con", "text", ids, nil, nil, false)
-
+	b, tmp = jgpush.JgpushNcAndMsg("title", "bid", "msg_title", "msg_con", "text", ids, nil, nil, false)
+	log.Println("JgpushNcAndMsg", b, tmp)
 }