继续完善前端登录模块
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
<?php /*a:4:{s:59:"E:\Demos\DemoOwns\PHP\yunzer\app\index\view\index\index.php";i:1746865108;s:64:"E:\Demos\DemoOwns\PHP\yunzer\app\index\view\component\header.php";i:1747445574;s:62:"E:\Demos\DemoOwns\PHP\yunzer\app\index\view\component\main.php";i:1747646542;s:64:"E:\Demos\DemoOwns\PHP\yunzer\app\index\view\component\footer.php";i:1747617266;}*/ ?>
|
||||
<?php /*a:4:{s:59:"E:\Demos\DemoOwns\PHP\yunzer\app\index\view\index\index.php";i:1746865108;s:64:"E:\Demos\DemoOwns\PHP\yunzer\app\index\view\component\header.php";i:1748316235;s:62:"E:\Demos\DemoOwns\PHP\yunzer\app\index\view\component\main.php";i:1747817496;s:64:"E:\Demos\DemoOwns\PHP\yunzer\app\index\view\component\footer.php";i:1747617266;}*/ ?>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
@@ -16,7 +16,38 @@
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div style="display: flex;flex-direction: column;">
|
||||
<?php
|
||||
// 获取当前登录状态
|
||||
$isLoggedIn = false;
|
||||
$userInfo = [
|
||||
'is_login' => false,
|
||||
'name' => '',
|
||||
'avatar' => '/static/images/avatar.png' // 默认头像
|
||||
];
|
||||
|
||||
// 检查cookie
|
||||
$userAccount = cookie('user_account');
|
||||
if ($userAccount) {
|
||||
$isLoggedIn = true;
|
||||
$userInfo = [
|
||||
'is_login' => true,
|
||||
'name' => cookie('user_name'),
|
||||
'avatar' => cookie('user_avatar') ? cookie('user_avatar') : '/static/images/avatar.png'
|
||||
];
|
||||
}
|
||||
|
||||
// 添加一个隐藏的div来存储登录状态
|
||||
$loginStatus = [
|
||||
'isLoggedIn' => $isLoggedIn,
|
||||
'userAccount' => $userAccount ?? ''
|
||||
];
|
||||
?>
|
||||
|
||||
<!-- 添加一个隐藏的div来存储登录状态 -->
|
||||
<div id="loginStatus" style="display: none;" data-is-logged-in="<?php echo htmlentities((string) $isLoggedIn); ?>" data-user-account="<?php echo isset($userAccount) ? htmlentities((string) $userAccount) : ''; ?>">
|
||||
</div>
|
||||
|
||||
<div style="display: flex;flex-direction: column;">
|
||||
<div class="topbar-one">
|
||||
<div class="container">
|
||||
<div style="width: 70%;">
|
||||
@@ -32,8 +63,6 @@
|
||||
</ul>
|
||||
</div>
|
||||
<div class="topbar-one__social" style="width: 30%;">
|
||||
<a href="/index/user/login" class="mr-10"><i class="layui-icon layui-icon-username"></i> 登录</a>
|
||||
<a href="/index/user/register" class="mr-10"><i class="layui-icon layui-icon-user"></i> 注册</a>
|
||||
<a href="javascript:;" class="qrcode-trigger"><i class="layui-icon layui-icon-qrcode"></i> 公众号</a>
|
||||
<div class="qrcode-popup"
|
||||
style="display:none;position:absolute;right:54px;top:32px;background:#fff;padding:10px;box-shadow:0 0 10px rgba(0,0,0,0.1); z-index: 1000;">
|
||||
@@ -57,27 +86,40 @@
|
||||
</ul>
|
||||
</div>
|
||||
<div class="main-menu__right">
|
||||
<div class="username">
|
||||
<?php if ($userInfo['is_login']): ?>
|
||||
<span class="username-text"><?php echo htmlentities((string) $userInfo['name']); ?></span>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<div class="layui-inline">
|
||||
<div class="layui-inline" style="position: relative;">
|
||||
<img src="/static/images/avatar.webp" class="layui-circle"
|
||||
style="width: 40px; height: 40px; cursor: pointer;" id="userAvatarMain">
|
||||
<div class="user-dropdown" id="userDropdownMain">
|
||||
<ul>
|
||||
<li>
|
||||
<a href="/index/user/profile"><i
|
||||
class="layui-icon layui-icon-user"></i><span>个人中心</span></a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="/index/user/settings"><i
|
||||
class="layui-icon layui-icon-set"></i><span>账号管理</span></a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="javascript:;" class="logout-btn"><i
|
||||
class="layui-icon layui-icon-logout"></i><span>退出登录</span></a>
|
||||
</li>
|
||||
</ul>
|
||||
<!-- 根据登录状态显示不同的内容 -->
|
||||
<?php if ($userInfo['is_login']): ?>
|
||||
<div class="layui-inline" style="position: relative;margin-left:20px;">
|
||||
<img src="<?php echo htmlentities((string) $userInfo['avatar']); ?>" class="layui-circle"
|
||||
style="width: 40px; height: 40px; cursor: pointer;" id="userAvatarMain">
|
||||
<div class="user-dropdown" id="userDropdownMain">
|
||||
<ul>
|
||||
<li>
|
||||
<a href="/index/user/profile"><i
|
||||
class="layui-icon layui-icon-user"></i><span>个人中心</span></a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="/index/user/settings"><i
|
||||
class="layui-icon layui-icon-set"></i><span>账号管理</span></a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="javascript:;" class="logout-btn"><i
|
||||
class="layui-icon layui-icon-logout"></i><span>退出登录</span></a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<?php else: ?>
|
||||
<div class="layui-inline">
|
||||
<a href="/index/user/login" class="layui-btn layui-btn-normal">登录</a>
|
||||
<a href="/index/user/register" class="layui-btn layui-btn-primary">注册</a>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -107,27 +149,40 @@
|
||||
</div>
|
||||
<div class="sticky-nav__right">
|
||||
<div class="main-menu__right">
|
||||
<div class="username">
|
||||
<?php if ($userInfo['is_login']): ?>
|
||||
<span class="username-text"><?php echo htmlentities((string) $userInfo['name']); ?></span>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<div class="layui-inline">
|
||||
<div class="layui-inline" style="position: relative;">
|
||||
<img src="/static/images/avatar.webp" class="layui-circle"
|
||||
style="width: 40px; height: 40px; cursor: pointer;" id="userAvatarSticky">
|
||||
<div class="user-dropdown" id="userDropdownSticky">
|
||||
<ul>
|
||||
<li>
|
||||
<a href="/index/user/profile"><i
|
||||
class="layui-icon layui-icon-user"></i><span>个人中心</span></a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="/index/user/settings"><i
|
||||
class="layui-icon layui-icon-set"></i><span>账号管理</span></a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="javascript:;" class="logout-btn"><i
|
||||
class="layui-icon layui-icon-logout"></i><span>退出登录</span></a>
|
||||
</li>
|
||||
</ul>
|
||||
<!-- 根据登录状态显示不同的内容 -->
|
||||
<?php if ($userInfo['is_login']): ?>
|
||||
<div class="layui-inline" style="position: relative;margin-left:20px;">
|
||||
<img src="<?php echo htmlentities((string) $userInfo['avatar']); ?>" class="layui-circle"
|
||||
style="width: 40px; height: 40px; cursor: pointer;" id="userAvatarSticky">
|
||||
<div class="user-dropdown" id="userDropdownSticky">
|
||||
<ul>
|
||||
<li>
|
||||
<a href="/index/user/profile"><i
|
||||
class="layui-icon layui-icon-user"></i><span>个人中心</span></a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="/index/user/settings"><i
|
||||
class="layui-icon layui-icon-set"></i><span>账号管理</span></a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="javascript:;" class="logout-btn"><i
|
||||
class="layui-icon layui-icon-logout"></i><span>退出登录</span></a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<?php else: ?>
|
||||
<div class="layui-inline">
|
||||
<a href="/index/user/login" class="layui-btn layui-btn-normal">登录</a>
|
||||
<a href="/index/user/register" class="layui-btn layui-btn-primary">注册</a>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -333,12 +388,108 @@
|
||||
#test10 [carousel-item]>* {
|
||||
background: none !important;
|
||||
}
|
||||
|
||||
.main-menu__right {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.username {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
</style>
|
||||
|
||||
<script>
|
||||
// 在页面加载时立即执行
|
||||
(function () {
|
||||
// 检查是否已经刷新过
|
||||
if (sessionStorage.getItem('has_refreshed') === 'true') {
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查localStorage中是否有用户账号
|
||||
var userAccount = localStorage.getItem('user_account');
|
||||
if (userAccount) {
|
||||
// 同步到cookie
|
||||
document.cookie = "user_account=" + userAccount + "; path=/";
|
||||
|
||||
// 如果有其他必要的数据,也同步到cookie
|
||||
var userId = localStorage.getItem('user_id');
|
||||
var userName = localStorage.getItem('user_name');
|
||||
var userAvatar = localStorage.getItem('user_avatar');
|
||||
|
||||
if (userId) document.cookie = "user_id=" + userId + "; path=/";
|
||||
if (userName) document.cookie = "user_name=" + userName + "; path=/";
|
||||
if (userAvatar) document.cookie = "user_avatar=" + userAvatar + "; path=/";
|
||||
|
||||
// 刷新页面以应用新的cookie,并标记已刷新
|
||||
if (!document.cookie.includes('user_id')) {
|
||||
sessionStorage.setItem('has_refreshed', 'true');
|
||||
window.location.reload();
|
||||
}
|
||||
}
|
||||
})();
|
||||
|
||||
layui.use(['carousel', 'form', 'layer'], function () {
|
||||
var carousel = layui.carousel, form = layui.form, layer = layui.layer, $ = layui.$;
|
||||
|
||||
// 检查本地存储并自动登录
|
||||
function checkAutoLogin() {
|
||||
// 如果已经登录,不再执行自动登录
|
||||
if ($('#userAvatarMain').length > 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果已经尝试过自动登录,不再执行
|
||||
if (sessionStorage.getItem('auto_login_attempted') === 'true') {
|
||||
return;
|
||||
}
|
||||
|
||||
// 从localStorage获取用户账号
|
||||
var userAccount = localStorage.getItem('user_account');
|
||||
if (userAccount) {
|
||||
// 标记已尝试自动登录
|
||||
sessionStorage.setItem('auto_login_attempted', 'true');
|
||||
|
||||
// 发送自动登录请求
|
||||
$.ajax({
|
||||
url: '/index/user/login',
|
||||
type: 'POST',
|
||||
data: {
|
||||
account: userAccount,
|
||||
password: atob(localStorage.getItem('user_password'))
|
||||
},
|
||||
dataType: 'json',
|
||||
success: function (res) {
|
||||
if (res.code === 0) {
|
||||
// 设置cookie
|
||||
document.cookie = "user_id=" + res.data.id + "; path=/";
|
||||
document.cookie = "user_name=" + res.data.name + "; path=/";
|
||||
document.cookie = "user_avatar=" + res.data.avatar + "; path=/";
|
||||
document.cookie = "user_account=" + userAccount + "; path=/";
|
||||
|
||||
// 同时更新localStorage
|
||||
localStorage.setItem('user_id', res.data.id);
|
||||
localStorage.setItem('user_name', res.data.name);
|
||||
localStorage.setItem('user_avatar', res.data.avatar);
|
||||
|
||||
// 登录成功,强制刷新页面
|
||||
window.location.href = window.location.href + '?t=' + new Date().getTime();
|
||||
} else {
|
||||
// 登录失败,清除所有相关存储
|
||||
localStorage.removeItem('user_account');
|
||||
localStorage.removeItem('user_password');
|
||||
sessionStorage.removeItem('auto_login_attempted');
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 页面加载时检查自动登录
|
||||
checkAutoLogin();
|
||||
|
||||
// 加载banner数据
|
||||
$.ajax({
|
||||
url: '/index/index/bannerlist',
|
||||
@@ -411,7 +562,36 @@
|
||||
layer.confirm('确定要退出登录吗?', {
|
||||
btn: ['确定', '取消']
|
||||
}, function () {
|
||||
window.location.href = '/index/user/logout';
|
||||
// 先发送退出请求
|
||||
$.ajax({
|
||||
url: '/index/user/logout',
|
||||
type: 'POST',
|
||||
dataType: 'json',
|
||||
success: function (res) {
|
||||
if (res.code === 0) {
|
||||
// 清除localStorage
|
||||
localStorage.removeItem('user_account');
|
||||
localStorage.removeItem('user_password');
|
||||
localStorage.removeItem('user_id');
|
||||
localStorage.removeItem('user_name');
|
||||
localStorage.removeItem('user_avatar');
|
||||
|
||||
// 清除sessionStorage
|
||||
sessionStorage.removeItem('auto_login_attempted');
|
||||
sessionStorage.removeItem('has_refreshed');
|
||||
|
||||
// 清除cookie
|
||||
document.cookie = "user_id=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
|
||||
document.cookie = "user_name=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
|
||||
document.cookie = "user_avatar=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
|
||||
document.cookie = "user_account=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
|
||||
document.cookie = "user_password=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
|
||||
|
||||
// 强制刷新页面,不使用缓存
|
||||
window.location.href = window.location.href + '?t=' + new Date().getTime();
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -448,6 +628,62 @@
|
||||
popup.addEventListener('mouseleave', function () {
|
||||
popup.style.display = 'none';
|
||||
});
|
||||
|
||||
form.on('submit(accountLogin)', function (data) {
|
||||
$.ajax({
|
||||
url: '<?php echo url("index/user/login"); ?>',
|
||||
type: 'POST',
|
||||
data: data.field,
|
||||
dataType: 'json',
|
||||
success: function (res) {
|
||||
if (res.code === 0) {
|
||||
// 存储登录数据,设置7天过期
|
||||
var expireTime = new Date().getTime() + 7 * 24 * 60 * 60 * 1000;
|
||||
|
||||
// 设置localStorage
|
||||
localStorage.setItem('user_account', data.field.account);
|
||||
localStorage.setItem('user_password', btoa(data.field.password));
|
||||
localStorage.setItem('expire_time', expireTime);
|
||||
localStorage.setItem('is_auto_login', 'true');
|
||||
|
||||
// 设置cookie
|
||||
document.cookie = "user_id=" + res.data.id + "; path=/";
|
||||
document.cookie = "user_name=" + res.data.name + "; path=/";
|
||||
document.cookie = "user_avatar=" + res.data.avatar + "; path=/";
|
||||
document.cookie = "expire_time=" + expireTime + "; path=/";
|
||||
document.cookie = "is_auto_login=true; path=/";
|
||||
document.cookie = "user_account=" + data.field.account + "; path=/";
|
||||
document.cookie = "user_password=" + btoa(data.field.password) + "; path=/";
|
||||
|
||||
// 设置sessionStorage
|
||||
sessionStorage.setItem('auto_login_attempted', 'true');
|
||||
|
||||
layer.msg('登录成功', {
|
||||
icon: 1,
|
||||
time: 2000,
|
||||
shade: 0.3
|
||||
}, function () {
|
||||
// 获取当前页面URL,如果是从其他页面跳转来的,则返回上一页
|
||||
var currentUrl = window.location.href;
|
||||
var referrer = document.referrer;
|
||||
|
||||
// 如果是从登录页面跳转来的,则返回上一页
|
||||
if (referrer && referrer.includes('/index/user/login')) {
|
||||
window.location.href = referrer;
|
||||
} else {
|
||||
// 否则刷新当前页面
|
||||
window.location.href = currentUrl + '?t=' + new Date().getTime();
|
||||
}
|
||||
});
|
||||
layer.msg(res.msg, {
|
||||
icon: 2,
|
||||
time: 2000
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
return false;
|
||||
});
|
||||
});
|
||||
</script>
|
||||
<main class="main-content">
|
||||
@@ -536,6 +772,27 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 游戏下载模块 -->
|
||||
<div class="core-block core-module" id="gameDownload" style="order: 3;">
|
||||
<div class="module-header">
|
||||
<div>
|
||||
<div class="ModuleTitle_titleWrapper">
|
||||
<h3 class="ModuleTitle_title">游戏下载</h3>
|
||||
<div class="tab-container">
|
||||
<div class="tab-header">
|
||||
<div class="tab-item active" data-tab="all">全部</div>
|
||||
<!-- 分类标签将通过JavaScript动态加载 -->
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="more-btn">更多</div>
|
||||
</div>
|
||||
<div class="product-list" id="gameDownloadList">
|
||||
<!-- 游戏将通过JavaScript动态加载 -->
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</main>
|
||||
|
||||
@@ -656,6 +913,32 @@
|
||||
});
|
||||
}
|
||||
|
||||
// 加载游戏下载
|
||||
function loadGames() {
|
||||
fetch('/index/index/gameList')
|
||||
.then(response => response.json())
|
||||
.then(result => {
|
||||
if (result.code === 0) {
|
||||
// 渲染分类标签
|
||||
if (result.data.categories) {
|
||||
renderCategoryTabs(result.data.categories, 'gameDownload');
|
||||
}
|
||||
// 渲染游戏列表
|
||||
if (result.data.games && result.data.games.length > 0) {
|
||||
renderGames(result.data.games, 'gameDownloadList');
|
||||
} else {
|
||||
showNoData('gameDownloadList');
|
||||
}
|
||||
} else {
|
||||
showNoData('gameDownloadList');
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('请求失败:', error);
|
||||
showError('gameDownloadList');
|
||||
});
|
||||
}
|
||||
|
||||
// 显示无数据提示
|
||||
function showNoData(containerId) {
|
||||
document.getElementById(containerId).innerHTML = '<div class="no-data">暂无数据</div>';
|
||||
@@ -718,6 +1001,9 @@
|
||||
case 'programDownload':
|
||||
loadCategoryPrograms(selectedCategoryId, 'programDownloadList');
|
||||
break;
|
||||
case 'gameDownload':
|
||||
loadCategoryGames(selectedCategoryId, 'gameDownloadList');
|
||||
break;
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -958,12 +1244,71 @@
|
||||
});
|
||||
}
|
||||
|
||||
// 渲染游戏列表
|
||||
function renderGames(games, containerId) {
|
||||
const container = document.getElementById(containerId);
|
||||
if (!container) return;
|
||||
|
||||
let html = '';
|
||||
if (Array.isArray(games)) {
|
||||
games.forEach(game => {
|
||||
html += createGameHtml(game);
|
||||
});
|
||||
}
|
||||
|
||||
container.innerHTML = html || '<div class="no-data">暂无数据</div>';
|
||||
}
|
||||
|
||||
// 创建游戏HTML
|
||||
function createGameHtml(game) {
|
||||
if (!game) return '';
|
||||
|
||||
return `
|
||||
<div class="opencourse product-item" onclick="window.open('/index/game/detail?id=${game.id || ''}', '_blank')">
|
||||
<div class="video">
|
||||
<img src="${game.icon || '/static/images/default-game.png'}" alt="" class="cover">
|
||||
</div>
|
||||
<div class="introduction">
|
||||
<div class="title">${game.title || '无标题'}</div>
|
||||
<div class="publishdate">${game.create_time || ''}</div>
|
||||
</div>
|
||||
<div class="bottom">
|
||||
<div class="views"><i class="fa-solid fa-eye"></i><span style="margin-left: 5px;">${game.views || 0}</span></div>
|
||||
<div class="author"><i class="fa-regular fa-user"></i><span style="margin-left: 5px;">${game.uploader || '未知作者'}</span></div>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
// 加载分类游戏
|
||||
function loadCategoryGames(categoryId, containerId) {
|
||||
fetch('/index/index/gameList')
|
||||
.then(response => response.json())
|
||||
.then(result => {
|
||||
if (result.code === 0) {
|
||||
if (categoryId === 'all') {
|
||||
renderGames(result.data.games, containerId);
|
||||
} else {
|
||||
const filteredGames = result.data.games.filter(game => game.cate == categoryId);
|
||||
renderGames(filteredGames, containerId);
|
||||
}
|
||||
} else {
|
||||
showNoData(containerId);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('请求失败:', error);
|
||||
showError(containerId);
|
||||
});
|
||||
}
|
||||
|
||||
// 页面加载完成后执行
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
loadWebArticles();
|
||||
loadTechArticles();
|
||||
loadResources();
|
||||
loadPrograms();
|
||||
loadGames();
|
||||
});
|
||||
</script>
|
||||
<footer class="footer" style="background-image: url(/static/images/footer-bg-1.png)">
|
||||
|
||||
Reference in New Issue
Block a user