Qq.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 ?: request()->get('', null, null);
  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 = $data['access_token'] ?? '';
  64. $refresh_token = $data['refresh_token'] ?? '';
  65. $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['avatar'] = $userinfo['figureurl_qq_2'] ?? '';
  80. $data = [
  81. 'access_token' => $access_token,
  82. 'refresh_token' => $refresh_token,
  83. 'expires_in' => $expires_in,
  84. 'openid' => $openid,
  85. 'userinfo' => $userinfo
  86. ];
  87. return $data;
  88. }
  89. }
  90. return [];
  91. }
  92. /**
  93. * 获取access_token
  94. * @param string $code
  95. * @return array
  96. */
  97. public function getAccessToken($code = '')
  98. {
  99. if (!$code) {
  100. return [];
  101. }
  102. $queryarr = array(
  103. "grant_type" => "authorization_code",
  104. "client_id" => $this->config['app_id'],
  105. "client_secret" => $this->config['app_secret'],
  106. "redirect_uri" => $this->config['callback'],
  107. "code" => $code,
  108. );
  109. $ret = Http::get(self::GET_ACCESS_TOKEN_URL, $queryarr);
  110. $params = [];
  111. parse_str($ret, $params);
  112. return $params ?: [];
  113. }
  114. /**
  115. * 获取open_id
  116. * @param string $access_token
  117. * @return string
  118. */
  119. private function getOpenId($access_token = '')
  120. {
  121. $response = Http::get(self::GET_OPENID_URL, ['access_token' => $access_token]);
  122. if (strpos($response, "callback") !== false) {
  123. $lpos = strpos($response, "(");
  124. $rpos = strrpos($response, ")");
  125. $response = substr($response, $lpos + 1, $rpos - $lpos - 1);
  126. }
  127. $user = (array)json_decode($response, true);
  128. return $user['openid'] ?? '';
  129. }
  130. }