console.dir('load') window.bexSource = 49; if(typeof window.$ === 'undefined') window.$ = window.jQuery; window.BEXCatchProduct = () => { let products = []; let urlExp = /[http(s):]?\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/; //判断是否有Colorme if( typeof(Colorme) === "object" && Colorme.hasOwnProperty('product') && typeof(Colorme.product) === "object" && Colorme.product.hasOwnProperty('id') && Colorme.product.hasOwnProperty('name') && Colorme.product.hasOwnProperty('sales_price') ){ let item_image = $('#product .product_image .main_image__wrap img.mainImage').attr('src'); let productDetail = { item_code: Colorme.product.id, item_num: parseInt($('#product').find('input.product_init_num').val() ?? 1), item_attribute: "", item_price: parseInt(Colorme.product.sales_price.toString().replace(/[^(0-9)(\.)]/ig, "")), item_name: Colorme.product.name, item_img: urlExp.test(item_image) ? ((item_image.indexOf('http:') === 0 || item_image.indexOf('https:') === 0 ) ? item_image : 'https:'+item_image) : window.location.protocol + '//' + window.location.host + item_image, // item_url: urlExp.test(item_url) ? item_url : window.location.protocol + '//' + window.location.host + item_url, item_url: location.href, } if(productDetail.item_price && productDetail.item_name) products.push(productDetail); }else{ $(window.bexSourceInfo.item_list_selector).each(function (index, item) { //获取属性 let attribute = []; $(item).find(window.bexSourceInfo.item_attr_list_selector).each(function(k,v){ attribute.push( $.trim($(v).find(window.bexSourceInfo.item_attr_name_selector).text()) +":"+ $.trim($(v).find(window.bexSourceInfo.item_attr_option_selector).text()) ) }) let item_image = $(item).find(window.bexSourceInfo.item_img_selector).attr('src'); let productDetail = { item_code: index + 1, // item_num: parseInt($(item).find(window.bexSourceInfo.item_num_selector).val()), item_num: parseInt($(item).find(window.bexSourceInfo.item_num_selector).val() ?? 1), // item_attribute: $.trim($(item).find(window.bexSourceInfo.item_attr_selector).text()), item_attribute: attribute.join(';'), item_price: parseInt($(item).find(window.bexSourceInfo.item_price_selector).text().replace(/[^(0-9)(\.)]/ig, "")), item_name: $.trim($(item).find(window.bexSourceInfo.item_name_selector).text()), item_img: urlExp.test(item_image) ? ((item_image.indexOf('http:') === 0 || item_image.indexOf('https:') === 0 ) ? item_image : 'https:'+item_image) : window.location.protocol + '//' + window.location.host + item_image, // item_url: urlExp.test(item_url) ? item_url : window.location.protocol + '//' + window.location.host + item_url, item_url: location.href, } if(productDetail.item_price && productDetail.item_name) products.push(productDetail); console.dir(productDetail); }); } return products; } window.bexSourceInfo = {}; var bexProductsData = JSON.parse(localStorage.getItem('bexProductsData') ?? '[]'); for(let i in bexProductsData){ if(bexProductsData[i].expire < Date.now()) bexProductsData.slice(i,1); } localStorage.setItem('bexProductsData',JSON.stringify(bexProductsData)); document.addEventListener('alpine:init', () => { console.dir('alpine:init') Alpine.data('bexInit', () => ({ //语言包部分 bexLanguageBoxOpen:false, setLanguage:function(lang,langText){ localStorage.setItem('bexLanguage',lang); localStorage.setItem('bexLanguageText',langText); this.bexLanguageBoxOpen = false; Alpine.store('bexLanguage', lang); Alpine.store('bexLanguageText', langText); }, //购物车部分 bexProducts:bexProductsData, deleteProduct(index){ this.bexProducts.splice(index,1) localStorage.setItem('bexProductsData',JSON.stringify(this.bexProducts)); }, addProduct(){ let products = window.BEXCatchProduct(); if(products.length > 0){ Alpine.store('bexConfirmShow', true); Alpine.store('cartMiniShow', false); Alpine.store('cartDetailShow', false); Alpine.store('cartToolShow', true); for(let j in this.bexProducts){ for(let i in products){ if( products[i].item_url === this.bexProducts[j].item_url && products[i].item_name === this.bexProducts[j].item_name && products[i].item_price === this.bexProducts[j].item_price && products[i].item_attribute === this.bexProducts[j].item_attribute ){ this.bexProducts[j].item_num = parseInt(this.bexProducts[j].item_num+products[i].item_num); this.bexProducts[j].item_img = products[i].item_img; products.splice(i,1) } } } for(let i in products){ products[i].expire = Date.now()+(2*60*60*1000) this.bexProducts.push(products[i]) } localStorage.setItem('bexProductsData',JSON.stringify(this.bexProducts)); }else{ console.dir('ERROR') } }, submitCart(){ let _this = this; $.ajax({ url:'https://benlyexpress.com/shopextend/index/cartDataSave', method:"POST", data:{cartData:this.bexProducts,lang:localStorage.getItem('bexLanguage') ?? 'en'}, dataType: 'json', success:function(rs){ if(rs.code==0) { //清除数据 _this.bexProducts = []; localStorage.removeItem('bexProductsData'); Alpine.store('cartMiniShow', false); Alpine.store('cartDetailShow', false); Alpine.store('cartToolShow', true); window.open(rs.redirect_url+'&source='+window.bexSource) }else{ console.dir(rs.msg); } }, error:function(rs){ console.dir("Network Error."); } }) }, editNum(index,type){ if(this.bexProducts[index]){ if(type == 1 && this.bexProducts[index].item_num === 1) this.bexProducts.splice(index,1) else{ let item_num_ = this.bexProducts[index].item_num; item_num_ = type === 1 ? item_num_-1 : item_num_+1; this.bexProducts[index].item_num = item_num_; } } localStorage.setItem('bexProductsData',JSON.stringify(this.bexProducts)); }, })) let bexAlertCloseExpire = localStorage.getItem('bexAlertCloseExpire'); if(bexAlertCloseExpire && bexAlertCloseExpire > Date.now()){ Alpine.store('bexAlertShow', false); }else{ Alpine.store('bexAlertShow', true); } Alpine.store('bexConfirmShow', false); Alpine.store('cartMiniShow', false); Alpine.store('cartDetailShow', false); Alpine.store('cartToolShow', true); Alpine.store('bexSourceLogo',false); Alpine.store('bexLanguage',localStorage.getItem('bexLanguage') ?? 'en'); Alpine.store('bexLanguageText',localStorage.getItem('bexLanguageText') ?? 'English'); }) window.bexShowCartDetail = function(){ Alpine.store('bexConfirmShow', false); Alpine.store('cartMiniShow', false); Alpine.store('cartDetailShow', true); Alpine.store('cartToolShow', false); } $(function(){ $.ajax({ url:'https://benlyexpress.com/shoppulgin/index/data', data:{lang:"en",source:window.bexSource}, dataType: 'json', success:function(res){ if(res.hasOwnProperty('enable') && res.enable === true){ window.bexSourceInfo = res.source_info; Alpine.store('bexSourceLogo',res.source_info.logo ?? false); if(typeof window.BEXMatchProductPage === 'function'){ if(window.BEXMatchProductPage()){ $('body').append(res.confirm_html); $('body').append(res.cart_html); }else{ $('body').append(res.alert_html); } }else{ if(window.location.href.indexOf(res.source_info.cart_page_url) !== -1){ $('body').append(res.confirm_html); $('body').append(res.cart_html); }else{ $('body').append(res.alert_html); } } } } }) });