WeixinAuth.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace common\modules\user\clients;
  3. use yii\authclient\OAuth2;
  4. class WeixinAuth extends OAuth2
  5. {
  6. public $authUrl = 'https://open.weixin.qq.com/connect/qrconnect';
  7. public $tokenUrl = 'https://api.weixin.qq.com/sns/oauth2/access_token';
  8. public $apiBaseUrl = 'https://api.weixin.qq.com';
  9. public $scope = 'snsapi_login';
  10. /**
  11. * Composes user authorization URL.
  12. *
  13. * @param array $params
  14. * additional auth GET params.
  15. * @return string authorization URL.
  16. */
  17. public function buildAuthUrl(array $params = [])
  18. {
  19. $defaultParams = [
  20. 'appid' => $this->clientId,
  21. 'redirect_uri' => $this->getReturnUrl(),
  22. 'response_type' => 'code'
  23. ];
  24. if (! empty($this->scope)) {
  25. $defaultParams['scope'] = $this->scope;
  26. }
  27. return $this->composeUrl($this->authUrl, array_merge($defaultParams, $params));
  28. }
  29. /**
  30. * Fetches access token from authorization code.
  31. *
  32. * @param string $authCode
  33. * authorization code, usually comes at $_GET['code'].
  34. * @param array $params
  35. * additional request params.
  36. * @return OAuthToken access token.
  37. */
  38. public function fetchAccessToken($authCode, array $params = [])
  39. {
  40. $defaultParams = [
  41. 'appid' => $this->clientId,
  42. 'secret' => $this->clientSecret,
  43. 'code' => $authCode,
  44. 'grant_type' => 'authorization_code'
  45. ];
  46. $response = $this->sendRequest('POST', $this->tokenUrl, array_merge($defaultParams, $params));
  47. $token = $this->createToken([
  48. 'params' => $response
  49. ]);
  50. $this->setAccessToken($token);
  51. return $token;
  52. }
  53. /**
  54. * @inheritdoc
  55. */
  56. protected function apiInternal($accessToken, $url, $method, array $params, array $headers)
  57. {
  58. $params['access_token'] = $accessToken->getToken();
  59. $params['openid'] = $this->getOpenid();
  60. return $this->sendRequest($method, $url, $params, $headers);
  61. }
  62. /**
  63. *
  64. * @return []
  65. * @see https://open.weixin.qq.com/cgi-bin/showdocument?action=doc&id=open1419316518&t=0.14920092844688204
  66. */
  67. protected function initUserAttributes()
  68. {
  69. $attributes = $this->api('sns/userinfo');
  70. $attributes['id'] = $attributes['openid'];
  71. return $attributes;
  72. }
  73. /**
  74. * @inheritdoc
  75. */
  76. public function getEmail()
  77. {
  78. return isset($this->getUserAttributes()['email']) ? $this->getUserAttributes()['email'] : null;
  79. }
  80. /**
  81. * @inheritdoc
  82. */
  83. public function getUsername()
  84. {
  85. return isset($this->getUserAttributes()['login']) ? $this->getUserAttributes()['login'] : null;
  86. }
  87. protected function defaultName()
  88. {
  89. return 'weixin';
  90. }
  91. protected function defaultTitle()
  92. {
  93. return '微信';
  94. }
  95. }