-
Notifications
You must be signed in to change notification settings - Fork 157
/
application.go
322 lines (272 loc) · 8.1 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
package openWork
import (
"github.com/ArtisanCloud/PowerLibs/v3/logger"
"github.com/ArtisanCloud/PowerLibs/v3/object"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/providers"
miniProgram2 "github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram"
"github.com/ArtisanCloud/PowerWeChat/v3/src/openWork/auth"
"github.com/ArtisanCloud/PowerWeChat/v3/src/openWork/base"
"github.com/ArtisanCloud/PowerWeChat/v3/src/openWork/contact"
"github.com/ArtisanCloud/PowerWeChat/v3/src/openWork/corp"
"github.com/ArtisanCloud/PowerWeChat/v3/src/openWork/device"
"github.com/ArtisanCloud/PowerWeChat/v3/src/openWork/license"
"github.com/ArtisanCloud/PowerWeChat/v3/src/openWork/media"
"github.com/ArtisanCloud/PowerWeChat/v3/src/openWork/miniProgram"
"github.com/ArtisanCloud/PowerWeChat/v3/src/openWork/provider"
"github.com/ArtisanCloud/PowerWeChat/v3/src/openWork/server"
suit "github.com/ArtisanCloud/PowerWeChat/v3/src/openWork/suitAuth"
workMiniProgram "github.com/ArtisanCloud/PowerWeChat/v3/src/work/miniProgram"
)
type OpenWork struct {
*kernel.ServiceContainer
Base *base.Client
Server *server.Guard
Corp *corp.Client
Provider *provider.Client
SuiteAccessToken *suit.AccessToken
ProviderAccessToken *auth.AccessToken
SuiteTicket *suit.SuiteTicket
MiniProgram *miniProgram.Client
Media *media.Client
Contact *contact.Client
LicenseOrder *license.Client
LicenseAccount *license.Account
Device *device.Client
Encryptor *kernel.Encryptor
Config *kernel.Config
Logger *logger.Logger
}
type UserConfig struct {
AppID string
Secret string
AuthCode 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 NewOpenWork(config *UserConfig) (*OpenWork, 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{
"timeout": 5.0,
"base_uri": "https://api.weixin.qq.com/",
},
},
}
// init app
app := &OpenWork{
ServiceContainer: container,
}
//-------------- global app config --------------
// global app config
app.Config = providers.RegisterConfigProvider(app)
app.Logger, err = logger.NewLogger("", &object.HashMap{
"env": app.Config.GetString("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 --------------
app.SuiteTicket, app.SuiteAccessToken, err = suit.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- register Base --------------
app.Base, err = base.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- register Encryptor and Server --------------
app.Encryptor, app.Server, err = server.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- register Corp --------------
app.Corp, err = corp.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- register Provider --------------
app.Provider, err = provider.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- register ProviderAccessToken --------------
app.ProviderAccessToken, err = auth.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- register Media --------------
app.Media, err = media.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- register Contact --------------
app.Contact, err = contact.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- register License --------------
app.LicenseOrder, app.LicenseAccount, err = license.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- register Device --------------
app.Device, err = device.RegisterProvider(app)
if err != nil {
return nil, err
}
return app, err
}
func (app *OpenWork) GetContainer() *kernel.ServiceContainer {
return app.ServiceContainer
}
func (app *OpenWork) GetAccessToken() *kernel.AccessToken {
return app.ProviderAccessToken.AccessToken
}
func (app *OpenWork) GetConfig() *kernel.Config {
return app.Config
}
func (app *OpenWork) GetComponent(name string) interface{} {
switch name {
case "Corp":
return app.Corp
case "Provider":
return app.Provider
case "SuiteAccessToken":
return app.SuiteAccessToken
case "ProviderAccessToken":
return app.ProviderAccessToken
case "SuiteTicket":
return app.SuiteTicket
case "MiniProgram":
return app.MiniProgram
case "Media":
return app.Media
case "Contact":
return app.Contact
case "LicenseOrder":
return app.LicenseOrder
case "LicenseAccount":
return app.LicenseAccount
case "Device":
return app.Device
default:
return nil
}
}
func MapUserConfig(userConfig *UserConfig) (*object.HashMap, error) {
config := &object.HashMap{
"app_id": userConfig.AppID,
"secret": userConfig.Secret,
"auth_code": userConfig.AuthCode,
"token": userConfig.Token,
"aes_key": userConfig.AESKey,
"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,
"oauth": &object.HashMap{
"scopes": userConfig.OAuth.Scopes,
"callbacks": userConfig.OAuth.Callback,
},
}
return config, nil
}
func (app *OpenWork) GetMiniProgram() (application *workMiniProgram.Application, err error) {
config := app.GetConfig()
userConfig, err := app.convertUserConfigToWorkMiniProgramConfig(config.Collection)
application, err = workMiniProgram.NewApplication(userConfig)
return application, err
}
func (app *OpenWork) convertUserConfigToWorkMiniProgramConfig(config *object.Collection) (userConfig *workMiniProgram.UserConfig, err error) {
// log config
logConfig := config.Get("log", nil)
log := miniProgram2.Log{
Level: "",
File: "",
ENV: "",
}
if logConfig != nil {
log = logConfig.(miniProgram2.Log)
}
//// auth config
//logConfig := config.Get("log", nil)
//log := miniProgram2.Log{
// Level: "",
// File: "",
// ENV: "",
//}
//if logConfig != nil {
// log = logConfig.(miniProgram2.Log)
//}
//
//// cache config
//cacheConfig := config.Get("log", nil)
//var cache cache.CacheInterface
//if cacheConfig != nil {
// cache = cacheConfig.(cache.CacheInterface)
//}
// http config
httpConfig := config.Get("http", nil)
var mapHttpConfig *object.HashMap
if httpConfig != nil {
mapHttpConfig = httpConfig.(*object.HashMap)
}
userConfig = &workMiniProgram.UserConfig{
MiniProgramUserConfig: &miniProgram2.UserConfig{
AppID: config.GetString("appID", ""),
Secret: config.GetString("secret", ""),
RefreshToken: config.GetString("refreshToken", ""),
ComponentAppID: config.GetString("componentAppID", ""),
ComponentAppToken: config.GetString("componentAppToken", ""),
ResponseType: config.GetString("responseType", ""),
Log: log,
//OAuth: config.GetString("oauth", ""),
//Cache: cache,
HttpDebug: config.GetBool("httpDebug", false),
Debug: config.GetBool("debug", false),
NotifyURL: config.GetString("notifyURL", ""),
Sandbox: config.GetBool("sandbox", false),
},
CorpID: config.GetString("corpID", ""),
AgentID: config.GetInt("agentID", 0),
Secret: config.GetString("secret", ""),
Token: config.GetString("token", ""),
AESKey: config.GetString("aESKey", ""),
CallbackURL: config.GetString("callbackURL", ""),
Http: mapHttpConfig,
}
return userConfig, err
}