-
@@ -100,11 +134,16 @@ $isLoggedIn = session('user_id') ? true : false;
diff --git a/app/index/controller/BaseController.php b/app/index/controller/BaseController.php index 9aed009..77951c3 100644 --- a/app/index/controller/BaseController.php +++ b/app/index/controller/BaseController.php @@ -1,5 +1,5 @@ app = $app; + $this->app = $app; $this->request = $this->app->request; $this->visitStats = new VisitStatsService(); @@ -53,8 +55,7 @@ abstract class BaseController $this->visitStats->recordVisit($this->getControllerName()); // 获取配置 - $configList = Db::table('yz_admin_config') - ->where('config_status', 1) + $configList = AdminConfig::where('config_status', 1) ->order('config_sort DESC') ->select() ->toArray(); @@ -65,9 +66,34 @@ abstract class BaseController $config[$item['config_name']] = $item['config_value']; } + // 判断用户是否登录 + $userInfo = []; + if (session('user_id')) { + // 从数据库获取最新用户信息 + $user = Users::where('id', session('user_id'))->find(); + if ($user) { + $userInfo = [ + 'id' => $user->id, + 'name' => $user->name, + 'account' => $user->account, + 'avatar' => $user->avatar ?? '/static/images/default-avatar.png', + 'is_login' => true, + 'last_login_time' => $user->last_login_time + ]; + } else { + // 用户不存在,清除session + session('user_id', null); + session('user_name', null); + $userInfo = ['is_login' => false]; + } + } else { + $userInfo = ['is_login' => false]; + } + // 设置通用变量 View::assign([ - 'config' => $config + 'config' => $config, + 'userInfo' => $userInfo ]); } @@ -99,22 +125,22 @@ abstract class BaseController * @param string $url 跳转地址 * @param mixed $data 返回数据 * @param integer $wait 跳转等待时间 - * @return void + * @return \think\response\Json|string */ protected function success($msg = '', $url = null, $data = '', $wait = 3) { if (Request::isAjax()) { return json([ 'code' => 1, - 'msg' => $msg, + 'msg' => $msg, 'data' => $data, - 'url' => $url + 'url' => $url ]); } - + return View::fetch('common/success', [ - 'msg' => $msg, - 'url' => $url, + 'msg' => $msg, + 'url' => $url, 'data' => $data, 'wait' => $wait ]); @@ -126,28 +152,28 @@ abstract class BaseController * @param string $url 跳转地址 * @param mixed $data 返回数据 * @param integer $wait 跳转等待时间 - * @return void + * @return \think\response\Json|string */ protected function error($msg = '', $url = null, $data = '', $wait = 3) { if (Request::isAjax()) { return json([ 'code' => 0, - 'msg' => $msg, + 'msg' => $msg, 'data' => $data, - 'url' => $url + 'url' => $url ]); } - + return View::fetch('common/error', [ - 'msg' => $msg, - 'url' => $url, + 'msg' => $msg, + 'url' => $url, 'data' => $data, 'wait' => $wait ]); } - + protected function sendEmail($to, $content, $title) { // 获取邮件配置 diff --git a/app/index/controller/UserController.php b/app/index/controller/UserController.php index 3bbfcb4..3b80476 100644 --- a/app/index/controller/UserController.php +++ b/app/index/controller/UserController.php @@ -18,16 +18,47 @@ class UserController extends BaseController */ public function login() { - // 增加日志记录,记录用户访问登录页面的操作 - Log::record('用户访问登录页面', 'info'); + if ($this->request->isPost()) { + $data = $this->request->post(); - // 如果用户已经登录,直接跳转到主页 - if (session('user_id')) { - return redirect('index'); - } else { - //跳转登录界面 - return view('login'); + try { + // 验证数据 + $validate = validate([ + 'account' => 'require|email', + 'password' => 'require' + ]); + + if (!$validate->check($data)) { + return json(['code' => 1, 'msg' => $validate->getError()]); + } + + // 查询用户 + $user = Users::where('account', $data['account'])->find(); + if (!$user) { + return json(['code' => 1, 'msg' => '用户不存在']); + } + + // 验证密码 + if ($user->password !== md5($data['password'])) { + return json(['code' => 1, 'msg' => '密码错误']); + } + + // 登录成功,设置session + session('user_id', $user->id); + session('user_name', $user->name); + session('user_avatar', $user->avatar ?? '/static/images/avatar.png'); + + // 记录登录日志 + Log::record('用户登录成功:' . $user->account, 'info'); + + return json(['code' => 0, 'msg' => '登录成功']); + + } catch (\Exception $e) { + return json(['code' => 1, 'msg' => '登录失败:' . $e->getMessage()]); + } } + + return view('login'); } /** @@ -43,7 +74,7 @@ class UserController extends BaseController try { // 验证数据 $validate = validate([ - 'account' => 'require|email|unique:users', + 'account' => 'require|email|unique:users', 'code' => 'require|number|length:6', 'password' => 'require|min:6|max:20', 'repassword' => 'require|confirm:password' @@ -62,19 +93,19 @@ class UserController extends BaseController ]); if (!$validate->check($data)) { - return json(['code' => 1, 'msg' => $validate->getError()]); + return json(['code' => 1, 'msg' => $validate->getError()]); } // 验证邮箱验证码 $emailCode = cache('email_code_' . $data['account']); if (!$emailCode || $emailCode != $data['code']) { - return json(['code' => 1, 'msg' => '验证码错误或已过期']); + return json(['code' => 1, 'msg' => '验证码错误或已过期']); } // 创建用户 $user = new Users; $user->account = $data['account']; - $user->password = md5($data['password']); + $user->password = md5($data['password']); $user->name = $this->generateRandomName(); $user->create_time = time(); $user->save(); @@ -82,10 +113,10 @@ class UserController extends BaseController // 清除验证码缓存 cache('email_code_' . $data['account'], null); - return json(['code' => 0, 'msg' => '注册成功']); + return json(['code' => 0, 'msg' => '注册成功']); } catch (\Exception $e) { - return json(['code' => 1, 'msg' => '注册失败:' . $e->getMessage()]); + return json(['code' => 1, 'msg' => '注册失败:' . $e->getMessage()]); } } @@ -107,7 +138,17 @@ class UserController extends BaseController // 清除缓存中的用户信息 Cache::tag('user_cache')->clear(); - return redirect('login'); + + // 清除cookie + cookie('user_id', null); + cookie('user_name', null); + cookie('user_avatar', null); + cookie('expire_time', null); + cookie('is_auto_login', null); + cookie('auto_login_attempted', null); + + // 返回成功状态 + return json(['code' => 0, 'msg' => '退出成功']); } // 生成随机用户名 diff --git a/app/index/model/AdminConfig.php b/app/index/model/AdminConfig.php new file mode 100644 index 0000000..86c52c1 --- /dev/null +++ b/app/index/model/AdminConfig.php @@ -0,0 +1,8 @@ + $isLoggedIn, + 'name' => session('user_name'), + 'avatar' => session('user_avatar') ? '/static/uploads/avatar/' . session('user_avatar') : '/static/images/avatar.png' +]; +?> + + +
+