前端登录登出功能做好了

This commit is contained in:
云泽网
2025-05-27 01:30:41 +08:00
parent ab4a36a7cd
commit 770b8c9092
8 changed files with 753 additions and 64 deletions
+44 -18
View File
@@ -1,5 +1,5 @@
<?php
declare (strict_types = 1);
declare(strict_types=1);
namespace app\index\controller;
@@ -10,6 +10,8 @@ use think\facade\Db;
use app\service\VisitStatsService;
use PHPMailer\PHPMailer\PHPMailer;
use app\index\model\MailConfig;
use app\index\model\AdminConfig;
use app\index\model\Users;
/**
* 前台控制器基础类
@@ -36,7 +38,7 @@ abstract class BaseController
*/
public function __construct(App $app)
{
$this->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)
{
// 获取邮件配置
+56 -15
View File
@@ -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' => '退出成功']);
}
// 生成随机用户名