AuthIdentifier.php
1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
namespace App\Http\Middleware;
use App\Helper\ErrorCode;
use App\Helper\Identifier;
use App\Helper\Response;
use App\Models\Legal\Stakeholder;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
/**
* Class AuthIdentifier
* @package App\Http\Middleware
*/
class AuthIdentifier
{
protected $auth = [
'api'
];
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
$prefix = current(explode('/', $request->path()));
if (!in_array($prefix, $this->auth)) goto AUTH;
$identifier = $request->header('identifier');
if (env('APP_ENV') == 'local' && empty($identifier)) {
$identifier = env('identifier');
}
Log::info(__METHOD__. '解析前:', ['identifier'=>$identifier]);
!empty($identifier) && $identifier = Identifier::resolve($identifier);
Log::info(__METHOD__. '解析后', ['identifier'=>$identifier]);
if (empty($identifier)) return Response::error(ErrorCode::IDENTIFIER_FAIL);
$identifier = json_decode($identifier);
//通过身份证查询id
$stakeholder_ids = Stakeholder::stakeholderIds($identifier);
if (empty($stakeholder_ids)) return Response::error(ErrorCode::MATCH_IDENTIFIER_FAIL);
$request->attributes->add([
'identifier' => $identifier,
'stakeholder_ids' => $stakeholder_ids,
]);
AUTH:
return $next($request);
}
}