12345678910111213141516171819202122232425262728293031323334353637383940 |
- <?php
- namespace App\Http\Middleware;
- use Carbon\Carbon;
- use Closure;
- use Illuminate\Support\Facades\Cache;
- class CheckOpenToken
- {
- /**
- * Handle an incoming request.
- *
- * @param \Illuminate\Http\Request $request
- * @param \Closure $next
- * @return mixed
- */
- public function handle($request, Closure $next)
- {
- // $tokenTime = file_get_contents(storage_path('openApiToken.txt'));
- $tokenTime = Cache::store('file')->get(config('openApi.yadi.openTimeKey'));
- $ttl = config('openApi.yadi.ttl');
- $now = Carbon::now()->timestamp;
- if($now-$tokenTime > $ttl){
- return response()->json(['error'=>'token过期,请重新登录'],401);
- }
- $username = config('openApi.yadi.username');
- $password = config('openApi.yadi.password');
- $secret = config('openApi.yadi.secret');
- if ($request->header('authorization') == md5($username . $password . $secret)) {
- return $next($request);
- } else {
- return response()->json(['error' => 'token无效,请重新登录'], 401);
- }
- }
- }
|