/* 视频页面需要用到JS */
console.time('Video.js执行时长');
document.addEventListener('DOMContentLoaded', () => {
const p = new URLSearchParams(window.location.search);
const vod_id = p.get('vod_id');
if (vod_id) {
initVideoDetail();
} else {
initVideoList();
}
/* 初始化列表页 */
function initVideoList() {
/* 当前的分类id */
let queryData = { pg: '', t: '', wd: '' };
/* 总页数 */
let pagecount = '';
/* 是否正在加载列表 */
let isLoading = false;
/* 获取视频分类 */
$.ajax({
url: Joe.BASE_API,
type: 'POST',
data: { routeType: 'maccms_list' },
success(res) {
if (res.code !== 1) return $('.joe_video__type-list').html(`
${res.data}`);
if (!res.data.class.length) return $('.joe_video__type-list').html(`暂无数据!`);
let htmlStr = '全部';
res.data.class.forEach(_ => (htmlStr += `${_.type_name}`));
$('.joe_video__type-list').html(htmlStr);
$('.joe_video__type-list .item').first().click();
}
});
/* 点击切换分类 */
$('.joe_video__type-list').on('click', '.item', function () {
const t = $(this).attr('data-t');
if (isLoading) return;
window.scrollTo({ top: 0, behavior: 'smooth' });
$(this).addClass('active').siblings().removeClass('active');
queryData.pg = 1;
queryData.t = t;
queryData.wd = '';
$('.joe_video__list-search input').val('');
renderDom();
});
/* 渲染视频列表 */
function renderDom() {
$('.joe_video__list-item').css('display', '').html('');
isLoading = true;
$.ajax({
url: Joe.BASE_API,
type: 'POST',
data: { routeType: 'maccms_list', ac: 'videolist', t: queryData.t, pg: queryData.pg, wd: queryData.wd },
success(res) {
if (res.code !== 1) return $('.joe_video__list-item').css('display', 'block').html('数据加载失败!请检查!
');
if (!res.data.list.length) {
$('.joe_video__list-item').css('display', 'block').html('暂无数据!
');
} else {
let htmlStr = '';
res.data.list.forEach(_ => {
htmlStr += `
${_.vod_year}
${_.vod_name}
`;
});
$('.joe_video__list-item').html(htmlStr);
new LazyLoad('.video_lazyload');
}
pagecount = res.data.pagecount;
initPagination();
},
complete: () => (isLoading = false)
});
}
/* 初始化分页 */
function initPagination() {
if(pagecount == 0) return $(".joe_video__pagination").hide()
$(".joe_video__pagination").show()
let htmlStr = '';
if (queryData.pg != 1) {
htmlStr += `
`;
}
htmlStr += ``;
if (queryData.pg != pagecount) {
htmlStr += `
`;
}
if (queryData.pg < pagecount) htmlStr += ``;
$('.joe_video__pagination').html(htmlStr);
}
/* 切换分页 */
$('.joe_video__pagination').on('click', '.joe_video__pagination-item', function () {
const pg = $(this).attr('data-pg');
if (!pg || isLoading) return;
window.scrollTo({ top: 0, behavior: 'smooth' });
queryData.pg = Number(pg);
renderDom();
});
/* 搜索功能 */
const searchFn = () => {
const val = $('.joe_video__list-search input').val();
if (isLoading) return;
queryData.pg = 1;
queryData.t = '';
queryData.wd = val;
$('.joe_video__type-list .item').first().addClass('active').siblings().removeClass('active');
renderDom();
};
$('.joe_video__list-search .button').on('click', searchFn);
$('.joe_video__list-search .input').on('keyup', e => e.keyCode === 13 && searchFn());
}
/* 初始化播放页 */
function initVideoDetail() {
const player = $('.joe_video__player-play').attr('data-player');
$.ajax({
url: Joe.BASE_API,
type: 'POST',
data: {
routeType: 'maccms_list',
ac: 'detail',
ids: vod_id
},
success(res) {
if (res.code !== 1) return $('.joe_video__detail-info').html(`${res.data}
`);
if (!res.data.list.length) return $('.joe_video__detail-info').html(`数据抓取异常!请检查!
`);
const item = res.data.list[0];
/* 设置视频详情 */
$('.joe_video__detail-info').html(`
${item.vod_year}
- ${item.vod_name + (item.vod_remarks ? ' - ' + item.vod_remarks : '')}
- 类型:
${item.vod_class || '未知'}
- 主演:
${item.vod_actor || '未知'}
- 导演:
${item.vod_director || '未知'}
- 简介:
${getContent(item)}
`);
new LazyLoad('.video_lazyload');
/* 设置视频播放标题 */
$('.joe_video__player .joe_video__contain-title').html('正在播放:' + item.vod_name);
/* 设置播放链接 */
let parseList = str => {
let htmlStr = '';
let arr = str.split('#');
arr.forEach(_ => (htmlStr += `${_.split('$')[0] || ''}`));
return htmlStr;
};
let playFromArr = item.vod_play_from.split('$$$');
let playUrlArr = item.vod_play_url.split('$$$');
let maps = new Map();
playFromArr.forEach((element, index) => maps.set(element, playUrlArr[index] || []));
let htmlStr = '';
let index = 0;
for (let [key, value] of maps) {
index++;
htmlStr += `
`;
}
$('.joe_video__player').after(htmlStr);
$('.joe_video__source').first().find('.joe_video__source-list .item').first().click();
}
});
$(document).on('click', '.joe_video__source-list .item', function () {
$('.joe_video__source-list .item').removeClass('active');
$(this).addClass('active');
const url = $(this).attr('data-src') || $(this).html();
$('.joe_video__player-play').attr({ src: player + url });
const offset = $('.joe_video__player').offset().top - $('.joe_header').height() - 15;
window.scrollTo({ top: offset, behavior: 'smooth' });
});
}
function getContent(item) {
if (item.vod_content) {
return item.vod_content.replace(/<[^>]+>/g, '');
} else if (item.vod_blurb) {
return item.vod_blurb.replace(/<[^>]+>/g, '');
} else {
return '暂无简介';
}
}
console.timeEnd('Video.js执行时长');
});