base
This commit is contained in:
commit
1f94592e72
10
.gitigonre
Normal file
10
.gitigonre
Normal file
@ -0,0 +1,10 @@
|
||||
*.*\~
|
||||
*.*~
|
||||
*.*\#
|
||||
*.*#
|
||||
~*.*
|
||||
\#*.*
|
||||
*.tar
|
||||
*.tar.gz
|
||||
config
|
||||
.idea
|
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
[submodule "third_party/phpcommon"]
|
||||
path = third_party/phpcommon
|
||||
url = git@git.kingsome.cn:server_common/phpcommon.git
|
1
third_party/phpcommon
vendored
Submodule
1
third_party/phpcommon
vendored
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit e3e61fbbaec4d0baf4dc1891649309dd47f12c17
|
54
webapp/bootstrap/config_loader.php
Normal file
54
webapp/bootstrap/config_loader.php
Normal file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
$g_conf_mysql_cluster = require('../config/kefu.mysql.cluster.php');
|
||||
$g_conf_redis_cluster = require('../config/kefu.redis.cluster.php');
|
||||
|
||||
function checkMysqlConfig()
|
||||
{
|
||||
$instance_id = 1;
|
||||
global $g_conf_mysql_cluster;
|
||||
foreach ($g_conf_mysql_cluster as $instance) {
|
||||
if ($instance_id != $instance['instance_id']) {
|
||||
error_log('kefu.mysql.cluster.php config error');
|
||||
die();
|
||||
}
|
||||
$instance_id++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function checkRedisConfig()
|
||||
{
|
||||
$instance_id = 1;
|
||||
global $g_conf_redis_cluster;
|
||||
foreach ($g_conf_redis_cluster as $instance) {
|
||||
if ($instance_id != $instance['instance_id']) {
|
||||
error_log('kefu.redis.cluster.php config error');
|
||||
die();
|
||||
}
|
||||
$instance_id++;
|
||||
}
|
||||
}
|
||||
|
||||
function getMysqlConfig($hash_value)
|
||||
{
|
||||
if ($hash_value < 0) {
|
||||
die('hash_value < 0 ' . $hash_value);
|
||||
}
|
||||
global $g_conf_mysql_cluster;
|
||||
$idx = $hash_value % count($g_conf_mysql_cluster);
|
||||
return $g_conf_mysql_cluster[$idx];
|
||||
}
|
||||
|
||||
function getRedisConfig($hash_value)
|
||||
{
|
||||
if ($hash_value < 0) {
|
||||
die('hash_value < 0 ' . $hash_value);
|
||||
}
|
||||
global $g_conf_redis_cluster;
|
||||
$idx = $hash_value % count($g_conf_redis_cluster);
|
||||
return $g_conf_redis_cluster[$idx];
|
||||
}
|
||||
|
||||
checkMysqlConfig();
|
||||
checkRedisConfig();
|
6
webapp/bootstrap/init.php
Normal file
6
webapp/bootstrap/init.php
Normal file
@ -0,0 +1,6 @@
|
||||
<?php
|
||||
|
||||
ini_set('date.timezone','Asia/Shanghai');
|
||||
require 'phpcommon/common.php';
|
||||
|
||||
require 'config_loader.php';
|
146
webapp/controller/KefuController.class.php
Normal file
146
webapp/controller/KefuController.class.php
Normal file
@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
|
||||
define("TOKEN", ""); //填写自己的token
|
||||
|
||||
|
||||
class KefuController {
|
||||
|
||||
public function index(){
|
||||
|
||||
}
|
||||
|
||||
public function checkServer(){ // 校验服务器地址URL
|
||||
|
||||
echo('ssss');
|
||||
if ( isset ( $_GET ['echostr' ])) {
|
||||
$this -> valid();
|
||||
} else {
|
||||
$this -> responseMsg();
|
||||
}
|
||||
}
|
||||
public function valid()
|
||||
{
|
||||
$echoStr = $_GET ["echostr" ];
|
||||
if ( $this -> checkSignature()){
|
||||
header ('content-type:text' );
|
||||
echo $echoStr ;
|
||||
exit ;
|
||||
} else {
|
||||
echo $echoStr .'+++'. TOKEN;
|
||||
exit ;
|
||||
}
|
||||
}
|
||||
private function checkSignature()
|
||||
{
|
||||
$signature = $_GET ["signature" ];
|
||||
$timestamp = $_GET ["timestamp" ];
|
||||
$nonce = $_GET ["nonce" ];
|
||||
|
||||
$token = TOKEN;
|
||||
$tmpArr = array ( $token , $timestamp , $nonce );
|
||||
sort ( $tmpArr , SORT_STRING);
|
||||
$tmpStr = implode ( $tmpArr );
|
||||
$tmpStr = sha1 ( $tmpStr );
|
||||
|
||||
if ( $tmpStr == $signature ){
|
||||
return true ;
|
||||
} else {
|
||||
return false ;
|
||||
}
|
||||
}
|
||||
public function responseMsg()
|
||||
{
|
||||
$postStr = $GLOBALS ["HTTP_RAW_POST_DATA" ];
|
||||
|
||||
if (! empty ( $postStr ) && is_string ( $postStr )){
|
||||
// 禁止引用外部xml实体
|
||||
//libxml_disable_entity_loader(true);
|
||||
|
||||
//$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
|
||||
$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 -> get_accessToken();
|
||||
/*
|
||||
* 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 get_accessToken(){
|
||||
/* 在有效期,直接返回access_token */
|
||||
if (S('access_token' )){
|
||||
return S('access_token' );
|
||||
}
|
||||
/* 不在有效期,重新发送请求,获取access_token */
|
||||
else {
|
||||
$url = ' https :// api . weixin . qq . com / cgi - bin / token ? grant _ type = client _ credential & appid = wx6056 ** **& secret = 30e46f3ef07b **** ' ;
|
||||
$result = curl_get_https( $url );
|
||||
$res = json_decode($result , true ); // json字符串转数组
|
||||
|
||||
if ( $res ){
|
||||
S( 'access_token', $res ['access_token'],7100 );
|
||||
return S('access_token' );
|
||||
} else {
|
||||
return 'api return error' ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
24
webapp/index.php
Normal file
24
webapp/index.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
require 'bootstrap/init.php';
|
||||
|
||||
if (empty($_REQUEST['c']) || empty($_REQUEST['a'])) {
|
||||
die();
|
||||
return;
|
||||
}
|
||||
|
||||
function autoload_controller__($classname)
|
||||
{
|
||||
require_once "controller/$classname.class.php";
|
||||
spl_autoload_unregister('autoload_controller__');
|
||||
}
|
||||
spl_autoload_register('autoload_controller__');
|
||||
|
||||
try{
|
||||
$c = $_REQUEST['c'];
|
||||
$a = $_REQUEST['a'];
|
||||
$classname = $c .'Controller';
|
||||
$obj = eval('return (new $classname())->$a();');
|
||||
} catch (Exception $e){
|
||||
echo($e);
|
||||
}
|
1
webapp/phpcommon
Symbolic link
1
webapp/phpcommon
Symbolic link
@ -0,0 +1 @@
|
||||
../third_party/phpcommon
|
Loading…
x
Reference in New Issue
Block a user