Qq.php 4.5 KB

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