新版订单消耗系统

ApiLoginMiddleware.php 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Jason
  5. * Date: 17/3/12
  6. * Time: 下午11:09
  7. */
  8. namespace App\Http\Middleware;
  9. use App\Models\Users;
  10. use Illuminate\Http\Response;
  11. use Illuminate\Http\Request;
  12. use Closure;
  13. use App\RedisModel;
  14. use App\Http\Controllers\Api\Controller;
  15. class ApiLoginMiddleware
  16. {
  17. public function handle(Request $request, Closure $next)
  18. {
  19. $userId = $request->input('admin_id');
  20. $ttl = $request->input('ttl');
  21. $sign = $request->input('sign');
  22. $redisKey = env('LOGIN_REDIS_KEY') . '-'. $userId;
  23. $randomKey = RedisModel::get($redisKey);
  24. if(!$randomKey){
  25. $data = Controller::returnValue(['data' => '请重新登录'],4001);
  26. return new Response($data);
  27. }
  28. // RedisModel::expire($redisKey, 300);
  29. // 用户
  30. $user = Users::find($userId);
  31. // 用户不存在或被删除,退回重新登录
  32. if (empty($user)) {
  33. $data = Controller::returnValue(['data' => '请重新登录'],4001);
  34. return new Response($data);
  35. }
  36. if(Users::validSign($userId, $ttl, $randomKey, $sign) === false){
  37. $data = Controller::returnValue(['data' => '登录失败,请重新登录'],4002);
  38. return new Response($data);
  39. }
  40. \Auth::setUser($user);
  41. return $next($request);
  42. }
  43. }