审查视图

app/portal/controller/OpenServerController.php 15.2 KB
王晓刚 authored
1 2 3 4 5 6 7 8 9 10 11
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/19
 * Time: 11:32
 */

namespace app\portal\controller;

王晓刚 authored
12
use cmf\controller\HomeBaseController;
王晓刚 authored
13 14 15 16
use cmf\controller\WeChatBaseController;
use EasyWeChat\Foundation\Application;
use think\Db;
王晓刚 authored
17
class OpenServerController extends HomeBaseController
王晓刚 authored
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
{
    public function index(){
        if(!isset($_GET["echostr"])){
            $this->responseMsg();
        }else{
            $this->valid();
        }
    }

    public function valid()
    {
        $echoStr = $_GET["echostr"];
        echo $echoStr;
        exit;
    }
    public function test(){
王晓刚 authored
34
//        dump(cache('referrer'));
王晓刚 authored
35 36
        dump(cache('aaa'));
        dump(cache('bcc'));
王晓刚 authored
37
        dump(cache('c'));
王晓刚 authored
38
        dump(cache('data_id'));
王晓刚 authored
39
        exit();
王晓刚 authored
40 41 42 43 44 45 46
        dump(cache('result'));
        dump(cache('result1'));
        dump(cache('third_party_user'));
        dump(cache('user'));
    }
    public function responseMsg()
    {
王晓刚 authored
47 48
        $conf = config('wechat_config');
        $app=new Application($conf);
王晓刚 authored
49 50 51 52 53 54 55 56
        $server = $app->server;

        $server->setMessageHandler(function ($message) {
            switch ($message->MsgType) {
                case 'event':
                    switch ($message->Event) {
                        case 'subscribe':
                            if(empty($message->EventKey)){
王晓刚 authored
57 58 59
                                return '感谢您关注我,学考君帮助您的孩子爱上阅读,快速提分! 
这里有热门的教育资讯,海量的免费资源,优质的学习干货!  
点击右下角【学考商城】→进入【校园团购】→选择您想要的优惠书籍,即可点击购买! 
Cool authored
60
您在购买过程中有任何质疑,欢迎拨打投诉电话:15300081769
王晓刚 authored
61 62

回复关键词“中华国学”有11节精彩的国学课可以听哦!';
王晓刚 authored
63 64
                            }else{
                                //获取用户openid
王晓刚 authored
65
                                $openid=$message->FromUserName;
王晓刚 authored
66 67 68 69 70 71 72 73 74 75 76 77
                                $param=substr($message->EventKey,8);
                                $data = explode("-",$param);
                                $my_user_id = $data[0];
                                $status = $data[1];
                                $third_party_user = Db::name('third_party_user')->where('openid',$openid)->find();
                                if(!$third_party_user){
                                    //获取用户信息
                                    $options=config('wechat_config');
                                    $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";
王晓刚 authored
78 79
                                    $user_data = json_decode($this->http_get($url),true);
                                    $user_id = $this->new_user($user_data,$openid,$options['app_id']);
王晓刚 authored
80 81
                                }else{
                                    $user_id = $third_party_user['user_id'];
王晓刚 authored
82
                                }
王晓刚 authored
83 84 85 86 87
                                $new_data = [
                                    'my_user_id' => $my_user_id,
                                    'status_id' => $status,
                                ];
                                $this->bind($user_id,$new_data);
王晓刚 authored
88 89 90
                                return '感谢您关注我,学考君帮助您的孩子爱上阅读,快速提分! 
这里有热门的教育资讯,海量的免费资源,优质的学习干货!  
点击右下角【学考商城】→进入【校园团购】→选择您想要的优惠书籍,即可点击购买! 
91
您在购买过程中有任何质疑,欢迎拨打投诉电话:010-57257538
王晓刚 authored
92 93

回复关键词“中华国学”有11节精彩的国学课可以听哦!';
王晓刚 authored
94 95
                            }
                            break;
王晓刚 authored
96
                        case 'SCAN':
王晓刚 authored
97 98
                            //获取用户openid
                            $openid=$message->FromUserName;
王晓刚 authored
99
                            $param=$message->EventKey;
王晓刚 authored
100
                            $data = explode("-",$param);
王晓刚 authored
101 102
                            cache(null);
                            cache('data',$data);
王晓刚 authored
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
                            $my_user_id = $data[0];
                            $status = $data[1];
                            $third_party_user = Db::name('third_party_user')->where('openid',$openid)->find();
                            if(!$third_party_user){
                                //获取用户信息
                                $options=config('wechat_config');
                                $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);
                                $user_id = $this->new_user($user_data,$openid,$options['app_id']);
                            }else{
                                $user_id = $third_party_user['user_id'];
                            }
                            $new_data = [
                                'my_user_id' => $my_user_id,
                                'status_id' => $status,
                            ];
                            $this->bind($user_id,$new_data);
                            break;
王晓刚 authored
124 125 126 127 128 129
                        default:
//                            return '收到event消息';
                            break;
                    }
                    break;
                case 'text':
刘朕 authored
130
                    $text = '您好,欢迎关注“学考无忧”公众号。
王晓刚 authored
131 132
我们致力于打造一个为广大家长和同学们提升知识视野,提高学习成绩的优质服务号,这里有热门的教育资讯,海量的免费资源,优质的学习干货,会为大家提供教辅类以及课外阅读阅读书籍,帮助同学们提高学习成绩。
真正让家长省心,孩子无忧。感谢关注。';
刘朕 authored
133
                    switch ($message->Content) {
刘朕 authored
134
                        case '中华国学':
刘朕 authored
135 136 137
                            $text = 'https://appLuAK2Xa13279.h5.xiaoeknow.com/content_page/eyJ0eXBlIjozLCJyZXNvdXJjZV90eXBlIjoiIiwicmVzb3VyY2VfaWQiOiIiLCJwcm9kdWN0X2lkIjoicF81Y2ZkZmZhMmJmYzcxXzZVMGl2YTZpIiwiYXBwX2lkIjoiYXBwTHVBSzJYYTEzMjc5In0';
                    }
                    return $text;
王晓刚 authored
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
                    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;
        }
    }
王晓刚 authored
168 169 170 171 172 173 174 175 176 177 178 179 180

    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;
    }
王晓刚 authored
181 182 183 184 185 186 187 188

    public function new_user($wechat_user,$openid,$appId) {
        $currentTime = time();
        $ip          = $this->request->ip(0, true);
        $userId = Db::name("user")->insertGetId([
            'create_time'     => $currentTime,
            'user_status'     => 1,
            'user_type'       => 2,
王晓刚 authored
189
            'sex'             => $wechat_user['sex'],
王晓刚 authored
190
            'user_nickname'   => $wechat_user['nickname'],
王晓刚 authored
191
            'avatar'          => $wechat_user['headimgurl'],
王晓刚 authored
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
            'last_login_ip'   => $ip,
            'last_login_time' => $currentTime,
        ]);

        $row=Db::name("third_party_user")->insert([
            'openid'          => $openid,
            'user_id'         => $userId,
            'third_party'     => 'public',
            'nickname'        => $wechat_user['nickname'],
            'app_id'          => $appId,
            'last_login_ip'   => $ip,
            'union_id'        => '',
            'last_login_time' => $currentTime,
            'create_time'     => $currentTime,
            'login_times'     => 1,
            'status'          => 1,
            'more'            => json_encode($wechat_user)
        ]);
        return $userId;
    }
王晓刚 authored
212 213

    public function bind($uid,$data_id){
王晓刚 authored
214
        cache('data_id',$data_id);
王晓刚 authored
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
        $data_my_user = Db::name('my_user') -> where('uid',$uid) -> find();
        if($data_my_user){
            //用户是否为自由人 是自由人则绑定 不是自由人判断是不是老师
            if($data_my_user['status'] == 0 || $data_my_user['status'] == 1 || $data_my_user['status'] == 5 || $data_my_user['status'] == 6){
                //判断跳转链接是否存在数据 存在则绑定关系
                if(!empty($data_id)){
                    //判断上级权限是否存在
                    $my_user = Db::name('my_user') -> where('id',$data_id['my_user_id']) -> find();
                    if(empty($my_user)){
                        $this->error('查无此人','','','');
                    }
                    if($my_user['status'] != $data_id['status_id']){
                        $this->error('该二维码已失效','','','');
                    }
                    //判断数据是老师还是业务员
                    if($data_id['status_id'] == 2){
                        //业务员分享 更改用户身份为老师
                        $my_user_phone = Db::name('my_user') -> where('id',$data_id['my_user_id']) -> find();
                        $data_update['status'] = 3;
                        $data_update['inviter_phone'] = $my_user_phone['phone'];
                        $data_update['is_pro'] = 1;
                        $data_update['pid'] = $my_user_phone['id'];
                        $data_update['bind_time'] = time();
                        $data_update['bind_status'] = 1;
                        Db::name('my_user') -> where('uid',$uid) -> update($data_update);
                    }elseif ($data_id['status_id'] == 3){
                        //老师分享 更改用户身份为学生
王晓刚 authored
242
                        cache('aaa','SSSSSS');
王晓刚 authored
243
                        $my_user_phone = Db::name('my_user') -> where('id',$data_id['my_user_id']) -> find();
王晓刚 authored
244
                        cache('bbb','######');
王晓刚 authored
245 246 247 248 249 250 251 252 253
                        $data_update['status'] = 4;
                        $data_update['inviter_phone'] = $my_user_phone['phone'];
                        $data_update['is_pro'] = 0;
                        $data_update['pid'] = $my_user_phone['id'];
                        $data_update['bind_time'] = time();
                        $data_update['bind_status'] = 1;
                        Db::name('my_user') -> where('uid',$uid) -> update($data_update);
                    }
                }
王晓刚 authored
254 255
            }
            /*else{
王晓刚 authored
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
                //判断跳转链接是否存在数据
                if(!empty($data_id['status_id'])){
                    //判断上级权限是否存在
                    $my_user = Db::name('my_user') -> where('id',$data_id['my_user_id']) -> find();
                    if(empty($my_user)){
                        $this->error('查无此人','','','');
                    }
                    if($my_user['status'] != $data_id['status_id']){
                        $this->error('该二维码已失效','','','');
                    }
                    //判断用户是不是老师
                    if($data_my_user['status'] == 3){
                        //是老师则判断老师绑定状态
                        if($data_my_user['bind_status'] = 2){
                            //判断是否为业务员分享的链接
                            if($data_id['status_id'] == 2){
                                //是业务员分享则更新老师的pid
                                $my_user_phone = Db::name('my_user') -> where('id',$data_id['my_user_id']) -> find();
                                $data_update['pid'] = $my_user_phone['id'];
                                $data_update['bind_time'] = time();
                                $data_update['bind_status'] = 1;
                                Db::name('my_user') -> where('uid',$uid) -> update($data_update);
                            }
                        }
                    }
                }
王晓刚 authored
282
            }*/
王晓刚 authored
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
        }else{
            //判断跳转链接是否存在数据 如果不存在添加一条自由人用户
            if(!empty($data_id)){
                //判断上级权限是否存在
                $my_user = Db::name('my_user') -> where('id',$data_id['my_user_id']) -> find();
                if(empty($my_user)){
                    $this->error('查无此人','','','');
                }
                if($my_user['status'] != $data_id['status_id']){
                    $this->error('该二维码已失效','','','');
                }
                //判断数据是老师还是业务员
                if($data_id['status_id'] == 2){
                    //业务员分享 添加老师
                    $my_user_phone = Db::name('my_user') -> where('id',$data_id['my_user_id']) -> find();
                    $data_inser['status'] = 3;
                    $data_inser['uid'] = $uid;
                    $data_inser['inviter_phone'] = $my_user_phone['phone'];
                    $data_inser['create_time'] = time();
                    $data_inser['is_pro'] = 1;
                    $data_inser['pid'] = $my_user_phone['id'];
                    $data_inser['bind_time'] = time();
                    $data_inser['bind_status'] = 1;
                    Db::name('my_user') -> insert($data_inser);
                }elseif ($data_id['status_id'] == 3){
                    //老师分享 添加学生
                    $my_user_phone = Db::name('my_user') -> where('id',$data_id['my_user_id']) -> find();
                    $data_inser['status'] = 4;
                    $data_inser['uid'] = $uid;
                    $data_inser['inviter_phone'] = $my_user_phone['phone'];
                    $data_inser['create_time'] = time();
                    $data_inser['is_pro'] = 0;
                    $data_inser['pid'] = $my_user_phone['id'];
                    $data_inser['bind_time'] = time();
                    $data_inser['bind_status'] = 1;
                    Db::name('my_user') -> insert($data_inser);
                }
            }else{
                $data_inser['status'] = 0;
                $data_inser['uid'] = $uid;
                $data_inser['create_time'] = time();
                $data_inser['is_pro'] = 0;
                Db::name('my_user') -> insert($data_inser);
            }

        }
    }
王晓刚 authored
330
}