Wechat.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace fast\third;
  3. use fast\Http;
  4. use think\Config;
  5. use think\Session;
  6. /**
  7. * 微博
  8. */
  9. class Wechat
  10. {
  11. const GET_AUTH_CODE_URL = "https://api.weixin.qq.com/sns/oauth2/authorize";
  12. const GET_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/access_token";
  13. const GET_USERINFO_URL = "https://api.weixin.qq.com/sns/userinfo";
  14. /**
  15. * 配置信息
  16. * @var array
  17. */
  18. private $config = [];
  19. public function __construct($options = [])
  20. {
  21. if ($config = Config::get('third.wechat'))
  22. {
  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. "app_id" => $this->config['app_id'],
  43. "redirect_uri" => $this->config['callback'],
  44. "response_type" => "code",
  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) . '#wechat_redirect';
  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. {
  62. //获取access_token
  63. $data = isset($params['code']) ? $this->getAccessToken($params['code']) : $params;
  64. $access_token = isset($data['access_token']) ? $data['access_token'] : '';
  65. $refresh_token = isset($data['refresh_token']) ? $data['refresh_token'] : '';
  66. $expires_in = isset($data['expires_in']) ? $data['expires_in'] : 0;
  67. if ($access_token)
  68. {
  69. $openid = isset($data['openid']) ? $data['openid'] : '';
  70. //获取用户信息
  71. $queryarr = [
  72. "access_token" => $access_token,
  73. "openid" => $openid,
  74. "lang" => 'zh_CN'
  75. ];
  76. $ret = Http::post(self::GET_USERINFO_URL, $queryarr);
  77. $userinfo = json_decode($ret, TRUE);
  78. if (!$userinfo || isset($userinfo['errcode']))
  79. return [];
  80. $userinfo = $userinfo ? $userinfo : [];
  81. $userinfo['avatar'] = isset($userinfo['headimgurl']) ? $userinfo['headimgurl'] : '';
  82. $data = [
  83. 'access_token' => $access_token,
  84. 'refresh_token' => $refresh_token,
  85. 'expires_in' => $expires_in,
  86. 'openid' => $openid,
  87. 'userinfo' => $userinfo
  88. ];
  89. return $data;
  90. }
  91. }
  92. return [];
  93. }
  94. /**
  95. * 获取access_token
  96. * @param string code
  97. * @return array
  98. */
  99. private function getAccessToken($code = '')
  100. {
  101. if (!$code)
  102. return '';
  103. $queryarr = array(
  104. "appid" => $this->config['app_id'],
  105. "secret" => $this->config['app_secret'],
  106. "code" => $code,
  107. "grant_type" => "authorization_code",
  108. );
  109. $response = Http::post(self::GET_ACCESS_TOKEN_URL, $queryarr);
  110. $ret = json_decode($response, TRUE);
  111. return $ret ? $ret : [];
  112. }
  113. }