113 lines
3.3 KiB
PHP
113 lines
3.3 KiB
PHP
|
|
<?php
|
|||
|
|
|
|||
|
|
namespace App\Http\Middleware;
|
|||
|
|
|
|||
|
|
use App\Models\MerchantAccount;
|
|||
|
|
use App\Models\MerchantLog;
|
|||
|
|
use App\Models\Server\MerchantAdminLinks;
|
|||
|
|
use App\Models\Server\MerchantAdmins;
|
|||
|
|
use Illuminate\Auth\AuthenticationException;
|
|||
|
|
use Illuminate\Http\JsonResponse;
|
|||
|
|
use Illuminate\Contracts\Auth\Factory as Auth;
|
|||
|
|
|
|||
|
|
use Closure;
|
|||
|
|
use Illuminate\Http\Request;
|
|||
|
|
|
|||
|
|
|
|||
|
|
class merchant
|
|||
|
|
{
|
|||
|
|
protected $auth;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Create a new middleware instance.
|
|||
|
|
*
|
|||
|
|
* @param \Illuminate\Contracts\Auth\Factory $auth
|
|||
|
|
* @return void
|
|||
|
|
*/
|
|||
|
|
public function __construct(Auth $auth)
|
|||
|
|
{
|
|||
|
|
$this->auth = $auth;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Handle an incoming request.
|
|||
|
|
*
|
|||
|
|
|
|||
|
|
* @param Request $request
|
|||
|
|
* @param \Closure $next
|
|||
|
|
* @return mixed
|
|||
|
|
*/
|
|||
|
|
public function handle($request, Closure $next, ...$guards)
|
|||
|
|
{
|
|||
|
|
// try {
|
|||
|
|
$token = $request->bearerToken();
|
|||
|
|
|
|||
|
|
$result = $this->parseToken($token);
|
|||
|
|
if (!$result)
|
|||
|
|
return $this->fail('验证失败,请重新登录', 2);
|
|||
|
|
$key = explode('-', $result);
|
|||
|
|
$merchant = MerchantAccount::where('id',$key[0])->first();
|
|||
|
|
if(!$merchant) return $this->fail('验证失败,请重新登录',2);
|
|||
|
|
if(time()-$key[2]>60480000)
|
|||
|
|
return $this->fail('请重新登录',2);
|
|||
|
|
if($key[0]){
|
|||
|
|
if(!empty($key[4])){
|
|||
|
|
$admin = MerchantAdmins::where('id',$key[4])
|
|||
|
|
->first();
|
|||
|
|
$admin_link = MerchantAdminLinks::where('mch_id', $key[0])
|
|||
|
|
->where('admin_id', $key[4])
|
|||
|
|
->first();
|
|||
|
|
if (!$admin || !$admin_link) {
|
|||
|
|
return $this->fail('验证失败,请重新登录', 2);
|
|||
|
|
}
|
|||
|
|
$request->merchant_admin_id = $key[4];
|
|||
|
|
}
|
|||
|
|
$request->account_id = $key[0];
|
|||
|
|
}
|
|||
|
|
$method = $request->method();
|
|||
|
|
$path = $request->path();
|
|||
|
|
if ($method != 'GET' && config('app.env') == 'production') {
|
|||
|
|
$admin_log = new MerchantLog();
|
|||
|
|
$admin_log->path = $path;
|
|||
|
|
$admin_log->method = $method;
|
|||
|
|
$admin_log->m_id = $key[0];
|
|||
|
|
$admin_log->admin_id = $request->merchant_admin_id ?? 0;
|
|||
|
|
$admin_log->param = json_encode($request->all());
|
|||
|
|
$admin_log->save();
|
|||
|
|
}
|
|||
|
|
return $next($request);
|
|||
|
|
// } catch (\Exception $e) {
|
|||
|
|
// return $this->fail('信息有误,请重新登录', 2);
|
|||
|
|
// }
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//接口返回失败
|
|||
|
|
public function fail($msg, $code = 5, $path = '', $operate = '', $notice = '')
|
|||
|
|
{
|
|||
|
|
$result = [
|
|||
|
|
'code' => $code,
|
|||
|
|
'path' => $path,
|
|||
|
|
'message' => $msg,
|
|||
|
|
'operate' => $operate,
|
|||
|
|
'notice' => $notice,
|
|||
|
|
];
|
|||
|
|
return Response()->json($result);
|
|||
|
|
}
|
|||
|
|
//陈彬,用于本地的调试,固定商户为1,adminid为6
|
|||
|
|
public function parseToken($token){
|
|||
|
|
|
|||
|
|
if(env('APP_LOCAL', false)){
|
|||
|
|
return '1-0-'.time().'-0-6';
|
|||
|
|
}else{
|
|||
|
|
try {
|
|||
|
|
$res = decrypt($token);
|
|||
|
|
} catch( \Exception $e) {
|
|||
|
|
$res = false;
|
|||
|
|
}
|
|||
|
|
return $res;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|