Wechat.php 4.2 KB

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