kefu/webapp/controller/KefuController.class.php
2019-01-11 14:49:06 +08:00

158 lines
5.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
define("TOKEN", "kingsome");
define("MSG_KEY", 'MzebhFcmcIFT85xOr5TnrDVeLx5HMbpgAl5gw2PVvhX');
define("SHAOWEI_APPID", 'wxfa9c8ea6a55f00d0');
define("PRIVATE_KEY", '999712592d00ad2f75b04421e030ba04');
class KefuController {
public function checkServer() // 校验服务器地址URL
{
if ( isset($_GET['echostr'])) {
$this->valid();
} else {
$this->responseMsg();
}
}
public function valid()
{
$echoStr = $_GET['echostr'];
if ( $this->checkSignature()){
echo $echoStr;
exit;
} else {
echo $echoStr . '+++' . TOKEN;
exit;
}
}
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
error_log('token:' . $token);
$tmpArr = array ( $token , $timestamp , $nonce );
sort( $tmpArr , SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1($tmpStr);
error_log('tmpStr:' . $tmpStr);
error_log('signature:' . $signature);
if ( $tmpStr == $signature ){
return true ;
} else {
return false ;
}
}
public function responseMsg()
{
$postStr = $GLOBALS ["HTTP_RAW_POST_DATA" ];
if (! empty ( $postStr ) && is_string ( $postStr )){
$postArr = json_decode( $postStr , true );
if (! empty ( $postArr ['MsgType']) && $postArr ['MsgType'] == 'text'){ // 文本消息
$fromUsername = $postArr ['FromUserName']; // 发送者openid
$toUserName = $postArr ['ToUserName']; // 小程序id
$textTpl = array (
"ToUserName" =>$fromUsername ,
"FromUserName"=> $toUserName ,
"CreateTime"=> time (),
"MsgType"=>"transfer_customer_service",
);
exit (json_encode( $textTpl ));
} elseif (! empty ( $postArr ['MsgType']) && $postArr ['MsgType'] == 'image'){ // 图文消息
$fromUsername = $postArr ['FromUserName']; // 发送者openid
$toUserName = $postArr ['ToUserName']; // 小程序id
$textTpl = array (
"ToUserName"=> $fromUsername ,
"FromUserName"=> $toUserName ,
"CreateTime"=> time (),
"MsgType"=>"transfer_customer_service",
);
exit (json_encode( $textTpl ));
} elseif ( $postArr ['MsgType'] == 'event' && $postArr ['Event']=='user_enter_tempsession'){ // 进入客服动作
$fromUsername = $postArr ['FromUserName']; // 发送者openid
$content = '您好,有什么能帮助你?' ;
$data = array (
"touser"=> $fromUsername ,
"msgtype"=>"text",
"text"=> array ("content"=> $content )
);
$json = json_encode( $data ,JSON_UNESCAPED_UNICODE); // php5.4+
$access_token = $this->getAccessToken();
/*
* POST发送https请求客服接口api
*/
$url = " https :// api . weixin . qq . com / cgi - bin / message / custom / send ? access _ token = ". $access_token ;
// 以'json'格式发送post的https请求
$curl = curl_init();
curl_setopt( $curl , CURLOPT_URL, $url );
curl_setopt( $curl , CURLOPT_POST, 1); // 发送一个常规的Post请求
curl_setopt( $curl , CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt( $curl , CURLOPT_SSL_VERIFYHOST, FALSE );
if (! empty ( $json )){
curl_setopt( $curl , CURLOPT_POSTFIELDS, $json );
}
curl_setopt( $curl , CURLOPT_RETURNTRANSFER, 1 );
// curl_setopt($curl, CURLOPT_HTTPHEADER, $headers );
$output = curl_exec( $curl );
if (curl_errno( $curl )) {
echo 'Errno'.curl_error( $curl ); // 捕抓异常
}
curl_close( $curl );
if ( $output == 0 ){
echo 'success'; exit ;
}
} else {
exit ('aaa' );
}
} else {
echo "" ;
exit ;
}
}
/* 调用微信api获取access_token有效期7200s -xzz0704 */
public function getAccessToken(){
/* 在有效期直接返回access_token */
if (S('access_token' )){
return S('access_token' );
}
/* 不在有效期重新发送请求获取access_token */
else {
$appid = SHAOWEI_APPID;
$appkey = PRIVATE_KEY;
$url = "https://api.weixing.qq.com/cgi-bin/token?;grant_type=client_credential&appid=$appid&secret=$appkey";
$response = '';
if (!phpcommon\HttpClient::get($url, $params, $response)) {
phpcommon\sendError(ERR_RETRY, '系统繁忙');
return;
}
$res = json_decode($response, true );
if ( $res ){
S( 'access_token', $res ['access_token'],7100 );
return S('access_token' );
} else {
return 'api return error' ;
}
}
}
}