/** * 引入layer弹窗 * @type {Boolean} */ var ey_layer_1609663499 = false; if (window.jQuery && !window.layer) { ey_layer_1609663499 = true; } if (ey_layer_1609663499) { document.write(unescape("%3Cscript src='"+fe912b5dac71082e12c1827a3107f9b.root_dir+"/public/plugins/layer-v3.1.0/layer.js' type='text/javascript'%3E%3C/script%3E")); } // 加入购物车 function shop_add_cart() { var JsonData = goodsInfo; var QuantityObj = document.getElementById("quantity"); var SelectValueIds = document.getElementById(JsonData.SelectValueIds); var goods_id = JsonData.goods_id; var num = QuantityObj.value; var url = JsonData.shop_add_cart_url; var ajaxdata = 'goods_id='+goods_id+'&goods_num='+num+'&goods_sku_id='+SelectValueIds.value; // 库存数量 var StockCountObj = document.getElementById('stock_num').value; if (parseInt(StockCountObj) == 0) { if (!window.layer) { alert('商品已售罄!'); } else { layer.alert('商品已售罄!', {icon: 5, title: false, closeBtn: false}); } return false; } else if (parseInt(StockCountObj) < parseInt(num)) { if (!window.layer) { alert('商品库存不足!'); } else { layer.alert('商品库存不足!', {icon: 5, title: false, closeBtn: false}); } return false; } $.ajax({ url:BASE_URL + "cart/add", data:ajaxdata, success:function(res){ if(res.code == 1){ layer.msg(res.msg,{icon:1},()=>{ window.location.href = BASE_URL + "cart" }) }else if(res.code == -1){ // 去登陆 is_login(); }else{ layer.msg(res.msg,{icon:2}) } } }) return; //创建异步对象 var ajaxObj = new XMLHttpRequest(); ajaxObj.open("post", url, true); ajaxObj.setRequestHeader("X-Requested-With","XMLHttpRequest"); ajaxObj.setRequestHeader("Content-type","application/x-www-form-urlencoded"); //发送请求 ajaxObj.send(ajaxdata); ajaxObj.onreadystatechange = function () { // 这步为判断服务器是否正确响应 if (ajaxObj.readyState == 4 && ajaxObj.status == 200) { var json = ajaxObj.responseText; var res = JSON.parse(json); if ('1' == res.code) { // 是否要去购物车 shop_cart_list(JsonData.shop_cart_list_url); }else{ if (-1 == res.data.code) { if (!window.layer) { confirm(res.msg); } else { layer.alert(res.msg, {icon: 5, title: false, closeBtn: false}); } }else{ // 去登陆 is_login(JsonData.login_url); } } } }; } /** * 获取url参数值的方法 * @param {[type]} name [description] * @return {[type]} [description] */ function getUrlParam_1607507428(name) { var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if (r!=null) return unescape(r[2]); return null; } // 表单提交 function submitForm_1607507428(formname) { $("form[name="+formname+"]").submit(); } // 立即购买 function BuyNow(goods_id){ var SelectValueIds = document.getElementById("SelectValueIds"); // 数量 var QuantityObj = document.getElementById("quantity"); // 库存数量 var StockNum = document.getElementById('stock_num').value; var num = QuantityObj.value; var spec_value_id = SelectValueIds.value; if (parseInt(StockNum) == 0) { if (!window.layer) { alert('商品已售罄!'); } else { layer.alert('商品已售罄!', {icon: 5, title: false, closeBtn: false}); } return false; } else if (parseInt(StockNum) < parseInt(num)) { if (!window.layer) { alert('商品库存不足!'); } else { layer.alert('商品库存不足!', {icon: 5, title: false, closeBtn: false}); } return false; } window.location.href = BASE_URL + `checkout?goods_id=${goods_id}&goods_sku_id=${spec_value_id}&goods_num=${num}` //创建异步对象 var ajaxObj = new XMLHttpRequest(); ajaxObj.open("post", url, true); ajaxObj.setRequestHeader("X-Requested-With","XMLHttpRequest"); ajaxObj.setRequestHeader("Content-type","application/x-www-form-urlencoded"); //发送请求 ajaxObj.send(ajaxdata); ajaxObj.onreadystatechange = function () { // 这步为判断服务器是否正确响应 if (ajaxObj.readyState == 4 && ajaxObj.status == 200) { var json = ajaxObj.responseText; var res = JSON.parse(json); if ('1' == res.code) { // 去购买 window.location.href = res.url; }else{ if (-1 == res.data.code) { if (!window.layer) { confirm(res.msg); } else { layer.alert(res.msg, {icon: 5, title: false, closeBtn: false}); } }else{ // 去登陆 is_login(JsonData.login_url); } } } }; } // 数量加减处理 function CartUnifiedAlgorithm(symbol){ // 数量 var QuantityObj = document.getElementById("quantity"); // 库存数量 var StockCountObj = document.getElementById('stock_num'); // 默认数量 var quantity = ''; if ('change' == symbol) { // 直接修改数量 if ('1' > QuantityObj.value || '' == QuantityObj.value) { quantity = 1; // if (!window.layer) { // alert('商品数量最少为1'); // } else { // layer.alert('商品数量最少为1', {icon: 5, title: false, closeBtn: false}); // } }else{ if (Number(QuantityObj.value) > Number(StockCountObj.value)) { quantity = Number(StockCountObj.value); }else{ quantity = Number(QuantityObj.value); } } }else if ('+' == symbol) { // 加数量 quantity = Number(QuantityObj.value) + 1; }else if ('-' == symbol && QuantityObj.value > '1') { // 减数量 quantity = Number(QuantityObj.value) - 1; }else{ quantity = 1; // 如果数量小于1则自动填充1 // if (!window.layer) { // alert('商品数量最少为1'); // } else { // layer.alert('商品数量最少为1', {icon: 5, title: false, closeBtn: false}); // } } // 数量是否大于库存量 if (StockCountObj.value < quantity) { if (!window.layer) { alert('这件产品库存仅为:'+StockCountObj.value); } else { layer.alert('这件产品库存仅为:'+StockCountObj.value, {icon: 5, title: false, closeBtn: false}); } return false; } // 加载数量 QuantityObj.value = quantity; // 计算总价 if (1 <= quantity && document.getElementById('totol_price')) { if (document.getElementById('users_price')) { var users_price = document.getElementById('users_price').innerText; } else if (document.getElementById('sell_price')) { var users_price = document.getElementById('sell_price').innerText; } else if (document.getElementById('spec_price')) { var users_price = document.getElementById('spec_price').innerText; } users_price = Number(users_price) * Number(quantity); document.getElementById('totol_price').innerText = parseFloat(users_price.toFixed(2)); } } // 去购车去 function shop_cart_list(url) { window.location.href = url; /* if (!window.layer) { var mymessage = confirm('加入购物车成功,前往购物车!'); if (mymessage == true) window.location.href = url; } else { var confirms = layer.confirm('已加入购物车成功!', { title: false, btn: ['前往购物车'] }, function (index) { layer.close(confirms); window.location.href = url; }); } */ } // 去登陆 function is_login(url){ if (document.getElementById('ey_login_id_1609665117')) { $('#ey_login_id_1609665117').trigger('click'); } else { if (!window.layer) { var mymessage = confirm('您还没未登录,请登录后购买!'); if(mymessage == true){ window.location.href = url; } } else { layer.alert('您还没未登录,请登录后购买!', {icon: 5, title: false}, function(){ window.location.href = url; }); } } } function sortNumber(a, b) { return a - b } function SpecSelect(spec_mark_id, spec_value_id, discount_price) { // 清除同一类下的所有选中参数class var ClassArray = document.getElementsByClassName("spec_mark_"+spec_mark_id); for (var i = 0; i < ClassArray.length; i++) { ClassArray[i].classList.remove('selected'); } // 当前点击的添加上class document.getElementsByClassName('spec_value_'+spec_value_id)[0].classList.add('selected'); /*规格值ID处理*/ // 获取所有选中的规格值ID var SelectValueIds = ''; var danger = document.getElementsByClassName('selected'); for (var i = 0; i < danger.length; i++) { if (danger[i].dataset.spec_value_id) { SelectValueIds += danger[i].dataset.spec_value_id; SelectValueIds += '_'; } } // 去除最后一个字符 SelectValueIds = SelectValueIds.substring(0, SelectValueIds.length-1); // 字符串转数组 SelectValueIds = SelectValueIds.split('_'); // 从小到大排序 SelectValueIds = SelectValueIds.sort(sortNumber); // 数组转字符串 SelectValueIds = SelectValueIds.join('_'); /* END */ // 解析json数据 //var SpecData = JSON.parse(JsonData.SpecData); console.log(SpecData) // 更新价格及库存 var selected = SpecData.filter(item=>item['spec_sku_id'] == SelectValueIds) console.log(selected) $("#SelectValueIds").val( selected[0].spec_sku_id) $("#stock_num").val( selected[0].form.stock_num) $("#spec_price").html(selected[0].form.goods_price) return for(var i = 0; i < SpecData.length; i++){ if (SelectValueIds == SpecData[i]['spec_sku_id']) { document.getElementById('SelectValueIds').value = SelectValueIds; if (discount_price) { if (document.getElementById('old_price')) { var old_price = Number(SpecData[i]['spec_price']); document.getElementById('old_price').innerText = parseFloat(old_price.toFixed(2)); } var users_price = Number(SpecData[i]['spec_price']) * discount_price; if (document.getElementById('users_price')) { document.getElementById('users_price').innerText = parseFloat(users_price.toFixed(2)); } if (document.getElementById('totol_price')) { document.getElementById('totol_price').innerText = parseFloat(users_price.toFixed(2)); } }else{ var users_price = Number(SpecData[i]['spec_price']); if (document.getElementById('users_price')) { document.getElementById('users_price').innerText = parseFloat(users_price.toFixed(2)); } if (document.getElementById('totol_price')) { document.getElementById('totol_price').innerText = parseFloat(users_price.toFixed(2)); } } if (document.getElementById('stock_count')) { document.getElementById('stock_count').innerText = SpecData[i]['spec_stock']; } if (document.getElementById('sales_num')) { document.getElementById('sales_num').innerText = SpecData[i]['spec_sales_num']; } if (document.getElementById('ey_stock_1565602291')) { document.getElementById('ey_stock_1565602291').value = SpecData[i]['spec_stock']; } // 更新价格和库存后,购买数量重置为 1 document.getElementById(fe912b5dac71082e12c1827a3107f9b.quantity).value = 1; } } }