<?php

namespace Home\Controller;

use Think\Controller;

class ComController extends Controller
{
    public $iphone;

    public function _initialize()
    {
        header("Access-Control-Allow-Origin:*");
        header("Access-Control-Allow-Methods:GET, POST, OPTIONS, DELETE");
        header("Access-Control-Allow-Headers:DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type, Accept-Language, Origin, Accept-Encoding");
//        $_SESSION['phone'] = '17621295894';
//        $_SESSION['permissionLabel'] = 1;
    }

    public function delphone()
    {
        $_SESSION['permissionLabel'] = '';
        $_SESSION['idopenid'] = '';
        $_SESSION['phone'] = '';
        $_SESSION['openid'] = '';
    }

    public function getAuthorization()
    {
        $R['flag'] = true;
//        $_SESSION['permissionLabel']='';
        $R['permissionLabel'] = $_SESSION['permissionLabel'];
        $R['msg'] = '当前用户权限';
        echo json_encode($R);
    }
    
    public function getPageAuthorization1()
    {
        $http = 'https://';
        $Data = I('');
        $code = $Data['code'];
        $key = $Data['key'];
        $secret = $Data['secret'];
        if (!empty($key) && !empty($secret)) {
            $gettoken = M()->query("SELECT id,`wc_key`,wc_secret FROM wc_key_secret WHERE `wc_key`='$key' AND wc_secret='$secret'");
            if (count($gettoken) > 0) {
                $token = md5($key . uniqid() . $secret);
                $res = M()->execute("insert into wc_user_token(token,`create_time`) VALUES('$token',NOW())");
                if ($res > 0) {
                    $code = true;
                    $messign = 'token Successfully generated';
                    $val = $token;
                } else {
                    $code = false;
                    $messign = 'token Build failed';
                }
            } else {
                $code = false;
                $messign = 'key-secret Validation failed';
            }
            echo json_encode(array('code' => $code, 'messign' => $messign, 'token' => $val));
            exit;
        } else {

            if (!strpos($_SERVER['HTTP_USER_AGENT'], "MicroMessenger") != false) {//PC端
                if (!empty($_SESSION['permissionLabel']) && !empty($_SESSION['phone']) && !empty($_SESSION['openid']) && !empty($_SESSION['idopenid'])) {
//                    print_r($_SESSION);
//                    exit();
                    header('Location:https://wx.ewoogi.com/index.html#/pages/index/index?op=' . $_SESSION['openid']);//项目首页
                } else {
                    header('Location:https://wx.ewoogi.com/index.html#/pages/login/login');//登录页
                }
                exit();
            } else {//微信
                if (isset($_SESSION['phone']) && !empty($_SESSION['phone'])) {
                    $res = M()->query("SELECT phone,open_id FROM wc_user WHERE enable=1 and phone='" . $_SESSION['phone'] . "' and registration_type='wx'");
                    if (count($res) > 0) {
                        $_SESSION['idopenid'] = $res[0]['open_id'];
                        $_SESSION['openid'] = $res[0]['open_id'];
                        $_SESSION['phone'] = $res[0]['phone'];
                        $res1 = M()->query("select personnel_function from personnel where enable = 1 and  personnel_phone='" . $res[0]['phone'] . "' limit 1");
                        if (!empty($res1)) {
                            $_SESSION['permissionLabel'] = $res1[0]['personnel_function'];
                            header('Location:https://wx.ewoogi.com/index.html#/pages/index/index?op=' . $_SESSION['openid']);//项目首页
                        } else {
//                            print_r($res1);
//                            print_r("c");
//                            print_r($_SESSION);
//                            exit();
                            header("Location: https://wx.ewoogi.com/index.html#/pages/authority/authority");//未授权页
                        }
                    } else {
                        unset($_SESSION['idopenid']);
                        unset($_SESSION['openid']);
                        unset($_SESSION['phone']);
                        header('Location:https://wx.ewoogi.com/index.html#/pages/login/login');//登录页
                    }
                    exit();
                } else if (!isset($_SESSION['idopenid']) && empty($_SESSION['idopenid'])) {
                    if (empty($code)) {
                        $tourl = urlencode($http . $_SERVER['SERVER_NAME'] . $_SERVER["REQUEST_URI"]);
                        header('Location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx4eab2e3b5531d58b&redirect_uri=' . $tourl . '&response_type=code&scope=snsapi_base&state=abc123#wechat_redirect ');
                    } else {
                        $user_openid = $this->s_curl('https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx4eab2e3b5531d58b&secret=1026580a52dc89f564395113017c165b&code=' . $code . '&grant_type=authorization_code');
                        $res = M()->query("SELECT phone,open_id FROM wc_user WHERE enable=1 and open_id='" . $user_openid['openid'] . "' and registration_type='wx'");
                        if (count($res) > 0) {
                            $_SESSION['idopenid'] = $res[0]['open_id'];
                            $_SESSION['openid'] = $res[0]['open_id'];
                            $_SESSION['phone'] = $res[0]['phone'];
                            $res1 = M()->query("select personnel_function from personnel where enable=1 and  personnel_phone='" . $res[0]['phone'] . "' limit 1");
                            if (!empty($res1)) {
                                $_SESSION['permissionLabel'] = $res1[0]['personnel_function'];
                                header('Location:https://wx.ewoogi.com/index.html#/pages/index/index?op=' . $_SESSION['openid']);//项目首页
                            } else {
                                header("Location: https://wx.ewoogi.com/index.html#/pages/authority/authority");//未授权页
                            }
                        } else {
                            unset($_SESSION['idopenid']);
                            unset($_SESSION['openid']);
                            unset($_SESSION['phone']);
                            header('Location:https://wx.ewoogi.com/index.html#/pages/login/login');//登录页
                        }
                    }
                } else {
                    $res = M()->query("SELECT phone,open_id FROM wc_user WHERE enable=1 and openid='" . $_SESSION['idopenid'] . "' and registration_type='wx'");
                    $_SESSION['idopenid'] = $res[0]['open_id'];
                    $_SESSION['openid'] = $res[0]['open_id'];
                    $_SESSION['phone'] = $res[0]['phone'];
                    if (count($res) > 0) {
                        $res1 = M()->query("select personnel_function from personnel where enable=1 and  personnel_phone='" . $res[0]['phone'] . "' limit 1");
                        if (!empty($res1)) {
                            $_SESSION['permissionLabel'] = $res1[0]['personnel_function'];
                            header('Location: https://wx.ewoogi.com/index.html#/pages/index/index?op=' . $_SESSION['openid']);//项目首页
                        } else {
                            header("Location: https://wx.ewoogi.com/index.html#/pages/authority/authority");//未授权页
                        }
                    } else {
                        unset($_SESSION['idopenid']);
                        unset($_SESSION['openid']);
                        unset($_SESSION['phone']);
                        header('Location:https://wx.ewoogi.com/index.html#/pages/login/login');//登录页
                    }
                }
            }
        }

    }

    private function s_curl($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
        $file_contents = curl_exec($ch);
        curl_close($ch);
        $arr = json_decode($file_contents, true);
        return $arr;
    }
}