作者 yangfu

1.推送修改日志

@@ -17,6 +17,7 @@ func Notification(header *protocol.RequestHeader, request *protocol.PushInfoRequ @@ -17,6 +17,7 @@ func Notification(header *protocol.RequestHeader, request *protocol.PushInfoRequ
17 repDevice, _ = repository.NewPushDeviceRepository(nil) 17 repDevice, _ = repository.NewPushDeviceRepository(nil)
18 appInfo *protocol.AppInfo 18 appInfo *protocol.AppInfo
19 receivers []string 19 receivers []string
  20 + receiverIds []int64
20 deviceList []*protocol.Device 21 deviceList []*protocol.Device
21 requestOriginal *protocol.PushInfoOriginalRequest = &protocol.PushInfoOriginalRequest{ 22 requestOriginal *protocol.PushInfoOriginalRequest = &protocol.PushInfoOriginalRequest{
22 Type: request.Type, 23 Type: request.Type,
@@ -42,13 +43,15 @@ func Notification(header *protocol.RequestHeader, request *protocol.PushInfoRequ @@ -42,13 +43,15 @@ func Notification(header *protocol.RequestHeader, request *protocol.PushInfoRequ
42 } 43 }
43 for i := range deviceList { 44 for i := range deviceList {
44 receivers = append(receivers, deviceList[i].ClientId) 45 receivers = append(receivers, deviceList[i].ClientId)
  46 + receiverIds = append(receiverIds, deviceList[i].Uid)
45 } 47 }
  48 + request.ActualReceivers = receiverIds
46 requestOriginal.AppKey = appInfo.AppKey 49 requestOriginal.AppKey = appInfo.AppKey
47 requestOriginal.AppId = appInfo.AppId 50 requestOriginal.AppId = appInfo.AppId
48 requestOriginal.Secret = appInfo.AppMasterSecret 51 requestOriginal.Secret = appInfo.AppMasterSecret
49 requestOriginal.ClientIdList = receivers 52 requestOriginal.ClientIdList = receivers
50 - _, err = NotificationOriginal(header, requestOriginal)  
51 - repDevice.SaveLog(request, err) 53 + rsp, err = NotificationOriginal(header, requestOriginal)
  54 + repDevice.SaveLog(request, rsp.ExtData, err)
52 if err != nil { 55 if err != nil {
53 err = protocol.NewCustomMessage(1, err.Error()) 56 err = protocol.NewCustomMessage(1, err.Error())
54 } 57 }
@@ -71,7 +74,7 @@ func NotificationOriginal(header *protocol.RequestHeader, request *protocol.Push @@ -71,7 +74,7 @@ func NotificationOriginal(header *protocol.RequestHeader, request *protocol.Push
71 //push.TransmissionContent(utils.JsonAssertString(request.Ext)), 74 //push.TransmissionContent(utils.JsonAssertString(request.Ext)),
72 } 75 }
73 ) 76 )
74 - 77 + rsp = &protocol.PushInfoResponse{}
75 if v, ok := request.Ext["transData"]; ok { 78 if v, ok := request.Ext["transData"]; ok {
76 options = append(options, push.TransmissionContent(utils.JsonAssertString(v))) 79 options = append(options, push.TransmissionContent(utils.JsonAssertString(v)))
77 } 80 }
@@ -100,11 +103,10 @@ func NotificationOriginal(header *protocol.RequestHeader, request *protocol.Push @@ -100,11 +103,10 @@ func NotificationOriginal(header *protocol.RequestHeader, request *protocol.Push
100 log.Error(err) 103 log.Error(err)
101 return 104 return
102 } 105 }
103 - if err = pushService.Send(sendData); err != nil { 106 + if rsp.ExtData, err = pushService.Send(sendData); err != nil {
104 log.Error(err) 107 log.Error(err)
105 return 108 return
106 } 109 }
107 - rsp = &protocol.PushInfoResponse{}  
108 return 110 return
109 } 111 }
110 112
@@ -13,6 +13,7 @@ type PushInfoOriginalRequest struct { @@ -13,6 +13,7 @@ type PushInfoOriginalRequest struct {
13 Ext map[string]interface{} `json:"ext"` //key->transData:透传数据 13 Ext map[string]interface{} `json:"ext"` //key->transData:透传数据
14 } 14 }
15 type PushInfoResponse struct { 15 type PushInfoResponse struct {
  16 + ExtData map[string]interface{} `json:"-"`
16 } 17 }
17 18
18 /*PushInfo 推送信息*/ 19 /*PushInfo 推送信息*/
@@ -24,6 +25,8 @@ type PushInfoRequest struct { @@ -24,6 +25,8 @@ type PushInfoRequest struct {
24 Title string `json:"title" valid:"Required"` 25 Title string `json:"title" valid:"Required"`
25 Content string `json:"content" valid:"Required"` 26 Content string `json:"content" valid:"Required"`
26 Ext map[string]interface{} `json:"ext"` //key->transData:透传数据 27 Ext map[string]interface{} `json:"ext"` //key->transData:透传数据
  28 +
  29 + ActualReceivers []int64 `json:"-"` //实际发送的人员id列表
27 } 30 }
28 31
29 /*UpdateDevice 更新设备*/ 32 /*UpdateDevice 更新设备*/
@@ -46,23 +46,23 @@ func (notify *GetuiNotification) Init(options ...push.Option) error { @@ -46,23 +46,23 @@ func (notify *GetuiNotification) Init(options ...push.Option) error {
46 notify.retry = 3 46 notify.retry = 3
47 return nil 47 return nil
48 } 48 }
49 -func (notify *GetuiNotification) Send(option map[string]interface{}) (err error) { 49 +func (notify *GetuiNotification) Send(option map[string]interface{}) (rsp map[string]interface{}, err error) {
50 retry := 1 50 retry := 1
51 for { 51 for {
52 switch notify.Options.PushType { 52 switch notify.Options.PushType {
53 case push.PushToSingle: 53 case push.PushToSingle:
54 - err = notify.pushToSingle(option) 54 + rsp, err = notify.pushToSingle(option)
55 case push.PushToList: 55 case push.PushToList:
56 - err = notify.pushToList(option) 56 + rsp, err = notify.pushToList(option)
57 default: 57 default:
58 - err = notify.pushToSingle(option) 58 + rsp, err = notify.pushToSingle(option)
59 } 59 }
60 if err == nil { 60 if err == nil {
61 break 61 break
62 } 62 }
63 //重试 63 //重试
64 if err != nil && retry > notify.retry { 64 if err != nil && retry > notify.retry {
65 - return err 65 + return
66 } 66 }
67 log.Error(fmt.Sprintf("【个推】 重试:%v 失败:%v", retry, err)) 67 log.Error(fmt.Sprintf("【个推】 重试:%v 失败:%v", retry, err))
68 retry++ 68 retry++
@@ -70,14 +70,15 @@ func (notify *GetuiNotification) Send(option map[string]interface{}) (err error) @@ -70,14 +70,15 @@ func (notify *GetuiNotification) Send(option map[string]interface{}) (err error)
70 break 70 break
71 } 71 }
72 } 72 }
73 - return err 73 + return
74 } 74 }
75 75
76 //pushToSingle 单推 76 //pushToSingle 单推
77 -func (notify *GetuiNotification) pushToSingle(option map[string]interface{}) (err error) { 77 +func (notify *GetuiNotification) pushToSingle(option map[string]interface{}) (rsp map[string]interface{}, err error) {
78 var token string 78 var token string
  79 + rsp = make(map[string]interface{})
79 if token, err = notify.GetAuthToken(); err != nil { 80 if token, err = notify.GetAuthToken(); err != nil {
80 - return err 81 + return
81 } 82 }
82 83
83 var ( 84 var (
@@ -89,29 +90,31 @@ func (notify *GetuiNotification) pushToSingle(option map[string]interface{}) (er @@ -89,29 +90,31 @@ func (notify *GetuiNotification) pushToSingle(option map[string]interface{}) (er
89 notify.Request.Header("authtoken", token) 90 notify.Request.Header("authtoken", token)
90 notify.Request.JSONBody(m) 91 notify.Request.JSONBody(m)
91 if err = notify.Request.ToJSON(&result); err != nil { 92 if err = notify.Request.ToJSON(&result); err != nil {
92 - return err 93 + return
93 } 94 }
  95 + rsp[push.TaskID] = result.TaskId
94 notify.print(url, m, result, result) 96 notify.print(url, m, result, result)
95 if err = handleResult(url, result); err != nil { 97 if err = handleResult(url, result); err != nil {
96 - return err 98 + return
97 } 99 }
98 - return nil 100 + return
99 } 101 }
100 102
101 //pushToList 群推 103 //pushToList 群推
102 //步骤1.获取token 104 //步骤1.获取token
103 //步骤2.save_list_body保存消息共同体 105 //步骤2.save_list_body保存消息共同体
104 //步骤3.push_list 106 //步骤3.push_list
105 -func (notify *GetuiNotification) pushToList(option map[string]interface{}) (err error) { 107 +func (notify *GetuiNotification) pushToList(option map[string]interface{}) (rsp map[string]interface{}, err error) {
106 var ( 108 var (
107 token string 109 token string
108 taskId string 110 taskId string
109 ) 111 )
  112 + rsp = make(map[string]interface{})
110 if token, err = notify.GetAuthToken(); err != nil { 113 if token, err = notify.GetAuthToken(); err != nil {
111 - return err 114 + return
112 } 115 }
113 if taskId, err = notify.saveListBody(token, option); err != nil { 116 if taskId, err = notify.saveListBody(token, option); err != nil {
114 - return err 117 + return
115 } 118 }
116 var ( 119 var (
117 result *Result 120 result *Result
@@ -130,13 +133,14 @@ func (notify *GetuiNotification) pushToList(option map[string]interface{}) (err @@ -130,13 +133,14 @@ func (notify *GetuiNotification) pushToList(option map[string]interface{}) (err
130 notify.Request.Header("authtoken", token) 133 notify.Request.Header("authtoken", token)
131 notify.Request.JSONBody(m) 134 notify.Request.JSONBody(m)
132 if err = notify.Request.ToJSON(&result); err != nil { 135 if err = notify.Request.ToJSON(&result); err != nil {
133 - return err 136 + return
134 } 137 }
  138 + rsp[push.TaskID] = result.TaskId
135 notify.print(url, m, result, result) 139 notify.print(url, m, result, result)
136 if err = handleResult(url, result); err != nil { 140 if err = handleResult(url, result); err != nil {
137 - return err 141 + return
138 } 142 }
139 - return nil 143 + return
140 } 144 }
141 145
142 //saveListBody 保存消息共同体 146 //saveListBody 保存消息共同体
1 package push 1 package push
2 2
  3 +const (
  4 + TaskID = "task_id"
  5 +)
  6 +
3 type INotification interface { 7 type INotification interface {
4 Init(option ...Option) error 8 Init(option ...Option) error
5 - Send(option map[string]interface{}) error 9 + Send(option map[string]interface{}) (map[string]interface{}, error)
6 } 10 }
@@ -75,7 +75,7 @@ func (repository *PushDeviceRepository) UpdateDevice(uid int64, clientId, device @@ -75,7 +75,7 @@ func (repository *PushDeviceRepository) UpdateDevice(uid int64, clientId, device
75 return nil 75 return nil
76 } 76 }
77 77
78 -func (repository *PushDeviceRepository) SaveLog(request *domain.PushInfoRequest, err error) error { 78 +func (repository *PushDeviceRepository) SaveLog(request *domain.PushInfoRequest, extData map[string]interface{}, err error) error {
79 var ( 79 var (
80 result int 80 result int
81 errmsg string 81 errmsg string
@@ -88,15 +88,16 @@ func (repository *PushDeviceRepository) SaveLog(request *domain.PushInfoRequest, @@ -88,15 +88,16 @@ func (repository *PushDeviceRepository) SaveLog(request *domain.PushInfoRequest,
88 } 88 }
89 } 89 }
90 90
91 - sql := `INSERT INTO push_log(msg_type,project,receivers,message,result,err_msg,create_at)VALUES(?,?,?,?,?,?,now())` 91 + sql := `INSERT INTO push_log(msg_type,project,receivers,message,result,err_msg,create_at,ext_data)VALUES(?,?,?,?,?,?,now(),?)`
92 o := orm.NewOrm() 92 o := orm.NewOrm()
93 _, err = o.Raw(sql, 93 _, err = o.Raw(sql,
94 request.Type, 94 request.Type,
95 request.ProjectKey, 95 request.ProjectKey,
96 - utils.JsonAssertString(request.Receivers), 96 + utils.JsonAssertString(request.ActualReceivers),
97 utils.JsonAssertString(request), 97 utils.JsonAssertString(request),
98 result, 98 result,
99 errmsg, 99 errmsg,
  100 + utils.JsonAssertString(extData),
100 ).Exec() 101 ).Exec()
101 if err != nil { 102 if err != nil {
102 return err 103 return err