作者 yangfu

1.推送修改日志

... ... @@ -17,6 +17,7 @@ func Notification(header *protocol.RequestHeader, request *protocol.PushInfoRequ
repDevice, _ = repository.NewPushDeviceRepository(nil)
appInfo *protocol.AppInfo
receivers []string
receiverIds []int64
deviceList []*protocol.Device
requestOriginal *protocol.PushInfoOriginalRequest = &protocol.PushInfoOriginalRequest{
Type: request.Type,
... ... @@ -42,13 +43,15 @@ func Notification(header *protocol.RequestHeader, request *protocol.PushInfoRequ
}
for i := range deviceList {
receivers = append(receivers, deviceList[i].ClientId)
receiverIds = append(receiverIds, deviceList[i].Uid)
}
request.ActualReceivers = receiverIds
requestOriginal.AppKey = appInfo.AppKey
requestOriginal.AppId = appInfo.AppId
requestOriginal.Secret = appInfo.AppMasterSecret
requestOriginal.ClientIdList = receivers
_, err = NotificationOriginal(header, requestOriginal)
repDevice.SaveLog(request, err)
rsp, err = NotificationOriginal(header, requestOriginal)
repDevice.SaveLog(request, rsp.ExtData, err)
if err != nil {
err = protocol.NewCustomMessage(1, err.Error())
}
... ... @@ -71,7 +74,7 @@ func NotificationOriginal(header *protocol.RequestHeader, request *protocol.Push
//push.TransmissionContent(utils.JsonAssertString(request.Ext)),
}
)
rsp = &protocol.PushInfoResponse{}
if v, ok := request.Ext["transData"]; ok {
options = append(options, push.TransmissionContent(utils.JsonAssertString(v)))
}
... ... @@ -100,11 +103,10 @@ func NotificationOriginal(header *protocol.RequestHeader, request *protocol.Push
log.Error(err)
return
}
if err = pushService.Send(sendData); err != nil {
if rsp.ExtData, err = pushService.Send(sendData); err != nil {
log.Error(err)
return
}
rsp = &protocol.PushInfoResponse{}
return
}
... ...
... ... @@ -13,6 +13,7 @@ type PushInfoOriginalRequest struct {
Ext map[string]interface{} `json:"ext"` //key->transData:透传数据
}
type PushInfoResponse struct {
ExtData map[string]interface{} `json:"-"`
}
/*PushInfo 推送信息*/
... ... @@ -24,6 +25,8 @@ type PushInfoRequest struct {
Title string `json:"title" valid:"Required"`
Content string `json:"content" valid:"Required"`
Ext map[string]interface{} `json:"ext"` //key->transData:透传数据
ActualReceivers []int64 `json:"-"` //实际发送的人员id列表
}
/*UpdateDevice 更新设备*/
... ...
... ... @@ -46,23 +46,23 @@ func (notify *GetuiNotification) Init(options ...push.Option) error {
notify.retry = 3
return nil
}
func (notify *GetuiNotification) Send(option map[string]interface{}) (err error) {
func (notify *GetuiNotification) Send(option map[string]interface{}) (rsp map[string]interface{}, err error) {
retry := 1
for {
switch notify.Options.PushType {
case push.PushToSingle:
err = notify.pushToSingle(option)
rsp, err = notify.pushToSingle(option)
case push.PushToList:
err = notify.pushToList(option)
rsp, err = notify.pushToList(option)
default:
err = notify.pushToSingle(option)
rsp, err = notify.pushToSingle(option)
}
if err == nil {
break
}
//重试
if err != nil && retry > notify.retry {
return err
return
}
log.Error(fmt.Sprintf("【个推】 重试:%v 失败:%v", retry, err))
retry++
... ... @@ -70,14 +70,15 @@ func (notify *GetuiNotification) Send(option map[string]interface{}) (err error)
break
}
}
return err
return
}
//pushToSingle 单推
func (notify *GetuiNotification) pushToSingle(option map[string]interface{}) (err error) {
func (notify *GetuiNotification) pushToSingle(option map[string]interface{}) (rsp map[string]interface{}, err error) {
var token string
rsp = make(map[string]interface{})
if token, err = notify.GetAuthToken(); err != nil {
return err
return
}
var (
... ... @@ -89,29 +90,31 @@ func (notify *GetuiNotification) pushToSingle(option map[string]interface{}) (er
notify.Request.Header("authtoken", token)
notify.Request.JSONBody(m)
if err = notify.Request.ToJSON(&result); err != nil {
return err
return
}
rsp[push.TaskID] = result.TaskId
notify.print(url, m, result, result)
if err = handleResult(url, result); err != nil {
return err
return
}
return nil
return
}
//pushToList 群推
//步骤1.获取token
//步骤2.save_list_body保存消息共同体
//步骤3.push_list
func (notify *GetuiNotification) pushToList(option map[string]interface{}) (err error) {
func (notify *GetuiNotification) pushToList(option map[string]interface{}) (rsp map[string]interface{}, err error) {
var (
token string
taskId string
)
rsp = make(map[string]interface{})
if token, err = notify.GetAuthToken(); err != nil {
return err
return
}
if taskId, err = notify.saveListBody(token, option); err != nil {
return err
return
}
var (
result *Result
... ... @@ -130,13 +133,14 @@ func (notify *GetuiNotification) pushToList(option map[string]interface{}) (err
notify.Request.Header("authtoken", token)
notify.Request.JSONBody(m)
if err = notify.Request.ToJSON(&result); err != nil {
return err
return
}
rsp[push.TaskID] = result.TaskId
notify.print(url, m, result, result)
if err = handleResult(url, result); err != nil {
return err
return
}
return nil
return
}
//saveListBody 保存消息共同体
... ...
package push
const (
TaskID = "task_id"
)
type INotification interface {
Init(option ...Option) error
Send(option map[string]interface{}) error
Send(option map[string]interface{}) (map[string]interface{}, error)
}
... ...
... ... @@ -75,7 +75,7 @@ func (repository *PushDeviceRepository) UpdateDevice(uid int64, clientId, device
return nil
}
func (repository *PushDeviceRepository) SaveLog(request *domain.PushInfoRequest, err error) error {
func (repository *PushDeviceRepository) SaveLog(request *domain.PushInfoRequest, extData map[string]interface{}, err error) error {
var (
result int
errmsg string
... ... @@ -88,15 +88,16 @@ func (repository *PushDeviceRepository) SaveLog(request *domain.PushInfoRequest,
}
}
sql := `INSERT INTO push_log(msg_type,project,receivers,message,result,err_msg,create_at)VALUES(?,?,?,?,?,?,now())`
sql := `INSERT INTO push_log(msg_type,project,receivers,message,result,err_msg,create_at,ext_data)VALUES(?,?,?,?,?,?,now(),?)`
o := orm.NewOrm()
_, err = o.Raw(sql,
request.Type,
request.ProjectKey,
utils.JsonAssertString(request.Receivers),
utils.JsonAssertString(request.ActualReceivers),
utils.JsonAssertString(request),
result,
errmsg,
utils.JsonAssertString(extData),
).Exec()
if err != nil {
return err
... ...