TokenManager.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. //decode by http://www.yunlu99.com/
  3. namespace Biz;
  4. /**
  5. * token
  6. */
  7. class TokenManager {
  8. public function __construct() {
  9. $this->model = lm('account', 'Commons');
  10. }
  11. function publish($identity) {
  12. $token = $this->getToken();
  13. $this->saveToken($identity, $token);
  14. // echo 'tokne';
  15. $_SESSION['globalToken'] = $token;
  16. return $token;
  17. }
  18. function refreshToken() {
  19. return isset($_SESSION['globalToken']) ? $_SESSION['globalToken'] : '';
  20. }
  21. function check($token) {
  22. $user = lm('account_detailed', 'Commons')->where('token', $token)->first();
  23. if ($user) {
  24. return $user;
  25. }
  26. return 0;
  27. }
  28. function doLogout() {
  29. $_SESSION['uinfo'] = null;
  30. $_SESSION['globalToken'] = null;
  31. }
  32. /**
  33. * 登录操作
  34. *
  35. * @param $token
  36. * @return int
  37. * @throws \Exception
  38. */
  39. function doLogin($token) {
  40. $user = $this->check($token);
  41. $userInfo = $this->model->join('account_detailed', 'account.identity', '=', 'account_detailed.account_identity')->where('account.identity', $user->account_identity)->get();
  42. if (empty($userInfo) || !$user) {
  43. return false;
  44. }
  45. lm('account_detailed', 'Commons')->updateLoginStatus($user->identity);
  46. $userInfoArray = $userInfo->toArray();
  47. \InitSess::init()->iniSession($token);
  48. $_SESSION['uinfo'] = $userInfoArray[0];
  49. return $userInfoArray;
  50. }
  51. /**
  52. * 获取token
  53. *
  54. * @return string
  55. */
  56. public function getToken() {
  57. $enc = GenEncryption();
  58. $time = time();
  59. $token = substr(uniqid($enc . $time), 0, 35);
  60. return $token;
  61. }
  62. /**
  63. * 更新token记录
  64. *
  65. * @param $identity
  66. * @param $token
  67. * @throws \Exception
  68. */
  69. public function saveToken($identity, $token)
  70. {
  71. lm('account_detailed', 'Commons')->where('account_identity', $identity)->update(array('token' => $token));
  72. $accountToken = lm('account_token', 'Commons')->where('account_identity', $identity)->first();
  73. // $data = ['last_time' => date('Y-m-d H:i:s'), 'effective_time' => strtotime('now') + 3600];
  74. $data = ['last_time' => date('Y-m-d H:i:s'), 'effective_time' => time()];
  75. if ($accountToken) {
  76. lm('account_token', 'Commons')->where('account_identity', $identity)->update($data);
  77. } else {
  78. // echo 567;
  79. $data['account_identity'] = $identity;
  80. lm('account_token', 'Commons')->insert($data);
  81. }
  82. }
  83. /**
  84. * @param $token
  85. * @return mixed
  86. * @throws \Exception
  87. */
  88. public function getAccountInfo($token)
  89. {
  90. $userInfo = lm('account_detailed', 'Commons')->join('account', 'account_detailed.account_identity', '=', 'account.identity')->where('account_detailed.token', $token)->first();
  91. \InitSess::init()->iniSession($token);
  92. return $userInfo;
  93. }
  94. /**
  95. * @param $token
  96. * @return mixed
  97. * @throws \Exception
  98. */
  99. public function getAgentInfo($token)
  100. {
  101. $userInfo = lm('agent_detailed', 'Commons')->where('agent_token', $token)->first();
  102. return $userInfo;
  103. }
  104. }