|
|
@@ -1,134 +1,6 @@
|
|
|
<?php
|
|
|
namespace open\controllers;
|
|
|
-use common\services\xhWxOpenService;
|
|
|
-
|
|
|
-use Yii;
|
|
|
-use yii\web\Controller;
|
|
|
-use yii\filters\AccessControl;
|
|
|
-use yii\helpers\Json;
|
|
|
-use common\components\weixinUtil;
|
|
|
-use common\components\configDict;
|
|
|
-use common\components\stringUtil;
|
|
|
-use common\models\xhWxOpen;
|
|
|
-use common\models\xhUser;
|
|
|
-use common\components\weixinOpenUtil;
|
|
|
-use common\models\xhMerchant;
|
|
|
-use common\services\xhMerchantService;
|
|
|
-use common\models\xhAdmin;
|
|
|
-use common\services\xhCommonService;
|
|
|
-use back\controllers\MerchantController;
|
|
|
-use common\models\xhInviteCode;
|
|
|
-use common\models\xhInviteList;
|
|
|
-use common\components\util;
|
|
|
|
|
|
class WxAuthController extends PublicController{
|
|
|
|
|
|
-
|
|
|
-
|
|
|
- public function actionApi()
|
|
|
- {
|
|
|
- $openData = file_get_contents('php://input');
|
|
|
- if (isset ( $openData ) == false || empty($openData)){
|
|
|
- util::stop('has no post data');
|
|
|
- }
|
|
|
- $weixinSecret = Yii::getAlias("@vendor/weixinSecret");
|
|
|
- require_once($weixinSecret.'/wxBizMsgCrypt.php');
|
|
|
- $get = Yii::$app->request->get();
|
|
|
- $encryptMsg = $openData;
|
|
|
- $openId = configDict::getConfig('openId');
|
|
|
- $open = xhWxOpenService::getById($openId);
|
|
|
- $encodingAesKey = $open['aesKey'];
|
|
|
- $token = $open['token'];
|
|
|
- $appId = $open['appId'];
|
|
|
- $signature = isset($get['signature']) ? $get['signature'] : '';
|
|
|
- $timestamp = isset($get['timestamp']) ? $get['timestamp'] : '';
|
|
|
- $nonce = isset($get['nonce']) ? $get['nonce'] : '';
|
|
|
- $encrypt_type = isset($get['encrypt_type']) ? $get['encrypt_type'] : '';
|
|
|
- $msg_signature = isset($get['msg_signature']) ? $get['msg_signature'] : '';
|
|
|
- $xml_tree = new \DOMDocument();
|
|
|
- $xml_tree->loadXML($encryptMsg);
|
|
|
- $array_e = $xml_tree->getElementsByTagName('Encrypt');
|
|
|
- $encrypt = $array_e->item(0)->nodeValue;
|
|
|
- $format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
|
|
|
- $from_xml = sprintf($format, $encrypt);
|
|
|
-
|
|
|
- $formString = '1:'.$token.' 2:'.$encodingAesKey.' 3:'.$appId.' 4:'.$msg_signature.' 5:'.$timestamp.' 6:'.$nonce.' 7:'.$from_xml;
|
|
|
- Yii::warning("formString:".$formString);
|
|
|
-
|
|
|
- //第三方收到公众号平台发送的消息
|
|
|
- $pc = new \WXBizMsgCrypt($token, $encodingAesKey, $appId);
|
|
|
- $msg = '';//解密后的消息
|
|
|
- $errCode = $pc->decryptMsg($msg_signature, $timestamp, $nonce, $from_xml, $msg);
|
|
|
- if ($errCode != 0) {
|
|
|
- Yii::warning("解密未成功,错误代码:".$errCode);
|
|
|
- Yii::$app->end();
|
|
|
- }
|
|
|
- $postObj = simplexml_load_string ( $msg, 'SimpleXMLElement', LIBXML_NOCDATA );
|
|
|
- $infoType = $postObj->InfoType;
|
|
|
- switch($infoType){
|
|
|
- //推送 componentVerifyTicket
|
|
|
- case 'component_verify_ticket':
|
|
|
- $componentVerifyTicket = $postObj->ComponentVerifyTicket;
|
|
|
- if(!empty($componentVerifyTicket)){
|
|
|
- $oData = [];
|
|
|
- $oData['verifyTicket'] = (string)$componentVerifyTicket;
|
|
|
- $oData['verifyTicketTime'] = date("Y-m-d H:i:s",$timestamp+600);//每10分钟(600秒)推一次
|
|
|
- xhWxOpenService::updateById($openId, $oData);
|
|
|
- Yii::warning("component_verify_ticket:".(string)$componentVerifyTicket);
|
|
|
- util::stop('success');
|
|
|
- }
|
|
|
- break;
|
|
|
- //取消授权通知
|
|
|
- case 'unauthorized':
|
|
|
- $AppId = $postObj->AppId;
|
|
|
- $AuthorizerAppid = $postObj->AuthorizerAppid;
|
|
|
- $merchant = xhMerchantService::getByAppId($AuthorizerAppid);
|
|
|
- if(!empty($merchant)){
|
|
|
- $merchantId = $merchant['id'];
|
|
|
- xhMerchantService::updateById($merchantId, ['status' => 5]);//自主冻结
|
|
|
- }
|
|
|
- break;
|
|
|
- //授权成功通知
|
|
|
- case 'authorized':
|
|
|
- $AppId = $postObj->AppId;//第三方平台appid
|
|
|
- $CreateTime = $postObj->CreateTime;//公众号
|
|
|
- $AuthorizerAppid = $postObj->AuthorizerAppid;//授权码,可用于换取公众号的接口调用凭据,详细见上面的说明
|
|
|
- $AuthorizationCode = $postObj->AuthorizationCode;
|
|
|
- $AuthorizationCodeExpiredTime = $postObj->AuthorizationCodeExpiredTime;//授权码过期时间
|
|
|
- break;
|
|
|
- //授权更新通知
|
|
|
- case 'updateauthorized':
|
|
|
- $AppId = $postObj->AppId;
|
|
|
- $CreateTime = $postObj->CreateTime;
|
|
|
- $AuthorizerAppid = $postObj->AuthorizerAppid;
|
|
|
- $AuthorizationCode = $postObj->AuthorizationCode;
|
|
|
- $AuthorizationCodeExpiredTime = $postObj->AuthorizationCodeExpiredTime;
|
|
|
- $authorizer = weixinOpenUtil::getAuthorizer($AuthorizationCode);
|
|
|
- if(isset($authorizer['authorization_info']) == false){
|
|
|
- $msg = '授权回调获取到auth_code,但未换取公众号的接口调用凭据和授权信息!!';
|
|
|
- Yii::warning($msg, __METHOD__);
|
|
|
- Yii::$app->end();
|
|
|
- }
|
|
|
- $info = $authorizer['authorization_info'];
|
|
|
- $authorizer_appid = $info['authorizer_appid'];
|
|
|
- $authorizer_access_token = $info['authorizer_access_token'];
|
|
|
- $expires_in = $info['expires_in'];//7200
|
|
|
- $authorizer_refresh_token = $info['authorizer_refresh_token'];
|
|
|
- $merchant = xhMerchantService::getByAppId($authorizer_appid);
|
|
|
- if(empty($merchant)){
|
|
|
- return false;
|
|
|
- }
|
|
|
- $merchantId = $merchant['id'];
|
|
|
- $wxData = [];
|
|
|
- $wxData['wxAccessToken'] = $authorizer_access_token;
|
|
|
- $wxData['wxRefreshToken'] = $authorizer_refresh_token;
|
|
|
- $wxData['wxAccessTokenTime'] = date("Y-m-d H:i:s",(time() + $expires_in - 100));
|
|
|
- $wxData['status'] = 1;
|
|
|
- xhMerchantService::updateById($merchantId, $wxData);
|
|
|
- break;
|
|
|
- default:
|
|
|
- }
|
|
|
- Yii::warning("---wx open data end--- \n");
|
|
|
- }
|
|
|
-
|
|
|
}
|