CheckOpenToken.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. namespace App\Http\Middleware;
  3. use Carbon\Carbon;
  4. use Closure;
  5. use Illuminate\Support\Facades\Cache;
  6. class CheckOpenToken
  7. {
  8. /**
  9. * Handle an incoming request.
  10. *
  11. * @param \Illuminate\Http\Request $request
  12. * @param \Closure $next
  13. * @return mixed
  14. */
  15. public function handle($request, Closure $next)
  16. {
  17. // $tokenTime = file_get_contents(storage_path('openApiToken.txt'));
  18. $tokenTime = Cache::store('file')->get(config('openApi.yadi.openTimeKey'));
  19. $ttl = config('openApi.yadi.ttl');
  20. $now = Carbon::now()->timestamp;
  21. if($now-$tokenTime > $ttl){
  22. return response()->json(['error'=>'token过期,请重新登录'],401);
  23. }
  24. $username = config('openApi.yadi.username');
  25. $password = config('openApi.yadi.password');
  26. $secret = config('openApi.yadi.secret');
  27. if ($request->header('authorization') == md5($username . $password . $secret)) {
  28. return $next($request);
  29. } else {
  30. return response()->json(['error' => 'token无效,请重新登录'], 401);
  31. }
  32. }
  33. }