<?php /** * Created by PhpStorm. * User: Administrator * Date: 2019/1/19 * Time: 11:32 */ namespace app\home\controller; use app\common\controller\WechatBase; use EasyWeChat\Foundation\Application; use think\Db; class Server extends WechatBase { 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')); $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); $share_id=substr($message->EventKey,8); cache('share_id',$share_id); $third = Db::name('third')->where('openid',$openid)->find(); cache('third',$third); if(empty($third)){ //获取用户信息 $options=config('wechat'); $app = new Application($options); $accessToken = $app->access_token; // EasyWeChat\Core\AccessToken 实例 $token = $accessToken->getToken(false); $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$token&openid=$openid&lang=zh_CN"; $user_data = json_decode($this->http_get($url),true); $this->new_user($user_data,$openid,$options['app_id'],$share_id); } } 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; } } public function http_get($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); $result = curl_exec($ch); // grab URL, and print if(curl_errno($ch)){ print curl_error($ch); } curl_close($ch); return $result; } public function new_user($wechat_user,$openid,$appId,$share_id) { $currentTime = time(); $ip = $this->request->ip(0, true); $userId = Db::name("user")->insertGetId([ 'status' => 'normal', 'gender' => $wechat_user['original']['sex'], 'nickname' => $wechat_user['nickname'], 'avatar' => $wechat_user['avatar'], 'joinip' => $ip, 'jointime' => $currentTime, 'createtime' => $currentTime, 'updatetime' => $currentTime, 'loginip' => $ip, 'logintime' => $currentTime, 'type' => 1, 'parent_id' => $share_id, ]); $row=Db::name("third")->insert([ 'openid' => $openid, 'user_id' => $userId, 'nickname' => $wechat_user['nickname'], 'app_id' => $appId, 'loginip' => $ip, 'union_id' => '', 'logintime' => $currentTime, 'createtime' => $currentTime, 'login_times' => 1, 'more' => json_encode($wechat_user) ]); return $userId; } }