Wxauth.class.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * 微信oAuth认证示例
  4. * 官方文档:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html
  5. * UCToo示例:http://git.oschina.net/uctoo/uctoo/blob/master/Addons/Ucuser/UcuserAddon.class.php
  6. *
  7. * 微信oAuth认证类,适配Thinkphp框架,
  8. * @命名空间版本
  9. * @author uctoo (www.uctoo.com)
  10. * @date 2015-5-15 14:10
  11. */
  12. namespace Com;
  13. class Wxauth {
  14. private $options;
  15. public $open_id;
  16. public $wxuser;
  17. public function __construct($options){
  18. $this->options = $options;
  19. $this->wxoauth();
  20. }
  21. public function wxoauth(){
  22. $scope = 'snsapi_base';
  23. $code = isset($_GET['code'])?$_GET['code']:'';
  24. $token_time = isset($_SESSION['token_time'])?$_SESSION['token_time']:0;
  25. if(!$code && isset($_SESSION['open_id']) && isset($_SESSION['user_token']) && $token_time>time()-3600)
  26. {
  27. if (!$this->wxuser) {
  28. $this->wxuser = $_SESSION['wxuser'];
  29. }
  30. $this->open_id = $_SESSION['open_id'];
  31. return $this->open_id;
  32. }
  33. else
  34. {
  35. $options = array(
  36. 'token'=>$this->options["token"], //填写你设定的key
  37. 'encodingaeskey'=>$this->options["encodingaeskey"], //填写加密用的EncodingAESKey
  38. 'appid'=>$this->options["appid"], //填写高级调用功能的app id
  39. 'appsecret'=>$this->options["appsecret"] //填写高级调用功能的密钥
  40. );
  41. $we_obj = new TPWechat($options);
  42. if ($code) {
  43. $json = $we_obj->getOauthAccessToken();
  44. if (!$json) {
  45. unset($_SESSION['wx_redirect']);
  46. die('获取用户授权失败,请重新确认');
  47. }
  48. $_SESSION['open_id'] = $this->open_id = $json["openid"];
  49. $access_token = $json['access_token'];
  50. $_SESSION['user_token'] = $access_token;
  51. $_SESSION['token_time'] = time();
  52. $userinfo = $we_obj->getUserInfo($this->open_id);
  53. if ($userinfo && !empty($userinfo['nickname'])) {
  54. $this->wxuser = array(
  55. 'open_id'=>$this->open_id,
  56. 'nickname'=>$userinfo['nickname'],
  57. 'sex'=>intval($userinfo['sex']),
  58. 'location'=>$userinfo['province'].'-'.$userinfo['city'],
  59. 'avatar'=>$userinfo['headimgurl']
  60. );
  61. } elseif (strstr($json['scope'],'snsapi_userinfo')!==false) {
  62. $userinfo = $we_obj->getOauthUserinfo($access_token,$this->open_id);
  63. if ($userinfo && !empty($userinfo['nickname'])) {
  64. $this->wxuser = array(
  65. 'open_id'=>$this->open_id,
  66. 'nickname'=>$userinfo['nickname'],
  67. 'sex'=>intval($userinfo['sex']),
  68. 'location'=>$userinfo['province'].'-'.$userinfo['city'],
  69. 'avatar'=>$userinfo['headimgurl']
  70. );
  71. } else {
  72. return $this->open_id;
  73. }
  74. }
  75. if ($this->wxuser) {
  76. $_SESSION['wxuser'] = $this->wxuser;
  77. $_SESSION['open_id'] = $json["openid"];
  78. unset($_SESSION['wx_redirect']);
  79. return $this->open_id;
  80. }
  81. $scope = 'snsapi_userinfo';
  82. }
  83. if ($scope=='snsapi_base') {
  84. $url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
  85. $_SESSION['wx_redirect'] = $url;
  86. } else {
  87. $url = $_SESSION['wx_redirect'];
  88. }
  89. if (!$url) {
  90. unset($_SESSION['wx_redirect']);
  91. die('获取用户授权失败');
  92. }
  93. $oauth_url = $we_obj->getOauthRedirect($url,"wxbase",$scope);
  94. redirect ( $oauth_url );
  95. }
  96. }
  97. }
  98. //$options = array(
  99. // 'token'=>'uctoo', //填写你设定的key
  100. // 'appid'=>'wxdk1234567890', //填写高级调用功能的app id, 请在微信开发模式后台查询
  101. // 'appsecret'=>'xxxxxxxxxxxxxxxxxxx', //填写高级调用功能的密钥
  102. //);
  103. //$auth = new Wxauth($options);
  104. //var_dump($auth->wxuser);