...
|
...
|
@@ -24,13 +24,18 @@ var ( |
|
|
authtoken = ""
|
|
|
expire time.Time
|
|
|
authMux sync.RWMutex
|
|
|
expireSpan = time.Second * 3600 * 6
|
|
|
expireSpan = time.Second * 3600
|
|
|
)
|
|
|
|
|
|
const (
|
|
|
error_not_auth = "not_auth"
|
|
|
)
|
|
|
|
|
|
//GetuiNotification 个推消息推送
|
|
|
type GetuiNotification struct {
|
|
|
Options *push.Options
|
|
|
Request *httplib.BeegoHTTPRequest
|
|
|
retry int
|
|
|
}
|
|
|
|
|
|
func (notify *GetuiNotification) Init(options ...push.Option) error {
|
...
|
...
|
@@ -38,9 +43,12 @@ func (notify *GetuiNotification) Init(options ...push.Option) error { |
|
|
for _, o := range options {
|
|
|
o(notify.Options)
|
|
|
}
|
|
|
notify.retry = 3
|
|
|
return nil
|
|
|
}
|
|
|
func (notify *GetuiNotification) Send(option map[string]interface{}) (err error) {
|
|
|
retry := 1
|
|
|
for {
|
|
|
switch notify.Options.PushType {
|
|
|
case push.PushToSingle:
|
|
|
err = notify.pushToSingle(option)
|
...
|
...
|
@@ -49,9 +57,19 @@ func (notify *GetuiNotification) Send(option map[string]interface{}) (err error) |
|
|
default:
|
|
|
err = notify.pushToSingle(option)
|
|
|
}
|
|
|
if err != nil {
|
|
|
if err == nil {
|
|
|
break
|
|
|
}
|
|
|
//重试
|
|
|
if err != nil && retry > notify.retry {
|
|
|
return err
|
|
|
}
|
|
|
log.Error(fmt.Sprintf("【个推】 重试:%v 失败:%v", retry, err))
|
|
|
retry++
|
|
|
if retry > notify.retry {
|
|
|
break
|
|
|
}
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
|
...
|
...
|
@@ -206,6 +224,7 @@ func (notify *GetuiNotification) GetAuthToken() (token string, err error) { |
|
|
authtoken = rsp.AuthToken
|
|
|
token = rsp.AuthToken
|
|
|
expire = time.Now().Add(expireSpan)
|
|
|
log.Info(fmt.Sprintf("【个推】token:%v expire:%v", token, expire))
|
|
|
return
|
|
|
}
|
|
|
|
...
|
...
|
@@ -222,6 +241,11 @@ func handleResult(url string, result *Result) (err error) { |
|
|
if strings.ToLower(result.Result) == "ok" {
|
|
|
return
|
|
|
}
|
|
|
switch result.Result {
|
|
|
case error_not_auth:
|
|
|
setToken("")
|
|
|
break
|
|
|
}
|
|
|
err = fmt.Errorf("grequest fail,url:%v error:%v", url, result.Result)
|
|
|
return err
|
|
|
}
|
...
|
...
|
@@ -230,3 +254,9 @@ func sign(appkey, timestamp, mastersecret string) string { |
|
|
sha.Write([]byte(appkey + timestamp + mastersecret))
|
|
|
return fmt.Sprintf("%x", sha.Sum(nil))
|
|
|
}
|
|
|
|
|
|
func setToken(token string) {
|
|
|
authMux.Lock()
|
|
|
defer authMux.Unlock()
|
|
|
authtoken = ""
|
|
|
} |
...
|
...
|
|