-
Notifications
You must be signed in to change notification settings - Fork 147
/
application.go
321 lines (285 loc) · 9.96 KB
/
application.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package officialAccount
import (
"github.com/ArtisanCloud/PowerLibs/v2/object"
providers2 "github.com/ArtisanCloud/PowerSocialite/v2/src/providers"
"github.com/ArtisanCloud/PowerWeChat/v2/src/basicService/jssdk"
"github.com/ArtisanCloud/PowerWeChat/v2/src/basicService/media"
"github.com/ArtisanCloud/PowerWeChat/v2/src/basicService/qrCode"
"github.com/ArtisanCloud/PowerWeChat/v2/src/basicService/subscribeMessage"
"github.com/ArtisanCloud/PowerWeChat/v2/src/basicService/url"
"github.com/ArtisanCloud/PowerWeChat/v2/src/kernel"
"github.com/ArtisanCloud/PowerWeChat/v2/src/kernel/providers"
"github.com/ArtisanCloud/PowerWeChat/v2/src/officialAccount/auth"
"github.com/ArtisanCloud/PowerWeChat/v2/src/officialAccount/autoReply"
"github.com/ArtisanCloud/PowerWeChat/v2/src/officialAccount/base"
"github.com/ArtisanCloud/PowerWeChat/v2/src/officialAccount/broadcasting"
"github.com/ArtisanCloud/PowerWeChat/v2/src/officialAccount/card"
"github.com/ArtisanCloud/PowerWeChat/v2/src/officialAccount/comment"
"github.com/ArtisanCloud/PowerWeChat/v2/src/officialAccount/customerService"
"github.com/ArtisanCloud/PowerWeChat/v2/src/officialAccount/customerService/session"
"github.com/ArtisanCloud/PowerWeChat/v2/src/officialAccount/dataCube"
"github.com/ArtisanCloud/PowerWeChat/v2/src/officialAccount/device"
"github.com/ArtisanCloud/PowerWeChat/v2/src/officialAccount/goods"
"github.com/ArtisanCloud/PowerWeChat/v2/src/officialAccount/guide"
"github.com/ArtisanCloud/PowerWeChat/v2/src/officialAccount/material"
"github.com/ArtisanCloud/PowerWeChat/v2/src/officialAccount/menu"
"github.com/ArtisanCloud/PowerWeChat/v2/src/officialAccount/oauth"
"github.com/ArtisanCloud/PowerWeChat/v2/src/officialAccount/ocr"
"github.com/ArtisanCloud/PowerWeChat/v2/src/officialAccount/poi"
"github.com/ArtisanCloud/PowerWeChat/v2/src/officialAccount/semantic"
"github.com/ArtisanCloud/PowerWeChat/v2/src/officialAccount/server"
"github.com/ArtisanCloud/PowerWeChat/v2/src/officialAccount/shakeAround"
"github.com/ArtisanCloud/PowerWeChat/v2/src/officialAccount/store"
"github.com/ArtisanCloud/PowerWeChat/v2/src/officialAccount/templateMessage"
"github.com/ArtisanCloud/PowerWeChat/v2/src/officialAccount/user"
"github.com/ArtisanCloud/PowerWeChat/v2/src/officialAccount/user/tag"
"github.com/ArtisanCloud/PowerWeChat/v2/src/officialAccount/wifi"
)
type OfficialAccount struct {
*kernel.ServiceContainer
Base *base.Client
AccessToken *auth.AccessToken
Config *kernel.Config
// basic services
Media *media.Client
URL *url.Client
QRCode *qrCode.Client
JSSDK *jssdk.Client
SubscribeMessage *subscribeMessage.Client
Server *server.Guard
Encryptor *kernel.Encryptor
User *user.Client
UserTag *tag.Client
Menu *menu.Client
TemplateMessage *templateMessage.Client
Material *material.Client
CustomerService *customerService.Client
CustomerServiceSession *session.Client
Semantic *semantic.Client
DataCube *dataCube.Client
AutoReply *autoReply.Client
Broadcasting *broadcasting.Client
Card *card.Client
Device *device.Client
ShakeAround *shakeAround.ShakeAround
POI *poi.Client
Store *store.Client
Comment *comment.Client
OCR *ocr.Client
Goods *goods.Client
OAuth *providers2.WeChat
Wifi *wifi.Client
WifiCard *wifi.CardClient
WifiDevice *wifi.DeviceClient
WifiShop *wifi.ShopClient
Guide *guide.Client
}
type UserConfig struct {
AppID string
Secret string
ResponseType string
Log Log
OAuth OAuth
Cache kernel.CacheInterface
HttpDebug bool
Debug bool
}
type Log struct {
Level string
File string
}
type OAuth struct {
Callback string
Scopes []string
}
func NewOfficialAccount(config *UserConfig) (*OfficialAccount, error) {
var err error
userConfig, err := MapUserConfig(config)
if err != nil {
return nil, err
}
// init an app container
container := &kernel.ServiceContainer{
UserConfig: userConfig,
DefaultConfig: &object.HashMap{
"http": &object.HashMap{
"base_uri": "https://api.weixin.qq.com/",
},
},
}
container.GetConfig()
// init app
app := &OfficialAccount{
ServiceContainer: container,
}
//-------------- global app config --------------
// global app config
app.Config = providers.RegisterConfigProvider(app)
//-------------- register Auth --------------
app.AccessToken = auth.RegisterProvider(app)
//-------------- register Base --------------
app.Base = base.RegisterProvider(app)
//-------------- media --------------
app.Media = media.RegisterProvider(app)
//-------------- register QRCode --------------
app.QRCode = qrCode.RegisterProvider(app)
//-------------- register URL --------------
app.URL = url.RegisterProvider(app)
//-------------- register JSSDK --------------
app.JSSDK = jssdk.RegisterProvider(app)
//-------------- register SubscribeMessage --------------
app.SubscribeMessage = subscribeMessage.RegisterProvider(app)
//-------------- register Menu --------------
app.Menu = menu.RegisterProvider(app)
//-------------- register Material --------------
app.Material = material.RegisterProvider(app)
//-------------- register CustomerService --------------
app.CustomerService, app.CustomerServiceSession = customerService.RegisterProvider(app)
//-------------- register Semantic --------------
app.Semantic = semantic.RegisterProvider(app)
//-------------- register Encryptor and Server --------------
app.Encryptor, app.Server = server.RegisterProvider(app)
//-------------- register User --------------
app.User, app.UserTag = user.RegisterProvider(app)
//-------------- register Menu --------------
app.Menu = menu.RegisterProvider(app)
//-------------- register TemplateMessage --------------
app.TemplateMessage = templateMessage.RegisterProvider(app)
//-------------- register Material --------------
app.Material = material.RegisterProvider(app)
//-------------- register CustomerService --------------
app.CustomerService, app.CustomerServiceSession = customerService.RegisterProvider(app)
//-------------- register Semantic --------------
app.Semantic = semantic.RegisterProvider(app)
//-------------- register DataCube --------------
app.DataCube = dataCube.RegisterProvider(app)
//-------------- register AutoReply --------------
app.AutoReply = autoReply.RegisterProvider(app)
//-------------- register Broadcasting --------------
app.Broadcasting = broadcasting.RegisterProvider(app)
//-------------- register Card --------------
app.Card = card.RegisterProvider(app)
//-------------- register Device --------------
app.Device = device.RegisterProvider(app)
//-------------- register ShakeAround --------------
app.ShakeAround = shakeAround.RegisterProvider(app)
//-------------- register POI --------------
app.POI = poi.RegisterProvider(app)
//-------------- register Store --------------
app.Store = store.RegisterProvider(app)
//-------------- register Comment --------------
app.Comment = comment.RegisterProvider(app)
//-------------- register OCR --------------
app.OCR = ocr.RegisterProvider(app)
//-------------- register Goods --------------
app.Goods = goods.RegisterProvider(app)
//-------------- register OAuth --------------
app.OAuth = oauth.RegisterProvider(app)
//-------------- register wifi --------------
app.Wifi, app.WifiCard, app.WifiDevice, app.WifiShop = wifi.RegisterProvider(app)
//-------------- register Guide --------------
app.Guide = guide.RegisterProvider(app)
return app, err
}
func (app *OfficialAccount) GetContainer() *kernel.ServiceContainer {
return app.ServiceContainer
}
func (app *OfficialAccount) GetAccessToken() *kernel.AccessToken {
return app.AccessToken.AccessToken
}
func (app *OfficialAccount) GetConfig() *kernel.Config {
return app.Config
}
func (app *OfficialAccount) GetComponent(name string) interface{} {
switch name {
case "Base":
return app.Base
case "AccessToken":
return app.AccessToken
case "Config":
return app.Config
case "Media":
return app.Media
case "QRCode":
return app.QRCode
case "SubscribeMessage":
return app.SubscribeMessage
case "URL":
return app.URL
case "Server":
return app.Server
case "Encryptor":
return app.Encryptor
case "User":
return app.User
case "UserTag":
return app.UserTag
case "Menu":
return app.Menu
case "TemplateMessage":
return app.TemplateMessage
case "Material":
return app.Material
case "CustomerService":
return app.CustomerService
case "CustomerServiceSession":
return app.CustomerServiceSession
case "Semantic":
return app.Semantic
case "DataCube":
return app.DataCube
case "AutoReply":
return app.AutoReply
case "Broadcasting":
return app.Broadcasting
case "Card":
return app.Card
case "Device":
return app.Device
case "ShakeAround":
return app.ShakeAround
case "POI":
return app.POI
case "Store":
return app.Store
case "Comment":
return app.Comment
case "OCR":
return app.OCR
case "Goods":
return app.Goods
case "OAuth":
return app.OAuth
case "Wifi":
return app.Wifi
case "WifiCard":
return app.WifiCard
case "WifiDevice":
return app.WifiDevice
case "WifiShop":
return app.WifiShop
case "Guide":
return app.Guide
default:
return nil
}
}
func MapUserConfig(userConfig *UserConfig) (*object.HashMap, error) {
config := &object.HashMap{
"app_id": userConfig.AppID,
"secret": userConfig.Secret,
"response_type": userConfig.ResponseType,
"log": &object.StringMap{
"level": userConfig.Log.Level,
"file": userConfig.Log.File,
},
"cache": userConfig.Cache,
"http_debug": userConfig.HttpDebug,
"debug": userConfig.Debug,
"oauth": &object.HashMap{
"scopes": userConfig.OAuth.Scopes,
"callback": userConfig.OAuth.Callback,
},
}
return config, nil
}