public function wxlogin($code = null)
    {
        //判断是否微信浏览器打开,并判断是否登录
        if ((strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger')) !== false && empty($_SESSION['openid']) || empty($_SESSION['userid']) || empty($_SESSION['username'])) {
       //引导用户进入授权页面同意授权,获取code
            if ($code !== null) {
                //获取openID
                $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?';
                $params = array(
                    'appid'     => '',
                    'secret'    => '',
                    'code'      => $code,
                    'grant_type'=> 'authorization_code',
                );
                $openId = json_decode(file_get_contents($url . http_build_query($params)), true);
                if(!isset($openId['openid'])) $openId['openid'] = -1;
                //设置session
                $_SESSION['openid']  = $openId['openid'];
                //判断数据库是否存在该用户信息
                $url = 'http://'.SITE_DOMAIN.'user/isuser?';
                $params = array(
                    'openid'     => $openId['openid'],
                    'token'      => md5('cunzaiganit.com'),
                );
                $user = json_decode(file_get_contents($url . http_build_query($params)), true);
                //如果数据库没有该用户信息
                if($user['result']){
                    //获取用户信息
                    $url = 'https://api.weixin.qq.com/sns/userinfo?';
                    $params = array(
                        'access_token' => $openId['access_token'],
                        'openid' => $openId['openid'],
                        'lang' => 'zh_CN'
                    );
                    $info = json_decode(file_get_contents($url . http_build_query($params)), true);

                    //将信息存数据库
                    $url = 'http://'.SITE_DOMAIN.'user/adduser?';
                    $params = array(
                        'openid'     => $info['openid'],
                        'name'     => $info['nickname'],
                        'login_name'     => $info['nickname'],
                        'sex'     => $info['sex'],
                        'province'     => $info['province'],
                        'city'     => $info['city'],
                        'country'     => $info['country'],
                        'headlink'     => $info['headimgurl'],
                    );
                    $rows = json_decode(file_get_contents($url . http_build_query($params)),true);
                    $_SESSION['userid']  = $rows['id'];
                    $_SESSION['username']  = $rows['name'];
                } else {
                    //如果有该用户
                    $_SESSION['userid']  = $user['id'];
                    $_SESSION['username']  = $user['name'];
                }
            } else {
                $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?';

                $params = array(
                    'appid'         => '',
                    'redirect_uri'  => 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'],
                    'response_type' => 'code',
                    'scope'         => 'snsapi_userinfo',
                    'state'         => md5('baidu.com') . '#wechat_redirect'
                );
                die(header('Location:'.$url . http_build_query($params)));
            }
        }
    }