审查视图

application/home/controller/Server.php 5.0 KB
王晓刚 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
<?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,
王晓刚 authored
140
            'type'       => 1,
王晓刚 authored
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
            '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;
    }
}