|
|
<?php
|
|
|
/**
|
|
|
* Created by PhpStorm.
|
|
|
* User: Administrator
|
|
|
* Date: 2019/1/19
|
|
|
* Time: 11:32
|
|
|
*/
|
|
|
|
|
|
namespace app\portal\controller;
|
|
|
|
|
|
|
|
|
use cmf\controller\WeChatBaseController;
|
|
|
use EasyWeChat\Foundation\Application;
|
|
|
use think\Db;
|
|
|
|
|
|
class OpenServerController extends WeChatBaseController
|
|
|
{
|
|
|
public function index(){
|
|
|
if(!isset($_GET["echostr"])){
|
|
|
$this->responseMsg();
|
|
|
}else{
|
|
|
$this->valid();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public function valid()
|
|
|
{
|
|
|
$echoStr = $_GET["echostr"];
|
|
|
echo $echoStr;
|
|
|
exit;
|
|
|
}
|
|
|
public function test(){
|
|
|
dump(cache('referrer'));
|
|
|
dump(cache('openid'));
|
|
|
dump(cache('admin_id'));
|
|
|
dump(cache('result'));
|
|
|
dump(cache('result1'));
|
|
|
dump(cache('third_party_user'));
|
|
|
dump(cache('user'));
|
|
|
}
|
|
|
public function responseMsg()
|
|
|
{
|
|
|
$app=new Application(config('wechat_config'));
|
|
|
$server = $app->server;
|
|
|
|
|
|
$server->setMessageHandler(function ($message) {
|
|
|
switch ($message->MsgType) {
|
|
|
case 'event':
|
|
|
switch ($message->Event) {
|
|
|
case 'subscribe':
|
|
|
if(empty($message->EventKey)){
|
|
|
return '欢迎关注“学考无忧”!';
|
|
|
}else{
|
|
|
//获取用户openid
|
|
|
/*$openid=$message->FromUserName;
|
|
|
cache('openid',$openid);
|
|
|
$admin_id=substr($message->EventKey,8);
|
|
|
cache('admin_id',$admin_id);
|
|
|
$third_party_user = Db::name('third_party_user')->where('openid',$openid)->find();
|
|
|
cache('third_party_user',$third_party_user);
|
|
|
if(!empty($third_party_user)){
|
|
|
$user = Db::name('user')->where('id',$third_party_user['user_id'])->find();
|
|
|
cache('user',$user);
|
|
|
if($user['channel'] == 2){
|
|
|
$result1 = Db::name('user')->where('id',$user['id'])->update(['admin_id'=>$admin_id,'channel'=>1]);
|
|
|
cache('result1',$result1);
|
|
|
}
|
|
|
}else{
|
|
|
//储存到推荐人推荐列表
|
|
|
$referrer = Db::name('referrer')->where(array('openid'=>$openid))->find();
|
|
|
cache('referrer',$referrer);
|
|
|
if(empty($referrer)){
|
|
|
$result = Db::name('referrer')->insert(array('admin_id'=>$admin_id,'openid'=>$openid,'create_time'=>time()));
|
|
|
cache('result',$result);
|
|
|
// return "推荐人关注!";
|
|
|
}else{
|
|
|
// return "已有推荐人!";
|
|
|
}
|
|
|
}*/
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
// return '收到event消息';
|
|
|
break;
|
|
|
}
|
|
|
break;
|
|
|
case 'text':
|
|
|
// return '收到文字消息';
|
|
|
break;
|
|
|
default:
|
|
|
// return '收到其它消息';
|
|
|
break;
|
|
|
}
|
|
|
});
|
|
|
|
|
|
$response = $server->serve();
|
|
|
|
|
|
$response->send();
|
|
|
}
|
|
|
|
|
|
private function check_signature()
|
|
|
{
|
|
|
$signature = $_GET["signature"];
|
|
|
$timestamp = $_GET["timestamp"];
|
|
|
$nonce = $_GET["nonce"];
|
|
|
|
|
|
$token = TOKEN;
|
|
|
$tmpArr = array($token, $timestamp, $nonce);
|
|
|
sort($tmpArr);
|
|
|
$tmpStr = implode( $tmpArr );
|
|
|
$tmpStr = sha1( $tmpStr );
|
|
|
|
|
|
if( $tmpStr == $signature ){
|
|
|
return true;
|
|
|
}else{
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
} |
|
|
\ No newline at end of file |
...
|
...
|
|