WxAuthController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace openend\controllers;
  3. use common\services\xhWxOpenService;
  4. use Yii;
  5. use yii\web\Controller;
  6. use yii\filters\AccessControl;
  7. use yii\helpers\Json;
  8. use common\components\weixinUtil;
  9. use common\components\configDict;
  10. use common\components\stringUtil;
  11. use common\models\xhWxOpen;
  12. use common\models\xhUser;
  13. use common\components\weixinOpenUtil;
  14. use common\models\xhMerchant;
  15. use common\services\xhMerchantService;
  16. use common\models\xhAdmin;
  17. use common\services\xhCommonService;
  18. use backend\controllers\MerchantController;
  19. use common\models\xhInviteCode;
  20. use common\models\xhInviteList;
  21. use common\components\util;
  22. class WxAuthController extends PublicController{
  23. public $enableCsrfValidation = false;
  24. public function actionApi()
  25. {
  26. $openData = file_get_contents('php://input');
  27. if (isset ( $openData ) == false || empty($openData)){
  28. util::stop('has no post data');
  29. }
  30. $weixinSecret = Yii::getAlias("@vendor/weixinSecret");
  31. require_once($weixinSecret.'/wxBizMsgCrypt.php');
  32. $get = Yii::$app->request->get();
  33. $encryptMsg = $openData;
  34. $openId = configDict::getConfig('openId');
  35. $open = xhWxOpenService::getById($openId);
  36. $encodingAesKey = $open['aesKey'];
  37. $token = $open['token'];
  38. $appId = $open['appId'];
  39. $signature = isset($get['signature']) ? $get['signature'] : '';
  40. $timestamp = isset($get['timestamp']) ? $get['timestamp'] : '';
  41. $nonce = isset($get['nonce']) ? $get['nonce'] : '';
  42. $encrypt_type = isset($get['encrypt_type']) ? $get['encrypt_type'] : '';
  43. $msg_signature = isset($get['msg_signature']) ? $get['msg_signature'] : '';
  44. $xml_tree = new \DOMDocument();
  45. $xml_tree->loadXML($encryptMsg);
  46. $array_e = $xml_tree->getElementsByTagName('Encrypt');
  47. $encrypt = $array_e->item(0)->nodeValue;
  48. $format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
  49. $from_xml = sprintf($format, $encrypt);
  50. $formString = '1:'.$token.' 2:'.$encodingAesKey.' 3:'.$appId.' 4:'.$msg_signature.' 5:'.$timestamp.' 6:'.$nonce.' 7:'.$from_xml;
  51. Yii::warning("formString:".$formString);
  52. //第三方收到公众号平台发送的消息
  53. $pc = new \WXBizMsgCrypt($token, $encodingAesKey, $appId);
  54. $msg = '';//解密后的消息
  55. $errCode = $pc->decryptMsg($msg_signature, $timestamp, $nonce, $from_xml, $msg);
  56. if ($errCode != 0) {
  57. Yii::warning("解密未成功,错误代码:".$errCode);
  58. Yii::$app->end();
  59. }
  60. $postObj = simplexml_load_string ( $msg, 'SimpleXMLElement', LIBXML_NOCDATA );
  61. $infoType = $postObj->InfoType;
  62. switch($infoType){
  63. //推送 componentVerifyTicket
  64. case 'component_verify_ticket':
  65. $componentVerifyTicket = $postObj->ComponentVerifyTicket;
  66. if(!empty($componentVerifyTicket)){
  67. $oData = [];
  68. $oData['verifyTicket'] = (string)$componentVerifyTicket;
  69. $oData['verifyTicketTime'] = date("Y-m-d H:i:s",$timestamp+600);//每10分钟(600秒)推一次
  70. xhWxOpenService::updateById($openId, $oData);
  71. Yii::warning("component_verify_ticket:".(string)$componentVerifyTicket);
  72. util::stop('success');
  73. }
  74. break;
  75. //取消授权通知
  76. case 'unauthorized':
  77. $AppId = $postObj->AppId;
  78. $AuthorizerAppid = $postObj->AuthorizerAppid;
  79. $merchant = xhMerchantService::getByAppId($AuthorizerAppid);
  80. if(!empty($merchant)){
  81. $merchantId = $merchant['id'];
  82. xhMerchantService::updateById($merchantId, ['status' => 5]);//自主冻结
  83. }
  84. break;
  85. //授权成功通知
  86. case 'authorized':
  87. $AppId = $postObj->AppId;//第三方平台appid
  88. $CreateTime = $postObj->CreateTime;//公众号
  89. $AuthorizerAppid = $postObj->AuthorizerAppid;//授权码,可用于换取公众号的接口调用凭据,详细见上面的说明
  90. $AuthorizationCode = $postObj->AuthorizationCode;
  91. $AuthorizationCodeExpiredTime = $postObj->AuthorizationCodeExpiredTime;//授权码过期时间
  92. break;
  93. //授权更新通知
  94. case 'updateauthorized':
  95. $AppId = $postObj->AppId;
  96. $CreateTime = $postObj->CreateTime;
  97. $AuthorizerAppid = $postObj->AuthorizerAppid;
  98. $AuthorizationCode = $postObj->AuthorizationCode;
  99. $AuthorizationCodeExpiredTime = $postObj->AuthorizationCodeExpiredTime;
  100. $authorizer = weixinOpenUtil::getAuthorizer($AuthorizationCode);
  101. if(isset($authorizer['authorization_info']) == false){
  102. $msg = '授权回调获取到auth_code,但未换取公众号的接口调用凭据和授权信息!!';
  103. Yii::warning($msg, __METHOD__);
  104. Yii::$app->end();
  105. }
  106. $info = $authorizer['authorization_info'];
  107. $authorizer_appid = $info['authorizer_appid'];
  108. $authorizer_access_token = $info['authorizer_access_token'];
  109. $expires_in = $info['expires_in'];//7200
  110. $authorizer_refresh_token = $info['authorizer_refresh_token'];
  111. $merchant = xhMerchantService::getByAppId($authorizer_appid);
  112. if(empty($merchant)){
  113. return false;
  114. }
  115. $merchantId = $merchant['id'];
  116. $wxData = [];
  117. $wxData['wxAccessToken'] = $authorizer_access_token;
  118. $wxData['wxRefreshToken'] = $authorizer_refresh_token;
  119. $wxData['wxAccessTokenTime'] = date("Y-m-d H:i:s",(time() + $expires_in - 100));
  120. $wxData['status'] = 1;
  121. xhMerchantService::updateById($merchantId, $wxData);
  122. break;
  123. default:
  124. }
  125. Yii::warning("---wx open data end--- \n");
  126. }
  127. }