Skip to content

Commit accaafe

Browse files
ac1982overtrue
authored andcommitted
Make middle-ware compatible with wechat work service. (overtrue#307)
* Make middle-ware compatible with wechat work service. add type parameter and make it * StyleCI StyleCI
1 parent 47d85b5 commit accaafe

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

src/Middleware/OAuthAuthenticate.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@
1414
use Closure;
1515
use http\Env\Request;
1616
use Illuminate\Support\Arr;
17-
use Illuminate\Support\Str;
1817
use Overtrue\LaravelWeChat\Events\WeChatUserAuthorized;
1918

2019
/**
21-
* Class OAuthAuthenticate.
20+
* Class OAuthAuthenticate: 微信公众号, 企业微信的网页应用。
2221
*/
2322
class OAuthAuthenticate
2423
{
@@ -27,26 +26,24 @@ class OAuthAuthenticate
2726
*
2827
* @param \Illuminate\Http\Request $request
2928
* @param \Closure $next
30-
* @param string|null $scopes
29+
* @param string|null $scope
30+
* @param string|null $type : service(服务号), subscription(订阅号), work(企业微信)
3131
*
3232
* @return mixed
3333
*/
34-
public function handle($request, Closure $next, $account = 'default', $scopes = null)
34+
public function handle($request, Closure $next, $account = 'default', $scope = 'snsapi_base', $type = 'service')
3535
{
36-
// $account 与 $scopes 写反的情况
37-
if (is_array($scopes) || (\is_string($account) && Str::is('snsapi_*', $account))) {
38-
list($account, $scopes) = [$scopes, $account];
39-
$account || $account = 'default';
40-
}
41-
4236
$isNewSession = false;
43-
$sessionKey = \sprintf('wechat.oauth_user.%s', $account);
44-
$config = config(\sprintf('wechat.official_account.%s', $account), []);
45-
$officialAccount = app(\sprintf('wechat.official_account.%s', $account));
46-
$scopes = $scopes ?: Arr::get($config, 'oauth.scopes', ['snsapi_base']);
47-
48-
if (is_string($scopes)) {
49-
$scopes = array_map('trim', explode(',', $scopes));
37+
//保证兼容性
38+
$class = ('work' !== $type) ? 'wechat' : 'work';
39+
$prefix = ('work' !== $type) ? 'official_account' : 'work';
40+
$sessionKey = \sprintf($class.'.oauth_user.%s', $account);
41+
$config = config(\sprintf('wechat.'.$prefix.'.%s', $account), []);
42+
$officialAccount = app(\sprintf('wechat.'.$prefix.'.%s', $account));
43+
$scope = $scope ?: Arr::get($config, 'oauth.scopes', ['snsapi_base']);
44+
45+
if (is_string($scope)) {
46+
$scope = array_map('trim', explode(',', $scope));
5047
}
5148

5249
$session = session($sessionKey, []);
@@ -63,7 +60,7 @@ public function handle($request, Closure $next, $account = 'default', $scopes =
6360

6461
session()->forget($sessionKey);
6562

66-
return $officialAccount->oauth->scopes($scopes)->redirect($request->fullUrl());
63+
return $officialAccount->oauth->scopes($scope)->redirect($request->fullUrl());
6764
}
6865

6966
event(new WeChatUserAuthorized(session($sessionKey), $isNewSession, $account));

0 commit comments

Comments
 (0)