50 lines
1.8 KiB
PHP
50 lines
1.8 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\Http\Controllers\Api;
|
||
|
|
||
|
use App\Http\Requests\Api\SocialAuthorizationRequest;
|
||
|
use App\Models\User;
|
||
|
use Laravel\Socialite\Facades\Socialite;
|
||
|
|
||
|
class AuthorizationsController extends Controller
|
||
|
{
|
||
|
public function socialStore($type, SocialAuthorizationRequest $request)
|
||
|
{
|
||
|
if (!in_array($type, ['weixin'])) {
|
||
|
return $this->response->errorBadRequest();
|
||
|
}
|
||
|
$driver = Socialite::driver($type);
|
||
|
try {
|
||
|
if ($code = $request->get('code')) {
|
||
|
$response = $driver->getAccessTokenResponse($code);
|
||
|
$token = array_get($response, 'access_token');
|
||
|
} else {
|
||
|
$token = $request->get('access_token');
|
||
|
if ($type == 'weixin') {
|
||
|
$driver->setOpenId($request->get('openid'));
|
||
|
}
|
||
|
}
|
||
|
$oauthUser = $driver->userFromToken($token);
|
||
|
|
||
|
} catch (\Exception $exception) {
|
||
|
return $this->response->errorUnauthorized('参数错误,未获取用户信息');
|
||
|
}
|
||
|
switch ($type) {
|
||
|
case 'weixin':
|
||
|
$unionid = $oauthUser->offsetExists('unionid') ? $oauthUser->offsetGet('unionid') : null;
|
||
|
|
||
|
$user = $unionid ? User::where('weixin_unionid', $unionid)->first() : User::where('weixin_openid', $oauthUser->getId())->first();
|
||
|
if (!$user) {
|
||
|
$user = User::create([
|
||
|
'name' => $oauthUser->getNickname(),
|
||
|
'avatar' => $oauthUser->getAvatar(),
|
||
|
'weixin_openid' => $oauthUser->getId(),
|
||
|
'weixin_unionid' => $unionid,
|
||
|
]);
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
return $this->response->array(['token' => $user->id]);
|
||
|
}
|
||
|
}
|