model.go
2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package push
import (
"bytes"
"fmt"
"net/url"
"strings"
)
const (
Sound = "sound"
SoundXM = "sound_xm"
SoundHW = "sound_hw"
SoundChannelXM = "sound_channel_xm"
SoundChannelHW = "sound_channel_hw"
)
type Options struct {
AppId string
AppKey string
AppSecret string
AppMasterSecret string
ClientId string
ClientIds []string
MsgType int //消息类型
PushType int //推送类型
Title string
Content string
Extra interface{} //扩展数据 map[string]interface{} key:"sound" storein_voice.mp3
TransmissionContent string //透传内容
//多厂家支持
Intent string
DebugModule bool
}
type Option func(o *Options)
//个推appId
func AppId(id string) Option {
return func(o *Options) {
o.AppId = id
}
}
//个推appKey
func AppKey(key string) Option {
return func(o *Options) {
o.AppKey = key
}
}
//个推appSecret
func AppSecret(secret string) Option {
return func(o *Options) {
o.AppSecret = secret
}
}
//个推appMasterSecret
func AppMasterSecret(secret string) Option {
return func(o *Options) {
o.AppMasterSecret = secret
}
}
//个推 app clientId
func ClientId(clientId string) Option {
return func(o *Options) {
o.ClientId = clientId
}
}
func ClientIds(clientId []string) Option {
return func(o *Options) {
o.ClientIds = clientId
}
}
//消息类型
func MsgType(msgType int) Option {
return func(o *Options) {
o.MsgType = msgType
}
}
//推送类型
func PushType(pushType int) Option {
return func(o *Options) {
o.PushType = pushType
}
}
//消息内容
func Title(title string) Option {
return func(o *Options) {
o.Title = title
}
}
func Content(content string) Option {
return func(o *Options) {
o.Content = content
}
}
func Extra(extra interface{}) Option {
return func(o *Options) {
o.Extra = extra
}
}
func TransmissionContent(content string) Option {
return func(o *Options) {
o.TransmissionContent = content
}
}
func Intent(intent string) Option {
return func(o *Options) {
o.Intent = intent
}
}
func DebugModule(module bool) Option {
return func(o *Options) {
o.DebugModule = module
}
}
func (o *Options) GetExt(key string) (value interface{}, ok bool) {
var mapExt map[string]interface{}
if mapExt, ok = o.Extra.(map[string]interface{}); !ok {
return
}
if value, ok = mapExt[key]; ok {
return
}
return
}
func (o *Options) FormatTranDataToIntent() string {
tran, ok := o.GetExt("transData")
if !ok {
return o.Intent
}
var tranMap map[string]interface{}
tranMap, ok = tran.(map[string]interface{})
if !ok {
return o.Intent
}
var params = bytes.NewBuffer(nil)
for k, v := range tranMap {
params.WriteString(fmt.Sprintf("S.%s=%v;", url.QueryEscape(k), url.QueryEscape(fmt.Sprintf("%v", v))))
}
if idx := strings.Index(o.Intent, "end"); idx > 0 {
return o.Intent[0:idx] + params.String() + "end"
}
return o.Intent
}
const (
Message = iota + 1
Notification
)