This commit is contained in:
make
2025-05-05 00:54:34 +08:00
parent bce286a80b
commit cc61365c5d
31 changed files with 1260 additions and 700 deletions

View File

@@ -0,0 +1,429 @@
// 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 formatVideoUrl(strPinYin, intVId) {
return strVideoUrlTemp
.replace('{strPinYin}', encodeURIComponent(strPinYin))
.replace('{intVId}', intVId);
}
function formatVideoPlayUrl(strPinYin, intVId, strPlayType='defualt',intPlayIndex=1) {
return strVideoPlayUrlTemp
.replace('{strPinYin}', encodeURIComponent(strPinYin))
.replace('{intVId}', intVId)
.replace('{strPlayType}', strPlayType)
.replace('{intPlayIndex}', intPlayIndex);
}
function formatSearchUrl(strKey,intPage=1) {
return strSearchUrlTemp
.replace('{strSearchKeywords}', strKey)
.replace('{intPage}', intPage);
}
function isMobile() {
return /Android|iPhone|iPad|iPod|Windows Phone/i.test(navigator.userAgent);
}
// 懒加载
document.addEventListener("DOMContentLoaded", function () {
$(".stui-header__user li,.stui-header__menu li").click(function () { $(this).find(".dropdown").toggle(); });
// 通用懒加载函数
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 { pathname, origin, search } = window.location;
// 规范化路径名,将多个斜杠替换为单个斜杠
const normalizedPath = pathname.replace(/\/+/g, '/');
const strCurrentPath = normalizedPath || '/index.html';
// 构建URL确保斜杠正确
const buildUrl = (path) => {
// 确保路径以单个斜杠开头,并移除多余斜杠
const cleanPath = `/${path.replace(/\/+/g, '/')}`.replace(/^\/+/, '/');
return `${origin}${cleanPath}${search}`;
};
if (isMobile()) {
// 移动端逻辑将PC路径重定向到移动端路径
if (strCurrentPath.startsWith(strPcPath)) {
const newPath = strCurrentPath.replace(strPcPath, '') || '/';
window.location.href = buildUrl(newPath);
}
} else {
// PC端逻辑将非PC路径重定向到PC路径
if (!strCurrentPath.startsWith(strPcPath)) {
// 确保 PC 首页以斜杠结尾
const newPath = strCurrentPath === '/' ? `${strPcPath}/` : `${strPcPath}${strCurrentPath}`;
window.location.href = buildUrl(newPath);
}
}
}
// 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)
}
}
}
const strBookshelfKey = window.location.hostname + 'strBookshelfKey';
const strHistoryKey = window.location.hostname + 'strHistoryKey';
// 加入书架
function addBookshelfFun(NovelInfo) {
if (!NovelInfo) {
NovelInfo = arrNovel
}
let arrBookshelf = JSON.parse(localStorage.getItem(strBookshelfKey)) || [];
// 检查书籍是否已存在
const index = arrBookshelf.findIndex(item => item.n_id === NovelInfo.n_id);
if (index !== -1) {
// 如果存在,更新该书籍的信息
arrBookshelf[index] = { ...arrBookshelf[index], ...NovelInfo };
layerPopup('已存在书架中!', 3)
} else {
// 如果不存在,添加到历史记录
arrBookshelf.push(NovelInfo);
layerPopup('已成功加入书架!', 3)
}
// 更新本地存储
localStorage.setItem(strBookshelfKey, JSON.stringify(arrBookshelf));
}
function displayShujia() {
// 获取书架数据
const shujia = JSON.parse(localStorage.getItem(strBookshelfKey)) || [];
if (shujia.length === 0) {
console.log('书架为空');
return;
}
// 渲染书籍列表
const shujiaContainer = document.getElementById('shujiaContainer');
shujiaContainer.innerHTML = ''; // 清空原内容
shujia.forEach(book => {
const bookElement = document.createElement('div');
let strNovelUrl = formatNovelUrl(book.n_name_pinyin, book.n_id)
let strNovelChapterUrl = formatNovelChapterUrl(book.n_name_pinyin, book.n_id, book.c_sort_num)
let strSearchUrl = formatSearchUrl(book.n_author, 1)
if (isMobile()) {
bookElement.innerHTML = `
<dl class="rec-focus-book">
<dt class="book-img">
<a href='${strNovelUrl}'>
<img height="100" width="80" src="${book.n_cover_path}" >
</a>
</dt>
<dd class="book-info">
<h2><a href='${strNovelUrl}'>${book.n_name}</a></h2>
<p>作者: ${book.n_author}</p>
<p> ${book.c_name}</p>
</dd>
</dl>
`;
} else {
bookElement.innerHTML = `
<div class="item">
<div class="image">
<a href='${strPcPath}${strNovelUrl}'>
<img style="min-height:120px;" src="${book.n_cover_path}" alt="${book.n_name}">
</a>
</div>
<dl>
<dt>
<span>${book.n_author}</span>
<a href='${strPcPath}${strNovelUrl}'>${book.n_name}</a>
</dt>
<dd style="height:90px">
<a href='${strPcPath}${strNovelChapterUrl}'
style="color: #555">${book.c_name}</a>
</dd>
</dl>
</div>
`;
}
shujiaContainer.appendChild(bookElement);
});
}
// 加入阅读记录
function addHistoryFun(VideoInfo) {
// 如果没有传入 VideoInfo arrVideo 并补充字段
if (!VideoInfo) {
VideoInfo = arrVideo
}
// 获取本地存储中的历史记录
let arrHistory = JSON.parse(localStorage.getItem(strHistoryKey)) || [];
// 检查书籍是否已存在
const index = arrHistory.findIndex(item => item.v_id == VideoInfo.v_id);
if (index !== -1) {
// 如果存在,更新该书籍的信息
arrHistory[index] = { ...arrHistory[index], ...VideoInfo };
} else {
// 如果不存在,添加到历史记录
arrHistory.push(VideoInfo);
}
// 更新本地存储
localStorage.setItem(strHistoryKey, JSON.stringify(arrHistory));
}
function displayHistory() {
// 获取数据
const arrHistory = JSON.parse(localStorage.getItem(strHistoryKey)) || [];
if (arrHistory.length === 0) {
console.log('记录为空');
return;
}
// 渲染列表
const historyContainer = document.getElementById('historyContainer');
historyContainer.innerHTML = ''; // 清空原内容
arrHistory.forEach(Video => {
const VideoElement = document.createElement('li');
let strVideoUrl = formatVideoUrl(Video.v_name_en, Video.v_id)
let strVideoPlayUrl = formatVideoPlayUrl(Video.v_name_en,Video.v_id, 'default', 1)
let strSearchUrl = formatSearchUrl(Video.v_name, 1)
if (isMobile()) {
VideoElement.innerHTML = `
<li class="active clearfix">
<div class="thumb">
<a class="v-thumb stui-vodlist__thumb lazyload-bg"
href='${strVideoUrl}'
title="${Video.v_name}"
data-src="${Video.v_pic}">
<span class="play hidden-xs"></span>
<span class="pic-text text-right">${Video.v_remarks}</span>
</a>
</div>
<div class="detail">
<h3 class="title">
<a href='${strVideoUrl}'>${Video.v_name}</a>
</h3>
<p class="hidden-mi">
<span class="text-muted">简介:</span>
${Video.v_description}
</p>
<p class="hidden-mi">
<span class="text-muted">类型:</span>
${Video.v_category}
<span class="split-line"></span>
<span class="hidden-xs">
<span class="split-line"></span>
<span class="text-muted">年份:</span>
${Video.v_year}
</span>
</p>
<p class="margin-0">
<a class="btn btn-min btn-primary" href='${strVideoPlayUrl}'>立即播放</a>
&nbsp;&nbsp;
<a class="btn btn-min btn-default" href='${strVideoUrl}'>查看详情</a>
</p>
</div>
</li>
`;
} else {
VideoElement.innerHTML = `
<li class="active clearfix">
<div class="thumb">
<a class="v-thumb stui-vodlist__thumb lazyload-bg"
href='${strVideoUrl}'
title="${Video.v_name}"
data-src="${Video.v_pic}">
<span class="play hidden-xs"></span>
<span class="pic-text text-right">${Video.v_remarks}</span>
</a>
</div>
<div class="detail">
<h3 class="title">
<a href='${strVideoUrl}'>${Video.v_name}</a>
</h3>
<p class="hidden-mi">
<span class="text-muted">简介:</span>
${Video.v_description}
</p>
<p class="hidden-mi">
<span class="text-muted">类型:</span>
${Video.v_category}
<span class="split-line"></span>
<span class="hidden-xs">
<span class="split-line"></span>
<span class="text-muted">年份:</span>
${Video.v_year}
</span>
</p>
<p class="margin-0">
<a class="btn btn-min btn-primary" href='${strVideoPlayUrl}'>立即播放</a>
&nbsp;&nbsp;
<a class="btn btn-min btn-default" href='${strVideoUrl}'>查看详情</a>
</p>
</div>
</li>
`;
}
historyContainer.appendChild(VideoElement);
});
}
// 方法1使用 fetch API
async function reportStats(data) {
try {
const response = await fetch(strPushApi+'/publishv', {
method: 'POST',
// mode: 'no-cors', // 添加这行
headers: {
'Content-Type': 'application/json',
// 如果需要认证可以添加
'Authorization': 'Bearer your-token'
},
body: JSON.stringify(data)
});
if (!response.ok) {
throw new Error('上报失败');
}
// const result = await response.json();
// console.log('上报成功:', result);
// return result;
} catch (error) {
console.error('上报错误:', error);
}
}
document.addEventListener("DOMContentLoaded", function () {
if (typeof strVideoId !== "undefined") {
// 使用示例
const statsData = {
v_id: strVideoId,
};
// 调用方法1
reportStats(statsData);
addHistoryFun()
}
})