前端登录登出功能做好了

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)
{
// 获取邮件配置