作者 yangfu

推送修改

@@ -4,7 +4,7 @@ import "encoding/json" @@ -4,7 +4,7 @@ import "encoding/json"
4 4
5 /*PushInfo 推送信息*/ 5 /*PushInfo 推送信息*/
6 type PushInfoRequest struct { 6 type PushInfoRequest struct {
7 - Type int `json:"mmmType" valid:"Required"` 7 + Type int `json:"mmmType"`
8 DeviceToken json.RawMessage `json:"deviceToken" ` 8 DeviceToken json.RawMessage `json:"deviceToken" `
9 ClientId json.RawMessage `json:"clientId"` 9 ClientId json.RawMessage `json:"clientId"`
10 AppKey string `json:"appKey" valid:"Required"` 10 AppKey string `json:"appKey" valid:"Required"`
@@ -12,6 +12,6 @@ const ( @@ -12,6 +12,6 @@ const (
12 MsgType 消息类型 -> 决定发送的模板样式 12 MsgType 消息类型 -> 决定发送的模板样式
13 */ 13 */
14 const ( 14 const (
15 - SystemTransmission = iota + 1 //系统消息 15 + SystemTransmission = iota //系统消息透传
16 SystemNotification //系统通知 16 SystemNotification //系统通知
17 ) 17 )
@@ -24,13 +24,18 @@ var ( @@ -24,13 +24,18 @@ var (
24 authtoken = "" 24 authtoken = ""
25 expire time.Time 25 expire time.Time
26 authMux sync.RWMutex 26 authMux sync.RWMutex
27 - expireSpan = time.Second * 3600 * 6 27 + expireSpan = time.Second * 3600
  28 +)
  29 +
  30 +const (
  31 + error_not_auth = "not_auth"
28 ) 32 )
29 33
30 //GetuiNotification 个推消息推送 34 //GetuiNotification 个推消息推送
31 type GetuiNotification struct { 35 type GetuiNotification struct {
32 Options *push.Options 36 Options *push.Options
33 Request *httplib.BeegoHTTPRequest 37 Request *httplib.BeegoHTTPRequest
  38 + retry int
34 } 39 }
35 40
36 func (notify *GetuiNotification) Init(options ...push.Option) error { 41 func (notify *GetuiNotification) Init(options ...push.Option) error {
@@ -38,9 +43,12 @@ func (notify *GetuiNotification) Init(options ...push.Option) error { @@ -38,9 +43,12 @@ func (notify *GetuiNotification) Init(options ...push.Option) error {
38 for _, o := range options { 43 for _, o := range options {
39 o(notify.Options) 44 o(notify.Options)
40 } 45 }
  46 + notify.retry = 3
41 return nil 47 return nil
42 } 48 }
43 func (notify *GetuiNotification) Send(option map[string]interface{}) (err error) { 49 func (notify *GetuiNotification) Send(option map[string]interface{}) (err error) {
  50 + retry := 1
  51 + for {
44 switch notify.Options.PushType { 52 switch notify.Options.PushType {
45 case push.PushToSingle: 53 case push.PushToSingle:
46 err = notify.pushToSingle(option) 54 err = notify.pushToSingle(option)
@@ -49,9 +57,19 @@ func (notify *GetuiNotification) Send(option map[string]interface{}) (err error) @@ -49,9 +57,19 @@ func (notify *GetuiNotification) Send(option map[string]interface{}) (err error)
49 default: 57 default:
50 err = notify.pushToSingle(option) 58 err = notify.pushToSingle(option)
51 } 59 }
52 - if err != nil { 60 + if err == nil {
  61 + break
  62 + }
  63 + //重试
  64 + if err != nil && retry > notify.retry {
53 return err 65 return err
54 } 66 }
  67 + log.Error(fmt.Sprintf("【个推】 重试:%v 失败:%v", retry, err))
  68 + retry++
  69 + if retry > notify.retry {
  70 + break
  71 + }
  72 + }
55 return nil 73 return nil
56 } 74 }
57 75
@@ -206,6 +224,7 @@ func (notify *GetuiNotification) GetAuthToken() (token string, err error) { @@ -206,6 +224,7 @@ func (notify *GetuiNotification) GetAuthToken() (token string, err error) {
206 authtoken = rsp.AuthToken 224 authtoken = rsp.AuthToken
207 token = rsp.AuthToken 225 token = rsp.AuthToken
208 expire = time.Now().Add(expireSpan) 226 expire = time.Now().Add(expireSpan)
  227 + log.Info(fmt.Sprintf("【个推】token:%v expire:%v", token, expire))
209 return 228 return
210 } 229 }
211 230
@@ -222,6 +241,11 @@ func handleResult(url string, result *Result) (err error) { @@ -222,6 +241,11 @@ func handleResult(url string, result *Result) (err error) {
222 if strings.ToLower(result.Result) == "ok" { 241 if strings.ToLower(result.Result) == "ok" {
223 return 242 return
224 } 243 }
  244 + switch result.Result {
  245 + case error_not_auth:
  246 + setToken("")
  247 + break
  248 + }
225 err = fmt.Errorf("grequest fail,url:%v error:%v", url, result.Result) 249 err = fmt.Errorf("grequest fail,url:%v error:%v", url, result.Result)
226 return err 250 return err
227 } 251 }
@@ -230,3 +254,9 @@ func sign(appkey, timestamp, mastersecret string) string { @@ -230,3 +254,9 @@ func sign(appkey, timestamp, mastersecret string) string {
230 sha.Write([]byte(appkey + timestamp + mastersecret)) 254 sha.Write([]byte(appkey + timestamp + mastersecret))
231 return fmt.Sprintf("%x", sha.Sum(nil)) 255 return fmt.Sprintf("%x", sha.Sum(nil))
232 } 256 }
  257 +
  258 +func setToken(token string) {
  259 + authMux.Lock()
  260 + defer authMux.Unlock()
  261 + authtoken = ""
  262 +}