class User extends Common
{
public $db;
function __construct(){
parent::__construct();
$this->db=Db::name('user');
}
// 渲染主页
public function index()
{
$appid=config('appID');
//可以调用到getUserInfo的url
$redirect_uri = urlencode (config('REDIRECT_URL'));
$url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
$this->redirect($url);
// header("Location:".$url);
}
//新用户注册
public function getUserInfo(){
// echo 6666;
$appid = config('appID');
$secret = config('appsecret');
// print_r($appid);die;
$code = $_GET["code"];
//第一步:取全局access_token
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
$token = getJson($url);
//第二步:取得openid
$oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
$oauth2 = getJson($oauth2Url);
//第三步:根据全局access_token和openid查询用户信息
$access_token = $token["access_token"];
$openid = $oauth2['openid'];
$get_user_info_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";
$userinfo = getJson($get_user_info_url);
print_r($userinfo);
}
}
注意!!!!微信公众号授权跳转接口地址时最好使用redirect,header会出现莫名其妙的问题,尤其在TP框架中