Ai
2 Star 0 Fork 0

ijijni/wechat-php-sdk11

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Wxauth.class.php 3.46 KB
一键复制 编辑 原始数据 按行查看 历史
uctoo 提交于 2015-05-22 19:14 +08:00 . jssdk支付类V3版本,oAuth认证类适配Thinkphp
<?php
/**
* 微信oAuth认证示例
* 官方文档:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html
* UCToo示例:http://git.oschina.net/uctoo/uctoo/blob/master/Addons/Ucuser/UcuserAddon.class.php
*
* 微信oAuth认证类,适配Thinkphp框架,
* @命名空间版本
* @author uctoo (www.uctoo.com)
* @date 2015-5-15 14:10
*/
namespace Com;
class Wxauth {
private $options;
public $open_id;
public $wxuser;
public function __construct($options){
$this->options = $options;
$this->wxoauth();
}
public function wxoauth(){
$scope = 'snsapi_base';
$code = isset($_GET['code'])?$_GET['code']:'';
$token_time = isset($_SESSION['token_time'])?$_SESSION['token_time']:0;
if(!$code && isset($_SESSION['open_id']) && isset($_SESSION['user_token']) && $token_time>time()-3600)
{
if (!$this->wxuser) {
$this->wxuser = $_SESSION['wxuser'];
}
$this->open_id = $_SESSION['open_id'];
return $this->open_id;
}
else
{
$options = array(
'token'=>$this->options["token"], //填写你设定的key
'encodingaeskey'=>$this->options["encodingaeskey"], //填写加密用的EncodingAESKey
'appid'=>$this->options["appid"], //填写高级调用功能的app id
'appsecret'=>$this->options["appsecret"] //填写高级调用功能的密钥
);
$we_obj = new TPWechat($options);
if ($code) {
$json = $we_obj->getOauthAccessToken();
if (!$json) {
unset($_SESSION['wx_redirect']);
die('获取用户授权失败,请重新确认');
}
$_SESSION['open_id'] = $this->open_id = $json["openid"];
$access_token = $json['access_token'];
$_SESSION['user_token'] = $access_token;
$_SESSION['token_time'] = time();
$userinfo = $we_obj->getUserInfo($this->open_id);
if ($userinfo && !empty($userinfo['nickname'])) {
$this->wxuser = array(
'open_id'=>$this->open_id,
'nickname'=>$userinfo['nickname'],
'sex'=>intval($userinfo['sex']),
'location'=>$userinfo['province'].'-'.$userinfo['city'],
'avatar'=>$userinfo['headimgurl']
);
} elseif (strstr($json['scope'],'snsapi_userinfo')!==false) {
$userinfo = $we_obj->getOauthUserinfo($access_token,$this->open_id);
if ($userinfo && !empty($userinfo['nickname'])) {
$this->wxuser = array(
'open_id'=>$this->open_id,
'nickname'=>$userinfo['nickname'],
'sex'=>intval($userinfo['sex']),
'location'=>$userinfo['province'].'-'.$userinfo['city'],
'avatar'=>$userinfo['headimgurl']
);
} else {
return $this->open_id;
}
}
if ($this->wxuser) {
$_SESSION['wxuser'] = $this->wxuser;
$_SESSION['open_id'] = $json["openid"];
unset($_SESSION['wx_redirect']);
return $this->open_id;
}
$scope = 'snsapi_userinfo';
}
if ($scope=='snsapi_base') {
$url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$_SESSION['wx_redirect'] = $url;
} else {
$url = $_SESSION['wx_redirect'];
}
if (!$url) {
unset($_SESSION['wx_redirect']);
die('获取用户授权失败');
}
$oauth_url = $we_obj->getOauthRedirect($url,"wxbase",$scope);
redirect ( $oauth_url );
}
}
}
//$options = array(
// 'token'=>'uctoo', //填写你设定的key
// 'appid'=>'wxdk1234567890', //填写高级调用功能的app id, 请在微信开发模式后台查询
// 'appsecret'=>'xxxxxxxxxxxxxxxxxxx', //填写高级调用功能的密钥
//);
//$auth = new Wxauth($options);
//var_dump($auth->wxuser);
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PHP
1
https://gitee.com/ijijni/wechat-php-sdk.git
git@gitee.com:ijijni/wechat-php-sdk.git
ijijni
wechat-php-sdk
wechat-php-sdk11
master

搜索帮助