在Laravel 5.5下进行微信第三方开发(全网发布事件授权回

2017-11-28  本文已影响0人  沧海99
        /*
         * 事件文本消息回调
         */
        public function eventNotify($appid, Request $request)
        {
            //
            Log::info('事件授权回调');
            $timeStamp = empty($request->timestamp) ? "" : trim($request->timestamp); // 时间戳
            $nonce = empty($request->nonce) ? "" : trim($request->nonce); // 随机字符
            $msg_sign = empty($request->msg_signature) ? "" : trim($request->msg_signature); // 签名(公钥)
            $encryptMsg = file_get_contents('php://input'); // xml

            $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);

            // 第三方收到公众号平台发送的消息
            $appId = config('wechat_appid.app_id');
            $appKey = config('wechat_appid.app_key');
            $appToken = config('wechat_appid.app_token');

            $msg = '';
            $pc = new Encrypt($appToken, $appKey, $appId);
            $errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg);
            // 解密失败
            if ($errCode != 0) {
                Log::info('-- ** -- 解密失败');
                echo 'error';

                return false;
            }
            echo 'success';
            $xml = new \DOMDocument();
            $xml->loadXML($msg);
            Log::debug($msg);
            $openid = $xml->getElementsByTagName('FromUserName')->item(0)->nodeValue;
            $Content = $xml->getElementsByTagName('Content')->item(0)->nodeValue;
            $query_auth_code = trim(str_replace("QUERY_AUTH_CODE:", "", $Content));

            if (!Cache::store('database')->has('component_access_token')) {
                $this->component_access_token();
            }
            $accessToken = Cache::store('database')->get('component_access_token');
            $postData = array (
                'component_appid' => $appId,
                'authorization_code' => $query_auth_code,
            );
            $postData = json_encode($postData);
            $result = $this->curl_post($this->wx . '/cgi-bin/component/api_query_auth?component_access_token=' . $accessToken, $postData);
            $res = json_decode($result, true);
            $token = $res['authorization_info']['authorizer_access_token'];
            Log::notice($token);
            Log::warning($openid);
            $data = array (
                'touser' => $openid,
                'msgtype' => 'text',
                'text' => ['content' => $query_auth_code . '_from_api']
            );
            Log::debug($appid);
            $data = json_encode($data);
            $t_result = $this->curl_post($this->wx . '/cgi-bin/message/custom/send?access_token=' . $token, $data);
        }
上一篇下一篇

猜你喜欢

热点阅读