forked from thenbsp/wechat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractClient.php
More file actions
121 lines (101 loc) · 2.47 KB
/
AbstractClient.php
File metadata and controls
121 lines (101 loc) · 2.47 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
<?php
namespace Thenbsp\Wechat\OAuth;
use Thenbsp\Wechat\Bridge\Util;
use Thenbsp\Wechat\Bridge\Http;
abstract class AbstractClient
{
/**
* AccessToken URL
*/
const ACCESS_TOKEN = 'https://api.weixin.qq.com/sns/oauth2/access_token';
/**
* 公众号 Appid
*/
protected $appid;
/**
* 公众号 AppSecret
*/
protected $appsecret;
/**
* scope
*/
protected $scope;
/**
* state
*/
protected $state;
/**
* redirect url
*/
protected $redirectUri;
/**
* 构造方法
*/
public function __construct($appid, $appsecret)
{
$this->appid = $appid;
$this->appsecret = $appsecret;
}
/**
* 设置 scope
*/
public function setScope($scope)
{
$this->scope = $scope;
}
/**
* 设置 state
*/
public function setState($state)
{
$this->state = $state;
}
/**
* 设置 redirect uri
*/
public function setRedirectUri($redirectUri)
{
$this->redirectUri = $redirectUri;
}
/**
* 获取授权 URL
*/
public function getAuthorizeUrl()
{
$query = array(
'appid' => $this->appid,
'redirect_uri' => $this->redirectUri ?: Util::getCurrentUrl(),
'response_type' => 'code',
'scope' => $this->resolveScope(),
'state' => $this->state
);
return $this->resolveAuthorizeUrl().'?'.http_build_query($query);
}
/**
* 通过 code 换取 AccessToken
*/
public function getAccessToken($code)
{
$query = array(
'appid' => $this->appid,
'secret' => $this->appsecret,
'code' => $code,
'grant_type' => 'authorization_code'
);
$response = Http::request('GET', static::ACCESS_TOKEN)
->withQuery($query)
->send();
if( $response['errcode'] != 0 ) {
throw new \Exception($response['errmsg'], $response['errcode']);
}
return new AccessToken($this->appid, $response->toArray());
}
/**
* 授权接口地址
*/
abstract public function resolveAuthorizeUrl();
/**
* 授权作用域
*/
abstract public function resolveScope();
}