kefu/webapp/controller/KefuController.class.php
2019-01-16 15:40:52 +08:00

293 lines
8.7 KiB
PHP

<?php
include_once "wxBizMsgCrypt.php";
class KefuController {
protected function getRedis($openid)
{
$redis_conf = getRedisConfig(crc32($openid));
$r = new phpcommon\Redis(array(
'host' => $redis_conf['host'],
'port' => $redis_conf['port'],
'passwd' => $redis_conf['passwd']
));
return $r;
}
private function getAwardConfig($gameid, $condition)
{
$url = '';
if (SERVER_ENV != _ONLINE) {
$url = 'https://center-test.kingsome.cn/api/replays/';
} else {
$url = 'https://center-test.kingsome.cn/api/replays/';
}
$url .= $gameid . '/' . $condition;
$response = '';
$params = array();
if (!phpcommon\HttpClient::get($url,$params,$response)) {
phpcommon\sendError(100,'获取失败');
die();
return;
}
$ret = json_decode($response, true);
for($i = 0; $i < count($ret); $i++) {
$itemid = $ret[$i]['item_id'];
$itemcount = $ret[$i]['count'];
$url = '';
$response = '';
if (!phpcommon\HttpClient::post($url,
json_encode(
array(
'gameid' => $gameid,
'itemid' => $itemid,
'itemcount' => $itemcount
)
)
,
$response
)) {
phpcommon\sendError(100,'获取失败');
return;
}
$data = json_decode($response, true);
if(isset($data) && $data['ErrorCode'] == 0) {
} else{
phpcommon\sendError(100,'服务器内部错误');
return;
}
}
}
public function checkServer() // 校验服务器地址URL
{
$gameid = $_REQUEST['gameid'];
error_log($gameid);
$config_name = "../config/game$gameid/weixin/config.php";
require $config_name;
if ( isset($_REQUEST['echostr'])) {
$this->valid();
} else {
$this->responseMsg();
}
}
public function valid()
{
$echoStr = $_REQUEST['echostr'];
if ($this->checkSignature()) {
echo $echoStr;
exit;
} else {
echo $echoStr . '+++' . WEIXIN_TOKEN;
exit;
}
}
private function checkSignature()
{
$signature = $_REQUEST["signature"];
$timestamp = $_REQUEST["timestamp"];
$nonce = $_REQUEST["nonce"];
$token = WEIXIN_TOKEN;
$tmpArr = array ( $token , $timestamp , $nonce );
sort( $tmpArr , SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1($tmpStr);
if ( $tmpStr == $signature ){
return true ;
} else {
return false ;
}
}
private function sendMsg()
{
}
private function disposeText($postArr, $gameid)
{
error_log('test');
$openid = $postArr['FromUserName'];
$toUserName = $postArr['ToUserName'];
$CreateTime = $postArr['CreateTime'];
}
private function disposeImage($postArr, $gameid)
{
$openid = $postArr['FromUserName'];
$toUserName = $postArr['ToUserName'];
$CreateTime = $postArr['CreateTime'];
}
private function disposeEvent($postArr, $gameid)
{
$openid = $postArr['FromUserName'];
$toUserName = $postArr['ToUserName'];
$CreateTime = $postArr['CreateTime'];
if($postArr['Event'] == 'user_enter_tempsession')
{
$content = '您好,有什么能帮助你?' ;
$data = array (
"touser"=> $openid,
"msgtype"=>"text",
"text"=> array ("content" => $content)
);
$postarray = json_encode( $data ,JSON_UNESCAPED_UNICODE);
//POST发送https请求客服接口api
$access_token = $this->getAccessToken($openid, $gameid);
$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=$access_token";
$response = '';
if (!phpcommon\HttpClient::post($url, $postarray, $response)) {
phpcommon\sendError(ERR_RETRY, '系统繁忙');
return;
}
$ret_info = json_decode($response, true);
if ($ret_info['errcode'] == 0) {
echo(json_encode(array(
'errcode' => 0,
'errmsg' => ''
)));
exit ;
} else {
phpcommon\sendError(ERR_RETRY, 'token失败');
error_log($response);
}
}
}
public function responseMsg()
{
$postStr_key = $GLOBALS ["HTTP_RAW_POST_DATA" ];
error_log('!!!begin:' . $postStr_key);
error_log('request:' . json_encode($_REQUEST));
$pc = new WXBizMsgCrypt(WEIXIN_TOKEN, WEIXIN_MSG_KEY, WEIXIN_APP_ID);
$gameid = $_REQUEST['gameid'];
$msg = '';
$msg_sign = $_REQUEST['msg_signature'];
$timeStamp = $_REQUEST['timestamp'];
$nonce = $_REQUEST['nonce'];
$errCode = $pc->decryptJsonMsg($msg_sign, $timeStamp, $nonce, $postStr_key, $postStr);
error_log('error_log:' . json_encode($errCode));
if ($errCode == 0) {
error_log("解密后: " . $postStr . "\n");
$pc2 = new WXBizMsgCrypt(WEIXIN_TOKEN, WEIXIN_MSG_KEY, WEIXIN_APP_ID);
$encryptMsg = '';
$text = $postStr;
$errCode = $pc2->encryptJsonMsg($text, $timeStamp, $nonce, $encryptMsg_str);
if ($errCode == 0) {
error_log("加密后: " . $encryptMsg_str . "\n");
$encryptMsg = json_decode($encryptMsg_str, true);
$errCode = $pc2->decryptJsonMsg($encryptMsg['MsgSignature'], $timeStamp, $nonce, $encryptMsg_str, $postStr2);
error_log('error_log:' . json_encode($errCode));
if ($errCode == 0) {
error_log("解密后: " . $postStr2 . "\n");
}
} else {
error_log($errCode . "\n");
}
error_log('end');
exit;
if (!empty( $postStr ) && is_string( $postStr )){
$postArr = json_decode( $postStr , true );
if (!empty( $postArr ['MsgType']) && $postArr['MsgType'] == 'text'){ // 文本消息
$this->disposeText($postArr, $gameid);
} elseif (!empty( $postArr ['MsgType']) && $postArr['MsgType'] == 'image'){ // 图文消息
$this->disposeImage($postArr, $gameid);
} elseif (!empty( $postArr ['MsgType']) && $postArr['MsgType'] == 'event' ){ // 进入客服动作
$this->disposeEvent($postArr, $gameid);
} else {
exit ('aaa');
}
} else {
echo "" ;
exit ;
}
} else {
error_log($errCode . "\n");
}
}
public function getAccessToken($openid, $gameid)
{
$r = $this->getRedis($openid);
$access_token = $r->get('weixin_token:' . $gameid . ':' . $openid);
if (!empty($access_token)) {
return $access_token;
}
else {
$appid = WEIXIN_APP_ID;
$appkey = WEIXIN_APP_SECRET;
$url = "https://api.weixin.qq.com/cgi-bin/token?" .
"grant_type=client_credential&appid=$appid&secret=$appkey";
$params = array();
$response = '';
if (!phpcommon\HttpClient::get($url, $params, $response)) {
phpcommon\sendError(ERR_INTERNAL, '系统繁忙');
return;
}
$res = json_decode($response, true);
if ( $res ) {
$r->set('weixin_token:' . $gameid . ':' .
$openid, $res['access_token']); //刚获取的token放到redis中
$r->pexpire('weixin_token:' . $gameid . $openid, 7150); //微信限制过期时间为两小时
return $res['access_token'];
} else {
phpcommon\sendError(ERR_INTERNAL, '获取access_token失败');
die;
}
}
}
}