340 lines
14 KiB
PHP
340 lines
14 KiB
PHP
|
||
|
||
<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(['layer'], function () {
|
||
var layer = layui.layer;
|
||
var $ = layui.jquery;
|
||
|
||
// 执行搜索
|
||
function executeSearch() {
|
||
var searchInput = document.getElementById('searchInput');
|
||
if (!searchInput) {
|
||
layer.msg('搜索组件初始化失败');
|
||
return;
|
||
}
|
||
|
||
var keyword = searchInput.value.trim();
|
||
var type = document.getElementById('searchType').value;
|
||
|
||
if (!keyword) {
|
||
layer.msg('请输入搜索关键词');
|
||
return;
|
||
}
|
||
|
||
// 跳转到统一的搜索结果页面
|
||
window.location.href = '/index/search/index?keyword=' + encodeURIComponent(keyword) + '&type=' + type;
|
||
}
|
||
|
||
// 绑定事件
|
||
$(function () {
|
||
var searchMask = $('#searchMask');
|
||
var searchInput = $('#searchInput');
|
||
var searchBtn = $('#searchBtn');
|
||
var mainSearchIcon = $('#mainSearchIcon');
|
||
var stickySearchIcon = $('#stickySearchIcon');
|
||
|
||
// 显示搜索框
|
||
function showSearch() {
|
||
searchMask.addClass('show');
|
||
setTimeout(function () {
|
||
searchInput.focus();
|
||
}, 300);
|
||
}
|
||
|
||
// 隐藏搜索框
|
||
function hideSearch() {
|
||
searchMask.removeClass('show');
|
||
searchInput.val('');
|
||
}
|
||
|
||
// 绑定搜索图标点击事件
|
||
mainSearchIcon.on('click', showSearch);
|
||
stickySearchIcon.on('click', showSearch);
|
||
|
||
// 绑定搜索按钮点击事件
|
||
searchBtn.on('click', function (e) {
|
||
e.preventDefault();
|
||
executeSearch();
|
||
});
|
||
|
||
// 绑定回车键搜索
|
||
searchInput.on('keypress', function (e) {
|
||
if (e.which === 13) {
|
||
e.preventDefault();
|
||
executeSearch();
|
||
}
|
||
});
|
||
|
||
// 点击遮罩层关闭搜索框
|
||
searchMask.on('click', function (e) {
|
||
if ($(e.target).hasClass('search-mask')) {
|
||
hideSearch();
|
||
}
|
||
});
|
||
|
||
// 绑定ESC键关闭搜索框
|
||
$(document).on('keydown', function (e) {
|
||
if (e.keyCode === 27 && searchMask.hasClass('show')) {
|
||
hideSearch();
|
||
}
|
||
});
|
||
|
||
// 输入框获得焦点时选中所有文本
|
||
searchInput.on('focus', function () {
|
||
this.select();
|
||
});
|
||
});
|
||
});
|
||
|
||
// 其他功能相关代码
|
||
layui.use(['carousel', 'form'], function () {
|
||
var carousel = layui.carousel;
|
||
var form = layui.form;
|
||
var $ = 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();
|
||
|
||
$(document).ready(function () {
|
||
// 主导航头像
|
||
$("#userAvatarMain").click(function (e) {
|
||
e.stopPropagation();
|
||
$("#userDropdownMain").toggleClass("show");
|
||
$("#userDropdownSticky").removeClass("show"); // 保证只显示一个
|
||
});
|
||
// 固定导航头像
|
||
$("#userAvatarSticky").click(function (e) {
|
||
e.stopPropagation();
|
||
$("#userDropdownSticky").toggleClass("show");
|
||
$("#userDropdownMain").removeClass("show"); // 保证只显示一个
|
||
});
|
||
|
||
// 点击页面其他地方隐藏所有菜单
|
||
$(document).click(function (e) {
|
||
if (!$(e.target).closest('.user-dropdown, #userAvatarMain, #userAvatarSticky').length) {
|
||
$("#userDropdownMain, #userDropdownSticky").removeClass("show");
|
||
}
|
||
});
|
||
|
||
// 点击菜单项时隐藏菜单
|
||
$("#userDropdownMain li a, #userDropdownSticky li a").click(function () {
|
||
$("#userDropdownMain, #userDropdownSticky").removeClass("show");
|
||
});
|
||
});
|
||
|
||
// 退出登录
|
||
$('.logout-btn').on('click', function () {
|
||
layer.confirm('确定要退出登录吗?', {
|
||
btn: ['确定', '取消']
|
||
}, function () {
|
||
// 先发送退出请求
|
||
$.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();
|
||
}
|
||
}
|
||
});
|
||
});
|
||
});
|
||
|
||
// 监听滚动事件
|
||
$(window).scroll(function () {
|
||
var scrollTop = $(window).scrollTop();
|
||
if (scrollTop > 150) { // 当滚动超过150px时显示固定导航
|
||
$('.sticky-nav').fadeIn();
|
||
} else {
|
||
$('.sticky-nav').fadeOut();
|
||
}
|
||
});
|
||
|
||
// 公众号二维码
|
||
const trigger = document.querySelector('.qrcode-trigger');
|
||
const popup = document.querySelector('.qrcode-popup');
|
||
|
||
// // 鼠标移入显示二维码
|
||
// trigger.addEventListener('mouseenter', function () {
|
||
// popup.style.display = 'block';
|
||
// });
|
||
|
||
// // 鼠标移出隐藏二维码
|
||
// trigger.addEventListener('mouseleave', function () {
|
||
// popup.style.display = 'none';
|
||
// });
|
||
|
||
// // 鼠标移入二维码区域时保持显示
|
||
// popup.addEventListener('mouseenter', function () {
|
||
// popup.style.display = 'block';
|
||
// });
|
||
|
||
// // 鼠标移出二维码区域时隐藏
|
||
// popup.addEventListener('mouseleave', function () {
|
||
// popup.style.display = 'none';
|
||
// });
|
||
|
||
form.on('submit(accountLogin)', function (data) {
|
||
$.ajax({
|
||
url: '{: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>
|
||
</body>
|
||
|
||
</html> |