(function () { rdcp.request("!Manage/User/~java/User.getIs_UserHttp", {}, function (data) { console.log(data) $('#id_cart_num').text(data.body.count) }, {mask: true} ); //获取URL看后面的参数值 function requestParam(paramName) { var sSource = String(window.document.location); var sName = paramName; var sReturn = ""; var sQUS = "?"; var sAMP = "&"; var sEQ = "="; var iPos; iPos = sSource.indexOf(sQUS); if (iPos == -1) return; var strQuery = sSource.substr(iPos, sSource.length - iPos); var strLCQuery = strQuery.toLowerCase(); var strLCName = sName.toLowerCase(); iPos = strLCQuery.indexOf(sQUS + strLCName + sEQ); if (iPos == -1) { iPos = strLCQuery.indexOf(sAMP + strLCName + sEQ); } // 判断是否存在参数 if (iPos != -1) { sReturn = strQuery.substr(iPos + sName.length + 2, strQuery.length - (iPos + sName.length + 2)); var iPosAMP = sReturn.indexOf(sAMP); if (iPosAMP == -1) { return sReturn; } else { sReturn = sReturn.substr(0, iPosAMP); } } return sReturn; } // $('.city-picker-style').on("click", 'div[data-count="city"] a,div[data-count="district"] a', function () { var provinceCode = $('.city-picker-style span[data-count="province"]').attr("data-code"); var cityCode = $('.city-picker-style span[data-count="city"]').attr("data-code"); var areaCode = $('.city-picker-style span[data-count="district"]').attr("data-code"); var provinceName = $('.city-picker-style span[data-count="province"]').text(); var cityName = $('.city-picker-style span[data-count="city"]').text(); var areaName = $('.city-picker-style span[data-count="district"]').text(); freight(provinceCode, cityCode, areaCode, provinceName, cityName, areaName); }); //显示运费 function freight(provinceCode, cityCode, areaCode, provinceName, cityName, areaName) { var expressRule = $("#expressRule").val(); if (areaCode == undefined) { areaCode = ""; } if (expressRule == "") { $("#expressPrice").text("免运费"); } if (provinceName) { rdcp.request('!Manage/Express/~java/ExpressRuleAction.freight', { "goodsId": requestParam("id"), "province": provinceName, // "city": cityCode,//市ID // "area": areaCode,//区ID // "province_name": provinceName,//省名称 // "city_name": cityName,//市名称 // "area_name": areaName//区名称 }, function (data) { if (data.header.code == 0) { if (data.body != "") { // if (data.body.freight == 0) { $("#expressPrice").text(data.body.free_msg || ''); // } } } else { alertMsg('提示', data.header.message + "(" + data.header.code + ")"); } }, {mask: false}); } } var provinceCode = $("#city-picker").attr("attrProvince"); var cityCode = $("#city-picker").attr("attrCity"); var areaCode = $("#city-picker").attr("attrArea"); var provinceName = $("#city-picker").attr("attrProvinceName"); var cityName = $("#city-picker").attr("attrCityName"); var areaName = $("#city-picker").attr("attrAreaName"); freight(provinceCode, cityCode, areaCode, provinceName, cityName, areaName); var clicked = false; //选择商品属性 $('.details-msg-package span').click(function () { if (clicked) { return; } // clicked = !clicked; var siblings = $(this).siblings() var id = $(this).attr('data-id'); var path = requestParam('path'); var path_arr = path.split(','); var newPath = ''; // 如果本来就存在path,则进行一下处理 if (path) { var path_set = new Set(); // 将path的字符串分割成数组后放到set里面去重一下 path_arr.forEach(function (value) { if (value != '') { path_set.add(value) } }); // 先将当前规格的所有规格值清除(除了当前) $.each(siblings, function (value) { var attr1 = $(this).attr('data-id'); path_set.delete(attr1) }); // 如果选择的规格本已存在,则把该规格值id去除,否则将添加到set里面 if (path_set.has(id)) { path_set.delete(id) } else { path_set.add(id) } // 组装完后必须要排序一下,这里非常重要不要乱改,因为后台保存的是有顺序的,顺序乱了就会查询不了 path_set = Array.from(path_set).sort(); // 最后遍历set组合,拼装path字符串 path_set.forEach(function (value, idx) { newPath += value if (idx != path_set.length - 1) { newPath += ',' } }); } else { newPath += id } console.log(newPath) var goods_id = requestParam('id'); rdcp.go("!Front/Goods/~view/details?id=" + goods_id + "&path=" + newPath); }); //立即购买 $("#buyNow").click(function () { if (($("#goodsTotalStock").text() - 0) == 0) { alertMsg('提示', "商品库存为0,无法购买"); return; } var description = ""; var norms = $("#detailsMsg li[norms]").length; for (var i = 0; i < norms; i++) { var li = $("#detailsMsg li[norms]").eq(i); var name = li.children(".norms-name").text(); var attr = li.children(".com-button-selected").text(); name = name.split(':'); description += name[0] + " : " + attr + ';'; if (attr == "") { alertMsg('提示', "请选择商品" + name[0]); return; } } var skuId = $("#sku-id").val(); if (!skuId) { alertMsg('提示', "请选择正确的规格"); return; } var goods_id = requestParam('id') rdcp.request('!Manage/Goods/~java/CartManage.add', { "goods_id": goods_id, "goods_sku_id": skuId,//商品价格ID "quantity": $("#purchaseQuantity").val(),//数量 "description": description//商品规格(颜色:红色;内存:16G) }, function (data) { var id = data.body if (data.header.code == 0) { rdcp.go("!Front/Cart/~view/checkout" + "?type=view&id=" + id); // code = 2表示商品已存在用户购物车,但如果再添加一个就会超出库存,所以直接跳转页面就行, } else if (data.header.code == "2007") { // rdcp.go("!Front/Cart/~view/checkout" + "?type=view&id=" + id); alertMsg('提示', data.header.message); } else if (data.header.code == "2005") { rdcp.go('!Front/User/~view/login?goodsDetail=' + goods_id); } else if (data.header.code == "-5") { $("#alertLoginBox").css("display", "block"); } else { alertMsg('提示', data.header.message + "(" + data.header.code + ")"); } }, {mask: false}) }); //添加购物车 $("#addGoodCard").click(function () { if (($("#goodsTotalStock").text() - 0) == 0) { alertMsg('提示', "商品库存为0,不可添加到购物车"); return; } var description = ""; var norms = $("#detailsMsg li[norms]").length; for (var i = 0; i < norms; i++) { var li = $("#detailsMsg li[norms]").eq(i); var name = li.children(".norms-name").text(); var attr = li.children(".com-button-selected").text(); if (attr == "") { alertMsg('提示', "请选择商品" + name); return; } name = name.split(':'); description += name[0] + " : " + attr + ';'; // if (description == '') { // description = des; // } else { // description = description + ";" + des; // } } var skuId = $("#sku-id").val(); if (!skuId) { alertMsg('提示', "请选择正确的规格"); return; } var goods_id = requestParam('id') var params = { "goods_id": goods_id, "goods_sku_id": skuId,//商品价格ID "quantity": $("#purchaseQuantity").val(),//数量 "description": description//商品规格(颜色:红色;内存:16G) }; rdcp.request('!Manage/Goods/~java/CartManage.add', params, function (data) { if (data.header.code == 0) { alertMsg('提示', "商品加入成功", 'success'); setTimeout(function () { $('.com-float').remove(); }, 1000) // code = 2表示商品已存在用户购物车,但如果再添加一个就会超出库存,直接提示就行 } else if (data.header.code == "2007") { alertMsg('提示', "商品已加入购物车"); } else if (data.header.code == "2005") { rdcp.go('!Front/User/~view/login?goodsDetail=' + goods_id); } else if (data.header.code == "-5") { $("#alertLoginBox").css("display", "block"); } else { alertMsg('提示', data.header.message + "(" + data.header.code + ")"); } console.log('refresh') refreshCartCount(); }, {mask: false}) }); function refreshCartCount() { rdcp.request('!Manage/Goods/~java/CartManage.getCartCount', {}, function (data) { if (data.header.code == 0) { $('.shopNum').text(data.body.count) } else { console.log('提示', data.header.message + "(" + data.header.code + ")"); } }, {mask: false}) } var loadComm = false; var page = 1; var pageSize = 2; var pageCount; //点击评论商品,加载商品评论列表 function loadComments() { if (loadComm == false) { rdcp.request('!Manage/Comment/~java/Comment.getComment', { "page": page, "pageSize": pageSize, "goods_id": requestParam("id") }, function (data) { if (data.header.code == 0) { pageCount = data.body.pageCount; var rows = data.body.rows; if (rows.length == 0 || pageCount == page) { $("#moreComments").text("已无更多数据"); } var comment = ""; for (var i = 0; i < rows.length; i++) { comment = '
' + '
' + '
' + '
' + '' + rows[i].name + '' + '
' + '
' + rows[i].comment + '
' + '' + '
' + '
' + ' ' + '' + rows[i].create_time + '' + '' + '
' + '
' + ':' + '' + '
' + '
' + '
'; $("#commentLists").append(comment); var score = rows[i].score; $("#commentsStar" + page + i).children('i').removeClass('no-star'); for (var j = score; j < 5; j++) { $("#commentsStar" + page + i).children('i').eq(j).addClass('no-star'); } for (var k = 0; k < rows[i].pictureIds.length; k++) { var img = '
  • '; if (rows[i].pictureIds[k].picture_id != "") { $("#commentsImg" + page + i).append(img); } } for (var n = 0; n < rows[i].attrs.length; n++) { var attr = rows[i].attrs[n].key_as + ":" + rows[i].attrs[n].val + ";"; $("#commentsAttrs" + page + i).append(attr); } if (rows[i].replys.comment != undefined) { $("#replyContent" + page + i).parent(".reply-info").css("display", "block"); var replyName = rows[i].replys.name; var replyContent = rows[i].replys.comment; var replyTime = rows[i].replys.create_time; $("#replyName" + page + i).text(replyName); $("#replyContent" + page + i).text(replyContent); $("#replyTime" + page + i).text(replyTime); } loadComm = true; } //好评度 var praiseRate = data.body.analyse.praise_rate; $("#praiseRate").text(praiseRate); //点击评论图片图片放大 $("#commentLists .comments-img li img").on('click', function () { var src = $(this).attr('src'); var img = ''; var bigImgBox = $(this).parent('li').parent('.comments-img').siblings('.comments-big-img'); if (bigImgBox.children().length == 0) { bigImgBox.append(img); } else { bigImgBox.children("img").attr('src', src); } //点击评论图片减小 $("#commentLists .comments-big-img img").on('click', function () { $(this).remove(); }); }); } else { alertMsg('提示', data.header.message + "(" + data.header.code + ")"); } }, {mask: false}) } } $("#GoodsComment").click(function () { loadComments(); }); //加载更多 $("#moreComments").click(function () { loadComm = false; page++; if (pageCount == page) { $("#moreComments").text("已无更多数据"); loadComments(); } else if (page < pageCount) { $("#moreComments").text("点击加载更多数据"); loadComments(); } }); //购买数不可超过库存 $("#minusbutton").click(function () { if ($("#purchaseQuantity").val() <= 0) { $("#purchaseQuantity").val(1); } }); $("#purchaseQuantity").blur(function () { var purchaseQuantity = $("#purchaseQuantity").val(); var goodsTotalStock = $("#goodsTotalStock").text(); if ($("#purchaseQuantity").val() <= 0) { $("#purchaseQuantity").val(1); } if (purchaseQuantity - goodsTotalStock > 0) { $("#purchaseQuantity").val(goodsTotalStock); } }); $("#addbutton").click(function () { var purchaseQuantity = $("#purchaseQuantity").val(); var goodsTotalStock = $("#goodsTotalStock").text(); if (purchaseQuantity - goodsTotalStock > 0) { $("#purchaseQuantity").val(goodsTotalStock); } }); })();