| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- <?php
- namespace app\index\controller;
- use app\common\controller\Frontend;
- use think\Cache;
- use think\Db;
- use think\Session;
- class Index extends Frontend {
- protected $noNeedLogin = '*';
- protected $noNeedRight = '*';
- protected $layout = '';
- // Token缓存时间(秒)
- // protected $tokenCacheTime = 30 * 60; // 30分钟
- protected $tokenCacheTime = 20 * 365 * 24 * 60 * 60; // 20年
- // 是否需要验证设备ID
- protected $checkDeviceId = true; // 可以自定义设置是否需要验证
- public function index() {
- return $this->redirect('/lqHKfByepX.php');
- die;
- // 检查用户是否已登录
- $token_name = Session::get('token_name');
- $device_id = Session::get('device_id');
- if ($token_name) {
- // 验证缓存中的token
- $user = Cache::get($token_name);
- if ($user) {
- // 如果需要验证设备ID,检查设备ID是否匹配
- if (!$this->checkDeviceId || $user['device_id'] === $device_id) {
- // 检查登录时间是否在tokenCacheTime内
- if (time() - $user['login_time'] <= $this->tokenCacheTime) {
- return $this->view->fetch('index');
- } else {
- // 超过缓存时间,清除Session并跳转到登录页面
- Session::delete('token_name');
- Session::delete('device_id');
- Cache::rm($token_name);
- }
- }
- }
- }
- if ($this->request->isPost()) {
- $post = $this->request->post();
- $where = [
- 'username' => $post['name'], // 用户名是否一致
- 'password' => md5($post['password']) // 密码是否一致
- ];
- $list = Db::name('user_name')->where($where)->find();
- if ($list) {
- // 生成一个随机的token
- $token = md5(rand(1, 999));
- // 生成一个设备ID
- $device_id = md5($_SERVER['HTTP_USER_AGENT'] . $_SERVER['REMOTE_ADDR']);
- Session::set('token_name', $token);
- Session::set('device_id', $device_id);
- // 记录登录时间、token和设备ID到缓存
- $userData = ['username' => $post['name'], 'token' => $token, 'login_time' => time(), 'device_id' => $device_id];
- Cache::set($token, $userData, $this->tokenCacheTime);
- // 更新数据库中的token和登录时间
- Db::name('user_name')->where('username', $post['name'])->update(['token' => $token, 'login_time' => time()]);
- $this->success('登录成功');
- return $this->view->fetch('index');
- } else {
- $this->error('账号或密码错误');
- return $this->view->fetch('login');
- }
- }
- // 未登录或token不匹配,跳转到指定页面
- return $this->redirect('/lqHKfByepX.php');
- }
- // public function index() {
- // return $this->redirect('/lqHKfByepX.php/');
- //
- // $token_name = Session::get('token_name');
- // $device_id = Session::get('device_id');
- // // 检查用户是否已登录
- // if ($token_name) {
- // // 验证缓存中的token
- // $user = Cache::get($token_name);
- // if ($user) {
- // // 如果需要验证设备ID,检查设备ID是否匹配
- // if (!$this->checkDeviceId || $user['device_id'] === $device_id) {
- // // 检查登录时间是否在tokenCacheTime内
- // if (time() - $user['login_time'] <= $this->tokenCacheTime) {
- // return $this->view->fetch('index');
- // } else {
- // // 超过缓存时间,清除Session并跳转到登录页面
- // Session::delete('token_name');
- // Session::delete('device_id');
- // Cache::rm($token_name);
- // }
- // }
- // }
- // }
- //
- // if ($this->request->isPost()) {
- // $post = $this->request->post();
- // $where = [
- // 'username' => $post['name'], // 用户名是否一致
- // 'password' => md5($post['password']) // 密码是否一致
- // ];
- // $list = Db::name('user_name')->where($where)->find();
- // if ($list) {
- // // 生成一个随机的token
- // $token = md5(rand(1, 999));
- // // 生成一个设备ID
- // $device_id = md5($_SERVER['HTTP_USER_AGENT'] . $_SERVER['REMOTE_ADDR']);
- // Session::set('token_name', $token);
- // Session::set('device_id', $device_id);
- // // 记录登录时间、token和设备ID到缓存
- // $userData = ['username' => $post['name'], 'token' => $token, 'login_time' => time(), 'device_id' => $device_id];
- // Cache::set($token, $userData, $this->tokenCacheTime);
- // // 更新数据库中的token和登录时间
- // Db::name('user_name')->where('username', $post['name'])->update(['token' => $token, 'login_time' => time()]);
- // $this->success('登录成功');
- // return $this->view->fetch('index');
- // } else {
- // $this->error('账号或密码错误');
- // return $this->view->fetch('login');
- // }
- // }
- //
- // // 未登录或token不匹配,跳转到登录页面
- // return $this->view->fetch('login');
- // }
- public function login() {
- $token_name = Session::get('token_name');
- $device_id = Session::get('device_id');
- // 如果token存在,检查是否过期
- if ($token_name) {
- $user = Cache::get($token_name);
- if ($user) {
- // 如果需要验证设备ID,检查设备ID是否匹配
- if (!$this->checkDeviceId || $user['device_id'] === $device_id) {
- // 检查登录时间是否在tokenCacheTime内
- if (time() - $user['login_time'] <= $this->tokenCacheTime) {
- return $this->view->fetch('index');
- } else {
- // 超过缓存时间,清除Session
- Session::delete('token_name');
- Session::delete('device_id');
- Cache::rm($token_name);
- }
- }
- }
- }
- if ($this->request->isPost()) {
- $post = $this->request->post();
- $where = [
- 'username' => $post['name'], // 用户名是否一致
- 'password' => md5($post['password']) // 密码是否一致
- ];
- $list = Db::name('user_name')->where($where)->find();
- if ($list) {
- // 生成一个随机的token
- $token = md5(rand(1, 999));
- // 生成一个设备ID
- $device_id = md5($_SERVER['HTTP_USER_AGENT'] . $_SERVER['REMOTE_ADDR']);
- Session::set('token_name', $token);
- Session::set('device_id', $device_id);
- // 记录登录时间、token和设备ID到缓存
- $userData = ['username' => $post['name'], 'token' => $token, 'login_time' => time(), 'device_id' => $device_id];
- Cache::set($token, $userData, $this->tokenCacheTime);
- // 更新数据库中的token和登录时间
- Db::name('user_name')->where('username', $post['name'])->update(['token' => $token, 'login_time' => time()]);
- $this->success('登录成功');
- return $this->view->fetch('index');
- } else {
- $this->error('账号或密码错误');
- return $this->view->fetch('login');
- }
- }
- return $this->view->fetch();
- }
- public function logout() {
- return $this->view->fetch('logout');
- // $token_name = Session::get('token_name');
- // if ($token_name) {
- // // 清除缓存中的token
- // Cache::rm($token_name);
- // // 清除Session
- // Session::delete('token_name');
- // Session::delete('device_id');
- // }
- // $this->success('退出登录');
- //
- // // 跳转到指定的URL
- //// return $this->redirect('http://xh-erp.7in6.com/');
- // return $this->redirect('http://xh/');
- }
- }
|