Эх сурвалжийг харах

Merge branch 'master' of ssh://192.168.3.207:10022/qmx/jyapp

unknown 7 жил өмнө
parent
commit
be8c2ac53f

+ 0 - 69
golang/main.go

@@ -1,69 +0,0 @@
-package main
-
-import (
-	"encoding/base64"
-	"fmt"
-	"io/ioutil"
-	"net/http"
-	"strings"
-)
-
-const (
-	push_url  = "https://bjapi.push.jiguang.cn/v3/push"
-	appid     = "5efa1257867cf5d77d007ce6"
-	appSecret = "d7a4e79978cd73cd61a5c7f7"
-)
-
-//
-func main() {
-	bodystr := `{
-    "platform": "all",
-    "audience": {
-        "registration_id": [
-            "140fe1da9ea179bd470"
-        ]
-    },
-    "notification": {
-        "android": {
-            "alert": "剑鱼推送消息,最新的订阅内容",
-            "title": "剑鱼招标订阅",
-            "builder_id": 1,
-            "extras": {
-                "newsid": 321
-            }
-        },
-        "ios": {
-            "alert": "推送消息",
-            "sound": "default",
-            "badge": "+1",
-            "extras": {
-                "newsid": 321
-            }
-        }
-    },
-    "message": {
-        "msg_content": "Hi,JPush",
-        "content_type": "text",
-        "title": "msg",
-        "extras": {
-            "key": "value"
-        }
-    },
-    "options": {
-        "time_to_live": 60,
-        "apns_production": false,
-        "apns_collapse_id":"jiguang_test_201706011100"
-    }
-}`
-	req, _ := http.NewRequest("POST", push_url, strings.NewReader(bodystr))
-	req.Header.Add("Content-Type", "application/json")
-	req.Header.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(appid+":"+appSecret)))
-	resp, err := http.DefaultClient.Do(req)
-	if err != nil {
-		fmt.Println(err.Error())
-	} else {
-		bs, _ := ioutil.ReadAll(resp.Body)
-		resp.Body.Close()
-		fmt.Println(string(bs))
-	}
-}

+ 43 - 0
golang/src/jgpush.json

@@ -0,0 +1,43 @@
+{
+    "pushurl": "https://bjapi.push.jiguang.cn/v3/push",
+    "appid": "5efa1257867cf5d77d007ce6",
+    "appSecret": "d7a4e79978cd73cd61a5c7f7",
+    "alertlen": 20,
+    "title": {
+        "bid": "剑鱼推送消息,最新的订阅内容",
+        "project": "剑鱼推送消息,我关注的项目",
+        "entname": "剑鱼推送消息,我关注的企业",
+        "message": "剑鱼推送消息"
+    },
+    "config": {
+        "platform": "all",
+        "audience": {
+            "registration_id": []
+        },
+        "notification": {
+            "android": {
+                "alert": "剑鱼推送消息,最新的订阅内容",
+                "title": "剑鱼招标订阅",
+                "builder_id": 1,
+                "extras": {}
+            },
+            "ios": {
+                "alert": "剑鱼推送消息,最新的订阅内容",
+                "sound": "default",
+                "badge": "+1",
+                "extras": {}
+            }
+        },
+        "message": {
+            "msg_content": "剑鱼推送消息,最新的订阅内容",
+            "content_type": "剑鱼招标订阅,最新的订阅内容",
+            "title": "剑鱼招标订阅",
+            "extras": {}
+        },
+        "options": {
+            "time_to_live": 86400,
+            "apns_production": false,
+            "apns_collapse_id": "jiguang_test_201706011100"
+        }
+    }
+}

+ 309 - 0
golang/src/jgpush/jgpush.go

