<?php
$appid = "请输入您企业的appid";
$secret = "请输入您企业的secreat";
if (!isset($_GET['code'])) {
$this->redirect($this->qychat->getOauthRedirect($this->getUrl(), 'STATE', 'snsapi_userinfo', '1000003'));
}
$code = $_GET["code"]; //获取登录进来的用户code
header('content-type:text/html;charset=utf-8');//一定要加上这段代码,一定一定切记切记
$get_token_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' . $appid . '&corpsecret=' . $secret . '';
$access_token = json_decode(file_get_contents($get_token_url));
$access_tokens = $access_token->access_token;
//print_r( $access_tokens );
$get_Userid = 'https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=' . $access_tokens . '&code=' . $code . '&agentid=1000003';
$userIds = json_decode(file_get_contents($get_Userid));
$users = $userIds->UserId;
if ($userIds->UserId) {
//print_r($users );
$url = "http://www.baidu.com/";
echo '<script language="JavaScript">window.location.href="跳转的页面index.html?userId=' . $users . '";</script>';
} else if ($userIds->OpenId) {
//print_r("error");
echo '<script language="JavaScript">alter("您当前未授权,请联系管理员!");</script>';
}
/*
$openid = $access_token->openid;
$get_user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_tokens.'&openid='.$openid.'&lang=zh_CN'; //获取用户信息的url
$user_info = json_decode(file_get_contents($get_user_info_url));
//print_r($user_info);
$_SESSION['user'] = $user_info;
echo $user_info->nickname;//得到用户信息
echo $user_info->headimgurl;
*/
public
function getOauthRedirect($callback, $state = 'STATE', $scope = 'snsapi_base', $agentid = '')
{
if ($agentid == '') {
$agentid = $this->agentid;
}
$url = self::OAUTH_PREFIX . self::OAUTH_AUTHORIZE_URL . 'appid=' . $this->appid . '&redirect_uri=' . urlencode($callback) . '&response_type=code&scope=' . $scope . '&agentid=' . $agentid . '&state=' . $state . '#wechat_redirect';
return $url;
/**
* 跳转url
*/
public
function redirect($url = '/')
{
Header('Location: ' . $url);
exit;
}
}
?>