Qq.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace fast\third;
  3. use fast\Http;
  4. use think\Config;
  5. use think\Session;
  6. /**
  7. * QQ
  8. */
  9. class Qq
  10. {
  11. const GET_AUTH_CODE_URL = "https://graph.qq.com/oauth2.0/authorize";
  12. const GET_ACCESS_TOKEN_URL = "https://graph.qq.com/oauth2.0/token";
  13. const GET_USERINFO_URL = "https://graph.qq.com/user/get_user_info";
  14. const GET_OPENID_URL = "https://graph.qq.com/oauth2.0/me";
  15. /**
  16. * 配置信息
  17. * @var array
  18. */
  19. private $config = [];
  20. public function __construct($options = [])
  21. {
  22. if ($config = Config::get('third.qq'))
  23. {
  24. $this->config = array_merge($this->config, $config);
  25. }
  26. $this->config = array_merge($this->config, is_array($options) ? $options : []);
  27. }
  28. /**
  29. * 登陆
  30. */
  31. public function login()
  32. {
  33. header("Location:" . $this->getAuthorizeUrl());
  34. }
  35. /**
  36. * 获取authorize_url
  37. */
  38. public function getAuthorizeUrl()
  39. {
  40. $state = md5(uniqid(rand(), TRUE));
  41. Session::set('state', $state);
  42. $queryarr = array(
  43. "response_type" => "code",
  44. "client_id" => $this->config['app_id'],
  45. "redirect_uri" => $this->config['callback'],
  46. "scope" => $this->config['scope'],
  47. "state" => $state,
  48. );
  49. request()->isMobile() && $queryarr['display'] = 'mobile';
  50. $url = self::GET_AUTH_CODE_URL . '?' . http_build_query($queryarr);
  51. return $url;
  52. }
  53. /**
  54. * 获取用户信息
  55. * @param array $params
  56. * @return array
  57. */
  58. public function getUserInfo($params = [])
  59. {
  60. $params = $params ? $params : $_GET;
  61. if (isset($params['access_token']) || (isset($params['state']) && $params['state'] == Session::get('state') && isset($params['code'])))
  62. {
  63. //获取access_token
  64. $data = isset($params['code']) ? $this->getAccessToken($params['code']) : $params;
  65. $access_token = isset($data['access_token']) ? $data['access_token'] : '';
  66. $refresh_token = isset($data['refresh_token']) ? $data['refresh_token'] : '';
  67. $expires_in = isset($data['expires_in']) ? $data['expires_in'] : 0;
  68. if ($access_token)
  69. {
  70. $openid = $this->getOpenId($access_token);
  71. //获取用户信息
  72. $queryarr = [
  73. "access_token" => $access_token,
  74. "oauth_consumer_key" => $this->config['app_id'],
  75. "openid" => $openid,
  76. ];
  77. $ret = Http::get(self::GET_USERINFO_URL, $queryarr);
  78. $userinfo = json_decode($ret, TRUE);
  79. if (!$userinfo || !isset($userinfo['ret']) || $userinfo['ret'] !== 0)
  80. return [];
  81. $userinfo = $userinfo ? $userinfo : [];
  82. $userinfo['avatar'] = isset($userinfo['figureurl_qq_2']) ? $userinfo['figureurl_qq_2'] : '';
  83. $data = [
  84. 'access_token' => $access_token,
  85. 'refresh_token' => $refresh_token,
  86. 'expires_in' => $expires_in,
  87. 'openid' => $openid,
  88. 'userinfo' => $userinfo
  89. ];
  90. return $data;
  91. }
  92. }
  93. return [];
  94. }
  95. /**
  96. * 获取access_token
  97. * @param string $code
  98. * @return array
  99. */
  100. private function getAccessToken($code = '')
  101. {
  102. if (!$code)
  103. return '';
  104. $queryarr = array(
  105. "grant_type" => "authorization_code",
  106. "client_id" => $this->config['app_id'],
  107. "client_secret" => $this->config['app_secret'],
  108. "redirect_uri" => $this->config['callback'],
  109. "code" => $code,
  110. );
  111. $ret = Http::get(self::GET_ACCESS_TOKEN_URL, $queryarr);
  112. $params = [];
  113. parse_str($ret, $params);
  114. return $params ? $params : [];
  115. }
  116. /**
  117. * 获取open_id
  118. * @param string $access_token
  119. * @return string
  120. */
  121. private function getOpenId($access_token = '')
  122. {
  123. $response = Http::get(self::GET_OPENID_URL, ['access_token' => $access_token]);
  124. if (strpos($response, "callback") !== false)
  125. {
  126. $lpos = strpos($response, "(");
  127. $rpos = strrpos($response, ")");
  128. $response = substr($response, $lpos + 1, $rpos - $lpos - 1);
  129. }
  130. $user = json_decode($response, TRUE);
  131. return isset($user['openid']) ? $user['openid'] : '';
  132. }
  133. }