generated from panliang/owl-admin-starter
106 lines
3.5 KiB
PHP
106 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
use Closure;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\Session;
|
|
use Illuminate\Support\Str;
|
|
use JetBrains\PhpStorm\Pure;
|
|
use Overtrue\LaravelWeChat\Events\WeChatUserAuthorized;
|
|
use Overtrue\LaravelWeChat\EasyWeChat;
|
|
use Overtrue\LaravelWeChat\Middleware\OAuthAuthenticate;
|
|
use Overtrue\Socialite\User as SocialiteUser;
|
|
|
|
class UserSocialite extends OAuthAuthenticate
|
|
{
|
|
public function handle(
|
|
Request $request,
|
|
Closure $next,
|
|
string $account = 'default',
|
|
string $scope = null,
|
|
?string $type = 'service'
|
|
): mixed {
|
|
if (config('app.debug')) {
|
|
$user = new SocialiteUser([
|
|
'id' => 'omDRm6ial5hRdys0NpnHQpYJ44aY',
|
|
'name' => '潘亮',
|
|
'nickname' => '潘亮',
|
|
'avatar' => 'https://via.placeholder.com/64x64.png',
|
|
'email' => null,
|
|
'original' => [],
|
|
'provider' => 'WeChat',
|
|
]);
|
|
session(['easywechat.oauth_user.default' => $user]);
|
|
return $next($request);
|
|
}
|
|
// 保证兼容性参数处理
|
|
$prefix = ('work' !== $type) ? 'official_account' : 'work';
|
|
|
|
$sessionKey = \sprintf('easywechat.oauth_user.%s', $account);
|
|
$name = \sprintf('easywechat.%s.%s', $prefix, $account);
|
|
$config = config($name, []);
|
|
$service = $this->getWechatApp($name);
|
|
|
|
$scope = $scope ?: Arr::get($config, 'oauth.scopes', ['snsapi_base']);
|
|
|
|
if (\is_string($scope)) {
|
|
$scope = \array_map('trim', explode(',', $scope));
|
|
}
|
|
|
|
if (Session::has($sessionKey)) {
|
|
event(new WeChatUserAuthorized(session($sessionKey), false, $account));
|
|
|
|
return $next($request);
|
|
}
|
|
|
|
// 是否强制使用 HTTPS 跳转
|
|
$enforceHttps = Arr::get($config, 'oauth.enforce_https', false);
|
|
|
|
if ($request->has('code')) {
|
|
session([$sessionKey => $service->getOAuth()->userFromCode($request->query('code'))]);
|
|
|
|
event(new WeChatUserAuthorized(session($sessionKey), true, $account));
|
|
|
|
return redirect()->to($this->getIntendUrl($request, $enforceHttps));
|
|
}
|
|
|
|
session()->forget($sessionKey);
|
|
|
|
// 跳转到微信授权页
|
|
return redirect()->away(
|
|
$service->getOAuth()->scopes($scope)->redirect($this->getRedirectUrl($request, $enforceHttps))
|
|
);
|
|
}
|
|
|
|
// public function handle(Request $request, Closure $next): Response
|
|
// {
|
|
// if (config('app.debug')) {
|
|
// $user = new SocialiteUser([
|
|
// 'id' => 'omDRm6ial5hRdys0NpnHQpYJ44aY',
|
|
// 'name' => '潘亮',
|
|
// 'nickname' => '潘亮',
|
|
// 'avatar' => 'https://via.placeholder.com/64x64.png',
|
|
// 'email' => null,
|
|
// 'original' => [],
|
|
// 'provider' => 'WeChat',
|
|
// ]);
|
|
// session(['easywechat.oauth_user.default' => $user]);
|
|
// return $next($request);
|
|
// }
|
|
// }
|
|
|
|
protected function getWechatApp($name)
|
|
{
|
|
$app = EasyWeChat::officialAccount($name);
|
|
$app->setAccessToken(new \App\Services\WechatOfficialAccessToken(
|
|
appId: $app->getAccount()->getAppId(),
|
|
secret: $app->getAccount()->getSecret(),
|
|
cache: $app->getCache(),
|
|
httpClient: $app->getHttpClient(),
|
|
stable: $app->getConfig()->get('use_stable_access_token', false),
|
|
));
|
|
}
|
|
}
|