|
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
|
|
|
/** |
|
13
|
|
|
* PreAuth.php. |
|
14
|
|
|
* |
|
15
|
|
|
* Part of Overtrue\WeChat. |
|
16
|
|
|
* |
|
17
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
18
|
|
|
* file that was distributed with this source code. |
|
19
|
|
|
* |
|
20
|
|
|
* @author mingyoung <[email protected]> |
|
21
|
|
|
* @author lixiao <[email protected]> |
|
22
|
|
|
* @copyright 2016 |
|
23
|
|
|
* |
|
24
|
|
|
* @see https://github.com/overtrue |
|
25
|
|
|
* @see http://overtrue.me |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
namespace EasyWeChat\OpenPlatform\Api; |
|
29
|
|
|
|
|
30
|
|
|
use EasyWeChat\Core\Exceptions\InvalidArgumentException; |
|
31
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
32
|
|
|
|
|
33
|
|
|
class PreAuthorization extends AbstractComponent |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* Create pre auth code url. |
|
37
|
|
|
*/ |
|
38
|
|
|
const CREATE_PRE_AUTH_CODE = 'https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode'; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Pre auth link. |
|
42
|
|
|
*/ |
|
43
|
|
|
const PRE_AUTH_LINK = 'https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=%s&pre_auth_code=%s&redirect_uri=%s'; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Redirect Uri. |
|
47
|
|
|
* |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $redirectUri; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Get pre auth code. |
|
54
|
|
|
* |
|
55
|
|
|
* @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException |
|
56
|
|
|
* |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
2 |
|
public function getCode() |
|
60
|
|
|
{ |
|
61
|
|
|
$data = [ |
|
62
|
2 |
|
'component_appid' => $this->getAppId(), |
|
63
|
2 |
|
]; |
|
64
|
|
|
|
|
65
|
2 |
|
$result = $this->parseJSON('json', [self::CREATE_PRE_AUTH_CODE, $data]); |
|
66
|
|
|
|
|
67
|
2 |
|
if (empty($result['pre_auth_code'])) { |
|
68
|
|
|
throw new InvalidArgumentException('Invalid response.'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
2 |
|
return $result['pre_auth_code']; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Redirect to WeChat PreAuthorization page. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $url |
|
78
|
|
|
* |
|
79
|
|
|
* @return \Symfony\Component\HttpFoundation\RedirectResponse |
|
80
|
|
|
*/ |
|
81
|
1 |
|
public function redirect($url) |
|
82
|
|
|
{ |
|
83
|
1 |
|
return new RedirectResponse( |
|
84
|
1 |
|
sprintf(self::PRE_AUTH_LINK, $this->getAppId(), $this->getCode(), urlencode($url)) |
|
85
|
1 |
|
); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|