forked from silenceper/wechat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
officialaccount.go
214 lines (187 loc) · 6.87 KB
/
officialaccount.go
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package officialaccount
import (
stdcontext "context"
"net/http"
"github.com/neoguojing/wechat/v2/officialaccount/draft"
"github.com/neoguojing/wechat/v2/officialaccount/freepublish"
"github.com/neoguojing/wechat/v2/officialaccount/ocr"
"github.com/neoguojing/wechat/v2/officialaccount/datacube"
"github.com/neoguojing/wechat/v2/credential"
"github.com/neoguojing/wechat/v2/officialaccount/basic"
"github.com/neoguojing/wechat/v2/officialaccount/broadcast"
"github.com/neoguojing/wechat/v2/officialaccount/config"
"github.com/neoguojing/wechat/v2/officialaccount/context"
"github.com/neoguojing/wechat/v2/officialaccount/customerservice"
"github.com/neoguojing/wechat/v2/officialaccount/device"
"github.com/neoguojing/wechat/v2/officialaccount/js"
"github.com/neoguojing/wechat/v2/officialaccount/material"
"github.com/neoguojing/wechat/v2/officialaccount/menu"
"github.com/neoguojing/wechat/v2/officialaccount/message"
"github.com/neoguojing/wechat/v2/officialaccount/oauth"
"github.com/neoguojing/wechat/v2/officialaccount/server"
"github.com/neoguojing/wechat/v2/officialaccount/user"
)
// OfficialAccount 微信公众号相关API
type OfficialAccount struct {
ctx *context.Context
basic *basic.Basic
menu *menu.Menu
oauth *oauth.Oauth
material *material.Material
draft *draft.Draft
freepublish *freepublish.FreePublish
js *js.Js
user *user.User
templateMsg *message.Template
managerMsg *message.Manager
device *device.Device
broadcast *broadcast.Broadcast
datacube *datacube.DataCube
ocr *ocr.OCR
subscribeMsg *message.Subscribe
}
// NewOfficialAccount 实例化公众号API
func NewOfficialAccount(cfg *config.Config) *OfficialAccount {
defaultAkHandle := credential.NewDefaultAccessToken(cfg.AppID, cfg.AppSecret, credential.CacheKeyOfficialAccountPrefix, cfg.Cache)
ctx := &context.Context{
Config: cfg,
AccessTokenHandle: defaultAkHandle,
}
return &OfficialAccount{ctx: ctx}
}
// SetAccessTokenHandle 自定义access_token获取方式
func (officialAccount *OfficialAccount) SetAccessTokenHandle(accessTokenHandle credential.AccessTokenHandle) {
officialAccount.ctx.AccessTokenHandle = accessTokenHandle
}
// GetContext get Context
func (officialAccount *OfficialAccount) GetContext() *context.Context {
return officialAccount.ctx
}
// GetBasic qr/url 相关配置
func (officialAccount *OfficialAccount) GetBasic() *basic.Basic {
if officialAccount.basic == nil {
officialAccount.basic = basic.NewBasic(officialAccount.ctx)
}
return officialAccount.basic
}
// GetMenu 菜单管理接口
func (officialAccount *OfficialAccount) GetMenu() *menu.Menu {
if officialAccount.menu == nil {
officialAccount.menu = menu.NewMenu(officialAccount.ctx)
}
return officialAccount.menu
}
// GetServer 消息管理:接收事件,被动回复消息管理
func (officialAccount *OfficialAccount) GetServer(req *http.Request, writer http.ResponseWriter) *server.Server {
srv := server.NewServer(officialAccount.ctx)
srv.Request = req
srv.Writer = writer
return srv
}
// GetAccessToken 获取access_token
func (officialAccount *OfficialAccount) GetAccessToken() (string, error) {
return officialAccount.ctx.GetAccessToken()
}
// GetAccessTokenContext 获取access_token
func (officialAccount *OfficialAccount) GetAccessTokenContext(ctx stdcontext.Context) (string, error) {
if c, ok := officialAccount.ctx.AccessTokenHandle.(credential.AccessTokenContextHandle); ok {
return c.GetAccessTokenContext(ctx)
}
return officialAccount.ctx.GetAccessToken()
}
// GetOauth oauth2网页授权
func (officialAccount *OfficialAccount) GetOauth() *oauth.Oauth {
if officialAccount.oauth == nil {
officialAccount.oauth = oauth.NewOauth(officialAccount.ctx)
}
return officialAccount.oauth
}
// GetMaterial 素材管理
func (officialAccount *OfficialAccount) GetMaterial() *material.Material {
if officialAccount.material == nil {
officialAccount.material = material.NewMaterial(officialAccount.ctx)
}
return officialAccount.material
}
// GetDraft 草稿箱
func (officialAccount *OfficialAccount) GetDraft() *draft.Draft {
if officialAccount.draft == nil {
officialAccount.draft = draft.NewDraft(officialAccount.ctx)
}
return officialAccount.draft
}
// GetFreePublish 发布能力
func (officialAccount *OfficialAccount) GetFreePublish() *freepublish.FreePublish {
if officialAccount.freepublish == nil {
officialAccount.freepublish = freepublish.NewFreePublish(officialAccount.ctx)
}
return officialAccount.freepublish
}
// GetJs js-sdk配置
func (officialAccount *OfficialAccount) GetJs() *js.Js {
if officialAccount.js == nil {
officialAccount.js = js.NewJs(officialAccount.ctx)
}
return officialAccount.js
}
// GetUser 用户管理接口
func (officialAccount *OfficialAccount) GetUser() *user.User {
if officialAccount.user == nil {
officialAccount.user = user.NewUser(officialAccount.ctx)
}
return officialAccount.user
}
// GetTemplate 模板消息接口
func (officialAccount *OfficialAccount) GetTemplate() *message.Template {
if officialAccount.templateMsg == nil {
officialAccount.templateMsg = message.NewTemplate(officialAccount.ctx)
}
return officialAccount.templateMsg
}
// GetCustomerMessageManager 客服消息接口
func (officialAccount *OfficialAccount) GetCustomerMessageManager() *message.Manager {
if officialAccount.managerMsg == nil {
officialAccount.managerMsg = message.NewMessageManager(officialAccount.ctx)
}
return officialAccount.managerMsg
}
// GetDevice 获取智能设备的实例
func (officialAccount *OfficialAccount) GetDevice() *device.Device {
if officialAccount.device == nil {
officialAccount.device = device.NewDevice(officialAccount.ctx)
}
return officialAccount.device
}
// GetBroadcast 群发消息
// TODO 待完善
func (officialAccount *OfficialAccount) GetBroadcast() *broadcast.Broadcast {
if officialAccount.broadcast == nil {
officialAccount.broadcast = broadcast.NewBroadcast(officialAccount.ctx)
}
return officialAccount.broadcast
}
// GetDataCube 数据统计
func (officialAccount *OfficialAccount) GetDataCube() *datacube.DataCube {
if officialAccount.datacube == nil {
officialAccount.datacube = datacube.NewCube(officialAccount.ctx)
}
return officialAccount.datacube
}
// GetOCR OCR接口
func (officialAccount *OfficialAccount) GetOCR() *ocr.OCR {
if officialAccount.ocr == nil {
officialAccount.ocr = ocr.NewOCR(officialAccount.ctx)
}
return officialAccount.ocr
}
// GetSubscribe 公众号订阅消息
func (officialAccount *OfficialAccount) GetSubscribe() *message.Subscribe {
if officialAccount.subscribeMsg == nil {
officialAccount.subscribeMsg = message.NewSubscribe(officialAccount.ctx)
}
return officialAccount.subscribeMsg
}
// GetCustomerServiceManager 客服管理
func (officialAccount *OfficialAccount) GetCustomerServiceManager() *customerservice.Manager {
return customerservice.NewCustomerServiceManager(officialAccount.ctx)
}