<?php
include_once "wxBizMsgCrypt.php";
class AuthorizationAction extends Action
{
public $component_access_token;
public $weixin_account;
public $accountM;
public $userid;
public $token;
public $pubs;
public function __construct()
{
parent::__construct();
$this->userid = 1;
$this->token = $_SESSION['token'];
$this->accountM = new Model('weixin_account');
$this->pubs = new Model('pubs');
$this->weixin_account = $this->accountM->where(array(
'id' => $this->userid
))->find();
}
//发起授权页的体验URL
public function login()
{
$get_pre_auth_code = $this->get_pre_auth_code();
echo "<a href='https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=" . $this->weixin_account['appId'] . "&pre_auth_code=$get_pre_auth_code&redirect_uri=http://www.weiyes.com.cn/index.php/Authorization/test'><img src='https://open.weixin.qq.com/zh_CN/htmledition/res/assets/res-design-download/icon64_appwx_logo.png'></a>";
}
//公众号消息与事件接收URL
public function test()
{
$msg_signature = $_REQUEST['msg_signature'];
if ($msg_signature) {
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
file_put_contents('ccccsimplexml_load_string.txt',json_encode($postObj));
$toUsername = $postObj->ToUserName;
if ($toUsername == 'gh_3c884a361561') {
$this->encrypt($postStr);
}else{
$this->encrypt($postStr);
}
} else {
return;
}
}
/*
解密
*/
public function encrypt($encyptdata)
{
$encodingAesKey = $this->weixin_account['encodingAesKey'];
$token = $this->weixin_account['token'];
$appId = $this->weixin_account['appId'];
$timeStamp = empty($_GET['timestamp']) ? "" : trim($_GET['timestamp']);
$nonce = empty($_GET['nonce']) ? "" : trim($_GET['nonce']);
$msg_sign = empty($_GET['msg_signature']) ? "" : trim($_GET['msg_signature']);
$pc = new WXBizMsgCrypt($token, $encodingAesKey, $appId);
$xml_tree = new DOMDocument();
$xml_tree->loadXML($encyptdata);
$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);
//第三方收到公众号平台发送的消息
$msg = '';
$errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg);
if ($errCode == 0) {
$msgObj = json_decode(json_encode(simplexml_load_string($msg, 'SimpleXMLElement', LIBXML_NOCDATA)));
file_put_contents('cccchenzhiqmsgObj.txt',json_encode($msgObj));
// {"ToUserName":"gh_3c884a361561","FromUserName":"ozy4qt1eDxSxzCr0aNT0mXCWfrDE","CreateTime":"1446104321","MsgType":"event","Event":"LOCATION","Latitude":"111.000000","Longitude":"222.000000","Precision":"333.000000"}
switch ($msgObj->MsgType) {
case "event":
$this->replyEventMessage($timeStamp, $nonce, $msg_sign, $msgObj->Event, $msgObj->ToUserName, $msgObj->FromUserName);
break;
case "text":
$this->processTextMessage($timeStamp, $nonce, $msg_sign, $msgObj->Content, $msgObj->ToUserName, $msgObj->FromUserName);
break;
default:
break;
}
} else {
file_put_contents('cccxmlerr.txt', json_encode($errCode) . date('Y-m-d H:i:s', time()) . "/n", FILE_APPEND);
}
}
public function processTextMessage($timeStamp, $nonce, $msg_sign, $Content, $toUserName, $fromUserName)
{
if ('TESTCOMPONENT_MSG_TYPE_TEXT' == $Content) {
$text = $Content . '_callback';
file_put_contents('cccTESTCOMPONENT_MSG_TYPE_TEXT.txt', $Content);
$this->replyTextMessage($timeStamp, $nonce, $msg_sign, $text, $toUserName, $fromUserName);
} elseif (stristr($Content, "QUERY_AUTH_CODE")) {
$textArray = explode(':', $Content);
file_put_contents('cccQUERY_AUTH_CODE.txt', $Content);
$this->replyApiTextMessage($timeStamp, $nonce, $msg_sign, $textArray[1], $toUserName, $fromUserName);
}else{
$this->replyTextMessage($timeStamp, $nonce, $msg_sign, $Content, $toUserName, $fromUserName);
}
}
public function replyApiTextMessage($timeStamp, $nonce, $msg_sign, $query_auth_code, $toUserName, $fromUserName)
{
$url = 'https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=' . $this->get_Access_token();
$data = json_encode(array(
'component_appid' => $this->weixin_account['appId'],
'authorization_code' => $query_auth_code
));
$getreplyApiTextMessage = json_decode($this->curl_get_post($url, $data));
file_put_contents('ccccgetreplyApiTextMessage.txt', json_encode($getreplyApiTextMessage));
$text = $query_auth_code . '_from_api';
$sfromUserName = $getreplyApiTextMessage->authorization_info->authorizer_appid;
$authorizer_access_token = $getreplyApiTextMessage->authorization_info->authorizer_access_token;
$authorization = $this->get_authorization($sfromUserName, 'customer_service', '1');
if ($authorization == 'ok') {
file_put_contents('ccccget_authorization.txt', 'ok');
$this->processWechatTextMessage($text, $fromUserName, $authorizer_access_token);
}
}
/*
*推送客服回复信息
*/
function processWechatTextMessage($text, $fromUserName, $authorizer_access_token)
{
$url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=' . $authorizer_access_token;
$data = json_encode(array(
'touser' => $fromUserName,
'msgtype' => 'text',
'text' => array(
'content' => $text
)
));
file_put_contents('ccccprocessWechatTextMessagedata.txt', $data);
$getreplyApiTextMessage = json_decode($this->curl_get_post($url, $data));
file_put_contents('ccccprocessWechatTextMessage.txt', json_encode($getreplyApiTextMessage));
}
/*
获取授权
location_report(地理位置上报选项) 0无上报 1进入会话时上报 2每5s上报
voice_recognize(语音识别开关选项)0关 1开
customer_service(客服开关选项)0关 1开
*/
function get_authorization($authorizer_appid, $option_name, $option_value = '1')
{
$arraydata = array(
'location_report',
'voice_recognize',
'customer_service'
);
if (in_array($option_name, $arraydata)) {
$url = 'https://api.weixin.qq.com/cgi-bin/component/api_set_authorizer_option?component_access_token=' . $this->get_Access_token();
$data = json_encode(array(
'component_appid' => $this->weixin_account['appId'],
'authorizer_appid' => $authorizer_appid,
'option_name' => $option_name,
'option_value' => $option_value
));
$query_authorization = json_decode($this->curl_get_post