Completed
Push — master ( c385cb...b0036f )
by Wei
04:12
created

WechatOAuth::getSignPackage()   B

Complexity

Conditions 6
Paths 14

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 16
nc 14
nop 2
dl 0
loc 20
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * @license MIT
4
 * @author zhangv
5
 */
6
namespace zhangv\wechat;
7
8
class WechatOAuth {
9
	const TICKETTYPE_JSAPI = 'jsapi',TICKETTYPE_WXCARD = 'wx_card';
10
	public $responseJSON = null;
11
	public $errCode = null;
12
	public $errMsg = null;
13
14
	private $appId = null;
15
	private $appSecret = null;
16
	private $httpClient = null;
17
	private $accessToken = null;
18
19
	public function __construct($appId,$appSecret) {
20
		$this->appId = $appId;
21
		$this->appSecret = $appSecret;
22
		$this->httpClient = new HttpClient();
23
	}
24
25
	public function setHttpClient($httpClient){
26
		$this->httpClient = $httpClient;
27
	}
28
29
	public function authorizeURI($redirectURI,$scope = 'snsapi_userinfo',$state = ''){
30
		$redirectURI = urlencode($redirectURI);
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
	}
33
34
	public function authorize($code){
35
		$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->appId}&secret={$this->appSecret}&code=$code&grant_type=authorization_code";
36
		$this->responseJSON = $this->httpClient->get($url);
37
		return json_decode($this->responseJSON);
38
	}
39
40
	public function getUserInfo($openId){
41
		$url = "https://api.weixin.qq.com/sns/userinfo?access_token={$this->accessToken}&openid=$openId&lang=zh_CN";
42
		$this->responseJSON = $this->httpClient->get($url);
43
		return json_decode($this->responseJSON);
44
	}
45
46
	public function refreshToken($refreshToken){
47
		$url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={$this->appId}&grant_type=refresh_token&refresh_token=$refreshToken";
48
		$this->responseJSON = $this->httpClient->get($url);
49
		return json_decode($this->responseJSON);
50
	}
51
52
	public function verifyToken($accessToken,$openId){
53
		$url = "https://api.weixin.qq.com/sns/auth?access_token=$accessToken&openid=$openId";
54
		$this->responseJSON = $this->httpClient->get($url);
55
		return json_decode($this->responseJSON);
56
	}
57
58
	public function getAccessToken(){
59
		$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appId}&secret={$this->appSecret}";
60
		$this->responseJSON = $this->httpClient->get($url);
61
		$json = json_decode($this->responseJSON);
62
		$this->accessToken = $json->access_token;
63
		return $this->accessToken;
64
	}
65
66
	public function getTicket($type = WechatOAuth::TICKETTYPE_JSAPI, $accessToken = null){
67
		if(!$accessToken) $accessToken = $this->getAccessToken();
68
		// $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken";
69
		$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type={$type}&access_token=$accessToken";
70
		$this->responseJSON = $this->httpClient->get($url);
71
		return json_decode($this->responseJSON);
72
	}
73
74
	public function getSignPackage($url = null, $ticket = null){
75
		if(!$url){
76
			$url = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
77
			$url .= "{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
78
		}
79
		if(!$ticket) $ticket = $this->getTicket();
80
		$timestamp = time();
81
		$nonceStr = $this->getNonceStr();
82
		$rawString = "jsapi_ticket=$ticket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
83
		$signature = sha1($rawString);
84
85
		$signPackage = array(
86
			"appId" => $this->appId,
87
			"nonceStr" => $nonceStr,
88
			"timestamp" => $timestamp,
89
			"url" => $url,
90
			"signature" => $signature,
91
			"rawString" => $rawString
92
		);
93
		return $signPackage;
94
	}
95
96
	private function getNonceStr() {
97
		return substr(str_shuffle("abcdefghijklmnopqrstuvwxyz0123456789"),0,32);
98
	}
99
}
100