@@ -6,7 +6,7 @@ discard block |
||
6 | 6 | namespace zhangv\wechat; |
7 | 7 | |
8 | 8 | class WechatOAuth { |
9 | - const TICKETTYPE_JSAPI = 'jsapi',TICKETTYPE_WXCARD = 'wx_card'; |
|
9 | + const TICKETTYPE_JSAPI = 'jsapi', TICKETTYPE_WXCARD = 'wx_card'; |
|
10 | 10 | public $responseJSON = null; |
11 | 11 | public $errCode = null; |
12 | 12 | public $errMsg = null; |
@@ -16,46 +16,46 @@ discard block |
||
16 | 16 | private $httpClient = null; |
17 | 17 | private $accessToken = null; |
18 | 18 | |
19 | - public function __construct($appId,$appSecret) { |
|
19 | + public function __construct($appId, $appSecret) { |
|
20 | 20 | $this->appId = $appId; |
21 | 21 | $this->appSecret = $appSecret; |
22 | 22 | $this->httpClient = new HttpClient(); |
23 | 23 | } |
24 | 24 | |
25 | - public function setHttpClient($httpClient){ |
|
25 | + public function setHttpClient($httpClient) { |
|
26 | 26 | $this->httpClient = $httpClient; |
27 | 27 | } |
28 | 28 | |
29 | - public function authorizeURI($redirectURI,$scope = 'snsapi_userinfo',$state = ''){ |
|
29 | + public function authorizeURI($redirectURI, $scope = 'snsapi_userinfo', $state = '') { |
|
30 | 30 | $redirectURI = urlencode($redirectURI); |
31 | 31 | return "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->appId}&redirect_uri={$redirectURI}&response_type=code&scope=$scope&state=$state#wechat_redirect"; |
32 | 32 | } |
33 | 33 | |
34 | - public function authorize($code){ |
|
34 | + public function authorize($code) { |
|
35 | 35 | $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->appId}&secret={$this->appSecret}&code=$code&grant_type=authorization_code"; |
36 | 36 | $this->responseJSON = $this->httpClient->get($url); |
37 | 37 | return json_decode($this->responseJSON); |
38 | 38 | } |
39 | 39 | |
40 | - public function getUserInfo($openId){ |
|
40 | + public function getUserInfo($openId) { |
|
41 | 41 | $url = "https://api.weixin.qq.com/sns/userinfo?access_token={$this->accessToken}&openid=$openId&lang=zh_CN"; |
42 | 42 | $this->responseJSON = $this->httpClient->get($url); |
43 | 43 | return json_decode($this->responseJSON); |
44 | 44 | } |
45 | 45 | |
46 | - public function refreshToken($refreshToken){ |
|
46 | + public function refreshToken($refreshToken) { |
|
47 | 47 | $url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={$this->appId}&grant_type=refresh_token&refresh_token=$refreshToken"; |
48 | 48 | $this->responseJSON = $this->httpClient->get($url); |
49 | 49 | return json_decode($this->responseJSON); |
50 | 50 | } |
51 | 51 | |
52 | - public function verifyToken($accessToken,$openId){ |
|
52 | + public function verifyToken($accessToken, $openId) { |
|
53 | 53 | $url = "https://api.weixin.qq.com/sns/auth?access_token=$accessToken&openid=$openId"; |
54 | 54 | $this->responseJSON = $this->httpClient->get($url); |
55 | 55 | return json_decode($this->responseJSON); |
56 | 56 | } |
57 | 57 | |
58 | - public function getAccessToken(){ |
|
58 | + public function getAccessToken() { |
|
59 | 59 | $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appId}&secret={$this->appSecret}"; |
60 | 60 | $this->responseJSON = $this->httpClient->get($url); |
61 | 61 | $json = json_decode($this->responseJSON); |
@@ -63,27 +63,27 @@ discard block |
||
63 | 63 | return $this->accessToken; |
64 | 64 | } |
65 | 65 | |
66 | - public function getTicket($type = WechatOAuth::TICKETTYPE_JSAPI, $accessToken = null){ |
|
67 | - if(!$accessToken) $accessToken = $this->getAccessToken(); |
|
66 | + public function getTicket($type = WechatOAuth::TICKETTYPE_JSAPI, $accessToken = null) { |
|
67 | + if (!$accessToken) $accessToken = $this->getAccessToken(); |
|
68 | 68 | // $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken"; |
69 | 69 | $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type={$type}&access_token=$accessToken"; |
70 | 70 | $this->responseJSON = $this->httpClient->get($url); |
71 | 71 | return json_decode($this->responseJSON); |
72 | 72 | } |
73 | 73 | |
74 | - public function getSession($code,$accesstoken = null){ |
|
75 | - if(!$accesstoken) $accesstoken = $this->getAccessToken(); |
|
74 | + public function getSession($code, $accesstoken = null) { |
|
75 | + if (!$accesstoken) $accesstoken = $this->getAccessToken(); |
|
76 | 76 | $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$this->appId}&secret={$this->appSecret}&js_code=$code&grant_type=authorization_code"; |
77 | 77 | $this->responseJSON = $this->httpClient->get($url); |
78 | 78 | return json_decode($this->responseJSON); |
79 | 79 | } |
80 | 80 | |
81 | - public function getSignPackage($url = null, $ticket = null){ |
|
82 | - if(!$url){ |
|
81 | + public function getSignPackage($url = null, $ticket = null) { |
|
82 | + if (!$url) { |
|
83 | 83 | $url = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://"; |
84 | 84 | $url .= "{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}"; |
85 | 85 | } |
86 | - if(!$ticket) $ticket = $this->getTicket(); |
|
86 | + if (!$ticket) $ticket = $this->getTicket(); |
|
87 | 87 | $timestamp = time(); |
88 | 88 | $nonceStr = $this->getNonceStr(); |
89 | 89 | $rawString = "jsapi_ticket=$ticket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; |
@@ -101,6 +101,6 @@ discard block |
||
101 | 101 | } |
102 | 102 | |
103 | 103 | private function getNonceStr() { |
104 | - return substr(str_shuffle("abcdefghijklmnopqrstuvwxyz0123456789"),0,32); |
|
104 | + return substr(str_shuffle("abcdefghijklmnopqrstuvwxyz0123456789"), 0, 32); |
|
105 | 105 | } |
106 | 106 | } |
@@ -64,7 +64,9 @@ discard block |
||
64 | 64 | } |
65 | 65 | |
66 | 66 | public function getTicket($type = WechatOAuth::TICKETTYPE_JSAPI, $accessToken = null){ |
67 | - if(!$accessToken) $accessToken = $this->getAccessToken(); |
|
67 | + if(!$accessToken) { |
|
68 | + $accessToken = $this->getAccessToken(); |
|
69 | + } |
|
68 | 70 | // $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken"; |
69 | 71 | $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type={$type}&access_token=$accessToken"; |
70 | 72 | $this->responseJSON = $this->httpClient->get($url); |
@@ -72,7 +74,9 @@ discard block |
||
72 | 74 | } |
73 | 75 | |
74 | 76 | public function getSession($code,$accesstoken = null){ |
75 | - if(!$accesstoken) $accesstoken = $this->getAccessToken(); |
|
77 | + if(!$accesstoken) { |
|
78 | + $accesstoken = $this->getAccessToken(); |
|
79 | + } |
|
76 | 80 | $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$this->appId}&secret={$this->appSecret}&js_code=$code&grant_type=authorization_code"; |
77 | 81 | $this->responseJSON = $this->httpClient->get($url); |
78 | 82 | return json_decode($this->responseJSON); |
@@ -83,7 +87,9 @@ discard block |
||
83 | 87 | $url = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://"; |
84 | 88 | $url .= "{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}"; |
85 | 89 | } |
86 | - if(!$ticket) $ticket = $this->getTicket(); |
|
90 | + if(!$ticket) { |
|
91 | + $ticket = $this->getTicket(); |
|
92 | + } |
|
87 | 93 | $timestamp = time(); |
88 | 94 | $nonceStr = $this->getNonceStr(); |
89 | 95 | $rawString = "jsapi_ticket=$ticket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; |
@@ -4,15 +4,15 @@ discard block |
||
4 | 4 | * Time: 2018/5/29 16:41 |
5 | 5 | */ |
6 | 6 | namespace zhangv\wechat\cache; |
7 | -class JsonFileCacheProvider implements CacheProvider{ |
|
7 | +class JsonFileCacheProvider implements CacheProvider { |
|
8 | 8 | private $cacheDir = null; |
9 | 9 | |
10 | - public function __construct($cacheDir = null){ |
|
11 | - if(!$cacheDir) $this->cacheDir = __DIR__; |
|
10 | + public function __construct($cacheDir = null) { |
|
11 | + if (!$cacheDir) $this->cacheDir = __DIR__; |
|
12 | 12 | else $this->cacheDir = $cacheDir; |
13 | 13 | } |
14 | 14 | |
15 | - public function set($key,$jsonobj,$expireAt = null){ |
|
15 | + public function set($key, $jsonobj, $expireAt = null) { |
|
16 | 16 | $data = $jsonobj; |
17 | 17 | $data->expires_at = $expireAt; |
18 | 18 | $file = "{$this->cacheDir}/{$key}.json"; |
@@ -21,12 +21,12 @@ discard block |
||
21 | 21 | if ($fp) fclose($fp); |
22 | 22 | } |
23 | 23 | |
24 | - public function get($key){ |
|
24 | + public function get($key) { |
|
25 | 25 | $file = "{$this->cacheDir}/{$key}.json"; |
26 | 26 | $cache = null; |
27 | - if(file_exists($file)){ |
|
27 | + if (file_exists($file)) { |
|
28 | 28 | $cache = json_decode(file_get_contents($file)); |
29 | - if($cache->expires_at < time()){ |
|
29 | + if ($cache->expires_at < time()) { |
|
30 | 30 | $cache = null; |
31 | 31 | $this->clear($key); |
32 | 32 | } |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | return $cache; |
35 | 35 | } |
36 | 36 | |
37 | - public function clear($key){ |
|
37 | + public function clear($key) { |
|
38 | 38 | $file = "{$this->cacheDir}/{$key}.json"; |
39 | 39 | if (file_exists($file)) { |
40 | 40 | unlink($file); |
@@ -8,8 +8,11 @@ discard block |
||
8 | 8 | private $cacheDir = null; |
9 | 9 | |
10 | 10 | public function __construct($cacheDir = null){ |
11 | - if(!$cacheDir) $this->cacheDir = __DIR__; |
|
12 | - else $this->cacheDir = $cacheDir; |
|
11 | + if(!$cacheDir) { |
|
12 | + $this->cacheDir = __DIR__; |
|
13 | + } else { |
|
14 | + $this->cacheDir = $cacheDir; |
|
15 | + } |
|
13 | 16 | } |
14 | 17 | |
15 | 18 | public function set($key,$jsonobj,$expireAt = null){ |
@@ -18,7 +21,9 @@ discard block |
||
18 | 21 | $file = "{$this->cacheDir}/{$key}.json"; |
19 | 22 | $fp = fopen($file, "w"); |
20 | 23 | fwrite($fp, json_encode($data)); |
21 | - if ($fp) fclose($fp); |
|
24 | + if ($fp) { |
|
25 | + fclose($fp); |
|
26 | + } |
|
22 | 27 | } |
23 | 28 | |
24 | 29 | public function get($key){ |