Skip to content

Commit 9d5f281

Browse files
committed
更新,简化代码
1 parent ce426ff commit 9d5f281

File tree

1 file changed

+4
-112
lines changed

1 file changed

+4
-112
lines changed

src/oauth/OAuth.php

Lines changed: 4 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace xutl\wechat\oauth;
99

1010
use Yii;
11-
use yii\authclient\OAuth2;
11+
use xutl\authclient\WeChat;
1212
use yii\authclient\OAuthToken;
1313

1414
/**
@@ -18,7 +18,7 @@
1818
* @author Tongle Xu <xutongle@gmail.com>
1919
* @since 1.0
2020
*/
21-
class OAuth extends OAuth2
21+
class OAuth extends WeChat
2222
{
2323
/**
2424
* @inheritdoc
@@ -48,25 +48,8 @@ class OAuth extends OAuth2
4848
/**
4949
* @inheritdoc
5050
*/
51-
protected function defaultName()
52-
{
53-
return 'wechat';
54-
}
55-
56-
/**
57-
* @inheritdoc
58-
*/
59-
protected function defaultTitle()
60-
{
61-
return Yii::t('app', 'Wechat');
62-
}
63-
64-
/**
65-
* 初始化组件
66-
*/
6751
public function init()
6852
{
69-
parent::init();
7053
if (empty ($this->clientId)) {
7154
$this->clientId = Yii::$app->wechat->appId;
7255
}
@@ -76,6 +59,7 @@ public function init()
7659
if ($this->scope === null) {
7760
$this->scope = 'snsapi_userinfo';
7861
}
62+
parent::init();
7963
}
8064

8165
/**
@@ -98,8 +82,8 @@ public function buildAuthUrl(array $params = [])
9882
{
9983
$defaultParams = [
10084
'appid' => $this->clientId,
101-
'redirect_uri' => $this->getReturnUrl(),
10285
'response_type' => 'code',
86+
'redirect_uri' => $this->getReturnUrl(),
10387
];
10488
if (!empty($this->scope)) {
10589
$defaultParams['scope'] = $this->scope;
@@ -113,96 +97,4 @@ public function buildAuthUrl(array $params = [])
11397
$authUrl = $this->composeUrl($this->authUrl, array_merge($defaultParams, $params));
11498
return $authUrl . '#wechat_redirect';
11599
}
116-
117-
/**
118-
* Composes URL from base URL and GET params.
119-
* @param string $url base URL.
120-
* @param array $params GET params.
121-
* @return string composed URL.
122-
*/
123-
protected function composeUrl($url, array $params = [])
124-
{
125-
if (!empty($params)) {
126-
if (strpos($url, '?') === false) {
127-
$url .= '?';
128-
} else {
129-
$url .= '&';
130-
}
131-
$url .= http_build_query($params, '', '&', PHP_QUERY_RFC3986);
132-
}
133-
return $url;
134-
}
135-
136-
/**
137-
* @param \yii\httpclient\Request $request HTTP request instance.
138-
* @param OAuthToken $accessToken access token instance.
139-
*/
140-
public function applyAccessTokenToRequest($request, $accessToken)
141-
{
142-
$data = $request->getData();
143-
$data['access_token'] = $accessToken->getToken();
144-
$data['openid'] = $accessToken->getParam('openid');
145-
$request->setData($data);
146-
}
147-
148-
/**
149-
* Applies client credentials (e.g. [[clientId]] and [[clientSecret]]) to the HTTP request instance.
150-
* This method should be invoked before sending any HTTP request, which requires client credentials.
151-
* @param \yii\httpclient\Request $request HTTP request instance.
152-
* @since 2.1.3
153-
*/
154-
protected function applyClientCredentialsToRequest($request)
155-
{
156-
$request->addData([
157-
'appid' => $this->clientId,
158-
'secret' => $this->clientSecret,
159-
]);
160-
}
161-
162-
/**
163-
* 返回OpenId
164-
* @return mixed
165-
*/
166-
public function getOpenId()
167-
{
168-
return $this->getAccessToken()->getParam('openid');
169-
}
170-
171-
/**
172-
* @inheritdoc
173-
*/
174-
protected function initUserAttributes()
175-
{
176-
return $this->api('sns/userinfo', 'GET', [
177-
//'lang'=>
178-
]);
179-
}
180-
181-
/**
182-
* Gets new auth token to replace expired one.
183-
* @param OAuthToken $token expired auth token.
184-
* @return OAuthToken new auth token.
185-
* @throws \yii\authclient\InvalidResponseException
186-
*/
187-
public function refreshAccessToken(OAuthToken $token)
188-
{
189-
$params = [
190-
'grant_type' => 'refresh_token'
191-
];
192-
$params = array_merge($token->getParams(), $params);
193-
194-
$request = $this->createRequest()
195-
->setMethod('POST')
196-
->setUrl($this->refreshTokenUrl)
197-
->setData($params);
198-
199-
$this->applyClientCredentialsToRequest($request);
200-
201-
$response = $this->sendRequest($request);
202-
203-
$token = $this->createToken(['params' => $response]);
204-
$this->setAccessToken($token);
205-
206-
return $token;
207-
}
208100
}

0 commit comments

Comments
 (0)