Redis.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace App\Services\Auth;
  3. use App\Models\User;
  4. use App\Models\Node;
  5. use App\Services\RedisClient;
  6. use App\Utils\Tools;
  7. use App\Utils\Cookie;
  8. use App\Services\Config;
  9. class Redis extends Base
  10. {
  11. private $client;
  12. public function __construct()
  13. {
  14. $client = new RedisClient();
  15. $this->client = $client;
  16. }
  17. public function getClient()
  18. {
  19. $client = new RedisClient();
  20. return $client;
  21. }
  22. public function login($uid, $time)
  23. {
  24. $sid = Tools::genSID();
  25. Cookie::set([
  26. 'sid' => $sid
  27. ], $time+time());
  28. $value = $uid;
  29. $this->client->setex($sid, $time, $value);
  30. $this->client->setex($sid."ip", $time, $_SERVER["REMOTE_ADDR"]);
  31. }
  32. public function logout()
  33. {
  34. $sid = Cookie::get('sid');
  35. $this->client->del($sid);
  36. }
  37. public function getUser()
  38. {
  39. $sid = Cookie::get('sid');
  40. $value = $this->client->get($sid);
  41. $ip = $this->client->get($sid."ip");
  42. $nodes=Node::where("node_ip", "=", $_SERVER["REMOTE_ADDR"])->first();
  43. if ($ip != $_SERVER["REMOTE_ADDR"] && $nodes==null && Config::get('enable_login_bind_ip')=='true') {
  44. $user = new User();
  45. $user->isLogin = false;
  46. return $user;
  47. }
  48. if ($value == null) {
  49. $user = new User();
  50. $user->isLogin = false;
  51. return $user;
  52. }
  53. $uid = $value;
  54. $user = User::find($uid);
  55. if ($user == null) {
  56. $user = new User();
  57. $user->isLogin = false;
  58. return $user;
  59. }
  60. $user->isLogin = true;
  61. return $user;
  62. }
  63. }