-
Notifications
You must be signed in to change notification settings - Fork 171
/
application.go
472 lines (376 loc) · 11.2 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
package miniProgram
import (
"github.com/ArtisanCloud/PowerLibs/v3/logger"
"github.com/ArtisanCloud/PowerLibs/v3/object"
"github.com/ArtisanCloud/PowerWeChat/v3/src/basicService/subscribeMessage"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/providers"
"github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram/auth"
"github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram/base"
"github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram/customerServiceMessage"
"github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram/dataCube"
"github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram/express"
"github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram/image"
"github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram/immediateDelivery"
"github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram/internet"
"github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram/liveBroadcast"
"github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram/nearbyPoi"
"github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram/ocr"
"github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram/operation"
"github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram/phoneNumber"
"github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram/plugin"
"github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram/riskControl"
"github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram/search"
"github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram/security"
server2 "github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram/server"
"github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram/serviceMarket"
"github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram/shortLink"
"github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram/soter"
"github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram/uniformMessage"
"github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram/updatableMessage"
"github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram/urlLink"
"github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram/urlScheme"
"github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram/wxaCode"
"github.com/ArtisanCloud/PowerWeChat/v3/src/officialAccount/server"
)
type MiniProgram struct {
*kernel.ServiceContainer
Base *base.Client
AccessToken *auth.AccessToken
Auth *auth.Client
Server *server.Guard
ActiveMessage *updatableMessage.Client
Encryptor *Encryptor
Broadcast *liveBroadcast.Client
CustomerServiceMessage *customerServiceMessage.Client
DataCube *dataCube.Client
UniformMessage *uniformMessage.Client
Image *image.Client
Internet *internet.Client
Express *express.Client
Delivery *immediateDelivery.Client
OCR *ocr.Client
Operation *operation.Client
Plugin *plugin.Client
PhoneNumber *phoneNumber.Client
NearbyPoi *nearbyPoi.Client
WXACode *wxaCode.Client
URLScheme *urlScheme.Client
URLLink *urlLink.Client
Security *security.Client
Search *search.Client
ServiceMarket *serviceMarket.Client
SubscribeMessage *subscribeMessage.Client
ShortLink *shortLink.Client
Soter *soter.Client
UpdatableMessage *updatableMessage.Client
RiskControl *riskControl.Client
Config *kernel.Config
Logger *logger.Logger
}
type UserConfig struct {
AppID string
Secret string
RefreshToken string
ComponentAppID string
ComponentAppToken string
Token string
AESKey string
ResponseType string
Log Log
OAuth OAuth
Cache kernel.CacheInterface
HttpDebug bool
Debug bool
NotifyURL string
Sandbox bool
}
type Log struct {
Level string
File string
ENV string
}
type OAuth struct {
Callback string
Scopes []string
}
func NewMiniProgram(config *UserConfig, extraInfos ...*kernel.ExtraInfo) (*MiniProgram, error) {
var err error
userConfig, err := MapUserConfig(config)
if err != nil {
return nil, err
}
var extraInfo, _ = kernel.NewExtraInfo()
if len(extraInfos) > 0 {
extraInfo = extraInfos[0]
}
// init an app container
container, err := kernel.NewServiceContainer(userConfig, extraInfo)
if err != nil {
return nil, err
}
container.GetConfig()
// init app
app := &MiniProgram{
ServiceContainer: container,
}
//-------------- global app config --------------
// global app config
app.Config = providers.RegisterConfigProvider(app)
app.Logger, err = logger.NewLogger("", &object.HashMap{
"env": app.Config.GetString("log.env", "develop"),
"outputPath": app.Config.GetString("log.file", "./wechat.log"),
"errorPath": app.Config.GetString("log.file", "./wechat.log"),
})
if err != nil {
return nil, err
}
//-------------- register auth,AccessToken --------------
app.AccessToken, err = auth.RegisterProvider(app)
if err != nil {
return nil, err
}
app.Auth, err = auth.RegisterAuthProvider(app)
if err != nil {
return nil, err
}
app.PhoneNumber, err = phoneNumber.RegisterProvider(app)
if err != nil {
return nil, err
}
// -------------- register Encryptor --------------
app.Encryptor, err = NewEncryptor(
(app.Config).GetString("app_id", ""),
(app.Config).GetString("token", ""),
(app.Config).GetString("aes_key", ""),
)
if err != nil {
return nil, err
}
//-------------- register Encryptor and Server --------------
app.Server, err = server2.RegisterProvider(app)
//-------------- register Base --------------
app.Base, err = base.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- register Broadcast --------------
app.Broadcast, err = liveBroadcast.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- register CustomerServiceMessage --------------
app.CustomerServiceMessage, err = customerServiceMessage.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- register Data cube --------------
app.DataCube, err = dataCube.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- register ActiveMessage --------------
app.ActiveMessage, err = updatableMessage.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- register Message --------------
app.UniformMessage, err = uniformMessage.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- register Image --------------
app.Image, err = image.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- register Internet --------------
app.Internet, err = internet.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- register Express --------------
app.Express, err = express.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- register Delivery --------------
app.Delivery, err = immediateDelivery.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- register OCR --------------
app.OCR, err = ocr.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- register Operation --------------
app.Operation, err = operation.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- register Plugin --------------
app.Plugin, err = plugin.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- register NearbyPoi --------------
app.NearbyPoi, err = nearbyPoi.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- register WXACode --------------
app.WXACode, err = wxaCode.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- register URLScheme --------------
app.URLScheme, err = urlScheme.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- register URLLink --------------
app.URLLink, err = urlLink.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- register Security --------------
app.Security, err = security.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- register Search --------------
app.Search, err = search.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- register ShortLink --------------
app.ShortLink, err = shortLink.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- register Soter --------------
app.Soter, err = soter.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- register Service Market --------------
app.ServiceMarket, err = serviceMarket.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- register SubscribeMessage --------------
app.SubscribeMessage, err = subscribeMessage.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- register UpdatableMessage --------------
app.UpdatableMessage, err = updatableMessage.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- register RiskControl --------------
app.RiskControl, err = riskControl.RegisterProvider(app)
if err != nil {
return nil, err
}
return app, err
}
func (app *MiniProgram) GetContainer() *kernel.ServiceContainer {
return app.ServiceContainer
}
func (app *MiniProgram) GetAccessToken() *kernel.AccessToken {
return app.AccessToken.AccessToken
}
func (app *MiniProgram) GetConfig() *kernel.Config {
return app.Config
}
func (app *MiniProgram) GetComponent(name string) interface{} {
switch name {
case "Base":
return app.Base
case "AccessToken":
return app.AccessToken
case "auth":
return app.Auth
case "Config":
return app.Config
case "Server":
return app.Server
case "Encryptor":
return app.Encryptor.BaseEncryptor
case "ActiveMessage":
return app.ActiveMessage
case "Broadcast":
return app.Broadcast
case "CustomerServiceMessage":
return app.CustomerServiceMessage
case "DataCube":
return app.DataCube
case "Image":
return app.Image
case "Internet":
return app.Internet
case "UniformMessage":
return app.UniformMessage
case "Express":
return app.Express
case "Delivery":
return app.Delivery
case "OCR":
return app.OCR
case "Operation":
return app.Operation
case "Plugin":
return app.Plugin
case "NearbyPoi":
return app.NearbyPoi
case "WXACode":
return app.WXACode
case "URLScheme":
return app.URLScheme
case "URLLink":
return app.URLLink
case "Security":
return app.Security
case "Search":
return app.Search
case "ServiceMarket":
return app.ServiceMarket
case "ShortLink":
return app.ShortLink
case "Soter":
return app.Soter
case "SubscribeMessage":
return app.SubscribeMessage
case "UpdatableMessage":
return app.UpdatableMessage
case "RiskControl":
return app.RiskControl
case "Logger":
return app.Logger
default:
return nil
}
}
func MapUserConfig(userConfig *UserConfig) (*object.HashMap, error) {
config := &object.HashMap{
"app_id": userConfig.AppID,
"secret": userConfig.Secret,
"token": userConfig.Token,
"aes_key": userConfig.AESKey,
"refresh_token": userConfig.RefreshToken,
"component_app_id": userConfig.ComponentAppID,
"component_app_token": userConfig.ComponentAppToken,
"response_type": userConfig.ResponseType,
"log": &object.HashMap{
"level": userConfig.Log.Level,
"file": userConfig.Log.File,
"env": userConfig.Log.ENV,
},
"cache": userConfig.Cache,
"http_debug": userConfig.HttpDebug,
"debug": userConfig.Debug,
}
return config, nil
}