Wechat.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. protected $getAuthCodeUrl = "https://open.weixin.qq.com/connect/oauth2/authorize";
  15. protected $isWechat = true;
  16. protected $isWeb = false;
  17. /**
  18. * 配置信息
  19. * @var array
  20. */
  21. private $config = [];
  22. public function __construct($options = [])
  23. {
  24. if ($config = Config::get('third.wechat')) {
  25. $this->config = array_merge($this->config, $config);
  26. }
  27. $this->config = array_merge($this->config, is_array($options) ? $options : []);
  28. $this->isWechat = Service::isWechat();
  29. if (!$this->isWechat && request()->server('HTTP_USER_AGENT') && !request()->header('sid')) {
  30. //不在微信内调用PC端扫码登录
  31. $config = get_addon_config('third');
  32. $this->config = array_merge($this->config, $config['wechatweb'] ?? []);
  33. $this->isWeb = true;
  34. $this->getAuthCodeUrl = "https://open.weixin.qq.com/connect/qrconnect";
  35. }
  36. }
  37. /**
  38. * 登陆
  39. */
  40. public function login()
  41. {
  42. header("Location:" . $this->getAuthorizeUrl());
  43. }
  44. /**
  45. * 获取authorize_url
  46. */
  47. public function getAuthorizeUrl()
  48. {
  49. $state = md5(uniqid(rand(), true));
  50. Session::set('state', $state);
  51. $queryarr = array(
  52. "appid" => $this->config['app_id'],
  53. "redirect_uri" => $this->config['callback'],
  54. "response_type" => "code",
  55. "scope" => $this->config['scope'],
  56. "state" => $state,
  57. );
  58. if ($this->isWeb) {
  59. //PC端应用scope固定值
  60. $queryarr['scope'] = "snsapi_login";
  61. }
  62. $url = $this->getAuthCodeUrl . '?' . http_build_query($queryarr) . '#wechat_redirect';
  63. return $url;
  64. }
  65. /**
  66. * 获取用户信息
  67. * @param array $params
  68. * @return array
  69. */
  70. public function getUserInfo($params = [])
  71. {
  72. $params = $params ?: request()->get('', null, null);
  73. if (isset($params['access_token']) || (isset($params['state']) && $params['state'] == Session::get('state') && isset($params['code']))) {
  74. //获取access_token
  75. $data = isset($params['code']) ? $this->getAccessToken($params['code']) : $params;
  76. $access_token = $data['access_token'] ?? '';
  77. $refresh_token = $data['refresh_token'] ?? '';
  78. $expires_in = $data['expires_in'] ?? 0;
  79. if ($access_token) {
  80. $openid = $data['openid'] ?? '';
  81. $unionid = $data['unionid'] ?? '';
  82. if (preg_match("/(snsapi_userinfo|snsapi_login)/i", $this->config['scope'])) {
  83. //获取用户信息
  84. $queryarr = [
  85. "access_token" => $access_token,
  86. "openid" => $openid,
  87. "lang" => 'zh_CN'
  88. ];
  89. $ret = Http::get(self::GET_USERINFO_URL, $queryarr);
  90. $userinfo = (array)json_decode($ret, true);
  91. if (!$userinfo || isset($userinfo['errcode'])) {
  92. return [];
  93. }
  94. $userinfo['avatar'] = $userinfo['headimgurl'] ?? '';
  95. } else {
  96. $userinfo = [];
  97. }
  98. $data = [
  99. 'access_token' => $access_token,
  100. 'refresh_token' => $refresh_token,
  101. 'expires_in' => $expires_in,
  102. 'openid' => $openid,
  103. 'unionid' => $unionid,
  104. 'userinfo' => $userinfo,
  105. 'apptype' => $this->isWeb ? 'web' : 'mp' //自定义 web:网页应用 mp 公众号
  106. ];
  107. return $data;
  108. }
  109. }
  110. return [];
  111. }
  112. /**
  113. * 获取access_token
  114. * @param string $code code
  115. * @return array
  116. */
  117. public function getAccessToken($code = '')
  118. {
  119. if (!$code) {
  120. return [];
  121. }
  122. $queryarr = array(
  123. "appid" => $this->config['app_id'],
  124. "secret" => $this->config['app_secret'],
  125. "code" => $code,
  126. "grant_type" => "authorization_code",
  127. );
  128. $response = Http::get(self::GET_ACCESS_TOKEN_URL, $queryarr);
  129. $ret = (array)json_decode($response, true);
  130. return $ret ?: [];
  131. }
  132. }