Commit 139518a6 by yeran

commit

1 parent 3d979042
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<JetCodeStyleSettings>
<option name="PACKAGES_TO_USE_STAR_IMPORTS">
<value>
<package name="java.util" alias="false" withSubpackages="false" />
<package name="kotlinx.android.synthetic" alias="false" withSubpackages="true" />
<package name="io.ktor" alias="false" withSubpackages="true" />
</value>
</option>
<option name="PACKAGES_IMPORT_LAYOUT">
<value>
<package name="" alias="false" withSubpackages="true" />
<package name="java" alias="false" withSubpackages="true" />
<package name="javax" alias="false" withSubpackages="true" />
<package name="kotlin" alias="false" withSubpackages="true" />
<package name="" alias="true" withSubpackages="true" />
</value>
</option>
</JetCodeStyleSettings>
</code_scheme>
</component>
\ No newline at end of file
...@@ -55,6 +55,7 @@ ...@@ -55,6 +55,7 @@
<option name="width" value="1440" /> <option name="width" value="1440" />
<option name="height" value="807" /> <option name="height" value="807" />
</component> </component>
<component name="ProjectId" id="2Ehu3VHUh5Mlr4BYAChooCV4lXD" />
<component name="ProjectInspectionProfilesVisibleTreeState"> <component name="ProjectInspectionProfilesVisibleTreeState">
<entry key="Project Default"> <entry key="Project Default">
<profile-state> <profile-state>
...@@ -181,13 +182,15 @@ ...@@ -181,13 +182,15 @@
<option name="showLibraryContents" value="true" /> <option name="showLibraryContents" value="true" />
</component> </component>
<component name="PropertiesComponent"> <component name="PropertiesComponent">
<property name="settings.editor.selected.configurable" value="reference.projectsettings.compiler.javacompiler" /> <property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
<property name="project.structure.last.edited" value="Modules" />
<property name="project.structure.proportion" value="0.0" />
<property name="project.structure.side.proportion" value="0.2" />
<property name="WebServerToolWindowFactoryState" value="false" /> <property name="WebServerToolWindowFactoryState" value="false" />
<property name="aspect.path.notification.shown" value="true" /> <property name="aspect.path.notification.shown" value="true" />
<property name="go.import.settings.migrated" value="true" />
<property name="last_opened_file_path" value="$PROJECT_DIR$" /> <property name="last_opened_file_path" value="$PROJECT_DIR$" />
<property name="project.structure.last.edited" value="Modules" />
<property name="project.structure.proportion" value="0.0" />
<property name="project.structure.side.proportion" value="0.2" />
<property name="settings.editor.selected.configurable" value="reference.projectsettings.compiler.javacompiler" />
</component> </component>
<component name="RecentsManager"> <component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS"> <key name="CopyFile.RECENT_KEYS">
......
...@@ -2,14 +2,14 @@ ...@@ -2,14 +2,14 @@
"name": "ldy/wechatkit", "name": "ldy/wechatkit",
"description": "wechat base api tools", "description": "wechat base api tools",
"keywords": ["wechat", "weixinApi", "api"], "keywords": ["wechat", "weixinApi", "api"],
"homepage": "http://www.ledouya.com/", "homepage": "http://",
"type": "library", "type": "library",
"version": "1.2.0", "version": "1.2.1",
"authors": [ "authors": [
{ {
"name": "lvdouya", "name": "bingge",
"email": "bingge@ledouya.com", "email": "bingge@lestore.com",
"homepage": "http://www.ledouya.com" "homepage": "http://"
} }
], ],
"require": { "require": {
......
File mode changed
File mode changed
File mode changed
File mode changed
File mode changed
File mode changed
...@@ -13,18 +13,24 @@ class Config{ ...@@ -13,18 +13,24 @@ class Config{
'key_file'=>null, 'key_file'=>null,
'token'=>null, 'token'=>null,
'encodingAesKey'=>null, 'encodingAesKey'=>null,
'accessToken' => '',
'open'=>[ 'open'=>[
'appid'=>'', 'appid'=>'',
'secret'=>'', 'secret'=>'',
'token'=>null, 'token'=>null,
'encodingAesKey'=>null, 'encodingAesKey'=>null,
'componentAccessToken'=>''
], ],
'webLogin'=>[ 'webLogin'=>[
'appid'=>'', 'appid'=>'',
'secret'=>'' 'secret'=>''
] ]
]; ];
static public $type = 'O';
static public $config = [
'cachePath' => APP_ROOT.'/cache/file/%s', //%s 为APPID替代 此公众号下所有凭证都会缓存在此目录下
'cacheTime' => '7000', //缓存有效时间
];
/** /**
* *
* 第三方授权维护服务的链接 * 第三方授权维护服务的链接
...@@ -43,15 +49,9 @@ class Config{ ...@@ -43,15 +49,9 @@ class Config{
'openHandler'=>'O', //开放平台接入 'openHandler'=>'O', //开放平台接入
'mpHandler'=>'A' //普通接入 'mpHandler'=>'A' //普通接入
]; ];
static public $type;
static public $config = [
'cachePath' => APP_ROOT.'/cache/file/%s', //%s 为APPID替代 此公众号下所有凭证都会缓存在此目录下
'cacheTime' => '7000', //缓存有效时间
];
static public $accessToken;
static public $jsApiTicket; static public $jsApiTicket;
static public $oAuthAccessToken; static public $oAuthAccessToken;
static public $componentAccessToken;
static public $CARDJSTICKET; static public $CARDJSTICKET;
} }
\ No newline at end of file
File mode changed
File mode changed
File mode changed
File mode changed
File mode changed
...@@ -24,6 +24,9 @@ class CryptMsg ...@@ -24,6 +24,9 @@ class CryptMsg
private $encodingAesKey; private $encodingAesKey;
private $appId; private $appId;
/** /**
* 构造函数 * 构造函数
* @param $token string 公众平台上,开发者设置的token * @param $token string 公众平台上,开发者设置的token
...@@ -37,6 +40,7 @@ class CryptMsg ...@@ -37,6 +40,7 @@ class CryptMsg
$this->appId = $appId; $this->appId = $appId;
} }
/** /**
* 将公众平台回复用户的消息加密打包. * 将公众平台回复用户的消息加密打包.
* <ol> * <ol>
......
File mode changed
...@@ -60,39 +60,27 @@ class Prpcrypt ...@@ -60,39 +60,27 @@ class Prpcrypt
{ {
public $key; public $key;
function Prpcrypt($k) function __construct($k)
{ {
$this->key = base64_decode($k . "="); $this->key = base64_decode($k . "=");
} }
/** /**
* 对明文进行加密 * 加密方法
* @param string $text 需要加密的明文 * @param string $text
* @return string 加密后的密文 * @param $appid
* @return string
*/ */
public function encrypt($text, $appid) public function encrypt($text,$appid) {
{
try { try {
//获得16位随机字符串,填充到明文之前 //获得16位随机字符串,填充到明文之前
$random = $this->getRandomStr(); $random = $this->getRandomStr();//"aaaabbbbccccdddd";
$text = $random . pack("N", strlen($text)) . $text . $appid; $text = $random . pack("N", strlen($text)) . $text . $appid;
// 网络字节序
$size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
$iv = substr($this->key, 0, 16); $iv = substr($this->key, 0, 16);
//使用自定义的填充方式对明文进行补位填充
$pkc_encoder = new PKCS7Encoder; $pkc_encoder = new PKCS7Encoder;
$text = $pkc_encoder->encode($text); $text = $pkc_encoder->encode($text);
mcrypt_generic_init($module, $this->key, $iv); $encrypted = openssl_encrypt($text,'AES-256-CBC',substr($this->key, 0, 32),OPENSSL_ZERO_PADDING,$iv);
//加密 return array(ErrorCode::$OK, $encrypted);
$encrypted = mcrypt_generic($module, $text);
mcrypt_generic_deinit($module);
mcrypt_module_close($module);
//print(base64_encode($encrypted));
//使用BASE64对加密后的字符串进行编码
return array(ErrorCode::$OK, base64_encode($encrypted));
} catch (Exception $e) { } catch (Exception $e) {
//print $e; //print $e;
return array(ErrorCode::$EncryptAESError, null); return array(ErrorCode::$EncryptAESError, null);
...@@ -100,29 +88,18 @@ class Prpcrypt ...@@ -100,29 +88,18 @@ class Prpcrypt
} }
/** /**
* 对密文进行解密 * 解密方法
* @param string $encrypted 需要解密的密文 * @param string $encrypted
* @return string 解密得到的明文 * @param $appid
* @return string
*/ */
public function decrypt($encrypted, $appid) public function decrypt($encrypted,$appid) {
{
try { try {
//使用BASE64对需要解密的字符串进行解码
$ciphertext_dec = base64_decode($encrypted);
$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
$iv = substr($this->key, 0, 16); $iv = substr($this->key, 0, 16);
mcrypt_generic_init($module, $this->key, $iv); $decrypted = openssl_decrypt($encrypted,'AES-256-CBC',substr($this->key, 0, 32),OPENSSL_ZERO_PADDING,$iv);
//解密
$decrypted = mdecrypt_generic($module, $ciphertext_dec);
mcrypt_generic_deinit($module);
mcrypt_module_close($module);
} catch (Exception $e) { } catch (Exception $e) {
return array(ErrorCode::$DecryptAESError, null); return array(ErrorCode::$DecryptAESError, null);
} }
try { try {
//去除补位字符 //去除补位字符
$pkc_encoder = new PKCS7Encoder; $pkc_encoder = new PKCS7Encoder;
...@@ -135,16 +112,99 @@ class Prpcrypt ...@@ -135,16 +112,99 @@ class Prpcrypt
$xml_len = $len_list[1]; $xml_len = $len_list[1];
$xml_content = substr($content, 4, $xml_len); $xml_content = substr($content, 4, $xml_len);
$from_appid = substr($content, $xml_len + 4); $from_appid = substr($content, $xml_len + 4);
if (!$appid)
$appid = $from_appid;
//如果传入的appid是空的,则认为是订阅号,使用数据中提取出来的appid
} catch (Exception $e) { } catch (Exception $e) {
//print $e; //print $e;
return array(ErrorCode::$IllegalBuffer, null); return array(ErrorCode::$IllegalBuffer, null);
} }
if ($from_appid != $appid) if ($from_appid != $appid)
return array(ErrorCode::$ValidateAppidError, null); return array(ErrorCode::$ValidateAppidError, null);
return array(0, $xml_content); //不注释上边两行,避免传入appid是错误的情况
return array(0, $xml_content, $from_appid);
} }
// /**
// * 对明文进行加密
// * @param string $text 需要加密的明文
// * @return string 加密后的密文
// */
// public function encrypt($text, $appid)
// {
//
// try {
// //获得16位随机字符串,填充到明文之前
// $random = $this->getRandomStr();
// $text = $random . pack("N", strlen($text)) . $text . $appid;
// // 网络字节序
// $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
// $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
// $iv = substr($this->key, 0, 16);
// //使用自定义的填充方式对明文进行补位填充
// $pkc_encoder = new PKCS7Encoder;
// $text = $pkc_encoder->encode($text);
// mcrypt_generic_init($module, $this->key, $iv);
// //加密
// $encrypted = mcrypt_generic($module, $text);
// mcrypt_generic_deinit($module);
// mcrypt_module_close($module);
//
// //print(base64_encode($encrypted));
// //使用BASE64对加密后的字符串进行编码
// return array(ErrorCode::$OK, base64_encode($encrypted));
// } catch (Exception $e) {
// //print $e;
// return array(ErrorCode::$EncryptAESError, null);
// }
// }
// /**
// * 对密文进行解密
// * @param string $encrypted 需要解密的密文
// * @return string 解密得到的明文
// */
// public function decrypt($encrypted, $appid)
// {
//
// try {
// //使用BASE64对需要解密的字符串进行解码
// $ciphertext_dec = base64_decode($encrypted);
// $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
// $iv = substr($this->key, 0, 16);
// mcrypt_generic_init($module, $this->key, $iv);
//
// //解密
// $decrypted = mdecrypt_generic($module, $ciphertext_dec);
// mcrypt_generic_deinit($module);
// mcrypt_module_close($module);
// } catch (Exception $e) {
// return array(ErrorCode::$DecryptAESError, null);
// }
//
//
// try {
// //去除补位字符
// $pkc_encoder = new PKCS7Encoder;
// $result = $pkc_encoder->decode($decrypted);
// //去除16位随机字符串,网络字节序和AppId
// if (strlen($result) < 16)
// return "";
// $content = substr($result, 16, strlen($result));
// $len_list = unpack("N", substr($content, 0, 4));
// $xml_len = $len_list[1];
// $xml_content = substr($content, 4, $xml_len);
// $from_appid = substr($content, $xml_len + 4);
// } catch (Exception $e) {
// //print $e;
// return array(ErrorCode::$IllegalBuffer, null);
// }
// if ($from_appid != $appid)
// return array(ErrorCode::$ValidateAppidError, null);
// return array(0, $xml_content);
//
// }
/** /**
* 随机生成16位字符串 * 随机生成16位字符串
......
File mode changed
...@@ -17,16 +17,23 @@ class XMLParse ...@@ -17,16 +17,23 @@ class XMLParse
public function extract($xmltext) public function extract($xmltext)
{ {
try { try {
$xml = new DOMDocument(); $xml = new DOMDocument();
$xml->loadXML($xmltext); $xml->loadXML($xmltext);
$array_e = $xml->getElementsByTagName('Encrypt'); $array_e = $xml->getElementsByTagName('Encrypt');
$array_a = $xml->getElementsByTagName('ToUserName'); $array_a = $xml->getElementsByTagName('ToUserName');
$encrypt = $array_e->item(0)->nodeValue; $encrypt = $array_e->item(0)->nodeValue;
$tousername = null;
if(!empty($array_a) && $array_a->item(0)){
$tousername = $array_a->item(0)->nodeValue; $tousername = $array_a->item(0)->nodeValue;
}
return array(0, $encrypt, $tousername); return array(0, $encrypt, $tousername);
} catch (Exception $e) { } catch (Exception $e) {
//debug($e); //debug($e);
//print $e . "\n"; //print $e . "\n";
debug($e->getMessage());
return array(ErrorCode::$ParseXmlError, null, null); return array(ErrorCode::$ParseXmlError, null, null);
} }
} }
......
File mode changed
File mode changed
...@@ -60,32 +60,49 @@ class Prpcrypt ...@@ -60,32 +60,49 @@ class Prpcrypt
{ {
public $key; public $key;
function Prpcrypt( $k ) function __construct($k)
{ {
$this->key = $k; $this->key = $k;
} }
/** /**
* 对密文进行解密 * 加密方法
* @param string $aesCipher 需要解密的密文 * @param string $text
* @param string $aesIV 解密的初始向量 * @param $appid
* @return string 解密得到的明文 * @return string
*/ */
public function decrypt( $aesCipher, $aesIV ) public function encrypt($text,$appid) {
{
try { try {
//获得16位随机字符串,填充到明文之前
$random = $this->getRandomStr();//"aaaabbbbccccdddd";
$text = $random . pack("N", strlen($text)) . $text . $appid;
$iv = substr($this->key, 0, 16);
$pkc_encoder = new PKCS7Encoder;
$text = $pkc_encoder->encode($text);
$encrypted = openssl_encrypt($text,'AES-256-CBC',substr($this->key, 0, 32),OPENSSL_ZERO_PADDING,$iv);
return array(ErrorCode::$OK, $encrypted);
} catch (Exception $e) {
//print $e;
return array(ErrorCode::$EncryptAESError, null);
}
}
$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); /**
*
mcrypt_generic_init($module, $this->key, $aesIV); * 解密方法
* @param $aesCipher
* @param $aesIV
* @return array
*
*/
public function decrypt($aesCipher,$aesIV) {
try {
//解密 $decrypted = openssl_decrypt($aesCipher,'AES-128-CBC',$this->key,OPENSSL_RAW_DATA,$aesIV);
$decrypted = mdecrypt_generic($module, $aesCipher); $decrypted = trim($decrypted);
mcrypt_generic_deinit($module);
mcrypt_module_close($module);
} catch (Exception $e) { } catch (Exception $e) {
return array(ErrorCode::$IllegalBuffer, null);
return array(ErrorCode::$IllegalBuffer, $e->getMessage());
} }
...@@ -95,10 +112,11 @@ class Prpcrypt ...@@ -95,10 +112,11 @@ class Prpcrypt
$result = $pkc_encoder->decode($decrypted); $result = $pkc_encoder->decode($decrypted);
} catch (Exception $e) { } catch (Exception $e) {
//print $e;
return array(ErrorCode::$IllegalBuffer, null); return array(ErrorCode::$IllegalBuffer, null);
} }
return array(0, $result); return array(0, $decrypted);
} }
} }
......
...@@ -21,7 +21,7 @@ class WXBizDataCrypt ...@@ -21,7 +21,7 @@ class WXBizDataCrypt
* @param $sessionKey string 用户在小程序登录后获取的会话密钥 * @param $sessionKey string 用户在小程序登录后获取的会话密钥
* @param $appid string 小程序的appid * @param $appid string 小程序的appid
*/ */
public function WXBizDataCrypt( $appid, $sessionKey) public function __construct( $appid, $sessionKey)
{ {
$this->sessionKey = $sessionKey; $this->sessionKey = $sessionKey;
$this->appid = $appid; $this->appid = $appid;
...@@ -59,10 +59,13 @@ class WXBizDataCrypt ...@@ -59,10 +59,13 @@ class WXBizDataCrypt
} }
$dataObj=json_decode( $result[1] ); $dataObj=json_decode( $result[1] );
debug($dataObj);
if( $dataObj == NULL ) if( $dataObj == NULL )
{ {
return \ErrorCode::$IllegalBuffer; return \ErrorCode::$IllegalBuffer;
} }
if( $dataObj->watermark->appid != $this->appid ) if( $dataObj->watermark->appid != $this->appid )
{ {
return \ErrorCode::$IllegalBuffer; return \ErrorCode::$IllegalBuffer;
......
File mode changed
<?php <?php
namespace wechatkit\HttpFul; namespace wechatkit\HttpFul;
use wechatkit\Config\Config, \Httpful\Request; use Httpful\Request;
use wechatkit\Config\Config;
class HttpFul implements \wechatkit\Core\HttpFul class HttpFul implements \wechatkit\Core\HttpFul
{ {
public $accessToken = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%1%&secret=%2%'; public $accessToken = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%1%&secret=%2%';
public $jsApiTicket = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%1%&type=jsapi'; public $jsApiTicket = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%1%&type=jsapi';
...@@ -43,15 +43,17 @@ ...@@ -43,15 +43,17 @@
//小程序获取用户 //小程序获取用户
public $openOauth2JSAccessToken = 'https://api.weixin.qq.com/sns/jscode2session?appid=%1%&secret=%2%&js_code=%3%&grant_type=authorization_code'; public $openOauth2JSAccessToken = 'https://api.weixin.qq.com/sns/jscode2session?appid=%1%&secret=%2%&js_code=%3%&grant_type=authorization_code';
//小程序第三方授权获取登陆
public $thirdOauth2JsAccessToken = 'https://api.weixin.qq.com/sns/component/jscode2session?appid=%1%&js_code=%2%&grant_type=authorization_code&component_appid=%3%&component_access_token=%4%';
static public $self = NULL; static public $self = NULL;
static public function init() static public function init()
{ {
if (NULL == self::$self) { if (NULL == self::$self) {
return new HttpFul(); self::$self = new HttpFul();
} else {
return self::$self;
} }
return self::$self;
} }
/** /**
...@@ -62,7 +64,8 @@ ...@@ -62,7 +64,8 @@
*/ */
public function handler($name, array $args, $body=[],$bodyType='JSON', $method = 'GET',$ca=[]) public function handler($name, array $args, $body=[],$bodyType='JSON', $method = 'GET',$ca=[])
{ {
if ($this->$name) { //if ($this->$name)
{
switch ($method){ switch ($method){
case 'GET': case 'GET':
return $ret = $this->httpGet($name, $args); return $ret = $this->httpGet($name, $args);
...@@ -82,8 +85,6 @@ ...@@ -82,8 +85,6 @@
break; break;
} }
} else {
return FALSE;
} }
} }
...@@ -96,15 +97,22 @@ ...@@ -96,15 +97,22 @@
*/ */
public function httpGet($urlName, $args) public function httpGet($urlName, $args)
{ {
debug('----'.$urlName);
$searchArr = []; $searchArr = [];
for ($i = 1; $i <= count($args); $i++) { for ($i = 1; $i <= count($args); $i++) {
$searchArr[] = '%' . $i . '%'; $searchArr[] = '%' . $i . '%';
} }
if(startWith($urlName,'http')){
$url = str_replace($searchArr, $args, $urlName);
}else {
$url = str_replace($searchArr, $args, $this->$urlName); $url = str_replace($searchArr, $args, $this->$urlName);
}
$response = Request::get($url) $response = Request::get($url)
->withStrictSSL() ->withStrictSSL()
->send(); ->send();
debug($url.'---'.$response);
return $this->responseHandler($response, $urlName); return $this->responseHandler($response, $urlName);
} }
...@@ -115,9 +123,17 @@ ...@@ -115,9 +123,17 @@
$searchArr[] = '%' . $i . '%'; $searchArr[] = '%' . $i . '%';
} }
debug(serialize($body)); debug("------test---".$name);
debug("------args---".json_encode($args));
debug("------searchArr---".json_encode($searchArr));
if(startWith($name,'http')){
$url = str_replace($searchArr, $args, $name);
}else {
$url = str_replace($searchArr, $args, $this->$name); $url = str_replace($searchArr, $args, $this->$name);
}
$response = '';
if($type == 'JSON'){ if($type == 'JSON'){
$response = Request::post($url) $response = Request::post($url)
...@@ -133,8 +149,6 @@ ...@@ -133,8 +149,6 @@
->send(); ->send();
} }
return $this->responseHandler($response, $name); return $this->responseHandler($response, $name);
} }
...@@ -143,7 +157,11 @@ ...@@ -143,7 +157,11 @@
for ($i = 1; $i <= count($args); $i++) { for ($i = 1; $i <= count($args); $i++) {
$searchArr[] = '%' . $i . '%'; $searchArr[] = '%' . $i . '%';
} }
if(startWith($name,'http')){
$url = str_replace($searchArr, $args, $name);
}else {
$url = str_replace($searchArr, $args, $this->$name); $url = str_replace($searchArr, $args, $this->$name);
}
$response = \Httpful\Request::post($url) $response = \Httpful\Request::post($url)
->withStrictSSL() ->withStrictSSL()
...@@ -156,16 +174,18 @@ ...@@ -156,16 +174,18 @@
} }
public function httpCa($name,$body,$ca){ public function httpCa($name,$body,$ca){
debug('url:'.$this->$name);
debug($body); if(startWith($name,'http')){
//debug(file_get_contents($this->$name)); $url = str_replace('', '', $name);
$response = \Httpful\Request::post(str_replace(' ','',$this->$name)) }else {
$url = str_replace('', '', $this->$name);
}
$response = \Httpful\Request::post($url)
->authenticateWithCert($ca['cert_file'], $ca['key_file'], $ca['ca_file']) ->authenticateWithCert($ca['cert_file'], $ca['key_file'], $ca['ca_file'])
->withStrictSSL() ->withStrictSSL()
->body($body) ->body($body)
->send(); ->send();
// debug('pay_result==='.$response);
return $this->responseHandler($response, $name); return $this->responseHandler($response, $name);
} }
...@@ -184,7 +204,6 @@ ...@@ -184,7 +204,6 @@
if($this->is_Json($response->raw_body)){ if($this->is_Json($response->raw_body)){
$body = json_decode($response->raw_body,1); $body = json_decode($response->raw_body,1);
debug($body);
}else if($this->is_Xml($response->raw_body)){ }else if($this->is_Xml($response->raw_body)){
$body = json_decode(json_encode(simplexml_load_string($response->raw_body, 'SimpleXMLElement', LIBXML_NOCDATA)),1); $body = json_decode(json_encode(simplexml_load_string($response->raw_body, 'SimpleXMLElement', LIBXML_NOCDATA)),1);
debug($body); debug($body);
...@@ -253,4 +272,4 @@ ...@@ -253,4 +272,4 @@
return true; return true;
} }
} }
} }
File mode changed
File mode changed
File mode changed
File mode changed
File mode changed
...@@ -8,6 +8,10 @@ ...@@ -8,6 +8,10 @@
namespace wechatkit\Mini; namespace wechatkit\Mini;
use Lestore\Wxauthcenter\Services\AccessTokenService;
use wechatkit\HttpFul\HttpFul;
use wechatkit\Run\BaseRun;
/** /**
* *
* 微信授权登陆 * 微信授权登陆
...@@ -15,6 +19,60 @@ namespace wechatkit\Mini; ...@@ -15,6 +19,60 @@ namespace wechatkit\Mini;
* Class Login * Class Login
* @package wechatkit\Mini * @package wechatkit\Mini
*/ */
class Login{ class Login extends BaseRun{
public function mpAuthorize($jsCode){
return $this->jscode2session($jsCode);
}
/**
*
* 微信小程序登陆
*
* @param $jsCode
* @return bool|mixed|null
* @internal param $appid
* @internal param $secret
*/
public function jscode2session($jsCode){
if ($jsCode){
$appid = $this->options['appid'];
$componentAppid = $this->options['open']['appid'];
$componentAccessToken = AccessTokenService::componentAccessToken($this->options);
$result = null;
if($componentAccessToken) {
$result = $this->accessTokenFromJsCode($jsCode, $appid, $componentAppid, $componentAccessToken);
}
return $result;
}
return null;
}
/**
*
* 通过微信小程序code获取用户openid
*
* @param $jsCode
* @param $appid
* @param $componentAppid
* @param $componentAccessToken
* @return bool|mixed|null
* @internal param $code
* @internal param HttpFul $httpFul
*
*/
protected function accessTokenFromJsCode($jsCode,$appid,$componentAppid,$componentAccessToken){
if(!$jsCode)
return null;
$result = HttpFul::init()->handler('thirdOauth2JsAccessToken', [$appid,$jsCode,$componentAppid,$componentAccessToken]);
return $result;
}
} }
File mode changed
File mode changed
File mode changed
File mode changed
...@@ -2,10 +2,12 @@ ...@@ -2,10 +2,12 @@
namespace wechatkit\Oauth2; namespace wechatkit\Oauth2;
use Lestore\Wxauthcenter\Services\AccessTokenService;
use wechatkit\Config\Config; use wechatkit\Config\Config;
use wechatkit\HttpFul\HttpFul; use wechatkit\HttpFul\HttpFul;
use wechatkit\Run\BaseRun;
class Oauth2 class Oauth2 extends BaseRun
{ {
/** /**
...@@ -42,158 +44,91 @@ ...@@ -42,158 +44,91 @@
return null; return null;
$result = $httpFul->handler('openOauth2JSAccessToken', [$appid,$secret,$code]); $result = $httpFul->handler('openOauth2JSAccessToken', [$appid,$secret,$code]);
$this->result($result);
return $result; return $result;
} }
/** /**
* 公众号授权
*
* @param $redirect_uri * @param $redirect_uri
* @param string $scope * @param string $scope
* @param string $state * @param string $state
* @param bool $auto * @param bool $auto
* @return bool|mixed * @return bool|mixed
*/ */
public function mpAuthorize_v2($redirect_uri =null, $scope = 'snsapi_userinfo', $state = '', $auto = TRUE) public function mpAuthorize($redirect_uri =null, $scope = 'snsapi_userinfo', $state = '', $auto = TRUE)
{ {
if(null == $redirect_uri){ if(null == $redirect_uri){
$redirect_uri = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; $redirect_uri = httpType().$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
} }
if (Config::$type == 'O') { //开放平台授权
if (!$_GET['code']) {
header('location:' . sprintf( if ($this->authType == 'O') { //开放平台授权
'https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s&component_appid=%s#wechat_redirect', if (!isset($_GET['code']) || !$_GET['code']) {
Config::$options['appid'], $url = sprintf(
'https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s&component_appid=%s&connect_redirect=1#wechat_redirect',
$this->options['appid'],
urlencode($redirect_uri), urlencode($redirect_uri),
$scope, $scope,
$state, $state,
Config::$options['open']['appid'] $this->options['open']['appid']
)); );
header('location:' .$url,TRUE,301);
exit(255); exit(255);
} else { } else {
if ($auto) { if ($auto) {
$result = $this->accessTokenFormCode($_GET['code'], new HttpFul()); $result = $this->accessTokenFormCode($_GET['code']);
if ('snsapi_base' == $scope) { if(empty($result)){
return $result['openid']; return null;
}
// asyncLog('---openid--'.json_encode($result));
$info = $this->oAuth2AccessTokenUserInfo($result['openid'], new HttpFul());
return $info;
}
}
}
}
/**
* @param $redirect_uri
* @param string $scope
* @param string $state
* @param bool $auto
* @return bool|mixed
*/
public function mpAuthorize($redirect_uri =null, $scope = 'snsapi_userinfo', $state = '', $auto = TRUE)
{
if(null == $redirect_uri){
$redirect_uri = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
} }
if (Config::$type == 'A') { //普通授权
if (!$_GET['code']) {
header('location:' . sprintf(
'https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s#wechat_redirect',
Config::$options['appid'],
urlencode($redirect_uri),
$scope,
$state
));
exit(255);
} else {
if ($auto) {
$result = $this->accessTokenFormCode($_GET['code'], new HttpFul());
if ('snsapi_base' == $scope) { if ('snsapi_base' == $scope) {
return $result['openid']; debug($result['openid']);
} return ['openid' => $result['openid']];
return $this->oAuth2AccessTokenUserInfo($result['openid'], new HttpFul());
}
} }
} else if (Config::$type == 'O') { //开放平台授权
$info = $this->oAuth2AccessTokenUserInfo($result['openid'],$result['access_token']);
if (!$_GET['code']) { return $info;
header('location:' . sprintf(
'https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s&component_appid=%s#wechat_redirect',
Config::$options['appid'],
urlencode($redirect_uri),
$scope,
$state,
Config::$options['open']['appid']
));
exit(255);
} else {
if ($auto) {
$result = $this->accessTokenFormCode($_GET['code'], new HttpFul());
if ('snsapi_base' == $scope) {
return $result['openid'];
}
return $this->oAuth2AccessTokenUserInfo($result['openid'], new HttpFul());
} }
} }
} }
return null;
} }
/** /**
*
* 通过code获取accessToken
*
* @param $code * @param $code
* @param HttpFul $httpFul
* @return bool|mixed * @return bool|mixed
*/ */
protected function accessTokenFormCode($code, HttpFul $httpFul) protected function accessTokenFormCode($code)
{ {
if(Config::$type == 'A'){ $result = null;
$result = $httpFul->handler('oauth2AccessToken', [$code]); if($this->authType == 'O') {
}else{ $componentAccessToken = AccessTokenService::componentAccessToken($this->options);
$result = $httpFul->handler('openOauth2AccessToken', [Config::$options['appid'],$code,Config::$options['open']['appid'],Config::$componentAccessToken]);
}
$this->result($result);
if (!empty($componentAccessToken)) {
$result = HttpFul::init()->handler('openOauth2AccessToken'
, [$this->options['appid'], $code, $this->options['open']['appid'], $componentAccessToken]);
}
}
return $result; return $result;
} }
/** /**
* @param $openid * @param $openid
* @param HttpFul $httpFul * @param $accessToken //登录使用的accesstoken
* @return bool|mixed * @return bool|mixed
*/ */
protected function oAuth2AccessTokenUserInfo($openid, HttpFul $httpFul) protected function oAuth2AccessTokenUserInfo($openid,$accessToken)
{ {
$result = $httpFul->handler('oAuth2AccessTokenUserInfo', [$this->access_token, $openid]); $result = HttpFul::init()->handler('oAuth2AccessTokenUserInfo', [$accessToken, $openid]);
$this->result($result);
return $result; return $result;
} }
/**
* @param array $result
*/
public function result($result)
{
if(!$result && !is_array($result)){
return false;
}
foreach ($result as $key => $value) {
$this->$key = $value;
}
}
} }
...@@ -2,17 +2,14 @@ ...@@ -2,17 +2,14 @@
namespace wechatkit\Open; namespace wechatkit\Open;
use Lestore\Wxauthcenter\Services\AccessTokenService;
use wechatkit\Config\Config; use wechatkit\Config\Config;
use wechatkit\HttpFul\HttpFul; use wechatkit\HttpFul\HttpFul;
use wechatkit\Run\BaseRun;
class Open class Open extends BaseRun
{ {
public function __construct()
{
if (Config::$type != 'O') {
throw new \Exception('this api must open type');
}
}
//网页登入 //网页登入
public function webOAuth2($url = NULL, $type = 'URL', $state = NULL, $wxLoginConfig = ['style' => 'black', public function webOAuth2($url = NULL, $type = 'URL', $state = NULL, $wxLoginConfig = ['style' => 'black',
'href' => NULL]) 'href' => NULL])
...@@ -63,33 +60,62 @@ ...@@ -63,33 +60,62 @@
} }
//微信公众号授权e /**
public function authorizer($url=null){ * 微信公众号/小程序授权
if(!$_GET['auth_code']){ * @param $authType 1则商户扫码后,手机端仅展示公众号、2表示仅展示小程序,3表示公众号和小程序都展示,如果为未指定,则默认小程序和公众号都展示
$preAuthCode = HttpFul::init()->handler('preAuthCode',[Config::$componentAccessToken],['component_appid'=>Config::$options['open']['appid']],'JSON','POST'); * @param null $url
if($preAuthCode){ * @return bool|mixed
if(null == $url){ */
public function authorizer($authType=3,$url=null){
$http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://'; if(!array_key_exists('auth_code',$_GET) || !$_GET['auth_code']){
$url = $http_type.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; $componentAppid = $this->options['open']['appid'];
$componentToken = AccessTokenService::componentAccessToken($this->options);
if(empty($componentToken)){
return false;
} }
header(sprintf('Location:https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=%s&pre_auth_code=%s&redirect_uri=%s',Config::$options['open']['appid'],$preAuthCode['pre_auth_code'],urlencode($url))); $preAuthCode = HttpFul::init()->handler('preAuthCode'
,[$componentToken]
,['component_appid'=>$componentAppid],'JSON','POST');
if($preAuthCode){
if(null == $url){
$url = httpType().$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}
$url = sprintf('Location:https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=%s&pre_auth_code=%s&redirect_uri=%s'
,$componentAppid,$preAuthCode['pre_auth_code'],urlencode($url));
$url .= '&auth_type='.$authType;
header($url);
exit(255); exit(255);
} }
return false;
}else{ }else{
return $this->authinfo($_GET['auth_code']); return $this->authinfo($_GET['auth_code']);
} }
} }
//拉公众号信息
/**
* 拉公众号详细信息
*
* @param $authorizer_appid
* @return bool|mixed
*/
public function mpInfo($authorizer_appid){ public function mpInfo($authorizer_appid){
//$apiGetAuthorizerInfo
return HttpFul::init()->handler('apiGetAuthorizerInfo',[Config::$componentAccessToken],['component_appid'=>Config::$options['open']['appid'],'authorizer_appid'=>$authorizer_appid],'JSON','POST'); return HttpFul::init()->handler('apiGetAuthorizerInfo',[AccessTokenService::componentAccessToken($this->options)]
,['component_appid'=>$this->options['open']['appid'],'authorizer_appid'=>$authorizer_appid],'JSON','POST');
} }
/**
* 获取公众号授权信息
* @param $auth_code
* @return bool|mixed
*/
public function authinfo($auth_code){ public function authinfo($auth_code){
return HttpFul::init()->handler('apiQueryAuth',[Config::$componentAccessToken],['component_appid'=>Config::$options['open']['appid'],'authorization_code'=>$auth_code],'JSON','POST'); return HttpFul::init()->handler('apiQueryAuth',[AccessTokenService::componentAccessToken($this->options)]
,['component_appid'=>$this->options['open']['appid'],'authorization_code'=>$auth_code],'JSON','POST');
} }
} }
\ No newline at end of file
File mode changed
File mode changed
<?php <?php
namespace wechatkit\Run; namespace wechatkit\Run;
use wechatkit\Config, wechatkit\AccessToken; use wechatkit\Config, wechatkit\AccessToken;
class Run implements \wechatkit\Core\Run{ class Run implements \wechatkit\Core\Run{
static $self = NULL; static $self = NULL;
static $namespace;
public $options;
public $cacheConfig;
/** /**
* @param $type * @param $type string
* @param array $options [ appid secret mch_id key cert_file key_file ] * @param array $options [ appid secret mch_id key cert_file key_file ]
* @param array $config * @param array $config
* @return bool * @return $this
*/ */
public function App($type, array $options = [], array $config = []) public function App($type, array $options = [], array $config = [],array $authThird = [])
{ {
if (!in_array($type, Config\Config::$typeList)) {
return FALSE;
} else {
Config\Config::$type = $type;
}
Config\Config::$options = array_merge(Config\Config::$options, $options); Config\Config::$type = $type;
Config\Config::$config = array_merge(Config\Config::$config, $config); $this->options = array_merge(Config\Config::$options, $options);
$this->cacheConfig = array_merge(Config\Config::$config, $config);
$this->AccessToken()->certificate(false); // debug($this->options);
// $this->AccessToken()->certificate(false);
return $this; return $this;
} }
static public function go() static public function go($class=null)
{ {
if (NULL == self::$self) { if (NULL == self::$self) {
return new \wechatkit\Run\Run(); $instance = new \wechatkit\Run\Run();
self::$self = $instance;
} else { } else {
return self::$self; $instance = self::$self;
} }
if($class)
self::$namespace = $class;
else self::$namespace = null;
return $instance;
} }
public function getAccessToken($appid){ public function getAccessToken($appid){
...@@ -63,12 +73,14 @@ ...@@ -63,12 +73,14 @@
* @return mixed * @return mixed
* @throws \Exception * @throws \Exception
*/ */
public function __call($name, $arguments) public function __call($name = null, $arguments)
{ {
if (is_dir(__DIR__ . DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR . $name)) { $namespace = self::$namespace?:$name;
$className = '\wechatkit\\' . $name . '\\' . $name;
return new $className(); if (is_dir(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . $namespace)) {
$className = '\wechatkit\\' . $namespace . '\\' . $name;
return new $className($this->options);
} else { } else {
throw new \Exception('class ' . $name . ' not found!'); throw new \Exception('class ' . $name . ' not found!');
} }
...@@ -108,4 +120,4 @@ ...@@ -108,4 +120,4 @@
} }
} }
\ No newline at end of file
...@@ -2,57 +2,64 @@ ...@@ -2,57 +2,64 @@
namespace wechatkit\Service; namespace wechatkit\Service;
use wechatkit\Config\Config;
use wechatkit\CryptMsg\CryptMsg; use wechatkit\CryptMsg\CryptMsg;
use wechatkit\Run\BaseRun;
class Service { class Service extends BaseRun {
public $postData; public $postData;
public $xml; public $xml;
public $xmlArray; public $xmlArray;
public function __construct() public function handle($postData){
{
$this->postData = file_get_contents('php://input'); $this->postData = $postData;//file_get_contents('php://input');
if(!$this->postData){ if(!$this->postData){
exit(255); return $this;
} }
debug('=====================执行微信消息解密流程===================');
debug($this->postData);
$cryptmsg = new CryptMsg(); $cryptmsg = new CryptMsg();
debug('=====================进入service.php'); if($this->authType == 'A'){
if(Config::$type == 'A'){
$this->xml = simplexml_load_string($this->postData, 'SimpleXMLElement', LIBXML_NOCDATA); $this->xml = simplexml_load_string($this->postData, 'SimpleXMLElement', LIBXML_NOCDATA);
if($this->xml->Encrypt){ if($this->xml->Encrypt){
$cryptmsg->WXBizMsgCrypt(Config::$options['token'],Config::$options['encodingAesKey'], Config::$options['appid']); $cryptmsg->WXBizMsgCrypt($this->options['token'],$this->options['encodingAesKey'], $this->options['appid']);
$ret = $cryptmsg->decryptMsg($_GET['msg_signature'], $_GET['timestamp'], $_GET['nonce'], $this->postData, $this->xml); $ret = $cryptmsg->decryptMsg($_GET['msg_signature'], $_GET['timestamp'], $_GET['nonce'], $this->postData, $this->xml);
$this->xml = simplexml_load_string($this->xml, 'SimpleXMLElement', LIBXML_NOCDATA); $this->xml = simplexml_load_string($this->xml, 'SimpleXMLElement', LIBXML_NOCDATA);
} }
$this->xmlArray = json_decode(json_encode($this->xml),1); $this->xmlArray = json_decode(json_encode($this->xml),1);
}elseif(Config::$type == 'O'){ }elseif($this->authType == 'O'){
$cryptmsg->WXBizMsgCrypt(Config::$options['open']['token'],Config::$options['open']['encodingAesKey'], Config::$options['open']['appid']); $cryptmsg->WXBizMsgCrypt($this->options['open']['token'],$this->options['open']['encodingAesKey'], $this->options['open']['appid']);
$result = $cryptmsg->decryptMsg($_GET['msg_signature'], $_GET['timestamp'], $_GET['nonce'], $this->postData, $this->xml); $result = $cryptmsg->decryptMsg($_GET['msg_signature'], $_GET['timestamp'], $_GET['nonce'], $this->postData, $this->xml);
if(0 != $result){ if(0 != $result){
#解密失败 #解密失败
if(function_exists('debug')){ if(function_exists('debug')){
debug('微信数据解密失败'.$result); debug('微信数据解密失败'.$result);
} }
}else{ }else{
debug('=====================进入service.php');
$this->xml = simplexml_load_string($this->xml, 'SimpleXMLElement', LIBXML_NOCDATA); $this->xml = simplexml_load_string($this->xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$this->xmlArray = json_decode(json_encode($this->xml),1); $this->xmlArray = json_decode(json_encode($this->xml),1);
debug("===========微信信息解密成功==========".PHP_EOL.json_encode($this->xmlArray));
} }
}else{ }else{
$this->xml = simplexml_load_string($this->postData, 'SimpleXMLElement', LIBXML_NOCDATA); $this->xml = simplexml_load_string($this->postData, 'SimpleXMLElement', LIBXML_NOCDATA);
$xmlArray = $this->xmlArray = json_decode(json_encode($this->xml),1); $this->xmlArray = json_decode(json_encode($this->xml),1);
debug('================$xml'.$xml.'------------$xmlArray'.$xmlArray);
} }
return $this;
} }
public function run(){ public function decryptData(){
return $this->xml; return $this->xml;
} }
public function runArray(){ public function decryptArray(){
return $this->xmlArray; return $this->xmlArray;
} }
......
File mode changed
File mode changed
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!