1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Jason
- * Date: 17/3/12
- * Time: 下午11:09
- */
- namespace App\Http\Middleware;
- use App\Models\Users;
- use Illuminate\Http\Response;
- use Illuminate\Http\Request;
- use Closure;
- use App\RedisModel;
- use App\Http\Controllers\Api\Controller;
- class ApiLoginMiddleware
- {
- public function handle(Request $request, Closure $next)
- {
- $userId = $request->input('admin_id');
- $ttl = $request->input('ttl');
- $sign = $request->input('sign');
- $redisKey = env('LOGIN_REDIS_KEY') . '-'. $userId;
- $randomKey = RedisModel::get($redisKey);
- if(!$randomKey){
- $data = Controller::returnValue(['data' => '请重新登录'],4001);
- return new Response($data);
- }
- // RedisModel::expire($redisKey, 300);
- // 用户
- $user = Users::find($userId);
- // 用户不存在或被删除,退回重新登录
- if (empty($user)) {
- $data = Controller::returnValue(['data' => '请重新登录'],4001);
- return new Response($data);
- }
- if(Users::validSign($userId, $ttl, $randomKey, $sign) === false){
- $data = Controller::returnValue(['data' => '登录失败,请重新登录'],4002);
- return new Response($data);
- }
- \Auth::setUser($user);
- return $next($request);
- }
- }
|