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