Skip to content

Commit c0c55fe

Browse files
committed
[更新]代码格式化, 同时增加getConfig与getAppid方法
1 parent 4271ab6 commit c0c55fe

20 files changed

+584
-297
lines changed

Wechat/Lib/Cache.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
* @author Anyon <[email protected]>
1111
* @date 2016-08-20 17:50
1212
*/
13-
class Cache {
13+
class Cache
14+
{
1415

1516
/**
1617
* 缓存位置
@@ -25,7 +26,8 @@ class Cache {
2526
* @param int $expired
2627
* @return mixed
2728
*/
28-
static public function set($name, $value, $expired = 0) {
29+
static public function set($name, $value, $expired = 0)
30+
{
2931
if (isset(Loader::$callback['CacheSet'])) {
3032
return call_user_func_array(Loader::$callback['CacheSet'], func_get_args());
3133
}
@@ -38,7 +40,8 @@ static public function set($name, $value, $expired = 0) {
3840
* @param string $name
3941
* @return mixed
4042
*/
41-
static public function get($name) {
43+
static public function get($name)
44+
{
4245
if (isset(Loader::$callback['CacheGet'])) {
4346
return call_user_func_array(Loader::$callback['CacheGet'], func_get_args());
4447
}
@@ -56,7 +59,8 @@ static public function get($name) {
5659
* @param string $name
5760
* @return mixed
5861
*/
59-
static public function del($name) {
62+
static public function del($name)
63+
{
6064
if (isset(Loader::$callback['CacheDel'])) {
6165
return call_user_func_array(Loader::$callback['CacheDel'], func_get_args());
6266
}
@@ -69,7 +73,8 @@ static public function del($name) {
6973
* @param string $filename
7074
* @return mixed
7175
*/
72-
static public function put($line, $filename = '') {
76+
static public function put($line, $filename = '')
77+
{
7378
if (isset(Loader::$callback['CachePut'])) {
7479
return call_user_func_array(Loader::$callback['CachePut'], func_get_args());
7580
}
@@ -81,13 +86,14 @@ static public function put($line, $filename = '') {
8186
* 检查缓存目录
8287
* @return bool
8388
*/
84-
static protected function check() {
89+
static protected function check()
90+
{
8591
empty(self::$cachepath) && self::$cachepath = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'Cache' . DIRECTORY_SEPARATOR;
8692
self::$cachepath = rtrim(self::$cachepath, '/\\') . DIRECTORY_SEPARATOR;
87-
if (!is_dir(self::$cachepath) && !mkdir(self::$cachepath, 0755, TRUE)) {
88-
return FALSE;
93+
if (!is_dir(self::$cachepath) && !mkdir(self::$cachepath, 0755, true)) {
94+
return false;
8995
}
90-
return TRUE;
96+
return true;
9197
}
9298

9399
}

Wechat/Lib/Common.php

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
* @author Anyon <[email protected]>
1414
* @date 2016/05/28 11:55
1515
*/
16-
class Common {
16+
class Common
17+
{
1718

1819
/** API接口URL需要使用此前缀 */
1920
const API_BASE_URL_PREFIX = 'https://api.weixin.qq.com';
@@ -31,13 +32,14 @@ class Common {
3132
public $errCode = 0;
3233
public $errMsg = "";
3334
public $config = array();
34-
private $_retry = FALSE;
35+
private $_retry = false;
3536

3637
/**
3738
* 构造方法
3839
* @param array $options
3940
*/
40-
public function __construct($options = array()) {
41+
public function __construct($options = array())
42+
{
4143
$config = Loader::config($options);
4244
$this->token = isset($config['token']) ? $config['token'] : '';
4345
$this->appid = isset($config['appid']) ? $config['appid'] : '';
@@ -46,19 +48,39 @@ public function __construct($options = array()) {
4648
$this->config = $config;
4749
}
4850

51+
/**
52+
* 获取当前操作公众号APPID
53+
* @return string
54+
*/
55+
public function getAppid()
56+
{
57+
return $this->appid;
58+
}
59+
60+
/**
61+
* 获取SDK配置参数
62+
* @return array
63+
*/
64+
public function getConfig()
65+
{
66+
return $this->config;
67+
}
68+
69+
4970
/**
5071
* 接口验证
5172
* @return bool
5273
*/
53-
public function valid() {
74+
public function valid()
75+
{
5476
$encryptStr = "";
5577
if ($_SERVER['REQUEST_METHOD'] == "POST") {
5678
$postStr = file_get_contents("php://input");
5779
$array = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
5880
$this->encrypt_type = isset($_GET["encrypt_type"]) ? $_GET["encrypt_type"] : '';
5981
if ($this->encrypt_type == 'aes') {
6082
$encryptStr = $array['Encrypt'];
61-
!class_exists('Prpcrypt', FALSE) && require __DIR__ . '/Prpcrypt.php';
83+
!class_exists('Prpcrypt', false) && require __DIR__ . '/Prpcrypt.php';
6284
$pc = new Prpcrypt($this->encodingAesKey);
6385
$array = $pc->decrypt($encryptStr, $this->appid);
6486
if (!isset($array[0]) || intval($array[0]) > 0) {
@@ -91,7 +113,8 @@ public function valid() {
91113
* @param string $str
92114
* @return bool
93115
*/
94-
private function checkSignature($str = '') {
116+
private function checkSignature($str = '')
117+
{
95118
// 如果存在加密验证则用加密验证段
96119
$signature = isset($_GET["msg_signature"]) ? $_GET["msg_signature"] : (isset($_GET["signature"]) ? $_GET["signature"] : '');
97120
$timestamp = isset($_GET["timestamp"]) ? $_GET["timestamp"] : '';
@@ -112,7 +135,8 @@ private function checkSignature($str = '') {
112135
* @param string $token 手动指定access_token,非必要情况不建议用
113136
* @return bool|string
114137
*/
115-
public function getAccessToken($appid = '', $appsecret = '', $token = '') {
138+
public function getAccessToken($appid = '', $appsecret = '', $token = '')
139+
{
116140
if (!$appid || !$appsecret) {
117141
$appid = $this->appid;
118142
$appsecret = $this->appsecret;
@@ -151,7 +175,8 @@ public function getAccessToken($appid = '', $appsecret = '', $token = '') {
151175
* @param array $arguments SDK方法参数
152176
* @return bool|mixed
153177
*/
154-
protected function checkRetry($method, $arguments = array()) {
178+
protected function checkRetry($method, $arguments = array())
179+
{
155180
if (!$this->_retry && in_array($this->errCode, array('40014', '40001', '41001', '42001'))) {
156181
Tools::log("Run {$method} Faild. {$this->errMsg}[{$this->errCode}]", 'ERR');
157182
($this->_retry = true) && $this->resetAuth();
@@ -168,7 +193,8 @@ protected function checkRetry($method, $arguments = array()) {
168193
* @param string $appid 如在类初始化时已提供,则可为空
169194
* @return bool
170195
*/
171-
public function resetAuth($appid = '') {
196+
public function resetAuth($appid = '')
197+
{
172198
$authname = 'wechat_access_token_' . (empty($appid) ? $this->appid : $appid);
173199
Tools::log("Reset Auth And Remove Old AccessToken.");
174200
$this->access_token = '';

Wechat/Lib/Prpcrypt.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
* @subpackage library
77
* @date 2016/06/28 11:59
88
*/
9-
class PKCS7Encoder {
9+
class PKCS7Encoder
10+
{
1011

1112
public static $block_size = 32;
1213

@@ -15,7 +16,8 @@ class PKCS7Encoder {
1516
* @param string $text 需要进行填充补位操作的明文
1617
* @return string 补齐明文字符串
1718
*/
18-
function encode($text) {
19+
function encode($text)
20+
{
1921
$amount_to_pad = PKCS7Encoder::$block_size - (strlen($text) % PKCS7Encoder::$block_size);
2022
if ($amount_to_pad == 0) {
2123
$amount_to_pad = PKCS7Encoder::$block_size;
@@ -33,7 +35,8 @@ function encode($text) {
3335
* @param string $text 解密后的明文
3436
* @return string 删除填充补位后的明文
3537
*/
36-
function decode($text) {
38+
function decode($text)
39+
{
3740
$pad = ord(substr($text, -1));
3841
if ($pad < 1 || $pad > PKCS7Encoder::$block_size) {
3942
$pad = 0;
@@ -49,11 +52,13 @@ function decode($text) {
4952
* @subpackage library
5053
* @date 2016/06/28 11:59
5154
*/
52-
class Prpcrypt {
55+
class Prpcrypt
56+
{
5357

5458
public $key;
5559

56-
function __construct($k) {
60+
function __construct($k)
61+
{
5762
$this->key = base64_decode($k . "=");
5863
}
5964

@@ -63,7 +68,8 @@ function __construct($k) {
6368
* @param string $appid 公众号APPID
6469
* @return string 加密后的密文
6570
*/
66-
public function encrypt($text, $appid) {
71+
public function encrypt($text, $appid)
72+
{
6773
try {
6874
//获得16位随机字符串,填充到明文之前
6975
$random = $this->getRandomStr();//"aaaabbbbccccdddd";
@@ -84,7 +90,8 @@ public function encrypt($text, $appid) {
8490
* @param string $appid 公众号APPID
8591
* @return string 解密得到的明文
8692
*/
87-
public function decrypt($encrypted, $appid) {
93+
public function decrypt($encrypted, $appid)
94+
{
8895
try {
8996
$iv = substr($this->key, 0, 16);
9097
$decrypted = openssl_decrypt($encrypted, 'AES-256-CBC', substr($this->key, 0, 32), OPENSSL_ZERO_PADDING, $iv);
@@ -115,7 +122,8 @@ public function decrypt($encrypted, $appid) {
115122
* 随机生成16位字符串
116123
* @return string 生成的字符串
117124
*/
118-
function getRandomStr() {
125+
function getRandomStr()
126+
{
119127
$str = "";
120128
$str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
121129
$max = strlen($str_pol) - 1;
@@ -132,7 +140,8 @@ function getRandomStr() {
132140
* 不用于官方API接口的errCode码
133141
* Class ErrorCode
134142
*/
135-
class ErrorCode {
143+
class ErrorCode
144+
{
136145

137146
public static $OK = 0;
138147
public static $ValidateSignatureError = 40001;
@@ -166,7 +175,8 @@ class ErrorCode {
166175
* @param string $err
167176
* @return bool
168177
*/
169-
public static function getErrText($err) {
178+
public static function getErrText($err)
179+
{
170180
if (isset(self::$errCode[$err])) {
171181
return self::$errCode[$err];
172182
}

0 commit comments

Comments
 (0)