Commit ae46e864 authored by hujiebin's avatar hujiebin

feat:增加工具包

parent 952ddc73
......@@ -3,6 +3,7 @@ module git.hilo.cn/hilo-common
go 1.17
require (
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1274 // indirect
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
......
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1274 h1:u48e7I7h/BY5uDP8xiIFNaUkdTVk7hjj/Sucg8FrxNU=
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1274/go.mod h1:9CMdKNL3ynIGPpfTcdwTvIm8SGuAZYYC4jFVSSvE1YQ=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da h1:8GUt8eRujhVEGZFFEjBj46YV4rDjvGrNxb0KMWYkL2I=
......@@ -39,6 +41,7 @@ github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/goji/httpauth v0.0.0-20160601135302-2da839ab0f4d/go.mod h1:nnjvkQ9ptGaCkuDUx6wNykzzlUixGxvkme+H/lnzb+A=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
......@@ -62,6 +65,7 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/hashicorp/consul/api v1.7.0 h1:tGs8Oep67r8CcA2Ycmb/8BLBcJ70St44mF2X10a/qPg=
github.com/hashicorp/consul/api v1.7.0/go.mod h1:1NSuaUUkFaJzMasbfq/11wKYWSR67Xn6r2DXKhuDNFg=
......@@ -95,8 +99,11 @@ github.com/jinzhu/now v1.1.4 h1:tHnRBy1i5F2Dh8BAFxqFzxKqqvezXrL2OW1TnX+Mlas=
github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
......@@ -125,6 +132,8 @@ github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eI
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
......@@ -144,6 +153,8 @@ github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w=
github.com/spf13/cast v1.5.0/go.mod h1:SpXXQ5YoyJw6s3/6cMTQuxvgRl3PCJiyaX9p6b155UU=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
......@@ -218,6 +229,7 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190907020128-2ca718005c18/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
......@@ -251,13 +263,16 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/ini.v1 v1.63.2 h1:tGK/CyBg7SMzb60vP1M03vNZ3VDu3wGQJwn7Sxi9r3c=
gopkg.in/ini.v1 v1.63.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/mysql v1.4.3 h1:/JhWJhO2v17d8hjApTltKNADm7K7YI2ogkR7avJUL3k=
gorm.io/driver/mysql v1.4.3/go.mod h1:sSIebwZAVPiT+27jK9HIwvsqOGKx3YMPmrA3mBJR10c=
gorm.io/gorm v1.23.8 h1:h8sGJ+biDgBA1AD1Ha9gFCx7h8npU7AsLdlkX0n2TpE=
......
package emas
import (
"encoding/json"
"fmt"
"git.hilo.cn/hilo-common/mylogrus"
"git.hilo.cn/hilo-common/resource/config"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/services/push"
"strings"
)
var EmasClient *push.Client
func SendMsg(externalId string, deviceType string, body string) error {
if strings.ToLower(deviceType) == "android" {
return sendAndroidMsg(externalId, body)
} else if strings.ToLower(deviceType) == "ios" {
return sendIosMsg(externalId, body)
} else {
err1 := sendAndroidMsg(externalId, body)
err2 := sendIosMsg(externalId, body)
if err1 != nil {
return err1
} else if err2 != nil {
return err2
}
return nil
}
}
func SendNotice(externalId string, deviceType string, title string, body string) error {
if strings.ToLower(deviceType) == "android" {
return sendAndroidNotice(externalId, title, body)
} else if strings.ToLower(deviceType) == "ios" {
return sendIosNotice(externalId, title, body)
} else {
err1 := sendAndroidNotice(externalId, title, body)
err2 := sendIosNotice(externalId, title, body)
if err1 != nil {
return err1
} else if err2 != nil {
return err2
}
mylogrus.MyLog.Infof("emas send notice success externalId:%v", externalId)
return nil
}
}
func SendAndroidNoticeAll(title string, body string) error {
request := push.CreatePushRequest()
request.Scheme = "https"
request.AppKey = requests.Integer(config.GetEmasAndroidAppKey())
request.PushType = "NOTICE"
request.DeviceType = "ANDROID"
request.Target = "ALL"
request.TargetValue = "ALL"
request.Body = body
request.Title = title
//request.JobKey = "1"
//request.SendSpeed = requests.NewInteger(1)
//产品决定
request.StoreOffline = requests.NewBoolean(true)
//request.PushTime = "1"
//默认是72小时
//request.ExpireTime = time.Now().Add(time.Duration(time.Hour * 72)).UTC().Format(time.RFC3339)
/* request.iOSApnsEnv = "1"
request.iOSRemind = requests.NewBoolean(false)
request.iOSRemindBody = "1"
request.iOSBadge = requests.NewInteger(1)
request.iOSBadgeAutoIncrement = requests.NewBoolean(false)
request.iOSSilentNotification = requests.NewBoolean(false)
request.iOSMusic = "1"
request.iOSSubtitle = "1"
request.iOSNotificationCategory = "1"
request.iOSMutableContent = requests.NewBoolean(false)
request.iOSExtParameters = "1"*/
request.AndroidNotifyType = "BOTH"
request.AndroidOpenType = "APPLICATION"
//request.AndroidActivity = "1"
//request.AndroidMusic = "1"
//request.AndroidOpenUrl = "1"
//request.AndroidXiaoMiActivity = "1"
//request.AndroidXiaoMiNotifyTitle = "1"
//request.AndroidXiaoMiNotifyBody = "1"
request.AndroidPopupActivity = "MainActivity"
request.AndroidPopupTitle = title
request.AndroidPopupBody = body
//request.AndroidNotificationBarType = requests.NewInteger(1)
//request.AndroidNotificationBarPriority = requests.NewInteger(1)
//request.AndroidExtParameters = "1"
request.AndroidRemind = requests.NewBoolean(true)
//产品未给信息
request.AndroidNotificationChannel = "com.qiahao.nextvideo_channel_random_pairing"
//request.AndroidNotificationChannel = "2000"
//request.AndroidNotificationXiaomiChannel = "1000"
//
//request.SmsTemplateName = "1"
//request.SmsSignName = "1"
//request.SmsParams = "1"
//request.SmsDelaySecs = requests.NewInteger(1)
//request.SmsSendPolicy = requests.NewInteger(1)
//产品未给信息
//request.AndroidNotificationVivoChannel = "1"
//request.AndroidNotificationHuaweiChannel = "1"
//request.AndroidNotificationNotifyId = requests.NewInteger(1)
//request.iOSNotificationCollapseId = "1"
//mylogrus.MyLog.Infof("emas sendAndroidNotice expireTime:%v ", request.ExpireTime)
response, err := EmasClient.Push(request)
if err != nil {
buf, _ := json.Marshal(response)
mylogrus.MyLog.Errorf("emas sendAndroidNoticeAll fail err:%v title:%v body:%v response:%v", err, title, body, string(buf))
} else {
mylogrus.MyLog.Infof("emas sendAndroidNoticeAll success title:%v body:%v", title, body)
}
return err
}
func SendIosNoticeAll(title string, body string) error {
requestIOS := push.CreatePushNoticeToiOSRequest()
requestIOS.Scheme = "https"
requestIOS.AppKey = requests.Integer(config.GetEmasIosAppKey())
requestIOS.Target = "ALL"
requestIOS.TargetValue = "ALL"
requestIOS.Body = body
requestIOS.Title = title
requestIOS.ApnsEnv = config.GetEmasApns()
response, err := EmasClient.PushNoticeToiOS(requestIOS)
if err != nil {
buf, _ := json.Marshal(response)
mylogrus.MyLog.Errorf("emas SendIosNoticeAll fail err:%v response:%#v, title:%v, body:%v, apnsEnv:%v", err, string(buf), title, body, config.GetEmasApns())
} else {
mylogrus.MyLog.Infof("emas SendIosNoticeAll success title:%v body:%v", title, body)
}
return err
}
func sendAndroidNotice(externalId string, title string, body string) error {
request := push.CreatePushRequest()
request.Scheme = "https"
request.AppKey = requests.Integer(config.GetEmasAndroidAppKey())
request.PushType = "NOTICE"
request.DeviceType = "ANDROID"
request.Target = "ACCOUNT"
request.TargetValue = externalId
request.Body = body
request.Title = title
//request.JobKey = "1"
//request.SendSpeed = requests.NewInteger(1)
//产品决定
request.StoreOffline = requests.NewBoolean(true)
//request.PushTime = "1"
//默认是72小时
//request.ExpireTime = time.Now().Add(time.Duration(time.Hour * 72)).UTC().Format(time.RFC3339)
/* request.iOSApnsEnv = "1"
request.iOSRemind = requests.NewBoolean(false)
request.iOSRemindBody = "1"
request.iOSBadge = requests.NewInteger(1)
request.iOSBadgeAutoIncrement = requests.NewBoolean(false)
request.iOSSilentNotification = requests.NewBoolean(false)
request.iOSMusic = "1"
request.iOSSubtitle = "1"
request.iOSNotificationCategory = "1"
request.iOSMutableContent = requests.NewBoolean(false)
request.iOSExtParameters = "1"*/
request.AndroidNotifyType = "BOTH"
request.AndroidOpenType = "APPLICATION"
//request.AndroidActivity = "1"
//request.AndroidMusic = "1"
//request.AndroidOpenUrl = "1"
//request.AndroidXiaoMiActivity = "1"
//request.AndroidXiaoMiNotifyTitle = "1"
//request.AndroidXiaoMiNotifyBody = "1"
request.AndroidPopupActivity = "MainActivity"
request.AndroidPopupTitle = title
request.AndroidPopupBody = body
//request.AndroidNotificationBarType = requests.NewInteger(1)
//request.AndroidNotificationBarPriority = requests.NewInteger(1)
//request.AndroidExtParameters = "1"
request.AndroidRemind = requests.NewBoolean(true)
//产品未给信息
request.AndroidNotificationChannel = "com.qiahao.nextvideo_channel_random_pairing"
//request.AndroidNotificationChannel = "2000"
//request.AndroidNotificationXiaomiChannel = "1000"
//
//request.SmsTemplateName = "1"
//request.SmsSignName = "1"
//request.SmsParams = "1"
//request.SmsDelaySecs = requests.NewInteger(1)
//request.SmsSendPolicy = requests.NewInteger(1)
//产品未给信息
//request.AndroidNotificationVivoChannel = "1"
//request.AndroidNotificationHuaweiChannel = "1"
//request.AndroidNotificationNotifyId = requests.NewInteger(1)
//request.iOSNotificationCollapseId = "1"
//mylogrus.MyLog.Infof("emas sendAndroidNotice expireTime:%v ", request.ExpireTime)
response, err := EmasClient.Push(request)
if err != nil {
buf, _ := json.Marshal(response)
mylogrus.MyLog.Errorf("emas sendAndroidNotice fail err:%v externalId:%v, title:%v, body:%v response:%#v", err, externalId, title, body, string(buf))
} else {
mylogrus.MyLog.Infof("emas sendAndroidNotice success externalId:%v, title:%v body:%v", externalId, title, body)
}
return err
}
func SendMultNotice(externalIds []string, title string, body string, deviceType string) (string, error) {
if strings.ToLower(deviceType) == "android" {
return SendMultAndroidNotice(externalIds, title, body)
} else if strings.ToLower(deviceType) == "ios" {
return SendMultIosNotice(externalIds, title, body)
} else {
return "", fmt.Errorf("deviceType:%v not find", deviceType)
}
}
func SendMultIosNotice(externalIds []string, title string, body string) (string, error) {
externalIdStr := ""
if len(externalIds) > 0 {
externalIdStr += externalIds[0]
}
for i := 1; i < len(externalIds); i++ {
externalIdStr = externalIdStr + "," + externalIds[i]
}
requestIOS := push.CreatePushNoticeToiOSRequest()
requestIOS.Scheme = "https"
requestIOS.AppKey = requests.Integer(config.GetEmasIosAppKey())
requestIOS.Target = "ACCOUNT"
requestIOS.TargetValue = externalIdStr
requestIOS.Body = body
requestIOS.Title = title
requestIOS.ApnsEnv = config.GetEmasApns()
response, err := EmasClient.PushNoticeToiOS(requestIOS)
if err != nil {
buf, _ := json.Marshal(response)
mylogrus.MyLog.Errorf("emas SendMultIosNoticeAll fail err:%v response:%#v, title:%v, body:%v, apnsEnv:%v", err, string(buf), title, body, config.GetEmasApns())
} else {
mylogrus.MyLog.Infof("emas SendMultIosNoticeAll success title:%v body:%v", title, body)
}
messageId := ""
if response != nil {
messageId = response.MessageId
}
return messageId, err
}
func SendMultAndroidNotice(externalIds []string, title string, body string) (string, error) {
externalIdStr := ""
if len(externalIds) > 0 {
externalIdStr += externalIds[0]
}
for i := 1; i < len(externalIds); i++ {
externalIdStr = externalIdStr + "," + externalIds[i]
}
request := push.CreatePushRequest()
request.Scheme = "https"
request.AppKey = requests.Integer(config.GetEmasAndroidAppKey())
request.PushType = "NOTICE"
request.DeviceType = "ALL"
request.Target = "ACCOUNT"
request.TargetValue = externalIdStr
request.Body = body
request.Title = title
//request.JobKey = "1"
//request.SendSpeed = requests.NewInteger(1)
//产品决定
request.StoreOffline = requests.NewBoolean(true)
//request.PushTime = "1"
//默认是72小时
//request.ExpireTime = time.Now().Add(time.Duration(time.Hour * 72)).UTC().Format(time.RFC3339)
/* request.iOSApnsEnv = "1"
request.iOSRemind = requests.NewBoolean(false)
request.iOSRemindBody = "1"
request.iOSBadge = requests.NewInteger(1)
request.iOSBadgeAutoIncrement = requests.NewBoolean(false)
request.iOSSilentNotification = requests.NewBoolean(false)
request.iOSMusic = "1"
request.iOSSubtitle = "1"
request.iOSNotificationCategory = "1"
request.iOSMutableContent = requests.NewBoolean(false)
request.iOSExtParameters = "1"*/
request.AndroidNotifyType = "BOTH"
request.AndroidOpenType = "APPLICATION"
//request.AndroidActivity = "1"
//request.AndroidMusic = "1"
//request.AndroidOpenUrl = "1"
//request.AndroidXiaoMiActivity = "1"
//request.AndroidXiaoMiNotifyTitle = "1"
//request.AndroidXiaoMiNotifyBody = "1"
request.AndroidPopupActivity = "MainActivity"
request.AndroidPopupTitle = title
request.AndroidPopupBody = body
//request.AndroidNotificationBarType = requests.NewInteger(1)
//request.AndroidNotificationBarPriority = requests.NewInteger(1)
//request.AndroidExtParameters = "1"
request.AndroidRemind = requests.NewBoolean(true)
//产品未给信息
request.AndroidNotificationChannel = "com.qiahao.nextvideo_channel_random_pairing"
//request.AndroidNotificationChannel = "2000"
//request.AndroidNotificationXiaomiChannel = "1000"
//
//request.SmsTemplateName = "1"
//request.SmsSignName = "1"
//request.SmsParams = "1"
//request.SmsDelaySecs = requests.NewInteger(1)
//request.SmsSendPolicy = requests.NewInteger(1)
//产品未给信息
//request.AndroidNotificationVivoChannel = "1"
//request.AndroidNotificationHuaweiChannel = "1"
//request.AndroidNotificationNotifyId = requests.NewInteger(1)
//request.iOSNotificationCollapseId = "1"
//mylogrus.MyLog.Infof("emas sendAndroidNotice expireTime:%v ", request.ExpireTime)
response, err := EmasClient.Push(request)
if err != nil {
buf, _ := json.Marshal(response)
mylogrus.MyLog.Errorf("emas sendAndroidNotice fail err:%v externalId:%v, title:%v, body:%v response:%#v", err, externalIds, title, body, string(buf))
} else {
mylogrus.MyLog.Infof("emas sendAndroidNotice success externalId:%v, title:%v body:%v", externalIds, title, body)
}
messageId := ""
if response != nil {
messageId = response.MessageId
}
return messageId, err
}
/*func sendAndroidNotice(externalId string, title string, body string) error {
requestAndroid := push.CreatePushNoticeToAndroidRequest()
requestAndroid.Scheme = "https"
requestAndroid.AppKey = requests.Integer(config.GetEmasAndroidAppKey())
requestAndroid.Target = "ACCOUNT"
requestAndroid.TargetValue = externalId
requestAndroid.Body = body
requestAndroid.Title = title
response, err := EmasClient.PushNoticeToAndroid(requestAndroid)
if err != nil {
mylogrus.MyLog.Errorf("emas sendAndroidNotice externalId:%v, fail, response:%#v", externalId, response)
} else {
mylogrus.MyLog.Errorf("emas sendAndroidNotice externalId:%v, success", externalId)
}
return err
}*/
func sendIosNotice(externalId string, title string, body string) error {
requestIOS := push.CreatePushNoticeToiOSRequest()
requestIOS.Scheme = "https"
requestIOS.AppKey = requests.Integer(config.GetEmasIosAppKey())
requestIOS.Target = "ACCOUNT"
requestIOS.TargetValue = externalId
requestIOS.Body = body
requestIOS.Title = title
requestIOS.ApnsEnv = config.GetEmasApns()
response, err := EmasClient.PushNoticeToiOS(requestIOS)
if err != nil {
buf, _ := json.Marshal(response)
mylogrus.MyLog.Errorf("emas sendIosNotice fail err:%v externalId:%v response:%#v, title:%v, body:%v, apnsEnv:%v", err, externalId, string(buf), title, body, config.GetEmasApns())
} else {
mylogrus.MyLog.Infof("emas sendIosNotice success externalId:%v title:%v body:%v", externalId, title, body)
}
return err
}
func sendAndroidMsg(externalId string, body string) error {
request := push.CreatePushRequest()
request.Scheme = "https"
request.AppKey = requests.Integer(config.GetEmasAndroidAppKey())
request.PushType = "MESSAGE"
request.DeviceType = "ANDROID"
request.Target = "ACCOUNT"
request.TargetValue = externalId
request.Body = body
request.Title = body
//产品决定
//request.StoreOffline = requests.NewBoolean(true)
//request.PushTime = "1"
//默认是72小时
//request.ExpireTime = time.Now().Add(time.Minute).UTC().Format(time.RFC3339)
//
response, err := EmasClient.Push(request)
if err != nil {
buf, _ := json.Marshal(response)
mylogrus.MyLog.Errorf("emas sendAndroidMsg fail err:%v, externalId:%v , body:%v ,response:%#v", err, externalId, body, string(buf))
} else {
mylogrus.MyLog.Infof("emas sendAndroidMsg success externalId:%v, body:%v ", externalId, body)
}
return err
//暂时不修改
/* requestAndroid := push.CreatePushMessageToAndroidRequest()
requestAndroid.Scheme = "https"
requestAndroid.AppKey = requests.Integer(config.GetEmasAndroidAppKey())
requestAndroid.Target = "ACCOUNT"
requestAndroid.TargetValue = externalId
requestAndroid.Body = body
requestAndroid.Title = body
response, err := EmasClient.PushMessageToAndroid(requestAndroid)
if err != nil {
mylogrus.MyLog.Errorf("emas sendAndroidMsg fail externalId:%v, body:%v , response:%#v", externalId, body, response)
} else {
mylogrus.MyLog.Infof("emas sendAndroidMsg success externalId:%v, body:%v", externalId, body)
}
return err*/
}
func sendIosMsg(externalId string, body string) error {
request := push.CreatePushRequest()
request.Scheme = "https"
request.AppKey = requests.Integer(config.GetEmasIosAppKey())
request.PushType = "MESSAGE"
request.DeviceType = "iOS"
request.Target = "ACCOUNT"
request.TargetValue = externalId
request.Body = body
request.Title = body
//request.StoreOffline = requests.NewBoolean(true)
response, err := EmasClient.Push(request)
if err != nil {
buf, _ := json.Marshal(response)
mylogrus.MyLog.Errorf("emas sendIosMsg fail externalId:%v, err:%v body:%v, response:%#v", externalId, err, body, string(buf))
} else {
mylogrus.MyLog.Infof("emas sendIosMsg success externalId:%v body:%v", externalId, body)
}
return err
/* requestIos := push.CreatePushMessageToiOSRequest()
requestIos.Scheme = "https"
requestIos.AppKey = requests.Integer(config.GetEmasIosAppKey())
requestIos.Target = "ACCOUNT"
requestIos.TargetValue = externalId
requestIos.Body = body
requestIos.Title = body
response, err := EmasClient.PushMessageToiOS(requestIos)
if err != nil {
mylogrus.MyLog.Errorf("emas sendIosMsg fail externalId:%v body:%v response:%#v", externalId, body, response)
} else {
mylogrus.MyLog.Infof("emas sendIosMsg success externalId:%v body:%v ", externalId, body)
}
return err*/
}
func SendAndroidMsgAll(body string) error {
request := push.CreatePushRequest()
request.Scheme = "https"
request.AppKey = requests.Integer(config.GetEmasAndroidAppKey())
request.PushType = "MESSAGE"
request.DeviceType = "ANDROID"
request.Target = "ALL"
request.TargetValue = "ALL"
request.Body = body
request.Title = body
//产品决定
request.StoreOffline = requests.NewBoolean(true)
//request.PushTime = "1"
//默认是72小时
//request.ExpireTime = time.Now().Add(time.Duration(time.Hour * 72)).UTC().Format(time.RFC3339)
//
response, err := EmasClient.Push(request)
if err != nil {
buf, _ := json.Marshal(response)
mylogrus.MyLog.Errorf("emas sendAndroidMsgAll fail err:%v body:%v ,response:%#v", err, body, string(buf))
} else {
mylogrus.MyLog.Infof("emas sendAndroidMsgAll success body:%v ", body)
}
return err
//暂时不修改
/* requestAndroid := push.CreatePushMessageToAndroidRequest()
requestAndroid.Scheme = "https"
requestAndroid.AppKey = requests.Integer(config.GetEmasAndroidAppKey())
requestAndroid.Target = "ALL"
requestAndroid.TargetValue = "all"
requestAndroid.Body = body
requestAndroid.Title = body
response, err := EmasClient.PushMessageToAndroid(requestAndroid)
if err != nil {
mylogrus.MyLog.Errorf("emas sendAndroidMsgAll fail, body:%v err:%v, response:%#v",body, err, response)
} else {
mylogrus.MyLog.Infof("emas sendAndroidMsgAll success body:%v", body)
}
return err*/
}
func SendIosMsgAll(body string) error {
request := push.CreatePushRequest()
request.Scheme = "https"
request.AppKey = requests.Integer(config.GetEmasIosAppKey())
request.PushType = "MESSAGE"
request.DeviceType = "iOS"
request.Target = "ALL"
request.TargetValue = "ALL"
request.Body = body
request.Title = body
request.StoreOffline = requests.NewBoolean(true)
response, err := EmasClient.Push(request)
if err != nil {
buf, _ := json.Marshal(response)
mylogrus.MyLog.Errorf("emas sendIosMsgAll fail err:%v body:%v, response:%#v", err, body, string(buf))
} else {
mylogrus.MyLog.Infof("emas sendIosMsgAll success, body:%v", body)
}
return err
/* requestIos := push.CreatePushMessageToiOSRequest()
requestIos.Scheme = "https"
requestIos.AppKey = requests.Integer(config.GetEmasIosAppKey())
requestIos.Target = "ALL"
requestIos.TargetValue = "all"
requestIos.Body = body
requestIos.Title = body
response, err := EmasClient.PushMessageToiOS(requestIos)
if err != nil {
mylogrus.MyLog.Errorf("emas sendIosMsgAll fail, body:%v, err:%v, response:%#v", body, err, response)
} else {
mylogrus.MyLog.Infof("emas sendIosMsgAll, success, body:%v", body)
}
return err*/
}
func QueryPushStatByMsg(messageId string) (push.PushStats, error) {
request := push.CreateQueryPushStatByMsgRequest()
request.Scheme = "https"
request.AppKey = requests.Integer(config.GetEmasIosAppKey())
request.MessageId = requests.Integer(messageId)
response, err := EmasClient.QueryPushStatByMsg(request)
if err != nil {
buf, _ := json.Marshal(response)
mylogrus.MyLog.Errorf("emas QueryPushStatByMsg fail err:%v messageId:%v, response:%#v", err, messageId, string(buf))
return push.PushStats{}, err
}
return response.PushStats, nil
}
func init() {
var err error
EmasClient, err = push.NewClientWithAccessKey(config.GetEmasRegionId(), config.GetEmasAccessKeyId(), config.GetEmasAccessKeySecret())
if err != nil {
mylogrus.MyLog.Fatal(err)
}
}
package utils
import "time"
const DEFAULT_LANG = "en"
const DATETIME_FORMAT = "2006-01-02 15:04:05"
const COMPACT_DATE_FORMAT = "20060102"
const DATE_FORMAT = "2006-01-02"
func GetZeroTime(t time.Time) time.Time {
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
}
// 取最近的一个星期n
func GetLastDayOfWeek(t time.Time, n time.Weekday) time.Time {
weekDay := t.Weekday()
// 校正日期
if weekDay < n {
weekDay = 7 - n + weekDay
} else {
weekDay = weekDay - n
}
return t.AddDate(0, 0, -(int(weekDay)))
}
package utils
import (
"encoding/json"
)
// 去除slice中的重复元素
func UniqueSliceUInt64(sliceIn []uint64) []uint64 {
sliceOut := make([]uint64, 0, len(sliceIn))
m := make(map[uint64]struct{}, len(sliceIn))
for _, i := range sliceIn {
if _, ok := m[i]; !ok {
m[i] = struct{}{}
sliceOut = append(sliceOut, i)
}
}
return sliceOut
}
func ToString(s interface{}) (string, error) {
b, err := json.Marshal(s)
if err != nil {
return "", nil
}
return string(b), nil
}
func SliceToMapUInt64(s []uint64) map[uint64]struct{} {
m := make(map[uint64]struct{}, len(s))
for _, i := range s {
m[i] = struct{}{}
}
return m
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment