/*
*作者:一些事情
*时间:2017-5-5
*购物车方法*需要结合jquery一起使用
----------------------------------------------------------*/
//商品数量加减一
function addcartnum(num){
var numobj = $("#commodityselectnum");
var selectnum = 0;
if(numobj.val().length > 0){
selectnum = parseint(numobj.val());
}
selectnum += num;
//最小值
if(selectnum < 1){
selectnum = 1;
}
//最大值
if(selectnum > parseint(numobj.attr("maxvalue"))){
selectnum = parseint(numobj.attr("maxvalue"));
}
numobj.val(selectnum);
}
//初始化商品规格事件
function initgoodsspec(sendurl){
//检查是否有规格
if($("#goodsspecbox dl").length == 0){
$("#buybutton button").prop("disabled",false).removeclass("over");
}
//遍历规格属性
$("#goodsspecbox dl dd ul li a").each(function(){
$(this).click(function(){
if(!$(this).hasclass("selected")){
//标签选中状态
$(this).parent().siblings("li").children("a").removeclass("selected");
$(this).addclass("selected");
//获取商品价格
if($("#goodsspecbox dl dd ul li a.selected").length == $("#goodsspecbox dl").length){
var specids = '';
$("#goodsspecbox dl dd ul li a.selected").each(function(i) {
if(i == 0){
specids = ",";
}
specids += $(this).attr("specid") + ',';
});
//发送异步请求
$.ajax({
type: "post",
url: sendurl,
datatype: "json",
data: {
"article_id": $("#commodityarticleid").val(),
"ids": specids
},
timeout: 20000,
success: function(data, textstatus) {
if (data.status == 1){
$("#commoditygoodsid").val(data.goods_id);
$("#commoditygoodsno").text(data.goods_no);
$("#commoditymarketprice").text('¥' + data.market_price);
$("#commoditysellprice").text('¥' + data.sell_price);
$("#commoditystocknum").text(data.stock_quantity);
$("#commodityselectnum").attr("maxvalue",data.stock_quantity);
//检查是否足够库存
if(parseint(data.stock_quantity) > 0){
$("#buybutton button").prop("disabled",false).removeclass("over");
}else{
$("#buybutton button").prop("disabled",true).addclass("over");
}
} else {
alert(data.msg);
}
},
error: function (xmlhttprequest, textstatus, errorthrown) {
alert("查询出错:" + textstatus + ",提示:" + errorthrown);
}
});
}
}
});
});
}
//删除元素
function hintremove(obj){
$(obj).remove();
}
//添加进购物车
function cartadd(obj, webpath, linktype, linkurl) {
var channelid = parseint($("#commoditychannelid").val());
var articleid = parseint($("#commodityarticleid").val());
var goodsid = parseint($("#commoditygoodsid").val());
var selectnum = parseint($("#commodityselectnum").val());
if($(obj).prop("disabled") == true){
return false;
}
//检查频道id
if (isnan(channelid)) {
alert("频道参数不正确!");
return false;
}
//检查文章id
if (isnan(articleid)) {
alert("商品参数不正确!");
return false;
}
//检查商品是否有规格
if(goodsid == 0 && $("#goodsspecbox dl").length > 0){
alert("请先选择商品规格!");
return false;
}
//检查购买数量
if(isnan(selectnum) || selectnum == 0){
alert("购买数量不能为零!");
return false;
}
//检查库存数量
if(parseint(selectnum) > parseint($("#commoditystocknum").text())){
alert("购买数量大于库存数量,库存不足!");
return false;
}
//记住按钮文字
var buttontext = $(obj).text();
//如果是立即购买
if(linktype == 1){
var jsondata = '[{"channel_id":' + channelid + ', "article_id":' + articleid + ', "goods_id":' + goodsid + ', "quantity":' + selectnum + '}]'; //结合商品参数
$.ajax({
type: "post",
url: webpath + "tools/submit_ajax.ashx?action=cart_goods_buy",
data: { "jsondata": jsondata },
datatype: "json",
beforesend: function(xmlhttprequest) {
//发送前动作
$(obj).prop("disabled",true).text("请稍候...");
},
success: function(data, textstatus) {
if (data.status == 1) {
location.href = linkurl;
}else{
alert("尝试加入购物清单失败,请重试!");
}
},
error: function (xmlhttprequest, textstatus, errorthrown) {
alert("状态:" + textstatus + ";出错提示:" + errorthrown);
},
complete: function (xmlhttprequest, textstatus) {
$(obj).prop("disabled",false).text(buttontext);
},
timeout: 20000
});
return false;
}else{
$.ajax({
type: "post",
url: webpath + "tools/submit_ajax.ashx?action=cart_goods_add",
data: {
"channel_id": channelid,
"article_id" : articleid,
"goods_id" : goodsid,
"quantity" : selectnum
},
datatype: "json",
beforesend: function(xmlhttprequest) {
//发送前动作
$(obj).prop("disabled",true).text("请稍候...");
},
success: function(data, textstatus) {
if (data.status == 1) {
$("#cartinfohint").remove();
var hinthtml = '
'
+ '
'
+ '
'
+ '
商品已成功添加到购物车!'
+ '
购物车共有' + data.quantity + '件商品,合计:' + data.amount + '元
'
+ '
去结算 '
+ '
再逛逛'
+ '
'
+ '
'
+ '
'
$(obj).after(hinthtml); //添加节点
$("#shoppingcartcount").text(data.quantity); //赋值给显示购物车数量的元素
} else {
$("#cartinfohint").remove();
var hinthtml = ''
+ '
'
+ '
'
+ '
商品添加到购物车失败!'
+ '
' + data.msg + '
'
+ '
'
+ '
'
+ '
'
$(obj).after(hinthtml); //添加节点
}
},
error: function (xmlhttprequest, textstatus, errorthrown) {
alert("状态:" + textstatus + ";出错提示:" + errorthrown);
},
complete: function (xmlhttprequest, textstatus) {
$(obj).prop("disabled",false).text(buttontext);
},
timeout: 20000
});
return false;
}
}
//修改购物车数量
function updatecart(obj, webpath, num){
var objinput;
var goodsquantity; //购买数量
var stockquantity = parseint($(obj).parents("tr").find("input[name='hidestockquantity']").val()); //库存数量
var channelid = $(obj).parents("tr").find("input[name='hidechannelid']").val(); //频道id
var articleid = $(obj).parents("tr").find("input[name='hidearticleid']").val(); //文章id
var goodsid = $(obj).parents("tr").find("input[name='hidegoodsid']").val(); //商品id
var goodsprice = $(obj).parents("tr").find("input[name='hidegoodsprice']").val(); //商品单价
var goodspoint = $(obj).parents("tr").find("input[name='hidepoint']").val(); //商品积分
if(arguments.length == 2){
objinput = $(obj);
goodsquantity = parseint(objinput.val());
}else{
objinput = $(obj).siblings("input[name='goodsquantity']");
goodsquantity = parseint(objinput.val()) + parseint(num);
}
if(isnan(goodsquantity)){
alert('商品数量只能输入数字!');
return false;
}
if(isnan(stockquantity)){
alert('检测不到商品库存数量!');
return false;
}
if(goodsquantity < 1){
goodsquantity = 1;
}
if(goodsquantity > stockquantity){
alert('购买数量不能大于库存数量!');
goodsquantity = stockquantity;
}
$.ajax({
type: "post",
url: webpath + "tools/submit_ajax.ashx?action=cart_goods_update",
data: {
"channel_id": channelid,
"article_id": articleid,
"goods_id": goodsid,
"quantity": goodsquantity
},
datatype: "json",
beforesend: function(xmlhttprequest) {
//发送前动作
},
success: function(data, textstatus) {
if (data.status == 1) {
objinput.val(goodsquantity);
var totalprice = parsefloat(goodsprice)*goodsquantity; //金额
var totalpoint = parsefloat(goodspoint)*goodsquantity; //积分
$(obj).parents("tr").find("label[name='amountcount']").text(totalprice.tofixed(2));
if(totalpoint > 0){
$(obj).parents("tr").find("label[name='pointcount']").text("+"+totalpoint);
}else{
$(obj).parents("tr").find("label[name='pointcount']").text(totalpoint);
}
cartamounttotal(); //重新统计
} else {
alert(data.msg);
}
},
error: function (xmlhttprequest, textstatus, errorthrown) {
alert("状态:" + textstatus + ";出错提示:" + errorthrown);
},
timeout: 20000
});
return false;
}
//删除购物车商品
function deletecart(webpath, obj){
if(!confirm("您确认要从购物车中移除吗?")){
return false;
}
//组合参数
var datastr;
var arglength = arguments.length; //参数个数
if(arglength == 1){
datastr = {"clear": 1}
}else{
var channelid = $(obj).parents("tr").find("input[name='hidechannelid']").val();
var articleid = $(obj).parents("tr").find("input[name='hidearticleid']").val();
var goodsid = $(obj).parents("tr").find("input[name='hidegoodsid']").val();
datastr = { "channel_id": channelid, "article_id": articleid, "goods_id": goodsid }
}
$.ajax({
type: "post",
url: webpath + "tools/submit_ajax.ashx?action=cart_goods_delete",
data: datastr,
datatype: "json",
beforesend: function(xmlhttprequest) {
//发送前动作
},
success: function(data, textstatus) {
if (data.status == 1) {
if(arglength == 1){
location.reload();
}else{
$(obj).parents("tr").remove();
cartamounttotal(); //重新统计
}
} else {
alert(data.msg);
}
},
error: function (xmlhttprequest, textstatus, errorthrown) {
alert("状态:" + textstatus + ";出错提示:" + errorthrown);
},
timeout: 20000
});
return false;
}
//选择商品
function selectcart(obj){
var arglength = arguments.length; //参数个数
if(arglength == 1){
if($(obj).text()=="全选"){
$(obj).text("取消");
$(".checkall").prop("checked", true);
}else{
$(obj).text("全选");
$(".checkall").prop("checked", false);
}
}
cartamounttotal(); //统计商品
}
//计算购物车金额
function cartamounttotal(){
var jsondata = ""; //商品组合参数
var totalamount = 0; //商品总计
$(".checkall:checked").each(function(i) {
var channelid = $(this).parents("tr").find("input[name='hidechannelid']").val(); //频道id
var articleid = $(this).parents("tr").find("input[name='hidearticleid']").val(); //文章id
var goodsid = $(this).parents("tr").find("input[name='hidegoodsid']").val(); //商品id
var goodsprice = $(this).parents("tr").find("input[name='hidegoodsprice']").val(); //商品单价
var goodsquantity = $(this).parents("tr").find("input[name='goodsquantity']").val(); //购买数量
totalamount += parsefloat(goodsprice) * parsefloat(goodsquantity);
jsondata += '{"channel_id":"' + channelid + '", "article_id":"' + articleid + '", "goods_id":"' + goodsid + '", "quantity":"' + goodsquantity + '"}';
if(i < $(".checkall:checked").length-1){
jsondata += ',';
}
});
$("#totalquantity").text($(".checkall:checked").length);
$("#totalamount").text(totalamount.tofixed(2));
if(jsondata.length > 0){
jsondata = '[' + jsondata + ']';
}
$("#jsondata").val(jsondata);
}
//进入结算中心
function formsubmit(obj, webpath, linkurl){
var jsondata = $("#jsondata").val();
if(jsondata == ""){
alert("未选中要购买的商品,请选中后提交!");
return false;
}
//记住按钮文字
var buttontext = $(obj).text();
//加入购物清单
$.ajax({
type: "post",
url: webpath + "tools/submit_ajax.ashx?action=cart_goods_buy",
data: { "jsondata": jsondata },
datatype: "json",
beforesend: function(xmlhttprequest) {
//发送前动作
$(obj).prop("disabled",true).text("请稍候...");
},
success: function(data, textstatus) {
if (data.status == 1) {
location.href = linkurl;
}else{
$(obj).prop("disabled",false).text(buttontext);
alert("尝试进入结算中心失败,请重试!");
}
},
error: function (xmlhttprequest, textstatus, errorthrown) {
$(obj).prop("disabled",false).text(buttontext);
alert("状态:" + textstatus + ";出错提示:" + errorthrown);
},
timeout: 20000
});
return false;
}
/*初始化收货地址*/
function inituseraddress(obj){
//初始化省市区
var mypcas = new pcas("province,所属省份","city,所属城市","area,所属地区");
//初始化地址
$(obj).children("li").each(function() {
//初始化值
if($(this).hasclass("selected")){
$("#book_id").val($(this).find("input[name='user_book_id']").val());
$("#accept_name").val($(this).find("input[name='user_accept_name']").val());
$("#address").val($(this).find("input[name='user_address']").val());
$("#mobile").val($(this).find("input[name='user_mobile']").val());
$("#telphone").val($(this).find("input[name='user_telphone']").val());
$("#email").val($(this).find("input[name='user_email']").val());
$("#post_code").val($(this).find("input[name='user_post_code']").val());
$(this).find("input[name='user_book_id']").prop("checked", true); //设置选中
//改变省市区
var areaarr = $(this).find("input[name='user_area']").val().split(",");
if (areaarr.length == 3) {
mypcas.setvalue(areaarr[0], areaarr[1], areaarr[2]);
} else {
mypcas.setvalue();
}
}
//初始化事件
$(this).click(function(){
$(this).siblings("li").removeclass("selected");
$(this).addclass("selected");
$("#book_id").val($(this).find("input[name='user_book_id']").val());
$("#accept_name").val($(this).find("input[name='user_accept_name']").val());
$("#address").val($(this).find("input[name='user_address']").val());
$("#mobile").val($(this).find("input[name='user_mobile']").val());
$("#telphone").val($(this).find("input[name='user_telphone']").val());
$("#email").val($(this).find("input[name='user_email']").val());
$("#post_code").val($(this).find("input[name='user_post_code']").val());
$(this).find("input[name='user_book_id']").prop("checked", true); //设置选中
//改变省市区
var areaarr = $(this).find("input[name='user_area']").val().split(",");
if (areaarr.length == 3) {
mypcas.setvalue(areaarr[0], areaarr[1], areaarr[2]);
} else {
mypcas.setvalue();
}
});
});
}
//计算支付手续费总金额
function paymentamounttotal(obj){
var paymentprice = $(obj).siblings("input[name='payment_price']").val();
$("#paymentfee").text(paymentprice); //运费
orderamounttotal();
}
//计算配送费用总金额
function freightamounttotal(obj){
var expressprice = $(obj).siblings("input[name='express_price']").val();
$("#expressfee").text(expressprice); //运费
orderamounttotal();
}
//计算税金
function taxamouttotal(obj){
var taxesfee = 0 //税费
if($(obj).prop("checked") == true){
taxesfee = parsefloat($("#taxamout").val());
$("#invoicebox").show();
}else{
$("#invoicebox").hide();
}
$("#taxesfee").text(taxesfee.tofixed(2));
orderamounttotal();
}
//计算订单总金额
function orderamounttotal(){
var goodsamount = $("#goodsamount").text(); //商品总金额
var paymentfee = $("#paymentfee").text(); //手续费
var expressfee = $("#expressfee").text(); //运费
var taxesfee = 0 //税费
if($("#is_invoice").prop("checked") == true){
taxesfee = parsefloat($("#taxamout").val());
}
//订单总金额 = 商品金额 + 手续费 + 运费 + 税费
var totalamount = parsefloat(goodsamount) + parsefloat(paymentfee) + parsefloat(expressfee) + parsefloat(taxesfee);
$("#totalamount").text(totalamount.tofixed(2));
}