JwtUserProvider.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: ywl
  5. * Date: 2017/4/13
  6. * Time: 下午12:03
  7. */
  8. namespace App\Providers;
  9. use App\User\Models\UserModel;
  10. use Illuminate\Auth\EloquentUserProvider;
  11. use Illuminate\Contracts\Auth\Authenticatable as UserContract;
  12. use Illuminate\Support\Facades\Request;
  13. use Tymon\JWTAuth\JWTAuth;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Log;
  16. use App\Exceptions\ApiException;
  17. class JwtUserProvider extends EloquentUserProvider
  18. {
  19. /**
  20. * TODO 先将取用户与验证密码放到一个步骤里。 等新系统把取用户与验证密码分开成两个接口后再修改
  21. * @param array $credentials
  22. * @return UserModel|UserContract|null|void
  23. */
  24. public function retrieveByCredentials(array $credentials)
  25. {
  26. if (empty($credentials)) {
  27. return;
  28. }
  29. $userModel = new UserModel();
  30. $where = [];
  31. $where['user_name'] = $credentials['user_name'];
  32. $where['password'] = md5($credentials['password']);
  33. $where['status'] = 0;
  34. $userInfo = $userModel->alias('a')
  35. ->where($where)->selectRaw('a.*')->first();
  36. if (!empty($userInfo)) {
  37. $now = nowTime();
  38. $userInfo->last_login_time = $now;
  39. $userInfo->save();
  40. return $userInfo;
  41. } else {
  42. throw new ApiException(10001);
  43. }
  44. }
  45. public function validateCredentials(UserContract $user, array $credentials)
  46. {
  47. return true;
  48. }
  49. }