0){
new Swiper(".product_swiper .swiper", {
slidesPerView: "auto",
spaceBetween: 15,
navigation: {
nextEl: '.product_swiper .next',
prevEl: '.product_swiper .prev',
}
});
}
let positionHeight = $(".main_box .position").outerHeight();
if(positionHeight){
$("html,body").animate({scrollTop: positionHeight}, 200);
}
},
load: function () {
this.buyForm = this.productRight.find("[name='COR_FORM_BUY']");
this.productSpec = this.productRight.find("#COR_SPEC");
this.productAttr = this.productRight.find("#COR_ATTR");
this.loadPrice();
this.loadInfo();
this.loadSlide();
this.loadForm();
this.loadSpec();
this.loadAttr();
this.loadMore();
this.loadContent();
},
loadPrice: function () {
let that = this;
let attr_ids = [];
this.buyForm.find("input[name^='attr_ids']").each(function () {
if ($(this).is(":checked")) {
attr_ids.push($(this).val());
}
});
const post = {};
post.url = '/products/get_price';
post.data = {};
post.data.id = that.productId;
post.data.number = this.buyForm.find(".COR_NUMBER .number").val();
post.data.attr_ids = attr_ids;
post.success = function (res) {
that.buyForm.find(".COR_PRICE").html(res.content.product_price_format);
that.buyForm.find(".COR_AMOUNT").html(res.content.price_amount_format);
let activity_html = '';
if(res.content.hasOwnProperty('activity_html')){
activity_html = res.content['activity_html'];
}
$("#COR_ACTIVITY").html(activity_html);
};
Cor.ajax(post);
return false;
},
loadInfo: function () {
let that = this;
const post = {};
post.url = '/products/info';
post.data = {};
post.data.id = that.productId;
post.success = function (res) {
let info = res.content;
if(info.online != 0){
that.productBox.find(".product-order .order-cart").addClass("none");
that.productBox.find(".product-order .order-buy").addClass("none");
that.productBox.find(".product-order .offline").removeClass("none");
if(info.online == 2){
that.productBox.find(".product-order .offline").html("已下架");
}else{
that.productBox.find(".product-order .offline").html("售磬");
}
}
that.buyForm.find(".COR_STOCK .stockTip").attr("title", info.stock_tip_info.tips);
that.buyForm.find(".COR_STOCK .stockTip").html(info.stock_tip_info.name);
that.buyForm.find(".COR_STOCK .stockTip").addClass(`status_${info.stock_tip_info.id}`);
if (info.promote_price_format != '' && info.promote_end_date) {
that.buyForm.find(".COR_PROMOTE").removeClass("none");
that.buyForm.find(".COR_PROMOTE .endTime").attr("endtime", info.promote_end_date);
that.buyForm.find(".COR_PROMOTE .endTime").endTime({
nowTime: '1754838765',
d: '{d}天',
h: '{h}時',
m: '{m}分',
s: '{s}秒',
endTip: function () {
window.location.reload();
}
});
}
};
Cor.ajax(post);
return false;
},
loadSlide: function () {
let that = this;
if (that.productLeft.find(".swiper").length > 0) {
that.productSlide = that.productLeft.find(".swiper-wrapper .swiper-slide");
that.productImg = that.productLeft.find(".product-image img");
if (that.productSlide.length == 1) {
that.productLeft.find(".swiper").addClass("none");
} else {
that.productSwiper = new Swiper(".product-left .swiper", {
slidesPerView: "auto",
spaceBetween: 20
});
that.productSlide.hover(function () {
if (!$(this).hasClass("active")) {
that.productSlide.removeClass("active");
$(this).addClass("active");
let thisImg = $(this).find("img").data("rel");
that.productImg.attr("src", thisImg);
}
});
}
}
},
loadForm: function () {
let that = this;
Cor.buyNumber(this.buyForm.find(".COR_NUMBER"), {
fun: function (box, num) {
that.loadPrice();
}
});
},
loadSpec: function () {
let that = this;
if (that.productSpec.length > 0) {
that.disabledSpec();
that.productSpec.find('a').on('click', function(){
if($(this).hasClass('active')){
return false;
}
if($(this).hasClass('invalid')){
that.productSpec.find('a').removeClass('active invalid');
$(this).addClass('active');
that.disabledSpec();
return false;
}
$(this).parent().find('a').removeClass('active');
$(this).addClass('active');
let sku_id = that.getSku();
if(that.skuList[sku_id]){
let row = that.skuList[sku_id];
that.productId = row.id;
that.pageReload(row.url);
}else{
that.disabledSpec();
}
});
}
},
loadAttr: function () {
let that = this;
if (that.productAttr.length > 0) {
that.productAttr.find(".item").on('click', function () {
if($(this).hasClass("active")){
$(this).find("input").prop("checked", false);
}else{
$(this).find("input").prop("checked", true);
}
$(this).toggleClass("active");
that.loadPrice();
});
}
},
loadMore: function () {
let that = this;
that.productMore = that.productRight.find(".additional-product");
if (that.productMore.length > 0) {
Cor.buyNumber(that.productMore.find(".numberInput"), {
fun: function (box, num) {
let item = box.parents(".control");
let morePrice = item.find(".morePrice").data("val");
let originalPrice = item.find(".originalPrice").data("val");
item.find(".morePrice").html(Cor.priceFormat(num * parseFloat(morePrice)));
if (originalPrice) {
item.find(".originalPrice").html(Cor.priceFormat(num * parseFloat(originalPrice)));
}
}
});
that.productRight.on("click", ".additional-product .more .btn", function () {
let box = $(this).parents(".additionalProducts");
if (box.hasClass("moreAdditional")) {
$(this).html("更多");
box.removeClass("moreAdditional");
} else {
$(this).html("收起");
box.addClass("moreAdditional");
}
});
}
},
loadContent: function () {
let that = this;
this.productContent.on("click", ".content-menu button", function () {
if (!$(this).hasClass("active")) {
let index = $(this).index();
that.productContent.find(".content-menu button").removeClass("active");
$(this).addClass("active");
that.productContent.find(".content-main .html_content").addClass("none");
that.productContent.find(".content-main .html_content").eq(index).removeClass("none");
}
});
},
isSku: function (spec) {
let that = this;
let is = false;
$.each(that.skuList, function(key, val){
let i = true;
$.each(spec, function(k, v){
if($.inArray(v, key.split('|')) == -1){
i = false;
}
});
if(i){
is = true;
}
if(is){
return false;
}
});
return is;
},
getSku: function () {
let that = this;
let sku = [];
this.productSpec.find('a.active').each(function(){
sku.push($(this).attr('data-id'));
});
return sku.join('|');
},
disabledSpec: function () {
let that = this;
let select_spec = {};
let select_spec_id = [];
let select_box = that.productSpec.find('.spec_item');
select_box.each(function(){
if($(this).find(".item.active").length > 0){
select_spec[$(this).attr('data-id')] = $(this).find("a.active").attr('data-id');
select_spec_id.push($(this).attr('data-id'));
}
});
if(select_spec_id.length == 0){
select_box.find('a').removeClass('invalid');
}else{
select_box.each(function(){
if(select_box.length > 1 && select_spec_id.length == 1 && $(this).attr('data-id') == select_spec_id[0]){
return true;
}else{
let temp = $.extend({}, select_spec);
let spec_id = $(this).attr('data-id');
$(this).find('a').each(function(){
temp[spec_id] = $(this).attr('data-id');
if(that.isSku(temp)){
$(this).removeClass('invalid');
}else{
$(this).addClass('invalid');
}
});
}
});
}
},
pageReload: function (url) {
let that = this;
history.pushState({}, '', url);
const get = {};
get.loading = true;
get.url = url;
get.method = 'GET';
get.dataType = 'html';
get.success = function (res) {
let tempElement = document.createElement("div");
tempElement.innerHTML = res;
that.productLeft.html(tempElement.querySelector("#COR_LEFT").innerHTML);
that.productRight.html(tempElement.querySelector("#COR_RIGHT").innerHTML);
that.productContent.html(tempElement.querySelector("#COR_CONTENT").innerHTML);
that.load();
};
Cor.ajax(get)
return false;
}
};
$(function () {
ProductClass.init();
});