// 处于页面配置时,绑定方法 start
if ($isDecorateMode) {
document.addEventListener("diy_header/1", (event) => {
const { field, value } = event.detail;
console.log(field, value)
if (field === 'hots') {
let html = ''
value.forEach(e => {
html += `${e.name}`
})
$('.' + field).html(html)
}
});
}
// 处于页面配置时,绑定方法 end
$(function () {
$("#logout").click(function () {
$request
.post("/api/user/logout")
.then((res) => {
// 跳转页面
location.href = "/";
});
})
// 防止一开始进入页面在底部不显示
setHeaderSticky()
$(window).scroll(setHeaderSticky)
function setHeaderSticky(){
let offset = $("html,body").scrollTop()
if (offset) {
$('.ys-header').addClass('is-sticky')
} else {
$('.ys-header').removeClass('is-sticky')
}
}
// 导航栏悬停
$('.ys-header__nav .nav-item').hover(function () {
$(this).find('.ys-header__card').addClass('show')
$('.ys-header__cover').removeClass('display-none')
}, function () {
$(this).find('.ys-header__card').removeClass('show')
$('.ys-header__cover').addClass('display-none')
})
// 搜索框悬停事件
$('.ys-header__search').focus(function () {
$('.ys-header__nav').addClass('display-none')
$('.ys-header__right').addClass('open')
}, function() {
if (!$('.ys-header__search-card').hasClass('show')) {
$('.ys-header__nav').removeClass('display-none')
$('.ys-header__right').removeClass('open')
}
})
// 搜索框聚焦事件
$('.ys-header__search input').focus(function () {
openSearchCard()
})
$('.ys-header__search-card .icon-close').click(function () {
closeSearchCard()
})
$(document).on('click', function (e) {
if (!$('.ys-header__search-card')[0].contains(e.target) && !$('.ys-header__search input').is(':focus')) {
closeSearchCard()
}
})
// 搜索
$('.ys-header__search input').keyup(function (e) {
if (e.keyCode === 13) {
search()
}
})
$('.ys-header__search .search-btn').click(function () {
search()
})
// 清空历史记录
$('.ys-header__search-card .search-history span').click(function () {
localStorage.removeItem('searchHistory')
setHistory()
})
// 展开搜索卡片
function openSearchCard() {
$('.ys-header__nav').addClass('display-none')
$('.ys-header__search-card').addClass('show')
$('.ys-header__cover').removeClass('display-none')
$('.ys-header__right').addClass('open')
}
// 收起搜索卡片
function closeSearchCard() {
$('.ys-header__nav').removeClass('display-none')
$('.ys-header__search-card').removeClass('show')
$('.ys-header__cover').addClass('display-none')
$('.ys-header__right').removeClass('open')
}
// 搜索事件
function search() {
let val = $('.ys-header__search input').val()
if (!val) return $message.warning('请输入搜索关键词')
let history = JSON.parse(localStorage.getItem('searchHistory')) || []
history.unshift(val)
history = [...new Set(history)]
localStorage.setItem('searchHistory', JSON.stringify(history.slice(0, 10)))
window.open('/search_result.html?keyword=' + val)
closeSearchCard()
setHistory()
}
// 设置搜索历史
function setHistory() {
let history = JSON.parse(localStorage.getItem('searchHistory')) || []
let html = ''
history.forEach(e => {
html += `${e}`
})
$('.ys-header__search-card .search-history .search-words').html(html)
}
setHistory()
// 熊猫体育官网首页入口
tabs切换
const moreTypeList = ['information', 'catalog'] // 存在 more 的类型
$('.service-cate li').hover(function () {
let type = $(this).attr('data-type')
if (type) {
$('.service-cate li').removeClass('active')
$(this).addClass('active')
$('.ys-header__service .content-item').addClass('display-none')
$('.ys-header__service .content-item[data-type='+type+']').removeClass('display-none')
if(moreTypeList.includes(type)){
$('.service-content-more').not('.' + type).addClass('display-none')
$('.service-content-slide').removeClass('display-none')
$('.service-content-more.' + type).removeClass('display-none')
} else {
$('.service-content-more').addClass('display-none')
$('.service-content-slide').addClass('display-none')
}
}
})
// 关于我们tabs切换
$('.about-cate-item').hover(function () {
let type = $(this).attr('data-type')
if (type) {
$('.about-cate-item').removeClass('active')
$(this).addClass('active')
$('.about-content .about-item').addClass('display-none')
$('.about-content .about-item[data-type='+type+']').removeClass('display-none')
// 用于切换底部更多链接跳转,现在没有更多,如果需要参考上方取消注释
/* $('.about-content-slide').addClass('display-none')
$('.about-content-more').addClass('display-none') */
}
})
})
// 点击tabs项
$(".ys-search__tabs").on("click", "li", function (e) {
$(".ys-search__tabs li").removeClass('active')
$(this).addClass('active')
let type = $(this).attr('data-type')
$('.ys-search__result').each(function (e) {
if ($(this).attr('data-target') == type) {
$(this).removeClass('display-none')
} else {
$(this).addClass('display-none')
}
})
delete params.attributes
filters = []
$('.ys-product-filter-checked').remove()
if (type === 'content') {
result.type = $('.result-type input:checked').val()
} else {
result.type = type
}
params.page = 1
request()
e.preventDefault();
});
// 更多筛选的展开/收起
$(".ys-product-filter").on("click", ".ys-product-filter__more", function (e) {
if ($(this).hasClass('expand')) {
$(this).removeClass('expand')
$('.ys-product-filter__box .filter-item').each(function() {
if (+$(this).attr('data-index') >= 6) {
$(this).addClass('display-none')
}
})
} else {
$(this).addClass('expand')
$('.ys-product-filter__box .filter-item').removeClass('display-none')
showExpand()
}
e.preventDefault();
});
// 条件过滤项的展开/收起
$(".ys-product-filter__box").on("click", ".filter-item .filter-item-expand", function (e) {
if ($(this).hasClass('expand')) {
$(this).removeClass('expand')
$(this).parent().find('.filter-item-selection').removeClass('expand')
$(this).find('span').html('展开')
$(this).find('i').removeClass('expand')
} else {
$(this).addClass('expand')
$(this).parent().find('.filter-item-selection').addClass('expand')
$(this).find('span').html('收起')
$(this).find('i').addClass('expand')
}
e.preventDefault();
});
showExpand()
// 是否显示展开收起
function showExpand() {
$('.ys-product-filter__box .filter-item').each(function() {
let height = $(this).find('.selection-box').height()
if (height < 40) {
$(this).find('.filter-item-expand').addClass('display-none')
} else {
$(this).find('.filter-item-expand').removeClass('display-none')
}
})
}
// 点击筛选项
let filters = []
$(".ys-product-filter__box").on("click", ".filter-item input[type=checkbox]", function (e) {
let id = $(this).attr('id')
let checked = $(this).is(":checked")
let pid = $(this).parents('.filter-item').attr('data-filter_id')
if (checked) {
filters.push({
id,
pid,
title: $(this).attr('data-title')
})
} else {
filters.splice(filters.findIndex(i => i.id === id), 1)
}
params.attributes = {}
filters.forEach(e => {
params.attributes[e.pid] = +e.id
})
params.page = 1
result.list = []
request()
showFiltered()
});
$(".ys-product-filter").on("click", ".ys-product-filter-checked .icon-close", function () {
let id = $(this).parent().data('id')
let pid = $(this).parent().data('pid')
filters.splice(filters.findIndex(e => e.id == id && e.pid == pid), 1)
params.attributes = {}
filters.forEach(e => {
params.attributes[e.pid] = +e.id
})
params.page = 1
result.list = []
request()
showFiltered()
});
$(".ys-product-filter").on("click", ".ys-product-filter-checked button", function () {
filters = []
params.attributes = {}
filters.forEach(e => {
params.attributes[e.pid] = +e.id
})
params.page = 1
result.list = []
request()
showFiltered()
});
// 显示已选筛选项
function showFiltered() {
$('.ys-product-filter-checked').remove()
let html = ''
filters.forEach(e => {
html += `
${e.title}
`
})
if (filters.length) {
$('.ys-product-filter__box').before(`
${html}
`)
}
}
// 设置筛选项options
function setOptions(options) {
if (!options || !options.length) {
$('.ys-product-filter').addClass('display-none')
return
}
$('.ys-product-filter').removeClass('display-none')
let html = ''
options.forEach((e, i) => {
let c_html = ''
e.value.forEach(child => {
c_html += `
i.pid !== 'category_id' && +i.id === child.id) ? 'checked' : ''} />
`
})
html += `
`
})
$('.ys-product-filter__box').html(html)
showExpand()
if (options.length > 6) {
$('.ys-product-filter__more').removeClass('display-none')
$('.ys-product-filter__more').removeClass('expand')
} else {
$('.ys-product-filter__more').addClass('display-none')
}
}
const params = {
page: 1,
pageSize: 8,
keyword: ''
}
const result = {
total: 0,
list: [],
has_more: false,
type: 'product'
}
params.keyword = parsePath(window.location).query.keyword
if ($('.product-table-item').length) {
$(".c-pagination").zPager({
totalData: +$('.ys-search__result[data-target="product"]').find('.result-num span').text(),
pageStep: 5,
current: params.page,
pageData: params.pageSize
})
}
// 分页
function currentPage(currentPage) {
if (params.page === currentPage) return
params.page = currentPage
request()
}
function changePager(sizes) {
params.pageSize = sizes
params.page = 1
result.list = []
request()
}
// 搜索事件
$(".ys-search").on("click", ".ys-search__input i", function (e) {
let val = $(this).parent().find('input').val()
search(val)
});
$(".ys-search").on("keyup", ".ys-search__input input", function (e) {
let val = $(this).val()
if (e.keyCode === 13) {
search(val)
}
});
function search(query) {
if (!query) return $message.warning('请输入搜索关键词')
location.href = location.pathname + `?keyword=${query}`
// 1/11调整:执行搜索的时候,路由一起改变
// $('.ys-search__title').html('正在搜索“' + e + '”')
// params.keyword = e
// params.page = 1
// request()
}
function request() {
$loading.start()
$request
.post("/api/search/" + result.type, params)
.then((res) => {
if(res.data.redirect_url) {
window.location.href = res.data.redirect_url
return;
}
result.list = params.page === 1 || result.type === 'product' ? res.data.list : result.list.concat(res.data.list)
result.total = res.data.total
result.has_more = res.data.has_more
$('.ys-search__result .result-num span').html(res.data.total)
$('.ys-search__empty').remove()
let html = ''
$('.ys-search__result').each(function() {
if ($(this).attr('data-target') === result.type) {
$(this).removeClass('display-none')
}
})
if (result.type === 'content') {
$('.ys-search__result[data-target="product"]').addClass('display-none')
$('.ys-search__solution').css('display', 'none')
$('.ys-search__article').css('display', 'block')
result.list.forEach(content => {
html += `
${content.title}
${content.tags.length ? `
${content.tags
.map(
(s) => `${s}`
)
.join('')}
`:''}
${content.abstract}
${content.publish_time}
${content.clicks}
`
})
if(result.list.length) {
$('.ys-search__result[data-target="content"]').removeClass('display-none')
if (!$('.ys-search__result[data-target="content"]').find('.ys-search__slide').length) {
$('.ys-search__article').before('')
}
}
$('.ys-search__article').html(html)
} else if (result.type === 'solution') {
$('.ys-search__result[data-target="product"]').addClass('display-none')
$('.ys-search__article').css('display', 'none')
$('.ys-search__solution').css('display', 'flex')
result.list.forEach(sector => {
html += `
`
})
if(result.list.length) {
$('.ys-search__result[data-target="content"]').removeClass('display-none')
if (!$('.ys-search__result[data-target="content"]').find('.ys-search__slide').length) {
$('.ys-search__article').before('')
}
}
$('.ys-search__solution').html(html)
} else {
$('.ys-search__result[data-target="content"]').addClass('display-none')
result.list.forEach(e => {
html += `
${!Array.isArray(e.attribute) || !e.attribute.length
? `- 如需了解产品详情,请点击联系在线客服
客服电话:15016918160
`
: e.attribute.slice(0, 9).map(
(s) => `
- ${s.key_name || ''}: ${s.value_name || ''}
`
)
.join('')}
`
})
$('.ys-search__product').html(`
`+ html + `
`)
$('.ys-search__result button span').html(compare_skus.length + '/' + $('.ys-search__product .product-table-item').length)
if (!result.list.length) {
$('.ys-search__result[data-target="product"]').addClass('display-none')
} else {
$('.ys-search__result[data-target="product"]').removeClass('display-none')
$(".c-pagination").zPager({
totalData: result.total,
pageStep: 5,
current: params.page,
pageData: params.pageSize
})
}
setOptions(res.data.options)
}
if (!result.list.length) {
$('.ys-search__slide').remove()
$('.ys-search').append(`
抱歉,没有找到相关的内容
抱歉,没有找到相关的内容
`)
}
}).finally(() => {
$loading.end()
})
}
// 切换资讯/解决方案
$(".ys-search__result").on("click", ".result-type input", function (e) {
result.type = $(this).val()
params.page = 1
request()
});
// 滚动到底部
$(window).scroll(function () {
if (result.type === 'product') return
if ($(document).scrollTop() + 400 >= $(document).height() - $(window).height()) {
if (result.has_more && !$loading.status) {
params.page++
request()
}
}
})
// 点击表格行(选中/取消选中)
let compare_skus = []
$(".ys-search__result").on("click", ".product-table-item input", function (e) {
let checked = $(this).is(":checked")
if (checked) {
compare_skus.push($(this).attr('id'))
} else {
compare_skus.splice(compare_skus.findIndex(e => e === $(this).attr('id')), 1)
}
$('.ys-search__result button span').html(compare_skus.length + '/' + $('.product-table-item').length)
$('.ys-product__action .batch-action-num i').text(compare_skus.length)
if (compare_skus.length) {
$('.ys-search__result button').removeClass('disabled')
$('.ys-product__action').addClass('active')
} else {
$('.ys-search__result button').addClass('disabled')
$('.ys-product__action').removeClass('active')
}
});
// 开始对比按钮
$(".ys-search__result").on("click", ".ys-search__num button", function (e) {
if (!compare_skus.length) return
if (compare_skus.length < 2) return $message.info("请至少选择两种物料")
window.open('/compare.html?sku_code=' + compare_skus.join(','))
});
$(".ys-search__result").on("click", ".ys-search__num button div", function (e) {
compare_skus = []
$('.product-table-item').each(function() {
$(this).find('input').prop('checked', false)
})
$('.ys-search__result button span').html(compare_skus.length + '/' + $('.product-table-item').length)
$('.ys-search__result button').addClass('disabled')
$('.ys-product__action .batch-action-num i').text(compare_skus.length)
$('.ys-product__action').removeClass('active')
e.preventDefault();
});
// 保存至清单
$('.ys-search__product').on('click', '.save-sku', function (e) {
if (!$isLogin) {
$message.info('请先登录')
return
}
let sku_id = $(this).attr('data-id')
$request.post('/api/user/collect', { sku_id }).then((res) => {
$message.success('保存成功')
})
})
// 批量保存至清单
$('.ys-product__action .batch-action-save').on('click', function (e) {
if (!$isLogin) {
$message.info('请先登录')
return
}
$request
.post('/api/user/collect', {
sku_id: $('.product-table-item:has(input:checked) .save-sku')
.map(function () {
return $(this).data('id')
})
.toArray(),
})
.then((res) => {
$message.success('保存成功')
// 取消选择
$(".product-table-item input[type='checkbox']").prop('checked', false)
})
})
// empty 联系客服
$('.ys-search__product').on('click', '.product-table-service', function (e) {
window.dispatchEvent(
new CustomEvent('openService')
)
})
/* 埋点方法 start */
$(function() {
productViewListItem() // 搜索列表展示
productSelectItemClick() // 搜索列表点击
})
const ItemListId = 'search_products_list'; // ga view_item_list 列表id
const ItemListName = 'Search products list';// ga view_item_list 列表名称
function productViewListItem() {
const itemsInfoList = getProductListInfo({from_search: $('.ys-search__title > span').text()})
if(!Array.isArray(itemsInfoList) || !itemsInfoList.length) return
const ecommerceListObj = {
event: "view_item_list",
ecommerce: {
item_list_id: ItemListId,
item_list_name: ItemListName,
items: itemsInfoList,
}
}
customEventLog('ecommerce', ecommerceListObj, {onlyTrack: ["DataLayerTracker"]})
}
function getProductListInfo(otherInfo = {}) {
const itemInfoList = []
$('.ys-search__product .product-table-item').each(function (index, value) {
const itemEle = $(this)
const item = {
item_id: itemEle.find('.product-table-info > a').text().trim(),
item_name: itemEle.find('.product-table-info > p').text().trim(),
index: index + 1,
item_list_id: ItemListId,
item_list_name: ItemListName,
...otherInfo || {} // 其他自定义属性 主要为 {from_search: 搜索词} 分辨是否经过过滤
}
itemInfoList.push(item)
})
return itemInfoList
}
// 列表点击商品
function productSelectItemClick() {
$('.ys-search__product').on('click', '.product-table-button', productSelectItem)
$('.ys-search__product').on('click', '.product-table-info > a', productSelectItem)
}
function productSelectItem() {
const tableItemEle = $(this).parents('.product-table-item')
const ecommerceItemObj = {
event: "select_item",
ecommerce : [{
item_list_id: ItemListId,
item_list_name: ItemListName,
items: {
item_id: tableItemEle.find('.product-table-info > a').text().trim(),
item_name: tableItemEle.find('.product-table-info > p').text().trim(),
index: tableItemEle.index() + 1,
item_list_id: ItemListId,
item_list_name: ItemListName,
from_search: $('.ys-search__title > span').text()
}
}]
}
customEventLog('ecommerce', ecommerceItemObj, {onlyTrack: ["DataLayerTracker"]})
}
/* 埋点方法 end */
// 处于页面配置时,绑定方法
if ($isDecorateMode) {
document.addEventListener('diy_footer/1', (event) => {
const { field, value } = event.detail
console.log(field, value)
if (field === 'footer_ad') {
const nodes = $(
value
.map((item) => `
`)
.join('')
)
$('.ys-footer__ad').empty()
$('.ys-footer__ad').append(nodes)
} else if (field === 'contact_phone') {
$('.footer_phone').text(value)
} else if (field === 'work_time') {
$('.footer_work_time').text(value)
} else if (field === 'contact_email') {
$('.footer_email').text(value)
} else if (field === 'footer_nav') {
const nodes = $(
value
.map(
(c) => `
`
)
.join('')
)
$('.ys-footer__content .center-part').empty()
$('.ys-footer__content .center-part').append(nodes)
} else if (field === 'right_part') {
const nodes = $(
value
.map((item) => `
`)
.join('')
)
$('.ys-footer__content .right-part').empty()
$('.ys-footer__content .right-part').append(nodes)
} else if (field === 'friendly_links') {
const nodes = $(
value
.map(
(e) => `
${e.title}
`
)
.join('')
)
$('.ys-footer__friendly ul').empty()
$('.ys-footer__friendly ul').append(nodes)
} else if (field === 'copyright') {
$('.copyright-wrap span').text(value)
} else if (field === 'beian') {
$('.copyright-wrap a').text(value)
} else if (field === 'footer_logo') {
$('.footer-logo').attr('alt', value.alt)
$('.footer-logo').attr('src', value.src)
} else if (field === 'map_links') {
const nodes = $(
value
.map(
(e) => `
${e.title}
`
)
.join('')
)
$('.ys-footer__end .end-link').empty()
$('.ys-footer__end .end-link').append(nodes)
}
})
}
window.addEventListener('openService', openService) // 监听全局客服点击事件
$('#backTop').click(function () {
$('html, body').animate({ scrollTop: 0 }, 300)
})
$('.ys-rightNav .service').click(function() {
openService()
})
// 打开客服弹窗
function openService() {
if ($('#aff-im-root .embed-icon-pcIcon5').length > 0) {
$('#aff-im-root .embed-icon-pcIcon5').click()
} else {
$('#newBridge .nb-icon-wrap').click()
}
customEventLog("open_server"); // 打点
customEventLog("set", { key:'purchase', value:'service'}, {onlyTrack: ["ClarityTracker"]}); // clarity标签
}
// 监听页面滚动
$(window).scroll(function () {
$('.ys-rightNav #backTop').css('display', $(document).scrollTop() ? 'block' : 'none')
})