作者 yangfu

推送修改

... ... @@ -4,7 +4,7 @@ import "encoding/json"
/*PushInfo 推送信息*/
type PushInfoRequest struct {
Type int `json:"mmmType" valid:"Required"`
Type int `json:"mmmType"`
DeviceToken json.RawMessage `json:"deviceToken" `
ClientId json.RawMessage `json:"clientId"`
AppKey string `json:"appKey" valid:"Required"`
... ...
... ... @@ -12,6 +12,6 @@ const (
MsgType 消息类型 -> 决定发送的模板样式
*/
const (
SystemTransmission = iota + 1 //系统消息
SystemNotification //系统通知
SystemTransmission = iota //系统消息透传
SystemNotification //系统通知
)
... ...
... ... @@ -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,19 +43,32 @@ 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) {
switch notify.Options.PushType {
case push.PushToSingle:
err = notify.pushToSingle(option)
case push.PushToList:
err = notify.pushToList(option)
default:
err = notify.pushToSingle(option)
}
if err != nil {
return err
retry := 1
for {
switch notify.Options.PushType {
case push.PushToSingle:
err = notify.pushToSingle(option)
case push.PushToList:
err = notify.pushToList(option)
default:
err = notify.pushToSingle(option)
}
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 = ""
}
... ...