WxAuthController.php 5.3 KB

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