Skip to content

Commit

Permalink
[更新]代码格式化, 同时增加getConfig与getAppid方法
Browse files Browse the repository at this point in the history
  • Loading branch information
zoujingli committed Jul 18, 2017
1 parent 4271ab6 commit c0c55fe
Show file tree
Hide file tree
Showing 20 changed files with 584 additions and 297 deletions.
24 changes: 15 additions & 9 deletions Wechat/Lib/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
* @author Anyon <zoujingli@qq.com>
* @date 2016-08-20 17:50
*/
class Cache {
class Cache
{

/**
* 缓存位置
Expand All @@ -25,7 +26,8 @@ class Cache {
* @param int $expired
* @return mixed
*/
static public function set($name, $value, $expired = 0) {
static public function set($name, $value, $expired = 0)
{
if (isset(Loader::$callback['CacheSet'])) {
return call_user_func_array(Loader::$callback['CacheSet'], func_get_args());
}
Expand All @@ -38,7 +40,8 @@ static public function set($name, $value, $expired = 0) {
* @param string $name
* @return mixed
*/
static public function get($name) {
static public function get($name)
{
if (isset(Loader::$callback['CacheGet'])) {
return call_user_func_array(Loader::$callback['CacheGet'], func_get_args());
}
Expand All @@ -56,7 +59,8 @@ static public function get($name) {
* @param string $name
* @return mixed
*/
static public function del($name) {
static public function del($name)
{
if (isset(Loader::$callback['CacheDel'])) {
return call_user_func_array(Loader::$callback['CacheDel'], func_get_args());
}
Expand All @@ -69,7 +73,8 @@ static public function del($name) {
* @param string $filename
* @return mixed
*/
static public function put($line, $filename = '') {
static public function put($line, $filename = '')
{
if (isset(Loader::$callback['CachePut'])) {
return call_user_func_array(Loader::$callback['CachePut'], func_get_args());
}
Expand All @@ -81,13 +86,14 @@ static public function put($line, $filename = '') {
* 检查缓存目录
* @return bool
*/
static protected function check() {
static protected function check()
{
empty(self::$cachepath) && self::$cachepath = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'Cache' . DIRECTORY_SEPARATOR;
self::$cachepath = rtrim(self::$cachepath, '/\\') . DIRECTORY_SEPARATOR;
if (!is_dir(self::$cachepath) && !mkdir(self::$cachepath, 0755, TRUE)) {
return FALSE;
if (!is_dir(self::$cachepath) && !mkdir(self::$cachepath, 0755, true)) {
return false;
}
return TRUE;
return true;
}

}
44 changes: 35 additions & 9 deletions Wechat/Lib/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
* @author Anyon <zoujingli@qq.com>
* @date 2016/05/28 11:55
*/
class Common {
class Common
{

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

/**
* 构造方法
* @param array $options
*/
public function __construct($options = array()) {
public function __construct($options = array())
{
$config = Loader::config($options);
$this->token = isset($config['token']) ? $config['token'] : '';
$this->appid = isset($config['appid']) ? $config['appid'] : '';
Expand All @@ -46,19 +48,39 @@ public function __construct($options = array()) {
$this->config = $config;
}

/**
* 获取当前操作公众号APPID
* @return string
*/
public function getAppid()
{
return $this->appid;
}

/**
* 获取SDK配置参数
* @return array
*/
public function getConfig()
{
return $this->config;
}


/**
* 接口验证
* @return bool
*/
public function valid() {
public function valid()
{
$encryptStr = "";
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$postStr = file_get_contents("php://input");
$array = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$this->encrypt_type = isset($_GET["encrypt_type"]) ? $_GET["encrypt_type"] : '';
if ($this->encrypt_type == 'aes') {
$encryptStr = $array['Encrypt'];
!class_exists('Prpcrypt', FALSE) && require __DIR__ . '/Prpcrypt.php';
!class_exists('Prpcrypt', false) && require __DIR__ . '/Prpcrypt.php';
$pc = new Prpcrypt($this->encodingAesKey);
$array = $pc->decrypt($encryptStr, $this->appid);
if (!isset($array[0]) || intval($array[0]) > 0) {
Expand Down Expand Up @@ -91,7 +113,8 @@ public function valid() {
* @param string $str
* @return bool
*/
private function checkSignature($str = '') {
private function checkSignature($str = '')
{
// 如果存在加密验证则用加密验证段
$signature = isset($_GET["msg_signature"]) ? $_GET["msg_signature"] : (isset($_GET["signature"]) ? $_GET["signature"] : '');
$timestamp = isset($_GET["timestamp"]) ? $_GET["timestamp"] : '';
Expand All @@ -112,7 +135,8 @@ private function checkSignature($str = '') {
* @param string $token 手动指定access_token,非必要情况不建议用
* @return bool|string
*/
public function getAccessToken($appid = '', $appsecret = '', $token = '') {
public function getAccessToken($appid = '', $appsecret = '', $token = '')
{
if (!$appid || !$appsecret) {
$appid = $this->appid;
$appsecret = $this->appsecret;
Expand Down Expand Up @@ -151,7 +175,8 @@ public function getAccessToken($appid = '', $appsecret = '', $token = '') {
* @param array $arguments SDK方法参数
* @return bool|mixed
*/
protected function checkRetry($method, $arguments = array()) {
protected function checkRetry($method, $arguments = array())
{
if (!$this->_retry && in_array($this->errCode, array('40014', '40001', '41001', '42001'))) {
Tools::log("Run {$method} Faild. {$this->errMsg}[{$this->errCode}]", 'ERR');
($this->_retry = true) && $this->resetAuth();
Expand All @@ -168,7 +193,8 @@ protected function checkRetry($method, $arguments = array()) {
* @param string $appid 如在类初始化时已提供,则可为空
* @return bool
*/
public function resetAuth($appid = '') {
public function resetAuth($appid = '')
{
$authname = 'wechat_access_token_' . (empty($appid) ? $this->appid : $appid);
Tools::log("Reset Auth And Remove Old AccessToken.");
$this->access_token = '';
Expand Down
30 changes: 20 additions & 10 deletions Wechat/Lib/Prpcrypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
* @subpackage library
* @date 2016/06/28 11:59
*/
class PKCS7Encoder {
class PKCS7Encoder
{

public static $block_size = 32;

Expand All @@ -15,7 +16,8 @@ class PKCS7Encoder {
* @param string $text 需要进行填充补位操作的明文
* @return string 补齐明文字符串
*/
function encode($text) {
function encode($text)
{
$amount_to_pad = PKCS7Encoder::$block_size - (strlen($text) % PKCS7Encoder::$block_size);
if ($amount_to_pad == 0) {
$amount_to_pad = PKCS7Encoder::$block_size;
Expand All @@ -33,7 +35,8 @@ function encode($text) {
* @param string $text 解密后的明文
* @return string 删除填充补位后的明文
*/
function decode($text) {
function decode($text)
{
$pad = ord(substr($text, -1));
if ($pad < 1 || $pad > PKCS7Encoder::$block_size) {
$pad = 0;
Expand All @@ -49,11 +52,13 @@ function decode($text) {
* @subpackage library
* @date 2016/06/28 11:59
*/
class Prpcrypt {
class Prpcrypt
{

public $key;

function __construct($k) {
function __construct($k)
{
$this->key = base64_decode($k . "=");
}

Expand All @@ -63,7 +68,8 @@ function __construct($k) {
* @param string $appid 公众号APPID
* @return string 加密后的密文
*/
public function encrypt($text, $appid) {
public function encrypt($text, $appid)
{
try {
//获得16位随机字符串,填充到明文之前
$random = $this->getRandomStr();//"aaaabbbbccccdddd";
Expand All @@ -84,7 +90,8 @@ public function encrypt($text, $appid) {
* @param string $appid 公众号APPID
* @return string 解密得到的明文
*/
public function decrypt($encrypted, $appid) {
public function decrypt($encrypted, $appid)
{
try {
$iv = substr($this->key, 0, 16);
$decrypted = openssl_decrypt($encrypted, 'AES-256-CBC', substr($this->key, 0, 32), OPENSSL_ZERO_PADDING, $iv);
Expand Down Expand Up @@ -115,7 +122,8 @@ public function decrypt($encrypted, $appid) {
* 随机生成16位字符串
* @return string 生成的字符串
*/
function getRandomStr() {
function getRandomStr()
{
$str = "";
$str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
$max = strlen($str_pol) - 1;
Expand All @@ -132,7 +140,8 @@ function getRandomStr() {
* 不用于官方API接口的errCode码
* Class ErrorCode
*/
class ErrorCode {
class ErrorCode
{

public static $OK = 0;
public static $ValidateSignatureError = 40001;
Expand Down Expand Up @@ -166,7 +175,8 @@ class ErrorCode {
* @param string $err
* @return bool
*/
public static function getErrText($err) {
public static function getErrText($err)
{
if (isset(self::$errCode[$err])) {
return self::$errCode[$err];
}
Expand Down
Loading

0 comments on commit c0c55fe

Please sign in to comment.