console.dir('load') window.bexSource = 'SO000072'; if(typeof window.$ === 'undefined') window.$ = window.jQuery; window.BEXCatchProduct = () => { let products = []; let urlExp = /[http(s):]?\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/; $(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 item_url = $(item).find(window.bexSourceInfo.item_url_selector).attr('href'); 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)(\.)]/gi, ''), ), 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, item_brand: $.trim($(item).find(window.bexSourceInfo.item_brand_selector).text()), item_id: $.trim($(item).find(window.bexSourceInfo.item_id_selector).text()), }; 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); } var userLang = navigator.language || navigator.userLanguage; var userLangText = '日本語'; if (userLang == 'en-US' || userLang == 'en') { userLang = 'en'; userLangText = 'English'; } else if (userLang == 'zh-CN') { userLang = 'zh-CN'; userLangText = '简体'; } else if (userLang == 'zh-TW') { userLang = 'zh-TW'; userLangText = '繁體'; } else { userLang = 'ja'; userLangText = '日本語'; } localStorage.setItem('bexLanguage', userLang); localStorage.setItem('bexLanguageText', userLangText); 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; this.bexProducts[j].item_brand = products[i].item_brand; this.bexProducts[j].item_id = products[i].item_id; 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; const popup = window.open('', '_blank'); if (!popup) { alert('ポップアップがブロックされました。新しいウィンドウの表示を許可してください。'); return; } $.ajax({ url: 'https://api.bex.market/store/orders/intl/cart/checkout', type: 'POST', method: 'POST', data: { cartData: this.bexProducts, lang: localStorage.getItem('bexLanguage') ?? userLang }, dataType: 'json', success: function (rs) { if (rs.code == 200) { //清除数据 _this.bexProducts = []; localStorage.removeItem('bexProductsData'); Alpine.store('cartMiniShow', false); Alpine.store('cartDetailShow', false); Alpine.store('cartToolShow', true); let finalUrl = rs.redirect_url+'&source='+window.bexSource; popup.location.href = finalUrl; } else { console.dir(rs.msg); popup.close(); } }, error: function (rs) { console.dir('Network Error.'); popup.close(); }, }); }, 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') ?? userLang); Alpine.store('bexLanguageText', localStorage.getItem('bexLanguageText') ?? userLangText); }); window.bexShowCartDetail = function () { Alpine.store('bexConfirmShow', false); Alpine.store('cartMiniShow', false); Alpine.store('cartDetailShow', true); Alpine.store('cartToolShow', false); }; $(function () { $.ajax({ url: 'https://api.bex.market/store/product/intl-shopping/index/data', data: { lang: userLang, 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); } } } }, }); });