Application.php
6.2 KB
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
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\OpenPlatform;
use EasyWeChat\Kernel\ServiceContainer;
use EasyWeChat\Kernel\Traits\ResponseCastable;
use EasyWeChat\MiniProgram\Encryptor;
use EasyWeChat\OpenPlatform\Authorizer\Auth\AccessToken;
use EasyWeChat\OpenPlatform\Authorizer\MiniProgram\Application as MiniProgram;
use EasyWeChat\OpenPlatform\Authorizer\MiniProgram\Auth\Client;
use EasyWeChat\OpenPlatform\Authorizer\OfficialAccount\Account\Client as AccountClient;
use EasyWeChat\OpenPlatform\Authorizer\OfficialAccount\Application as OfficialAccount;
use EasyWeChat\OpenPlatform\Authorizer\OfficialAccount\OAuth\ComponentDelegate;
use EasyWeChat\OpenPlatform\Authorizer\Server\Guard;
use function EasyWeChat\Kernel\data_get;
/**
* Class Application.
*
* @property \EasyWeChat\OpenPlatform\Server\Guard $server
* @property \EasyWeChat\OpenPlatform\Auth\AccessToken $access_token
* @property \EasyWeChat\OpenPlatform\CodeTemplate\Client $code_template
* @property \EasyWeChat\OpenPlatform\Component\Client $component
*
* @method mixed handleAuthorize(string $authCode = null)
* @method mixed getAuthorizer(string $appId)
* @method mixed getAuthorizerOption(string $appId, string $name)
* @method mixed setAuthorizerOption(string $appId, string $name, string $value)
* @method mixed getAuthorizers(int $offset = 0, int $count = 500)
* @method mixed createPreAuthorizationCode()
*/
class Application extends ServiceContainer
{
use ResponseCastable;
/**
* @var array
*/
protected $providers = [
Auth\ServiceProvider::class,
Base\ServiceProvider::class,
Server\ServiceProvider::class,
CodeTemplate\ServiceProvider::class,
Component\ServiceProvider::class,
];
/**
* @var array
*/
protected $defaultConfig = [
'http' => [
'timeout' => 5.0,
'base_uri' => 'https://api.weixin.qq.com/',
],
];
/**
* Creates the officialAccount application.
*/
public function officialAccount(string $appId, string $refreshToken = null, AccessToken $accessToken = null): OfficialAccount
{
$application = new OfficialAccount($this->getAuthorizerConfig($appId, $refreshToken), $this->getReplaceServices($accessToken) + [
'encryptor' => $this['encryptor'],
'account' => function ($app) {
return new AccountClient($app, $this);
},
]);
$application->extend('oauth', function ($socialite) {
/* @var \Overtrue\Socialite\Providers\WeChatProvider $socialite */
return $socialite->component(new ComponentDelegate($this));
});
return $application;
}
/**
* Creates the miniProgram application.
*/
public function miniProgram(string $appId, string $refreshToken = null, AccessToken $accessToken = null): MiniProgram
{
return new MiniProgram($this->getAuthorizerConfig($appId, $refreshToken), $this->getReplaceServices($accessToken) + [
'encryptor' => function () {
return new Encryptor($this['config']['app_id'], $this['config']['token'], $this['config']['aes_key']);
},
'auth' => function ($app) {
return new Client($app, $this);
},
]);
}
/**
* Return the pre-authorization login page url.
*
* @param string|array|null $optional
*/
public function getPreAuthorizationUrl(string $callbackUrl, $optional = []): string
{
// 兼容旧版 API 设计
if (\is_string($optional)) {
$optional = [
'pre_auth_code' => $optional,
];
} else {
$optional['pre_auth_code'] = data_get($this->createPreAuthorizationCode(), 'pre_auth_code');
}
$queries = \array_merge($optional, [
'component_appid' => $this['config']['app_id'],
'redirect_uri' => $callbackUrl,
]);
return 'https://mp.weixin.qq.com/cgi-bin/componentloginpage?'.http_build_query($queries);
}
/**
* Return the pre-authorization login page url (mobile).
*
* @param string|array|null $optional
*
* @return string
* @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
*/
public function getMobilePreAuthorizationUrl(string $callbackUrl, $optional = []): string
{
// 兼容旧版 API 设计
if (\is_string($optional)) {
$optional = [
'pre_auth_code' => $optional,
];
} else {
$optional['pre_auth_code'] = data_get($this->createPreAuthorizationCode(), 'pre_auth_code');
}
$queries = \array_merge($optional, [
'component_appid' => $this['config']['app_id'],
'redirect_uri' => $callbackUrl,
'action' => 'bindcomponent',
'no_scan' => 1,
]);
return 'https://mp.weixin.qq.com/safe/bindcomponent?'.http_build_query($queries).'#wechat_redirect';
}
protected function getAuthorizerConfig(string $appId, string $refreshToken = null): array
{
return $this['config']->merge([
'component_app_id' => $this['config']['app_id'],
'app_id' => $appId,
'refresh_token' => $refreshToken,
])->toArray();
}
protected function getReplaceServices(AccessToken $accessToken = null): array
{
$services = [
'access_token' => $accessToken ?: function ($app) {
return new AccessToken($app, $this);
},
'server' => function ($app) {
return new Guard($app);
},
];
foreach (['cache', 'http_client', 'log', 'logger', 'request'] as $reuse) {
if (isset($this[$reuse])) {
$services[$reuse] = $this[$reuse];
}
}
return $services;
}
/**
* Handle dynamic calls.
*
* @param string $method
* @param array $args
*
* @return mixed
*/
public function __call($method, $args)
{
return $this->base->$method(...$args);
}
}