Skip to content

Commit 4d10fbd

Browse files
committed
完善代码注释
1 parent 02f8459 commit 4d10fbd

17 files changed

+635
-689
lines changed

Wechat/Lib/Cache.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Wechat\Loader;
66

77
/**
8-
* SDK缓存类
8+
* 微信SDK基础缓存类
99
*
1010
* @author Anyon <[email protected]>
1111
* @date 2016-08-20 17:50
@@ -14,7 +14,7 @@ class Cache {
1414

1515
/**
1616
* 缓存位置
17-
* @var type
17+
* @var string
1818
*/
1919
static public $cachepath;
2020

@@ -79,7 +79,7 @@ static public function put($line, $filename = '') {
7979

8080
/**
8181
* 检查缓存目录
82-
* @return boolean
82+
* @return bool
8383
*/
8484
static protected function check() {
8585
empty(self::$cachepath) && self::$cachepath = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'Cache' . DIRECTORY_SEPARATOR;

Wechat/Lib/Common.php

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
*/
1616
class Common {
1717

18+
/** API接口URL需要使用此前缀 */
19+
const API_BASE_URL_PREFIX = 'https://api.weixin.qq.com';
20+
const API_URL_PREFIX = 'https://api.weixin.qq.com/cgi-bin';
21+
const GET_TICKET_URL = '/ticket/getticket?';
22+
const AUTH_URL = '/token?grant_type=client_credential&';
1823
public $token;
1924
public $encodingAesKey;
2025
public $encrypt_type;
@@ -30,12 +35,6 @@ class Common {
3035
public $config = array();
3136
private $_retry = FALSE;
3237

33-
/** API接口URL需要使用此前缀 */
34-
const API_BASE_URL_PREFIX = 'https://api.weixin.qq.com';
35-
const API_URL_PREFIX = 'https://api.weixin.qq.com/cgi-bin';
36-
const GET_TICKET_URL = '/ticket/getticket?';
37-
const AUTH_URL = '/token?grant_type=client_credential&';
38-
3938
/**
4039
* 构造方法
4140
* @param type $options
@@ -49,28 +48,9 @@ public function __construct($options) {
4948
$this->config = $config;
5049
}
5150

52-
/**
53-
* 验证来自微信服务器
54-
* @param type $str
55-
* @return boolean
56-
*/
57-
private function checkSignature($str = '') {
58-
$signature = isset($_GET["signature"]) ? $_GET["signature"] : '';
59-
$signature = isset($_GET["msg_signature"]) ? $_GET["msg_signature"] : $signature; //如果存在加密验证则用加密验证段
60-
$timestamp = isset($_GET["timestamp"]) ? $_GET["timestamp"] : '';
61-
$nonce = isset($_GET["nonce"]) ? $_GET["nonce"] : '';
62-
$tmpArr = array($this->token, $timestamp, $nonce, $str);
63-
sort($tmpArr, SORT_STRING);
64-
if (sha1(implode($tmpArr)) == $signature) {
65-
return true;
66-
} else {
67-
return false;
68-
}
69-
}
70-
7151
/**
7252
* 接口验证
73-
* @return boolean
53+
* @return bool
7454
*/
7555
public function valid() {
7656
$encryptStr = "";
@@ -108,11 +88,31 @@ public function valid() {
10888
return true;
10989
}
11090

91+
/**
92+
* 验证来自微信服务器
93+
* @param string $str
94+
* @return bool
95+
*/
96+
private function checkSignature($str = '') {
97+
// 如果存在加密验证则用加密验证段
98+
$signature = isset($_GET["msg_signature"]) ? $_GET["msg_signature"] : (isset($_GET["signature"]) ? $_GET["signature"] : '');
99+
$timestamp = isset($_GET["timestamp"]) ? $_GET["timestamp"] : '';
100+
$nonce = isset($_GET["nonce"]) ? $_GET["nonce"] : '';
101+
$tmpArr = array($this->token, $timestamp, $nonce, $str);
102+
sort($tmpArr, SORT_STRING);
103+
if (sha1(implode($tmpArr)) == $signature) {
104+
return true;
105+
} else {
106+
return false;
107+
}
108+
}
109+
111110
/**
112111
* 获取公众号访问 access_token
113112
* @param string $appid 如在类初始化时已提供,则可为空
114113
* @param string $appsecret 如在类初始化时已提供,则可为空
115114
* @param string $token 手动指定access_token,非必要情况不建议用
115+
* @return bool|string
116116
*/
117117
public function getAccessToken($appid = '', $appsecret = '', $token = '') {
118118
if (!$appid || !$appsecret) {
@@ -148,20 +148,10 @@ public function getAccessToken($appid = '', $appsecret = '', $token = '') {
148148
}
149149

150150
/**
151-
* 删除验证数据
152-
* @param string $appid
153-
*/
154-
public function resetAuth($appid = '') {
155-
$authname = 'wechat_access_token_' . (empty($appid) ? $this->appid : $appid);
156-
Tools::log("Reset Auth And Remove Old AccessToken.");
157-
$this->access_token = '';
158-
Tools::removeCache($authname);
159-
return true;
160-
}
161-
162-
/**
163-
* 重试检测
164-
* @return boolean
151+
* 接口失败重试
152+
* @param $method SDK方法名称
153+
* @param array $arguments SDK方法参数
154+
* @return bool|mixed
165155
*/
166156
protected function checkRetry($method, $arguments = array()) {
167157
if (!$this->_retry && in_array($this->errCode, array('40014', '40001', '41001', '42001'))) {
@@ -175,4 +165,17 @@ protected function checkRetry($method, $arguments = array()) {
175165
return false;
176166
}
177167

168+
/**
169+
* 删除验证数据
170+
* @param string $appid 如在类初始化时已提供,则可为空
171+
* @return bool
172+
*/
173+
public function resetAuth($appid = '') {
174+
$authname = 'wechat_access_token_' . (empty($appid) ? $this->appid : $appid);
175+
Tools::log("Reset Auth And Remove Old AccessToken.");
176+
$this->access_token = '';
177+
Tools::removeCache($authname);
178+
return true;
179+
}
180+
178181
}

Wechat/Lib/Prpcrypt.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ class ErrorCode {
174174
/**
175175
* 获取错误消息内容
176176
* @param type $err
177-
* @return boolean
177+
* @return bool
178178
*/
179179
public static function getErrText($err) {
180180
if (isset(self::$errCode[$err])) {

Wechat/Lib/Tools.php

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
/**
88
* 微信接口通用类
9-
*
9+
*
1010
* @category WechatSDK
1111
* @subpackage library
1212
* @author Anyon <[email protected]>
@@ -32,7 +32,7 @@ static public function createNoncestr($length = 32, $str = "") {
3232
* 获取签名
3333
* @param array $arrdata 签名数组
3434
* @param string $method 签名方法
35-
* @return boolean|string 签名值
35+
* @return bool|string 签名值
3636
*/
3737
static public function getSignature($arrdata, $method = "sha1") {
3838
if (!function_exists($method)) {
@@ -48,9 +48,9 @@ static public function getSignature($arrdata, $method = "sha1") {
4848

4949
/**
5050
* 生成支付签名
51-
* @param type $option
52-
* @param type $partnerKey
53-
* @return type
51+
* @param array $option
52+
* @param string $partnerKey
53+
* @return string
5454
*/
5555
static public function getPaySign($option, $partnerKey) {
5656
ksort($option);
@@ -66,8 +66,7 @@ static public function getPaySign($option, $partnerKey) {
6666
* @param mixed $data 数据
6767
* @param string $root 根节点名
6868
* @param string $item 数字索引的子节点名
69-
* @param string $attr 根节点属性
70-
* @param string $id 数字索引子节点key转换的属性名
69+
* @param string $id 数字索引子节点key转换的属性名
7170
* @return string
7271
*/
7372
static public function arr2xml($data, $root = 'xml', $item = 'item', $id = 'id') {
@@ -83,7 +82,7 @@ function _data_to_xml($data, $item = 'item', $id = 'id', $content = '') {
8382
} else {
8483
$content .= '<![CDATA[' . preg_replace("/[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]/", '', $val) . ']]>';
8584
}
86-
list($_key, ) = explode(' ', $key . ' ');
85+
list($_key,) = explode(' ', $key . ' ');
8786
$content .= "</$_key>";
8887
}
8988
return $content;
@@ -94,16 +93,26 @@ function _data_to_xml($data, $item = 'item', $id = 'id', $content = '') {
9493

9594
/**
9695
* 将xml转为array
97-
* @param type $xml
98-
* @return type
96+
* @param string $xml
97+
* @return array
9998
*/
10099
static public function xml2arr($xml) {
101100
return json_decode(Tools::json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
102101
}
103102

104103
/**
105-
* GET 请求
106-
* @param string $url
104+
* 生成安全JSON数据
105+
* @param $array
106+
* @return string
107+
*/
108+
static public function json_encode($array) {
109+
return preg_replace_callback('/\\\\u([0-9a-f]{4})/i', create_function('$matches', 'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");'), json_encode($array));
110+
}
111+
112+
/**
113+
* 以get方式提交请求
114+
* @param $url
115+
* @return bool|mixed
107116
*/
108117
static public function httpGet($url) {
109118
$oCurl = curl_init();
@@ -125,10 +134,10 @@ static public function httpGet($url) {
125134
}
126135

127136
/**
128-
* 以post方式提交xml到对应的接口url
129-
* @param type $url
130-
* @param type $postdata
131-
* @return boolean
137+
* 以post方式提交请求
138+
* @param string $url
139+
* @param array $postdata
140+
* @return bool|mixed
132141
*/
133142
static public function httpPost($url, $postdata) {
134143
$ch = curl_init();
@@ -156,12 +165,12 @@ static public function httpPost($url, $postdata) {
156165

157166
/**
158167
* 使用证书,以post方式提交xml到对应的接口url
159-
* @param type $url POST提交的内容
160-
* @param type $postdata 请求的地址
161-
* @param type $ssl_cer 证书Cer路径 | 证书内容
162-
* @param type $ssl_key 证书Key路径 | 证书内容
163-
* @param type $second 设置请求超时时间
164-
* @return boolean
168+
* @param string $url POST提交的内容
169+
* @param array $postdata 请求的地址
170+
* @param string $ssl_cer 证书Cer路径 | 证书内容
171+
* @param string $ssl_key 证书Key路径 | 证书内容
172+
* @param int $second 设置请求超时时间
173+
* @return bool|mixed
165174
*/
166175
static public function httpsPost($url, $postdata, $ssl_cer = null, $ssl_key = null, $second = 30) {
167176
$ch = curl_init();
@@ -200,17 +209,8 @@ static public function httpsPost($url, $postdata, $ssl_cer = null, $ssl_key = nu
200209
}
201210

202211
/**
203-
* 生成JSON,保留汉字
204-
* @param type $array
205-
* @return type
206-
*/
207-
static public function json_encode($array) {
208-
return preg_replace_callback('/\\\\u([0-9a-f]{4})/i', create_function('$matches', 'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");'), json_encode($array));
209-
}
210-
211-
/**
212-
* 读取客户端IP
213-
* @return type
212+
* 读取微信客户端IP
213+
* @return null|string
214214
*/
215215
static public function getAddress() {
216216
foreach (array('HTTP_X_FORWARDED_FOR', 'HTTP_CLIENT_IP', 'HTTP_X_CLIENT_IP', 'HTTP_X_CLUSTER_CLIENT_IP', 'REMOTE_ADDR') as $header) {
@@ -232,7 +232,7 @@ static public function getAddress() {
232232
* @param string $cachename
233233
* @param mixed $value
234234
* @param int $expired
235-
* @return boolean
235+
* @return bool
236236
*/
237237
static public function setCache($cachename, $value, $expired = 0) {
238238
return Cache::set($cachename, $value, $expired);
@@ -250,16 +250,16 @@ static public function getCache($cachename) {
250250
/**
251251
* 清除缓存,按需重载
252252
* @param string $cachename
253-
* @return boolean
253+
* @return bool
254254
*/
255255
static public function removeCache($cachename) {
256256
return Cache::del($cachename);
257257
}
258258

259259
/**
260260
* SDK日志处理方法
261-
* @param type $msg
262-
* @param type $type
261+
* @param string $msg 日志行内容
262+
* @param string $type 日志级别
263263
*/
264264
static public function log($msg, $type = 'MSG') {
265265
Cache::put($type . ' - ' . $msg);

0 commit comments

Comments
 (0)