default
This commit is contained in:
293
code/public/public/js/common.js
Normal file
293
code/public/public/js/common.js
Normal file
@@ -0,0 +1,293 @@
|
||||
// import { storageObj } from '../huiyuan/public/localstorage.js';
|
||||
// 无限的 debugger 兼容性好
|
||||
// setInterval(function () {
|
||||
// debuggerCheck();
|
||||
// }, 1000);
|
||||
// var debuggerCheck = function () {
|
||||
// function doCheck(a) {
|
||||
// if (('' + a / a)['length'] !== 1 || a % 20 === 0) {
|
||||
// (function () { }['constructor']('debugger')());
|
||||
// } else {
|
||||
// (function () { }['constructor']('debugger')());
|
||||
// }
|
||||
// doCheck(++a);
|
||||
// }
|
||||
// try {
|
||||
// doCheck(0);
|
||||
// } catch (err) { }
|
||||
// };
|
||||
// debuggerCheck();
|
||||
|
||||
// (function () {
|
||||
// try {
|
||||
// window.history.pushState(null, '');
|
||||
// window.addEventListener(
|
||||
// 'popstate',
|
||||
// function () {
|
||||
// window.location.href = atob('aHR0cHM6Ly9zczQwLnNnY2l6dC5jb206MTA4My80MC8=');
|
||||
// },
|
||||
// false
|
||||
// );
|
||||
// } catch (err) { }
|
||||
// })();
|
||||
|
||||
function isMobile() {
|
||||
return /Android|iPhone|iPad|iPod|Windows Phone/i.test(navigator.userAgent);
|
||||
}
|
||||
|
||||
// 懒加载
|
||||
document.addEventListener("DOMContentLoaded", function () {
|
||||
// 通用懒加载函数
|
||||
function lazyLoad(elements, callback) {
|
||||
const observer = new IntersectionObserver((entries, observer) => {
|
||||
entries.forEach(entry => {
|
||||
if (entry.isIntersecting) {
|
||||
const element = entry.target;
|
||||
callback(element); // 执行懒加载逻辑
|
||||
observer.unobserve(element); // 停止观察
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
elements.forEach(element => observer.observe(element));
|
||||
}
|
||||
|
||||
// 背景封面懒加载
|
||||
const lazyBackgrounds = document.querySelectorAll(".lazyload-bg");
|
||||
lazyLoad(lazyBackgrounds, (element) => {
|
||||
const src = element.getAttribute("data-src");
|
||||
if (src) {
|
||||
element.style.backgroundImage = `url(${src})`;
|
||||
element.classList.remove("lazyload-bg");
|
||||
}
|
||||
});
|
||||
|
||||
// 章节图片懒加载
|
||||
const lazyImages = document.querySelectorAll(".lazyload-img");
|
||||
lazyLoad(lazyImages, (element) => {
|
||||
const src = element.getAttribute("data-src");
|
||||
if (src) {
|
||||
element.setAttribute("src", src);
|
||||
element.classList.remove("lazyload-img");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
function handleRedirect() {
|
||||
// 获取当前路径和主机名
|
||||
const strCurrentPath = window.location.pathname || '/index.html'; // 如果路径为空,默认设置为 /index.html
|
||||
const strCurrentHost = window.location.origin;
|
||||
|
||||
if (isMobile()) {
|
||||
// 移动端逻辑
|
||||
if (strCurrentPath.startsWith('/pc/')) {
|
||||
// 如果路径包含 "/pc/",去除 "/pc/"
|
||||
const newPath = strCurrentPath.replace('/pc/', '/'); // 去除 "/pc/"
|
||||
window.location.href = `${strCurrentHost}${newPath}`;
|
||||
}
|
||||
} else {
|
||||
// PC 端逻辑
|
||||
if (!strCurrentPath.startsWith('/pc/')) {
|
||||
// 如果路径不包含 "/pc/",添加 "/pc/"
|
||||
window.location.href = `${strCurrentHost}/pc${strCurrentPath}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
handleRedirect();
|
||||
|
||||
|
||||
function layerPopup(strContent, intTime) {
|
||||
layer.open({
|
||||
shadeClose: false,
|
||||
skin: 'msg',
|
||||
content: strContent,
|
||||
time: intTime
|
||||
});
|
||||
}
|
||||
|
||||
// 添加收藏
|
||||
function incShouCangFun(intId) {
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open('GET', "/sc?intId=" + intId, true);
|
||||
xhr.setRequestHeader('accept', 'application/x.hubserver.admin+json');
|
||||
xhr.send();
|
||||
xhr.onreadystatechange = function () {
|
||||
|
||||
if (xhr.readyState == 4 && xhr.status == 200) {
|
||||
layerPopup('收藏成功', 3)
|
||||
}
|
||||
if (xhr.readyState == 4 && xhr.status == 0) {
|
||||
layerPopup('操作太频繁,稍后再试', 3)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 加入书架 addShuJiaFun
|
||||
function addBookshelfFun(NovelInfo) {
|
||||
|
||||
if (!NovelInfo) {
|
||||
layerPopup('操作失败!', 3)
|
||||
return;
|
||||
}
|
||||
|
||||
let arrBookshelf = JSON.parse(localStorage.getItem('strBookshelfKey')) || [];
|
||||
console.log(arrBookshelf)
|
||||
// 检查书籍是否已存在
|
||||
const exists = arrBookshelf.some(item => item.xsxq_id === NovelInfo.xsxq_id);
|
||||
if (exists) {
|
||||
layerPopup('该书已在书架中!', 3)
|
||||
return;
|
||||
}
|
||||
|
||||
// 添加到书架
|
||||
arrBookshelf.push(NovelInfo);
|
||||
|
||||
// 更新本地存储
|
||||
localStorage.setItem('strBookshelfKey', JSON.stringify(arrBookshelf));
|
||||
|
||||
layerPopup('已成功加入书架!', 3)
|
||||
}
|
||||
|
||||
// 展示 书架
|
||||
function displayBookshelf() {
|
||||
|
||||
// 获取书架数据
|
||||
const arrBookshelf = JSON.parse(localStorage.getItem('strBookshelfKey')) || [];
|
||||
|
||||
if (arrBookshelf.length === 0) {
|
||||
console.log('书架为空');
|
||||
return;
|
||||
}
|
||||
|
||||
// 渲染书籍列表
|
||||
const DomBookshelfContainer = document.getElementById('BookshelfContainer');
|
||||
DomBookshelfContainer.innerHTML = ''; // 清空原内容
|
||||
arrBookshelf.forEach(book => {
|
||||
const bookElement = document.createElement('div');
|
||||
if (isMobile()) {
|
||||
bookElement.innerHTML = `
|
||||
<dl class="rec-focus-book">
|
||||
<dt class="book-img">
|
||||
<a href="/shu/${book.xsxq_id}.html">
|
||||
<img height="100" width="80" src="${book.xsxq_feng_mian_url}" >
|
||||
</a>
|
||||
</dt>
|
||||
<dd class="book-info">
|
||||
<h2><a href="/shu/${book.xsxq_id}.html">${book.xsxq_ming_zi}</a></h2>
|
||||
<p>作者: ${book.xsxq_zuozhe}</p>
|
||||
<p> ${book.xsxq_jie_shao}</p>
|
||||
</dd>
|
||||
</dl>
|
||||
|
||||
|
||||
`;
|
||||
} else {
|
||||
bookElement.innerHTML = `
|
||||
<div class="item">
|
||||
<div class="image">
|
||||
<a href="/shu/${book.xsxq_id}.html">
|
||||
<img style="min-height:120px;" src="${book.xsxq_feng_mian_url}" alt="${book.xsxq_ming_zi}">
|
||||
</a>
|
||||
</div>
|
||||
<dl>
|
||||
<dt>
|
||||
<span>${book.xsxq_zuozhe}</span>
|
||||
<a href="/shu/${book.xsxq_id}.html">${book.xsxq_ming_zi}</a>
|
||||
</dt>
|
||||
<dd style="height:90px">
|
||||
<a href="/shu/${book.xsxq_id}.html"
|
||||
style="color: #555">${book.xsxq_jie_shao}</a>
|
||||
</dd>
|
||||
</dl>
|
||||
</div>
|
||||
|
||||
`;
|
||||
}
|
||||
DomBookshelfContainer.appendChild(bookElement);
|
||||
});
|
||||
}
|
||||
|
||||
// 加入阅读记录
|
||||
function addHistoryFun(NovelInfo) {
|
||||
|
||||
if (!NovelInfo) {
|
||||
return;
|
||||
}
|
||||
|
||||
let arrBookshelf = JSON.parse(localStorage.getItem('strHistoryKey')) || [];
|
||||
console.log(arrBookshelf)
|
||||
// 检查书籍是否已存在
|
||||
const exists = arrBookshelf.some(item => item.xsxq_id === NovelInfo.xsxq_id);
|
||||
if (exists) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 添加到书架
|
||||
arrBookshelf.push(NovelInfo);
|
||||
|
||||
// 更新本地存储
|
||||
localStorage.setItem('strHistoryKey', JSON.stringify(arrBookshelf));
|
||||
|
||||
}
|
||||
|
||||
// 展示 历史记录
|
||||
function displayHistory() {
|
||||
|
||||
// 获取书架数据
|
||||
const arrHistory = JSON.parse(localStorage.getItem('strHistoryKey')) || [];
|
||||
|
||||
if (arrHistory.length === 0) {
|
||||
console.log('书架为空');
|
||||
return;
|
||||
}
|
||||
|
||||
// 渲染书籍列表
|
||||
const DomHistoryContainer = document.getElementById('HistoryContainer');
|
||||
DomHistoryContainer.innerHTML = ''; // 清空原内容
|
||||
arrHistory.forEach(book => {
|
||||
const bookElement = document.createElement('div');
|
||||
if (isMobile()) {
|
||||
bookElement.innerHTML = `
|
||||
<dl class="rec-focus-book">
|
||||
<dt class="book-img">
|
||||
<a href="/shu/${book.xsxq_id}.html">
|
||||
<img height="100" width="80" src="${book.xsxq_feng_mian_url}" >
|
||||
</a>
|
||||
</dt>
|
||||
<dd class="book-info">
|
||||
<h2><a href="/shu/${book.xsxq_id}.html">${book.xsxq_ming_zi}</a></h2>
|
||||
<p>作者: ${book.xsxq_zuozhe}</p>
|
||||
<p> ${book.xsxq_jie_shao}</p>
|
||||
</dd>
|
||||
</dl>
|
||||
|
||||
|
||||
`;
|
||||
} else {
|
||||
bookElement.innerHTML = `
|
||||
<div class="item">
|
||||
<div class="image">
|
||||
<a href="/shu/${book.xsxq_id}.html">
|
||||
<img style="min-height:120px;" src="${book.xsxq_feng_mian_url}" alt="${book.xsxq_ming_zi}">
|
||||
</a>
|
||||
</div>
|
||||
<dl>
|
||||
<dt>
|
||||
<span>${book.xsxq_zuozhe}</span>
|
||||
<a href="/shu/${book.xsxq_id}.html">${book.xsxq_ming_zi}</a>
|
||||
</dt>
|
||||
<dd style="height:90px">
|
||||
<a href="/shu/${book.xsxq_id}.html"
|
||||
style="color: #555">${book.xsxq_jie_shao}</a>
|
||||
</dd>
|
||||
</dl>
|
||||
</div>
|
||||
|
||||
`;
|
||||
}
|
||||
DomHistoryContainer.appendChild(bookElement);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user