shish 6 лет назад
Родитель
Сommit
9917d9d43a
1 измененных файлов с 70 добавлено и 72 удалено
  1. 70 72
      app/www/controllers/WxOpenController.php

+ 70 - 72
app/www/controllers/WxOpenController.php

@@ -21,15 +21,13 @@ use common\services\xhSetMealService;
  */
 class WxOpenController extends PublicController
 {
-
+	
 	public $withoutLogin = ['callback-create-first-merchant'];
-
+	
 	public function actionGetAuthUrl()
 	{
-		$redirect = "https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=12548&pre_auth_code=55665&redirect_uri=6789";
-		util::success(['url' => $redirect]);
-
-		$open = xhWxOpenService::getById(1);
+		$id = 1;
+		$open = xhWxOpenService::getById($id);
 		$pre = weixinOpenUtil::getPreAuthCode($open);
 		$appId = $open['appId'];
 		$oData = [];
@@ -43,7 +41,7 @@ class WxOpenController extends PublicController
 		$redirect = "https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid={$appId}&pre_auth_code={$pre}&redirect_uri={$url}";
 		util::success(['url' => $redirect]);
 	}
-	
+
 	public function actionCheckMerchantName()
 	{
 		$get = Yii::$app->request->get();
@@ -453,102 +451,102 @@ class WxOpenController extends PublicController
 	public function actionApi()
 	{
 		$openData = file_get_contents('php://input');
-		if (isset ( $openData ) == false || empty($openData)){
+		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();
+		$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);
+		$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);
+		$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);
+		$pc = new \WXBizMsgCrypt($token, $encodingAesKey, $appId);
+		$msg = '';//解密后的消息
+		$errCode = $pc->decryptMsg($msg_signature, $timestamp, $nonce, $from_xml, $msg);
 		if ($errCode != 0) {
-			Yii::warning("解密未成功,错误代码:".$errCode);
+			Yii::warning("解密未成功,错误代码:" . $errCode);
 			Yii::$app->end();
 		}
-		$postObj					= simplexml_load_string ( $msg, 'SimpleXMLElement', LIBXML_NOCDATA );
-		$infoType					= $postObj->InfoType;
-		switch($infoType){
+		$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秒)推一次
+				$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);
+					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'];
+				$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;//授权码过期时间
+				$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){
+				$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)){
+				$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;
+				$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: