1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the overtrue/wechat. |
5
|
|
|
* |
6
|
|
|
* (c) overtrue <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace EasyWeChat\OpenPlatform; |
13
|
|
|
|
14
|
|
|
use EasyWeChat\Kernel\ServiceContainer; |
15
|
|
|
use EasyWeChat\MiniProgram\Encryptor; |
16
|
|
|
use EasyWeChat\OpenPlatform\Authorizer\Auth\AccessToken; |
17
|
|
|
use EasyWeChat\OpenPlatform\Authorizer\MiniProgram\Application as MiniProgram; |
18
|
|
|
use EasyWeChat\OpenPlatform\Authorizer\MiniProgram\Auth\Client; |
19
|
|
|
use EasyWeChat\OpenPlatform\Authorizer\OfficialAccount\Account\Client as AccountClient; |
20
|
|
|
use EasyWeChat\OpenPlatform\Authorizer\OfficialAccount\Application as OfficialAccount; |
21
|
|
|
use EasyWeChat\OpenPlatform\Authorizer\Server\Guard; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class Application. |
25
|
|
|
* |
26
|
|
|
* @property \EasyWeChat\OpenPlatform\Server\Guard $server |
27
|
|
|
* @property \EasyWeChat\OpenPlatform\Auth\AccessToken $access_token |
28
|
|
|
* @property \EasyWeChat\OpenPlatform\CodeTemplate\Client $code_template |
29
|
|
|
* @property \EasyWeChat\OpenPlatform\Component\Client $component |
30
|
|
|
* |
31
|
|
|
* @method mixed handleAuthorize(string $authCode = null) |
32
|
|
|
* @method mixed getAuthorizer(string $appId) |
33
|
|
|
* @method mixed getAuthorizerOption(string $appId, string $name) |
34
|
|
|
* @method mixed setAuthorizerOption(string $appId, string $name, string $value) |
35
|
|
|
* @method mixed getAuthorizers(int $offset = 0, int $count = 500) |
36
|
|
|
* @method mixed createPreAuthorizationCode() |
37
|
|
|
*/ |
38
|
|
|
class Application extends ServiceContainer |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $providers = [ |
44
|
|
|
Auth\ServiceProvider::class, |
45
|
|
|
Base\ServiceProvider::class, |
46
|
|
|
Server\ServiceProvider::class, |
47
|
|
|
CodeTemplate\ServiceProvider::class, |
48
|
|
|
Component\ServiceProvider::class, |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
protected $defaultConfig = [ |
55
|
|
|
'http' => [ |
56
|
|
|
'timeout' => 5.0, |
57
|
|
|
'base_uri' => 'https://api.weixin.qq.com/', |
58
|
|
|
], |
59
|
|
|
]; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Creates the officialAccount application. |
63
|
|
|
* |
64
|
|
|
* @param string $appId |
65
|
|
|
* @param string|null $refreshToken |
66
|
|
|
* @param \EasyWeChat\OpenPlatform\Authorizer\Auth\AccessToken|null $accessToken |
67
|
|
|
* |
68
|
|
|
* @return \EasyWeChat\OpenPlatform\Authorizer\OfficialAccount\Application |
69
|
|
|
*/ |
70
|
2 |
|
public function officialAccount(string $appId, string $refreshToken = null, AccessToken $accessToken = null): OfficialAccount |
71
|
|
|
{ |
72
|
2 |
|
$application = new OfficialAccount($this->getAuthorizerConfig($appId, $refreshToken), $this->getReplaceServices($accessToken) + [ |
73
|
2 |
|
'encryptor' => $this['encryptor'], |
74
|
|
|
|
75
|
2 |
|
'account' => function ($app) { |
76
|
1 |
|
return new AccountClient($app, $this); |
77
|
2 |
|
}, |
78
|
|
|
]); |
79
|
|
|
|
80
|
2 |
|
$application->extend('oauth', function ($socialite) { |
81
|
|
|
/* @var \Overtrue\Socialite\Providers\WeChat $socialite */ |
82
|
1 |
|
return $socialite; |
83
|
2 |
|
}); |
84
|
|
|
|
85
|
2 |
|
return $application; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Creates the miniProgram application. |
90
|
|
|
* |
91
|
|
|
* @param string $appId |
92
|
|
|
* @param string|null $refreshToken |
93
|
|
|
* @param \EasyWeChat\OpenPlatform\Authorizer\Auth\AccessToken|null $accessToken |
94
|
|
|
* |
95
|
|
|
* @return \EasyWeChat\OpenPlatform\Authorizer\MiniProgram\Application |
96
|
|
|
*/ |
97
|
1 |
|
public function miniProgram(string $appId, string $refreshToken = null, AccessToken $accessToken = null): MiniProgram |
98
|
|
|
{ |
99
|
1 |
|
return new MiniProgram($this->getAuthorizerConfig($appId, $refreshToken), $this->getReplaceServices($accessToken) + [ |
100
|
1 |
|
'encryptor' => function () { |
101
|
1 |
|
return new Encryptor($this['config']['app_id'], $this['config']['token'], $this['config']['aes_key']); |
102
|
1 |
|
}, |
103
|
|
|
|
104
|
1 |
|
'auth' => function ($app) { |
105
|
1 |
|
return new Client($app, $this); |
106
|
1 |
|
}, |
107
|
|
|
]); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Return the pre-authorization login page url. |
112
|
|
|
* |
113
|
|
|
* @param string $callbackUrl |
114
|
|
|
* @param string|array|null $optional |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
1 |
|
public function getPreAuthorizationUrl(string $callbackUrl, $optional = []): string |
119
|
|
|
{ |
120
|
|
|
// 兼容旧版 API 设计 |
121
|
1 |
|
if (\is_string($optional)) { |
122
|
|
|
$optional = [ |
123
|
1 |
|
'pre_auth_code' => $optional, |
124
|
|
|
]; |
125
|
|
|
} else { |
126
|
1 |
|
$optional['pre_auth_code'] = $this->createPreAuthorizationCode()['pre_auth_code']; |
127
|
|
|
} |
128
|
|
|
|
129
|
1 |
|
$queries = \array_merge($optional, [ |
|
|
|
|
130
|
1 |
|
'component_appid' => $this['config']['app_id'], |
131
|
1 |
|
'redirect_uri' => $callbackUrl, |
132
|
|
|
]); |
133
|
|
|
|
134
|
1 |
|
return 'https://mp.weixin.qq.com/cgi-bin/componentloginpage?'.http_build_query($queries); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Return the pre-authorization login page url (mobile). |
139
|
|
|
* |
140
|
|
|
* @param string $callbackUrl |
141
|
|
|
* @param string|array|null $optional |
142
|
|
|
* |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
1 |
|
public function getMobilePreAuthorizationUrl(string $callbackUrl, $optional = []): string |
146
|
|
|
{ |
147
|
|
|
// 兼容旧版 API 设计 |
148
|
1 |
|
if (\is_string($optional)) { |
149
|
|
|
$optional = [ |
150
|
1 |
|
'pre_auth_code' => $optional, |
151
|
|
|
]; |
152
|
|
|
} else { |
153
|
1 |
|
$optional['pre_auth_code'] = $this->createPreAuthorizationCode()['pre_auth_code']; |
154
|
|
|
} |
155
|
|
|
|
156
|
1 |
|
$queries = \array_merge($optional, [ |
|
|
|
|
157
|
1 |
|
'component_appid' => $this['config']['app_id'], |
158
|
1 |
|
'redirect_uri' => $callbackUrl, |
159
|
1 |
|
'action' => 'bindcomponent', |
160
|
1 |
|
'no_scan' => 1, |
161
|
|
|
]); |
162
|
|
|
|
163
|
1 |
|
return 'https://mp.weixin.qq.com/safe/bindcomponent?'.http_build_query($queries).'#wechat_redirect'; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param string $appId |
168
|
|
|
* @param string|null $refreshToken |
169
|
|
|
* |
170
|
|
|
* @return array |
171
|
|
|
*/ |
172
|
3 |
|
protected function getAuthorizerConfig(string $appId, string $refreshToken = null): array |
173
|
|
|
{ |
174
|
3 |
|
return $this['config']->merge([ |
175
|
3 |
|
'component_app_id' => $this['config']['app_id'], |
176
|
3 |
|
'component_app_token' => $this['config']['token'], |
177
|
3 |
|
'app_id' => $appId, |
178
|
3 |
|
'refresh_token' => $refreshToken, |
179
|
3 |
|
])->toArray(); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param \EasyWeChat\OpenPlatform\Authorizer\Auth\AccessToken|null $accessToken |
184
|
|
|
* |
185
|
|
|
* @return array |
186
|
|
|
*/ |
187
|
3 |
|
protected function getReplaceServices(AccessToken $accessToken = null): array |
188
|
|
|
{ |
189
|
|
|
$services = [ |
190
|
3 |
|
'access_token' => $accessToken ?: function ($app) { |
191
|
2 |
|
return new AccessToken($app, $this); |
192
|
3 |
|
}, |
193
|
|
|
|
194
|
3 |
|
'server' => function ($app) { |
195
|
1 |
|
return new Guard($app); |
196
|
3 |
|
}, |
197
|
|
|
]; |
198
|
|
|
|
199
|
3 |
|
foreach (['cache', 'http_client', 'log', 'logger', 'request'] as $reuse) { |
200
|
3 |
|
if (isset($this[$reuse])) { |
201
|
3 |
|
$services[$reuse] = $this[$reuse]; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
3 |
|
return $services; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Handle dynamic calls. |
210
|
|
|
* |
211
|
|
|
* @param string $method |
212
|
|
|
* @param array $args |
213
|
|
|
* |
214
|
|
|
* @return mixed |
215
|
|
|
*/ |
216
|
1 |
|
public function __call($method, $args) |
217
|
|
|
{ |
218
|
1 |
|
return $this->base->$method(...$args); |
|
|
|
|
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|