@@ -0,0 +1,309 @@
+// jpush
+package jgpush
+
+import (
+	"container/list"
+	"encoding/base64"
+	"encoding/json"
+	"io/ioutil"
+	"net/http"
+	qu "qfw/util"
+	"strings"
+	"sync"
+	"time"
+
+	"github.com/donnie4w/go-logger/logger"
+)
+
+type jgconfig struct {
+	Pushurl, Appid, AppSecret string
+	Title                     map[string]interface{}
+	Config                    config
+	Alertlen                  int
+}
+type config struct {
+	Platform     string
+	Audience     map[string]interface{}
+	Notification map[string]interface{}
+	Message      map[string]interface{}
+	Options      map[string]interface{}
+}
+
+var lock *sync.Mutex
+var jpushList *list.List
+var Jgconfig jgconfig
+
+func init() {
+	logger.SetConsole(false)
+	logger.SetRollingDaily("./logs", "jpush.log")
+	qu.ReadConfig("./jgpush.json", &Jgconfig)
+	jpushList = list.New()
+	lock = new(sync.Mutex)
+}
+
+/**
+*通知推送
+alert:通知内容
+title:通知标题
+infotype:推送信息类型(bid,project,entname)
+ids:极光id组
+extras:自定义字段
+broadcast:是否广播,广播ids作废
+*/
+func JgpushNc(alter, infotype string, ids []string, extras map[string]interface{}, isbroadcast bool) {
+	temp := &Jgconfig.Config
+	temp.Audience["registration_id"] = ids
+	android := temp.Notification["android"]
+	if tmp, ok := android.(map[string]interface{}); ok {
+		tmp["title"] = Jgconfig.Title[infotype]
+		tmp["alert"] = shorAlert(alter, Jgconfig.Alertlen)
+		//extras["descript"] = tmp["alter"]
+		tmp["extras"] = map[string]interface{}{
+			"type": infotype,
+			"info": extras,
+		}
+	}
+	ios := temp.Notification["ios"]
+	if tmp, ok := ios.(map[string]interface{}); ok {
+		tmp["alert"] = shorAlert(alter, Jgconfig.Alertlen)
+		tmp["extras"] = map[string]interface{}{
+			"type": infotype,
+			"info": extras,
+		}
+	}
+
+	sendcon := map[string]interface{}{
+		"platform":     temp.Platform,
+		"audience":     temp.Audience,
+		"notification": temp.Notification,
+		"options":      temp.Options,
+	}
+	if isbroadcast {
+		sendcon["audience"] = "all"
+	}
+	bs, err := json.Marshal(sendcon)
+	if err == nil {
+		defer lock.Unlock()
+		lock.Lock()
+		jpushList.PushBack(string(bs))
+	} else {
+		logger.Error("json Marshal ", err.Error())
+	}
+}
+
+//直接推送,非队列
+func JgpushD_Nc(alert, infotype string, ids []string, extras map[string]interface{}, isbroadcast 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["title"] = Jgconfig.Title[infotype]
+		tmp["alert"] = shorAlert(alert, Jgconfig.Alertlen)
+		tmp["extras"] = map[string]interface{}{
+			"type": infotype,
+			"info": extras,
+		}
+	}
+	ios := temp.Notification["ios"]
+	if tmp, ok := ios.(map[string]interface{}); ok {
+		tmp["alert"] = shorAlert(alert, Jgconfig.Alertlen)
+		tmp["extras"] = map[string]interface{}{
+			"type": infotype,
+			"info": extras,
+		}
+	}
+
+	sendcon := map[string]interface{}{
+		"platform":     temp.Platform,
+		"audience":     temp.Audience,
+		"notification": temp.Notification,
+		"options":      temp.Options,
+	}
+	if isbroadcast {
+		sendcon["audience"] = "all"
+	}
+	return pushD(sendcon)
+}
+
+/**
+*消息推送
+msg_con:消息内容
+msg_title:消息标题
+msg_type:消息内容类型
+ids:极光id组
+extras:自定义字段
+broadcast:是否广播,广播ids作废
+*/
+func JgpushMsg(msg_title, msg_con, msg_type string, ids []string, extras map[string]interface{}, isbroadcast bool) {
+	temp := Jgconfig.Config
+	temp.Audience["registration_id"] = ids
+	message := temp.Message
+	message["msg_content"] = msg_con
+	message["content_type"] = msg_type
+	message["title"] = msg_title
+	message["extras"] = extras
+
+	sendcon := map[string]interface{}{
+		"platform": temp.Platform,
+		"audience": temp.Audience,
+		"options":  temp.Options,
+		"message":  temp.Message,
+	}
+	if isbroadcast {
+		sendcon["audience"] = "all"
+	}
+	bs, err := json.Marshal(sendcon)
+	if err == nil {
+		defer lock.Unlock()
+		lock.Lock()
+		jpushList.PushBack(string(bs))
+	} else {
+		logger.Error("json Marshal ", err.Error())
+	}
+}
+
+//直接推送,非队列
+func JgpushD_Msg(msg_title, msg_con, msg_type string, ids []string, extras map[string]interface{}, isbroadcast bool) map[string]interface{} {
+	temp := Jgconfig.Config
+	temp.Audience["registration_id"] = ids
+	message := temp.Message
+	message["msg_content"] = msg_con
+	message["content_type"] = msg_type
+	message["title"] = msg_title
+	message["extras"] = extras
+
+	sendcon := map[string]interface{}{
+		"platform": temp.Platform,
+		"audience": temp.Audience,
+		"options":  temp.Options,
+		"message":  temp.Message,
+	}
+	if isbroadcast {
+		sendcon["audience"] = "all"
+	}
+	return pushD(sendcon)
+}
+
+func pushD(data map[string]interface{}) map[string]interface{} {
+	bs, err := json.Marshal(data)
+	tmp := map[string]interface{}{}
+	if err == nil {
+		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)
+		defer resp.Body.Close()
+		if err != nil {
+			tmp["err"] = err.Error()
+			logger.Error(err.Error(), "data ", data)
+		} else {
+			bs, _ := ioutil.ReadAll(resp.Body)
+			json.Unmarshal(bs, &tmp)
+			logger.Info(string(bs))
+		}
+	} else {
+		tmp["err"] = err.Error()
+	}
+	return tmp
+}
+
+//通知、消息推送、广播
+func JgpushNcAndMsg(alert, infotype string, msg_title, msg_con, msg_type string, ids []string, extras, msg_extras map[string]interface{}, isbroadcast bool) {
+	temp := Jgconfig.Config
+	temp.Audience["registration_id"] = ids
+	android := temp.Notification["android"]
+	if tmp, ok := android.(map[string]interface{}); ok {
+		tmp["title"] = Jgconfig.Title[infotype]
+		tmp["alert"] = shorAlert(alert, Jgconfig.Alertlen)
+		tmp["extras"] = map[string]interface{}{
+			"type": infotype,
+			"info": extras,
+		}
+	}
+	ios := temp.Notification["ios"]
+	if tmp, ok := ios.(map[string]interface{}); ok {
+		tmp["alert"] = shorAlert(alert, Jgconfig.Alertlen)
+		tmp["extras"] = map[string]interface{}{
+			"type": infotype,
+			"info": extras,
+		}
+	}
+	temp.Audience["registration_id"] = ids
+	message := temp.Message
+	message["msg_content"] = msg_con
+	message["content_type"] = msg_type
+	message["title"] = msg_title
+	message["extras"] = extras
+
+	sendcon := map[string]interface{}{
+		"platform":     temp.Platform,
+		"audience":     temp.Audience,
+		"notification": temp.Notification,
+		"options":      temp.Options,
+		"message":      temp.Message,
+	}
+	if isbroadcast {
+		sendcon["audience"] = "all"
+	}
+	bs, err := json.Marshal(sendcon)
+	if err == nil {
+		defer lock.Unlock()
+		lock.Lock()
+		jpushList.PushBack(string(bs))
+	} else {
+		logger.Error("json Marshal ", err.Error())
+	}
+}
+
+func JPush() {
+	plist := getplist()
+	var n *list.Element
+	for e := plist.Front(); e != nil; e = n {
+		req, _ := http.NewRequest("POST", Jgconfig.Pushurl, strings.NewReader(qu.ObjToString(e.Value)))
+		req.Header.Add("Content-Type", "application/json")
+		req.Header.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(Jgconfig.Appid+":"+Jgconfig.AppSecret)))
+		b := false
+		tmp := map[string]interface{}{}
+		for i := 0; i < 5; i++ {
+			resp, err := http.DefaultClient.Do(req)
+			defer resp.Body.Close()
+			if err != nil {
+				tmp["err"] = err.Error()
+			} else {
+				bs, _ := ioutil.ReadAll(resp.Body)
+				json.Unmarshal(bs, &tmp)
+			}
+			if qu.Int64All(tmp["sendno"]) == 0 {
+				b = true
+				break
+			}
+			time.Sleep(5 * time.Second)
+		}
+		if !b {
+			logger.Warn("jpush fail ", tmp, e.Value)
+		} else {
+			logger.Info(tmp, "jpush success ", e.Value)
+		}
+		n = e.Next()
+		plist.Remove(e)
+		time.Sleep(2 * time.Millisecond)
+	}
+	time.AfterFunc(30*time.Second, JPush)
+}
+
+func shorAlert(alert string, l int) string {
+	lg := len([]rune(alert))
+	if lg > l {
+		return string([]rune(alert)[:l])
+	} else {
+		return alert
+	}
+}
+
+func getplist() list.List {
+	defer lock.Unlock()
+	lock.Lock()
+	tmp := *jpushList
+	jpushList = list.New()
+	return tmp
+}

+ 10 - 0
golang/src/main.go

@@ -0,0 +1,10 @@
+package main
+
+import (
+	"jgpush"
+)
+
+//13065ffa4e027b9d39e
+func main() {
+	jgpush.JgpushD_Nc("北京拓普丰联计算机有限公司", "entname", []string{"13065ffa4e027b9d39e"}, nil, false)
+}

+ 22 - 0
golang/src/main_test.go

@@ -0,0 +1,22 @@
+// main_test
+package main
+
+import (
+	"jgpush"
+	"log"
+	"testing"
+)
+
+func Test_jgpush(t *testing.T) {
+	ids := []string{"13065ffa4e027b9d39e"}
+	//通知
+	tmp := jgpush.JgpushD_Nc("招标公告", "bid", ids, map[string]interface{}{
+		"name": "张三", //信息以及其他自定义属性
+	}, false)
+	log.Println("JgpushD_Nc", tmp)
+	//消息
+	tmp = jgpush.JgpushMsg("msg_title", "msg_con", "text", ids, map[string]interface{}{
+		"name": "张三",
+	}, false)
+	log.Println("JgpushMsg", tmp)
+}