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
|
|
|
* PreAuthCode.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\Components; |
29
|
|
|
|
30
|
|
|
use EasyWeChat\Core\Exceptions\InvalidArgumentException; |
31
|
|
|
use EasyWeChat\Core\Exceptions\RuntimeException; |
32
|
|
|
use EasyWeChat\Support\Url; |
33
|
|
|
|
34
|
|
|
class PreAuthCode extends AbstractComponent |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* Create pre auth code url. |
38
|
|
|
*/ |
39
|
|
|
const CREATE_PRE_AUTH_CODE = 'https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Pre auth link. |
43
|
|
|
*/ |
44
|
|
|
const PRE_AUTH_LINK = 'https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=%s&pre_auth_code=%s&redirect_uri=%s'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Redirect Uri. |
48
|
|
|
* |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
protected $redirectUri; |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get pre auth code. |
56
|
|
|
* |
57
|
|
|
* @throws InvalidArgumentException |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public function getCode() |
62
|
|
|
{ |
63
|
|
|
$data = [ |
64
|
|
|
'component_appid' => $this->getAppId(), |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
$result = $this->parseJSON('json', [self::CREATE_PRE_AUTH_CODE, $data]); |
68
|
|
|
|
69
|
|
|
if (empty($result['pre_auth_code'])) { |
70
|
|
|
throw new InvalidArgumentException('Invalid response.'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $result['pre_auth_code']; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get Redirect url. |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
* |
81
|
|
|
* @throws RuntimeException |
82
|
|
|
*/ |
83
|
|
|
public function getRedirectUri() |
84
|
|
|
{ |
85
|
|
|
if (!$this->redirectUri) { |
86
|
|
|
throw new RuntimeException('You need to provided a redirect uri.'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $this->redirectUri; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Set redirect uri. |
94
|
|
|
* |
95
|
|
|
* @param string $uri |
96
|
|
|
* |
97
|
|
|
* @return $this |
98
|
|
|
*/ |
99
|
|
|
public function setRedirectUri($uri) |
100
|
|
|
{ |
101
|
|
|
$this->redirectUri = $uri; |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get auth page link. |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
public function getAuthLink() |
112
|
|
|
{ |
113
|
|
|
return sprintf(self::PRE_AUTH_LINK, |
114
|
|
|
$this->getAppId(), |
115
|
|
|
$this->getCode(), |
116
|
|
|
Url::encode($this->getRedirectUri()) |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|