Wechat.class.
[code lang=”
}
$result = $this->http_get(self::API_BASE_URL_PREFIX.self::OAUTH_USERINFO_URL.’access_token=’.$access_token.’&openid=’.$openid."&lang=zh_CN");
//var_dump($result);
if ($result)
{
$json = json_decode($result,true);
if (!$json || !empty($json[‘errcode’])) {
$this->errCode = $json[‘errcode’];
$this->errMsg = $json[‘errmsg’];
return false;
}
return $json;
}
return false;
}
public function get_token(){//
WechatController.class.php
[code lang=”php”]
static function do_oauth()
{
// 默认公众号信息
$wxinfo = model(‘Base’)->model->table(‘wechat’)
->field(‘id, token, appid, appsecret, oauth_redirecturi, type, oauth_status’)
->where(‘default_wx = 1 and status = 1’)
->find();
if (! empty($wxinfo) && $wxinfo[‘type’] == 2) {
$config[‘token’] = $wxinfo[‘token’];
$config[‘appid’] = $wxinfo[‘appid’];
$config[‘appsecret’] = $wxinfo[‘appsecret’];
// 微信通验证
$weObj = new Wechat($config);
// 微信浏览器浏览
if (self::is_wechat_browser() && ($_SESSION[‘user_id’] === 0 || empty($_SESSION[‘openid’])) && !empty($wxinfo[‘oauth_status’])) {
if (! isset($_SESSION[‘redirect_url’])) {
session(‘redirect_url’, __HOST__ . $_SERVER[‘REQUEST_URI’]);
}
$url = $weObj->getOauthRedirect($wxinfo[‘oauth_redirecturi’], 1,’snsapi_base’);
if (isset($_GET[‘code’]) && !empty($_GET[‘code’])) {
$token = $weObj->getOauthAccessToken();
if ($token) {
$userinfo = $weObj->getOauthUserinfo($token[‘access_token’], $token[‘openid’],’snsapi_base’);
//var_dump($userinfo);
// exit;
self::update_weixin_user($userinfo, $wxinfo[‘id’], $weObj);
if (! empty($_SESSION[‘redirect_url’])) {
$redirect_url = session(‘redirect_url’);
header(‘Location:’ . $redirect_url, true, 302);
exit();
}
} else {
header(‘Location:’ . $url, true, 302);
exit();
}
} else {
// var_dump($url);
header(‘Location:’ . $url);
exit();
}
}
}
}
[/code]
这里去掉 ‘snsapi_base’ 就变为原来的 snsapi_userinfo 授权页面登录。