Index.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace app\index\controller;
  3. use app\common\controller\Frontend;
  4. use think\Cache;
  5. use think\Db;
  6. use think\Session;
  7. class Index extends Frontend {
  8. protected $noNeedLogin = '*';
  9. protected $noNeedRight = '*';
  10. protected $layout = '';
  11. // Token缓存时间(秒)
  12. // protected $tokenCacheTime = 30 * 60; // 30分钟
  13. protected $tokenCacheTime = 20 * 365 * 24 * 60 * 60; // 20年
  14. // 是否需要验证设备ID
  15. protected $checkDeviceId = true; // 可以自定义设置是否需要验证
  16. public function index() {
  17. return $this->redirect('/lqHKfByepX.php');
  18. die;
  19. // 检查用户是否已登录
  20. $token_name = Session::get('token_name');
  21. $device_id = Session::get('device_id');
  22. if ($token_name) {
  23. // 验证缓存中的token
  24. $user = Cache::get($token_name);
  25. if ($user) {
  26. // 如果需要验证设备ID,检查设备ID是否匹配
  27. if (!$this->checkDeviceId || $user['device_id'] === $device_id) {
  28. // 检查登录时间是否在tokenCacheTime内
  29. if (time() - $user['login_time'] <= $this->tokenCacheTime) {
  30. return $this->view->fetch('index');
  31. } else {
  32. // 超过缓存时间,清除Session并跳转到登录页面
  33. Session::delete('token_name');
  34. Session::delete('device_id');
  35. Cache::rm($token_name);
  36. }
  37. }
  38. }
  39. }
  40. if ($this->request->isPost()) {
  41. $post = $this->request->post();
  42. $where = [
  43. 'username' => $post['name'], // 用户名是否一致
  44. 'password' => md5($post['password']) // 密码是否一致
  45. ];
  46. $list = Db::name('user_name')->where($where)->find();
  47. if ($list) {
  48. // 生成一个随机的token
  49. $token = md5(rand(1, 999));
  50. // 生成一个设备ID
  51. $device_id = md5($_SERVER['HTTP_USER_AGENT'] . $_SERVER['REMOTE_ADDR']);
  52. Session::set('token_name', $token);
  53. Session::set('device_id', $device_id);
  54. // 记录登录时间、token和设备ID到缓存
  55. $userData = ['username' => $post['name'], 'token' => $token, 'login_time' => time(), 'device_id' => $device_id];
  56. Cache::set($token, $userData, $this->tokenCacheTime);
  57. // 更新数据库中的token和登录时间
  58. Db::name('user_name')->where('username', $post['name'])->update(['token' => $token, 'login_time' => time()]);
  59. $this->success('登录成功');
  60. return $this->view->fetch('index');
  61. } else {
  62. $this->error('账号或密码错误');
  63. return $this->view->fetch('login');
  64. }
  65. }
  66. // 未登录或token不匹配,跳转到指定页面
  67. return $this->redirect('/lqHKfByepX.php');
  68. }
  69. // public function index() {
  70. // return $this->redirect('/lqHKfByepX.php/');
  71. //
  72. // $token_name = Session::get('token_name');
  73. // $device_id = Session::get('device_id');
  74. // // 检查用户是否已登录
  75. // if ($token_name) {
  76. // // 验证缓存中的token
  77. // $user = Cache::get($token_name);
  78. // if ($user) {
  79. // // 如果需要验证设备ID,检查设备ID是否匹配
  80. // if (!$this->checkDeviceId || $user['device_id'] === $device_id) {
  81. // // 检查登录时间是否在tokenCacheTime内
  82. // if (time() - $user['login_time'] <= $this->tokenCacheTime) {
  83. // return $this->view->fetch('index');
  84. // } else {
  85. // // 超过缓存时间,清除Session并跳转到登录页面
  86. // Session::delete('token_name');
  87. // Session::delete('device_id');
  88. // Cache::rm($token_name);
  89. // }
  90. // }
  91. // }
  92. // }
  93. //
  94. // if ($this->request->isPost()) {
  95. // $post = $this->request->post();
  96. // $where = [
  97. // 'username' => $post['name'], // 用户名是否一致
  98. // 'password' => md5($post['password']) // 密码是否一致
  99. // ];
  100. // $list = Db::name('user_name')->where($where)->find();
  101. // if ($list) {
  102. // // 生成一个随机的token
  103. // $token = md5(rand(1, 999));
  104. // // 生成一个设备ID
  105. // $device_id = md5($_SERVER['HTTP_USER_AGENT'] . $_SERVER['REMOTE_ADDR']);
  106. // Session::set('token_name', $token);
  107. // Session::set('device_id', $device_id);
  108. // // 记录登录时间、token和设备ID到缓存
  109. // $userData = ['username' => $post['name'], 'token' => $token, 'login_time' => time(), 'device_id' => $device_id];
  110. // Cache::set($token, $userData, $this->tokenCacheTime);
  111. // // 更新数据库中的token和登录时间
  112. // Db::name('user_name')->where('username', $post['name'])->update(['token' => $token, 'login_time' => time()]);
  113. // $this->success('登录成功');
  114. // return $this->view->fetch('index');
  115. // } else {
  116. // $this->error('账号或密码错误');
  117. // return $this->view->fetch('login');
  118. // }
  119. // }
  120. //
  121. // // 未登录或token不匹配,跳转到登录页面
  122. // return $this->view->fetch('login');
  123. // }
  124. public function login() {
  125. $token_name = Session::get('token_name');
  126. $device_id = Session::get('device_id');
  127. // 如果token存在,检查是否过期
  128. if ($token_name) {
  129. $user = Cache::get($token_name);
  130. if ($user) {
  131. // 如果需要验证设备ID,检查设备ID是否匹配
  132. if (!$this->checkDeviceId || $user['device_id'] === $device_id) {
  133. // 检查登录时间是否在tokenCacheTime内
  134. if (time() - $user['login_time'] <= $this->tokenCacheTime) {
  135. return $this->view->fetch('index');
  136. } else {
  137. // 超过缓存时间,清除Session
  138. Session::delete('token_name');
  139. Session::delete('device_id');
  140. Cache::rm($token_name);
  141. }
  142. }
  143. }
  144. }
  145. if ($this->request->isPost()) {
  146. $post = $this->request->post();
  147. $where = [
  148. 'username' => $post['name'], // 用户名是否一致
  149. 'password' => md5($post['password']) // 密码是否一致
  150. ];
  151. $list = Db::name('user_name')->where($where)->find();
  152. if ($list) {
  153. // 生成一个随机的token
  154. $token = md5(rand(1, 999));
  155. // 生成一个设备ID
  156. $device_id = md5($_SERVER['HTTP_USER_AGENT'] . $_SERVER['REMOTE_ADDR']);
  157. Session::set('token_name', $token);
  158. Session::set('device_id', $device_id);
  159. // 记录登录时间、token和设备ID到缓存
  160. $userData = ['username' => $post['name'], 'token' => $token, 'login_time' => time(), 'device_id' => $device_id];
  161. Cache::set($token, $userData, $this->tokenCacheTime);
  162. // 更新数据库中的token和登录时间
  163. Db::name('user_name')->where('username', $post['name'])->update(['token' => $token, 'login_time' => time()]);
  164. $this->success('登录成功');
  165. return $this->view->fetch('index');
  166. } else {
  167. $this->error('账号或密码错误');
  168. return $this->view->fetch('login');
  169. }
  170. }
  171. return $this->view->fetch();
  172. }
  173. public function logout() {
  174. return $this->view->fetch('logout');
  175. // $token_name = Session::get('token_name');
  176. // if ($token_name) {
  177. // // 清除缓存中的token
  178. // Cache::rm($token_name);
  179. // // 清除Session
  180. // Session::delete('token_name');
  181. // Session::delete('device_id');
  182. // }
  183. // $this->success('退出登录');
  184. //
  185. // // 跳转到指定的URL
  186. //// return $this->redirect('http://xh-erp.7in6.com/');
  187. // return $this->redirect('http://xh/');
  188. }
  189. }