-
Notifications
You must be signed in to change notification settings - Fork 156
/
appllication.go
413 lines (339 loc) · 11.1 KB
/
appllication.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
package openPlatform
import (
"context"
"errors"
"github.com/ArtisanCloud/PowerLibs/v3/cache"
"github.com/ArtisanCloud/PowerLibs/v3/logger"
"github.com/ArtisanCloud/PowerLibs/v3/logger/contract"
"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"
officialAccount2 "github.com/ArtisanCloud/PowerWeChat/v3/src/officialAccount"
"github.com/ArtisanCloud/PowerWeChat/v3/src/openPlatform/auth"
auth2 "github.com/ArtisanCloud/PowerWeChat/v3/src/openPlatform/authorizer/auth"
miniProgram3 "github.com/ArtisanCloud/PowerWeChat/v3/src/openPlatform/authorizer/miniProgram"
auth3 "github.com/ArtisanCloud/PowerWeChat/v3/src/openPlatform/authorizer/miniProgram/auth"
"github.com/ArtisanCloud/PowerWeChat/v3/src/openPlatform/authorizer/officialAccount"
"github.com/ArtisanCloud/PowerWeChat/v3/src/openPlatform/authorizer/officialAccount/account"
"github.com/ArtisanCloud/PowerWeChat/v3/src/openPlatform/base"
"github.com/ArtisanCloud/PowerWeChat/v3/src/openPlatform/codeTemplate"
"github.com/ArtisanCloud/PowerWeChat/v3/src/openPlatform/component"
"github.com/ArtisanCloud/PowerWeChat/v3/src/openPlatform/server"
)
type OpenPlatform struct {
*kernel.ServiceContainer
Base *base.Client
VerifyTicket *auth.VerifyTicket
AccessToken *auth.AccessToken
Server *server.Guard
Encryptor *kernel.Encryptor
CodeTemplate *codeTemplate.Client
Component *component.Client
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
Http Http
HttpDebug bool
Debug bool
NotifyURL string
Sandbox bool
}
type Http struct {
Timeout float64
BaseURI string
}
type Log struct {
Driver contract.LoggerInterface
Level string
File string
Error string
ENV string
}
type OAuth struct {
Callback string
Scopes []string
}
func NewOpenPlatform(config *UserConfig) (*OpenPlatform, 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 := &OpenPlatform{
ServiceContainer: container,
}
//-------------- global app config --------------
// global app config
app.Config = providers.RegisterConfigProvider(app)
app.Logger, err = logger.NewLogger(app.Config.Get("log.driver", nil), &object.HashMap{
"level": app.Config.GetString("log.level", "info"),
"env": app.Config.GetString("log.env", "develop"),
"outputPath": app.Config.GetString("log.file", "./wechat/info.log"),
"errorPath": app.Config.GetString("log.error", "./wechat/error.log"),
})
if err != nil {
return nil, err
}
//-------------- register auth --------------
app.VerifyTicket, app.AccessToken, err = auth.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 CodeTemplate --------------
app.CodeTemplate, err = codeTemplate.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- register Component --------------
app.Component, err = component.RegisterProvider(app)
if err != nil {
return nil, err
}
return app, err
}
func (app *OpenPlatform) GetContainer() *kernel.ServiceContainer {
return app.ServiceContainer
}
func (app *OpenPlatform) GetAccessToken() *kernel.AccessToken {
return app.AccessToken.AccessToken
}
func (app *OpenPlatform) GetConfig() *kernel.Config {
return app.Config
}
func (app *OpenPlatform) GetComponent(name string) interface{} {
switch name {
case "VerifyTicket":
return app.VerifyTicket
case "Base":
return app.Base
case "AccessToken":
return app.AccessToken
case "Config":
return app.Config
case "Server":
return app.Server
case "Encryptor":
return app.Encryptor
case "CodeTemplate":
return app.CodeTemplate
case "Component":
return app.Component
case "Logger":
return app.Logger
default:
return nil
}
}
func MapUserConfig(userConfig *UserConfig) (*object.HashMap, error) {
baseURI := "https://api.weixin.qq.com/"
if userConfig.Http.BaseURI != "" {
baseURI = userConfig.Http.BaseURI
}
timeout := 5.0
if userConfig.Http.Timeout > 0 {
timeout = userConfig.Http.Timeout
}
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,
"http": &object.HashMap{
"timeout": timeout,
"base_uri": baseURI,
},
"log": &object.HashMap{
"driver": userConfig.Log.Driver,
"level": userConfig.Log.Level,
"file": userConfig.Log.File,
"error": userConfig.Log.Error,
"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 *OpenPlatform) OfficialAccount(appID string, refreshToken string, accessToken *auth2.AccessToken) (application *officialAccount.Application, err error) {
userConfig, err := app.GetOfficialAuthorizerConfig(appID, refreshToken)
if err != nil {
return nil, err
}
// 先初始化一个OfficialAccount的账号
application, err = officialAccount.NewApplication(userConfig)
if err != nil {
return nil, err
}
// 重塑一个AccessToken
if accessToken == nil {
accessToken, err = auth2.NewAccessToken(application.OfficialAccount, app)
if err != nil {
return nil, err
}
}
application.AccessToken.AccessToken = accessToken.AccessToken
application.Encryptor = app.Encryptor
application.Account, err = account.NewClient(application, app)
token, err := app.AccessToken.GetToken(false)
if err != nil {
return nil, err
}
application.OAuth.WithComponent(&object.HashMap{
"id": app.Config.GetString("app_id", ""),
"token": token.ComponentAccessToken,
})
return application, err
}
func (app *OpenPlatform) MiniProgram(appID string, refreshToken string, accessToken *auth2.AccessToken) (application *miniProgram3.Application, err error) {
userConfig := app.GetMiniProgramAuthorizerConfig(appID, refreshToken)
application, err = miniProgram3.NewApplication(userConfig)
application.AccessToken.AccessToken = accessToken.AccessToken
application.Encryptor, err = miniProgram2.NewEncryptor(
app.Config.GetString("app_id", ""),
app.Config.GetString("token", ""),
app.Config.GetString("aes_key", ""),
)
application.Auth, err = auth3.NewClient(app, application)
return application, err
}
func (app *OpenPlatform) GetOfficialAuthorizerConfig(appID string, refreshToken string) (userConfig *officialAccount2.UserConfig, err error) {
token, _ := app.AccessToken.GetToken(false)
config := app.GetConfig()
cacheHandle := config.Get("cache", nil).(cache.CacheInterface)
var oauth = officialAccount2.OAuth{}
err = object.HashMapToStructure(config.Get("oauth", nil).(*object.HashMap), &oauth)
if err != nil {
return nil, err
}
log := config.Get("log", nil).(*object.HashMap)
userConfig = &officialAccount2.UserConfig{
ComponentAppID: config.GetString("app_id", ""),
ComponentAppToken: token.ComponentAccessToken,
AppID: appID,
RefreshToken: refreshToken,
Secret: config.GetString("secret", ""),
Token: config.GetString("token", ""),
AESKey: config.GetString("aes_key", ""),
ResponseType: config.GetString("response_type", ""),
Log: officialAccount2.Log{
Level: (*log)["level"].(string),
File: (*log)["file"].(string),
ENV: (*log)["env"].(string),
},
OAuth: oauth,
Cache: cacheHandle,
HttpDebug: config.GetBool("http_debug", false),
Debug: config.GetBool("debug", false),
NotifyURL: config.GetString("notify_url", ""),
Sandbox: config.GetBool("sandbox", false),
}
return userConfig, err
}
func (app *OpenPlatform) GetMiniProgramAuthorizerConfig(appID string, refreshToken string) (userConfig *miniProgram2.UserConfig) {
token, _ := app.AccessToken.GetToken(false)
config := app.GetConfig()
cache := config.Get("cache", nil).(cache.CacheInterface)
oauth := config.Get("oauth", nil).(miniProgram2.OAuth)
log := config.Get("log", nil).(*object.HashMap)
userConfig = &miniProgram2.UserConfig{
AppID: appID,
Secret: config.GetString("secret", ""),
RefreshToken: refreshToken,
ComponentAppID: config.GetString("app_id", ""),
ComponentAppToken: token.ComponentAccessToken,
ResponseType: config.GetString("secret", ""),
Log: miniProgram2.Log{
Level: (*log)["level"].(string),
File: (*log)["file"].(string),
ENV: (*log)["env"].(string),
},
OAuth: oauth,
Cache: cache,
HttpDebug: config.GetBool("http_debug", false),
Debug: config.GetBool("debug", false),
NotifyURL: config.GetString("notify_url", ""),
Sandbox: config.GetBool("sandbox", false),
}
return userConfig
}
// Return the pre-authorization login page url.
// https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/Before_Develop/Authorization_Process_Technical_Description.html
func (app *OpenPlatform) GetFastRegistrationURL(ctx context.Context, callbackUrl string, optional *object.StringMap) (url string, err error) {
config := app.GetConfig()
code, err := app.Base.CreatePreAuthorizationCode(ctx)
if err != nil {
return url, err
}
if code.ErrCode != 0 {
return url, errors.New(code.ErrMsg)
}
(*optional)["pre_auth_code"] = code.PreAuthCode
queries := object.MergeStringMap(optional, &object.StringMap{
"component_appid": config.GetString("app_id", ""),
"redirect_uri": callbackUrl,
})
return "https://mp.weixin.qq.com/cgi-bin/componentloginpage?" + object.GetJoinedWithKSort(queries), err
}
// Return the pre-authorization login page url (mobile).
func (app *OpenPlatform) GetMobilePreAuthorizationURL(ctx context.Context, callbackUrl string, optional *object.StringMap) (url string, err error) {
config := app.GetConfig()
code, err := app.Base.CreatePreAuthorizationCode(ctx)
if err != nil {
return url, err
}
if code.ErrCode != 0 {
return url, errors.New(code.ErrMsg)
}
(*optional)["pre_auth_code"] = code.PreAuthCode
queries := object.MergeStringMap(
&object.StringMap{
"auth_type": "3",
},
optional,
&object.StringMap{
"component_appid": config.GetString("app_id", ""),
"redirect_uri": callbackUrl,
"action": "bindcomponent",
"no_scan": "1",
})
return "https://mp.weixin.qq.com/safe/bindcomponent?" + object.GetJoinedWithKSort(queries) + "#wechat_redirect", err
}