Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
U
utils
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
library
utils
Commits
ef146a9f
Verified
Commit
ef146a9f
authored
Jan 17, 2019
by
Cui
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add umeng api
parent
9a4c7dd0
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
376 additions
and
0 deletions
+376
-0
go.mod
go.mod
+1
-0
go.sum
go.sum
+2
-0
umeng.go
sender/umeng.go
+373
-0
No files found.
go.mod
View file @
ef146a9f
module gitlab.qingclass.cn/cuiweiqiang/utils
require (
github.com/heycayc/chkcki v0.0.0-20180425093509-ad62647adf04
github.com/sirupsen/logrus v1.3.0
github.com/valyala/fasthttp v1.0.0
)
go.sum
View file @
ef146a9f
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/heycayc/chkcki v0.0.0-20180425093509-ad62647adf04 h1:S+9Ua2s9IVhZUzuqEBRtgvGyE0BNOwAVtl5zvVsoNAw=
github.com/heycayc/chkcki v0.0.0-20180425093509-ad62647adf04/go.mod h1:68nSgivWifFndv3X4OMS7EIuHaNxJb1PlYECLiH99jU=
github.com/klauspost/compress v1.4.0 h1:8nsMz3tWa9SWWPL60G1V6CUsf4lLjWLTNEtibhe8gh8=
github.com/klauspost/compress v1.4.0/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
github.com/klauspost/cpuid v0.0.0-20180405133222-e7e905edc00e h1:+lIPJOWl+jSiJOc70QXJ07+2eg2Jy2EC7Mi11BWujeM=
...
...
sender/umeng.go
0 → 100644
View file @
ef146a9f
/*
* @Author: cuiweiqiang
* @Date: 2019-01-17 11:47
*/
package
sender
import
(
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/heycayc/chkcki/httplib"
"log"
"strings"
"time"
)
const
(
umeng_host
=
"http://msg.umeng.com/api/send"
listcast_device_tokens_max
=
500
customizedcast_alias_max
=
500
// "type":"xx", // 必填,消息发送类型,其值可以为:
TYPE_UNICAST
=
"unicast"
// 单播
TYPE_LISTCAST
=
"listcast"
// 列播,要求不超过500个device_token
TYPE_FILECAST
=
"filecast"
// 文件播,多个device_token可通过文件形式批量发送
TYPE_BROADCAST
=
"broadcast"
// 广播
TYPE_GROUPCAST
=
"groupcast"
// 组播,按照filter筛选用户群, 请参照filter参数
TYPE_CUSTOMIZEDCAST
=
"customizedcast"
// 通过alias进行推送,包括以下两种case:
PAYLOAD_DISPLAY_TYPE_NOTIFICATION
=
"notification"
PAYLOAD_DISPLAY_TYPE_MESSAGE
=
"message"
// "after_open": "xx", // 可选,默认为"go_app",值可以为:
PAYLOAD_AFTER_OPEN_GO_APP
=
"go_app"
// 打开应用
PAYLOAD_AFTER_OPEN_GO_URL
=
"go_url"
// 跳转到URL
PAYLOAD_AFTER_OPEN_GO_ACTIVITY
=
"go_activity"
// 打开特定的activity
PAYLOAD_AFTER_OPEN_GO_CUSTOM
=
"go_custom"
// 用户自定义内容。
)
// https://developer.umeng.com/docs/66632/detail/68343#h3-u529Fu80FDu8BF4u660E
type
(
Reqd
struct
{
Appkey
string
`json:"appkey,omitempty"`
Timestamp
int64
`json:"timestamp,omitempty"`
Type
string
`json:"type,omitempty"`
DeviceTokens
string
`json:"device_tokens,omitempty"`
AliasType
string
`json:"alias_type,omitempty"`
Alias
string
`json:"alias,omitempty"`
FileID
string
`json:"file_id,omitempty"`
Filter
interface
{}
`json:"filter,omitempty"`
Payload
struct
{
DisplayType
string
`json:"display_type,omitempty"`
Body
struct
{
Ticker
string
`json:"ticker,omitempty"`
Title
string
`json:"title,omitempty"`
Text
string
`json:"text,omitempty"`
Icon
string
`json:"icon,omitempty"`
LargeIcon
string
`json:"largeIcon,omitempty"`
Img
string
`json:"img,omitempty"`
Sound
string
`json:"sound,omitempty"`
BuilderID
int
`json:"builder_id,omitempty"`
PlayVibrate
bool
`json:"play_vibrate,omitempty"`
PlayLights
bool
`json:"play_lights,omitempty"`
PlaySound
bool
`json:"play_sound,omitempty"`
AfterOpen
string
`json:"after_open,omitempty"`
URL
string
`json:"url,omitempty"`
Activity
string
`json:"activity,omitempty"`
Custom
interface
{}
`json:"custom,omitempty"`
}
`json:"body,omitempty"`
Extra
interface
{}
`json:"extra,omitempty"`
}
`json:"payload,omitempty"`
Policy
struct
{
StartTime
string
`json:"start_time,omitempty"`
ExpireTime
string
`json:"expire_time,omitempty"`
MaxSendNum
int
`json:"max_send_num,omitempty"`
OutBizNo
string
`json:"out_biz_no,omitempty"`
}
`json:"policy,omitempty"`
ProductionMode
bool
`json:"production_mode,omitempty"`
Description
string
`json:"description,omitempty"`
Mipush
bool
`json:"mipush,omitempty"`
MiActivity
string
`json:"mi_activity,omitempty"`
}
Resp
struct
{
Ret
string
`json:"ret"`
Data
map
[
string
]
interface
{}
`json:"data"`
}
Context
struct
{
AppKey
string
AppSecret
string
}
)
func
(
r
*
Reqd
)
AddDeviceToken
(
token
string
)
*
Reqd
{
if
r
.
Type
==
TYPE_UNICAST
{
r
.
DeviceTokens
=
token
}
else
if
r
.
Type
==
TYPE_LISTCAST
{
arr
:=
strings
.
Split
(
r
.
DeviceTokens
,
","
)
arr
=
append
(
arr
,
token
)
arr
=
arr
[
:
listcast_device_tokens_max
]
r
.
DeviceTokens
=
strings
.
Join
(
arr
,
","
)
}
return
r
}
func
(
r
*
Reqd
)
AddDeviceTokens
(
tokens
[]
string
)
*
Reqd
{
if
len
(
tokens
)
==
0
{
return
r
}
if
r
.
Type
==
TYPE_UNICAST
{
r
.
DeviceTokens
=
tokens
[
0
]
}
else
if
r
.
Type
==
TYPE_LISTCAST
{
arr
:=
strings
.
Split
(
r
.
DeviceTokens
,
","
)
arr
=
append
(
arr
,
tokens
...
)
arr
=
arr
[
:
listcast_device_tokens_max
]
r
.
DeviceTokens
=
strings
.
Join
(
arr
,
","
)
}
return
r
}
func
(
r
*
Reqd
)
SetAliasType
(
typ
string
)
*
Reqd
{
if
r
.
Type
!=
TYPE_CUSTOMIZEDCAST
{
return
r
}
r
.
AliasType
=
typ
return
r
}
func
(
r
*
Reqd
)
AddAlias
(
alias
string
)
*
Reqd
{
if
r
.
Type
!=
TYPE_CUSTOMIZEDCAST
{
return
r
}
arr
:=
strings
.
Split
(
r
.
Alias
,
","
)
if
len
(
arr
)
>
customizedcast_alias_max
{
return
r
}
arr
=
append
(
arr
,
alias
)
r
.
Alias
=
strings
.
Join
(
arr
,
","
)
return
r
}
func
(
r
*
Reqd
)
SetFileId
(
fn
string
)
*
Reqd
{
// not support
return
r
}
func
(
r
*
Reqd
)
SetFilter
(
ft
interface
{})
*
Reqd
{
r
.
Filter
=
ft
return
r
}
func
(
r
*
Reqd
)
SetPayloadDisplyType
(
typ
string
)
*
Reqd
{
r
.
Payload
.
DisplayType
=
typ
return
r
}
func
(
r
*
Reqd
)
SetPayloadTicker
(
val
string
)
*
Reqd
{
if
r
.
Payload
.
DisplayType
==
PAYLOAD_DISPLAY_TYPE_MESSAGE
{
return
r
}
r
.
Payload
.
Body
.
Ticker
=
val
return
r
}
func
(
r
*
Reqd
)
SetPayloadTitle
(
val
string
)
*
Reqd
{
if
r
.
Payload
.
DisplayType
==
PAYLOAD_DISPLAY_TYPE_MESSAGE
{
return
r
}
r
.
Payload
.
Body
.
Title
=
val
return
r
}
func
(
r
*
Reqd
)
SetPayloadText
(
val
string
)
*
Reqd
{
if
r
.
Payload
.
DisplayType
==
PAYLOAD_DISPLAY_TYPE_MESSAGE
{
return
r
}
r
.
Payload
.
Body
.
Text
=
val
return
r
}
func
(
r
*
Reqd
)
SetPayloadIcon
(
val
string
)
*
Reqd
{
if
r
.
Payload
.
DisplayType
==
PAYLOAD_DISPLAY_TYPE_MESSAGE
{
return
r
}
r
.
Payload
.
Body
.
Icon
=
val
return
r
}
func
(
r
*
Reqd
)
SetPayloadLargeIcon
(
val
string
)
*
Reqd
{
if
r
.
Payload
.
DisplayType
==
PAYLOAD_DISPLAY_TYPE_MESSAGE
{
return
r
}
r
.
Payload
.
Body
.
LargeIcon
=
val
return
r
}
func
(
r
*
Reqd
)
SetPayloadImg
(
val
string
)
*
Reqd
{
if
r
.
Payload
.
DisplayType
==
PAYLOAD_DISPLAY_TYPE_MESSAGE
{
return
r
}
r
.
Payload
.
Body
.
Img
=
val
return
r
}
func
(
r
*
Reqd
)
SetPayloadSound
(
val
string
)
*
Reqd
{
if
r
.
Payload
.
DisplayType
==
PAYLOAD_DISPLAY_TYPE_MESSAGE
{
return
r
}
r
.
Payload
.
Body
.
Sound
=
val
return
r
}
func
(
r
*
Reqd
)
SetPayloadBuilderID
(
val
int
)
*
Reqd
{
if
r
.
Payload
.
DisplayType
==
PAYLOAD_DISPLAY_TYPE_MESSAGE
{
return
r
}
r
.
Payload
.
Body
.
BuilderID
=
val
return
r
}
func
(
r
*
Reqd
)
SetPayloadPlayVibrate
(
val
bool
)
*
Reqd
{
if
r
.
Payload
.
DisplayType
==
PAYLOAD_DISPLAY_TYPE_MESSAGE
{
return
r
}
r
.
Payload
.
Body
.
PlayVibrate
=
val
return
r
}
func
(
r
*
Reqd
)
SetPayloadPlayLights
(
val
bool
)
*
Reqd
{
if
r
.
Payload
.
DisplayType
==
PAYLOAD_DISPLAY_TYPE_MESSAGE
{
return
r
}
r
.
Payload
.
Body
.
PlayLights
=
val
return
r
}
func
(
r
*
Reqd
)
SetPayloadPlaySound
(
val
bool
)
*
Reqd
{
if
r
.
Payload
.
DisplayType
==
PAYLOAD_DISPLAY_TYPE_MESSAGE
{
return
r
}
r
.
Payload
.
Body
.
PlaySound
=
val
return
r
}
func
(
r
*
Reqd
)
SetPayloadAfterOpen
(
typ
string
)
*
Reqd
{
if
r
.
Payload
.
DisplayType
==
PAYLOAD_DISPLAY_TYPE_MESSAGE
{
return
r
}
r
.
Payload
.
Body
.
AfterOpen
=
typ
return
r
}
func
(
r
*
Reqd
)
SetPayloadURL
(
url
string
)
*
Reqd
{
if
r
.
Payload
.
DisplayType
==
PAYLOAD_DISPLAY_TYPE_MESSAGE
{
return
r
}
if
r
.
Payload
.
Body
.
AfterOpen
!=
PAYLOAD_AFTER_OPEN_GO_URL
{
return
r
}
r
.
Payload
.
Body
.
URL
=
url
return
r
}
func
(
r
*
Reqd
)
SetPayloadActivity
(
act
string
)
*
Reqd
{
if
r
.
Payload
.
DisplayType
==
PAYLOAD_DISPLAY_TYPE_MESSAGE
{
return
r
}
if
r
.
Payload
.
Body
.
AfterOpen
!=
PAYLOAD_AFTER_OPEN_GO_ACTIVITY
{
return
r
}
r
.
Payload
.
Body
.
Activity
=
act
return
r
}
func
(
r
*
Reqd
)
SetPayloadCustom
(
data
string
)
*
Reqd
{
if
r
.
Payload
.
DisplayType
!=
PAYLOAD_DISPLAY_TYPE_MESSAGE
&&
r
.
Payload
.
Body
.
AfterOpen
!=
PAYLOAD_AFTER_OPEN_GO_ACTIVITY
{
return
r
}
r
.
Payload
.
Body
.
Custom
=
data
return
r
}
func
(
r
*
Reqd
)
SetPayloadExtra
(
i
interface
{})
*
Reqd
{
if
r
.
Payload
.
DisplayType
==
PAYLOAD_DISPLAY_TYPE_MESSAGE
{
return
r
}
r
.
Payload
.
Extra
=
i
return
r
}
// 发送策略
func
(
r
*
Reqd
)
SetTime
(
start
int64
,
expire
int64
)
*
Reqd
{
r
.
Policy
.
StartTime
=
time
.
Unix
(
start
,
0
)
.
Format
(
"2006-01-02 15:04:05"
)
r
.
Policy
.
ExpireTime
=
time
.
Unix
(
expire
,
0
)
.
Format
(
"2006-01-02 15:04:05"
)
return
r
}
func
(
r
*
Reqd
)
SetSendMaxNum
(
num
int
)
*
Reqd
{
r
.
Policy
.
MaxSendNum
=
num
return
r
}
func
(
r
*
Reqd
)
SetUniqueId
(
id
string
)
*
Reqd
{
r
.
Policy
.
OutBizNo
=
id
return
r
}
func
(
r
*
Reqd
)
SetProductionMode
(
b
bool
)
*
Reqd
{
r
.
ProductionMode
=
b
return
r
}
func
(
r
*
Reqd
)
SetDescription
(
str
string
)
*
Reqd
{
r
.
Description
=
str
return
r
}
func
(
r
*
Reqd
)
SetMipush
(
b
bool
)
*
Reqd
{
r
.
Mipush
=
b
return
r
}
func
(
r
*
Reqd
)
SetMiActivity
(
str
string
)
*
Reqd
{
r
.
MiActivity
=
str
return
r
}
func
NewConext
(
appKey
,
appSecret
string
)
*
Context
{
p
:=
&
Context
{}
p
.
AppKey
=
appKey
p
.
AppSecret
=
appSecret
return
p
}
func
(
r
*
Context
)
url
(
reqd
*
Reqd
)
string
{
bodystr
,
_
:=
json
.
Marshal
(
reqd
)
sign
:=
fmt
.
Sprintf
(
"%s%s%s%s"
,
"POST"
,
umeng_host
,
bodystr
,
r
.
AppSecret
)
md5sum
:=
md5
.
Sum
([]
byte
(
sign
[
:
]))
sign
=
hex
.
EncodeToString
(
md5sum
[
:
])
fmt
.
Println
(
sign
)
return
fmt
.
Sprintf
(
"%s?sign=%s"
,
umeng_host
,
sign
)
}
func
(
r
*
Context
)
NewReqd
(
typ
string
)
*
Reqd
{
p
:=
Reqd
{}
p
.
Appkey
=
r
.
AppKey
p
.
Timestamp
=
time
.
Now
()
.
Unix
()
p
.
Type
=
typ
return
&
p
}
func
(
r
*
Context
)
Send
(
reqd
*
Reqd
)
*
Resp
{
url
:=
r
.
url
(
reqd
)
fmt
.
Println
(
url
)
req
:=
httplib
.
Post
(
url
)
.
SetHeader
(
"Content-Type"
,
"application/json"
)
_
,
_
=
req
.
JSONBody
(
reqd
)
res
:=
Resp
{}
err
:=
req
.
ToJSON
(
&
res
)
if
err
!=
nil
{
log
.
Fatalln
(
err
)
}
return
&
res
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment