love_php/app/Http/Middleware/OfficialAuth.php
2026-04-02 09:20:51 +08:00

109 lines
2.8 KiB
PHP

<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Contracts\Auth\Factory as Auth;
use Illuminate\Auth\AuthenticationException;
use App\Http\Response\ResponseJson;
use App\Models\SystemBlacklist;
class OfficialAuth
{
use ResponseJson;
protected $auth;
public function __construct(Auth $auth)
{
$this->auth = $auth;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next, ...$guards)
{
$result = $this->authenticate($request, $guards);
if ($result === 0) {
return response()->json(['code'=>2, 'message' => '请登录后访问.'], 200);
}
$prefix = ($request->route()->getAction())['prefix'];
$user = auth()->user();
$date = date('Y-m-d H:i:s');
$blacklist = SystemBlacklist::where('user_id', $user->id)->first();
if (!empty($blacklist)) {
if($blacklist->end_time >= $date && $blacklist->start_time <= $date){
return $this->fail('由于您的账号涉及违规,暂无法使用福恋平台', 1);
}
}
return $next($request);
}
/**
* Determine if the user is logged in to any of the given guards.
*
* @param array $guards
* @return void
*
* @throws \Illuminate\Auth\AuthenticationException
*/
protected function authenticate($request, array $guards)
{
if (empty($guards)) {
return $this->auth->authenticate();
}
foreach ($guards as $guard) {
if ($this->auth->guard($guard)->check()) {
return $this->auth->shouldUse($guard);
}
return 0;
}
throw new AuthenticationException('Unauthenticated.', $guards);
}
public function success($msg, $data=[]){
$result = [
'code'=> 0,
'message'=> $msg,
'data'=> $data,
];
$response = Response()->json($result);
return $response;
}
//接口返回失败
public function failure($msg, $data=[], $jsonp=false){
$result = [
'code'=> 1,
'message'=> $msg,
'data'=> $data,
];
if($jsonp){
return Response()->jsonp('callback', $result);
}else{
return Response()->json($result);
}
}
//接口返回失败
public function fail($msg, $data=[], $jsonp=false){
$result = [
'code'=> 2,
'message'=> $msg,
'data'=> $data,
];
if($jsonp){
return Response()->jsonp('callback', $result);
}else{
return Response()->json($result);
}
}
}