119 lines
3.2 KiB
PHP
119 lines
3.2 KiB
PHP
<?php
|
|
namespace App\Domains\Auth\Http\Controllers;
|
|
|
|
use App\Core\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\App;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use App\Providers\Wechat\Services\OAuth;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use App\Domains\User\Services\UserService;
|
|
use App\Domains\Auth\Services\UserAuthService;
|
|
use App\Domains\Wechat\Services\WechatService;
|
|
|
|
class UserAuthController extends Controller
|
|
{
|
|
protected $request;
|
|
protected $auth;
|
|
|
|
/**
|
|
* 构造函数,自动注入.
|
|
*/
|
|
public function __construct(Request $request)
|
|
{
|
|
$this->request = $request;
|
|
$this->auth = app('auth:user');
|
|
$this->application = app('dipper')->application;
|
|
}
|
|
|
|
public function login($method = '')
|
|
{
|
|
$referer = referer();
|
|
|
|
if (app('dipper')->initUser()) {
|
|
return redirect('/');
|
|
}
|
|
|
|
// 微信登录跳转
|
|
if ($this->request->isMethod('GET') && empty($method)) {
|
|
if (WechatService::inWechat()) {
|
|
$wechat = $this->application->wechat;
|
|
$platform = in_array(4, $wechat['funcs']) && WechatService::checkPermision(4, $wechat['service'], $wechat['verify']);
|
|
$wechatLoginUrl = WechatService::oauth($referer, OAuth::API_OAUTH_SCOPE_USERINFO, ['login' => true], $platform)->getTargetUrl();
|
|
// 在微信中直接登录
|
|
return redirect($wechatLoginUrl);
|
|
}
|
|
|
|
$redirect = url_with_query(env('APP_LOGIN_URL'), [
|
|
'referer' => $referer
|
|
]);
|
|
|
|
return redirect($redirect);
|
|
}
|
|
|
|
$method = in_array($method, ['wechat', 'sms', 'password']) ? $method : 'password';
|
|
|
|
return App::call(static::class.'@'.$method);
|
|
}
|
|
|
|
/**
|
|
* 账号密码登录
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function password()
|
|
{
|
|
$rule = [
|
|
'username' => ['required'],
|
|
'password' => ['required', 'string'],
|
|
'remember' => ['in:0,1'],
|
|
];
|
|
|
|
$message = [
|
|
'username.required_unless' => '请输入用户名或手机号',
|
|
'password.required' => '请输入密码',
|
|
'password.string' => '密码不正确',
|
|
];
|
|
|
|
Validator::validate($this->request->all(), $rule, $message);
|
|
|
|
$username = $this->request->get('username');
|
|
$password = $this->request->get('password');
|
|
$remember = $this->request->get('remember', 0);
|
|
|
|
$token = $this->auth->login($username, $password, $remember);
|
|
|
|
return res($token, '登录成功', 200, [
|
|
'new-token' => $token
|
|
]);
|
|
}
|
|
|
|
public function sms(UserService $userService)
|
|
{
|
|
$mobile = $this->request->get('mobile');
|
|
$code = $this->request->get('code');
|
|
$this->smsService->verifyCode($mobile, $code);
|
|
|
|
$user = $userService->fetch($mobile);
|
|
|
|
$token = $this->guard->login($user);
|
|
|
|
return res($token, '登录成功', 200, [
|
|
'new-token' => $token
|
|
]);
|
|
}
|
|
|
|
|
|
/**
|
|
* 登出
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function logout()
|
|
{
|
|
$this->auth->logout();
|
|
|
|
return res(true, '登出成功');
|
|
}
|
|
}
|