/**
* Created by dongjingya on 16/4/6.
*/
function SunboxCheckout() {
this.elements = {
pickupCenter: {
modal: '#pickupCenterModal',
province: '#pc-province',
city: '#pc-city',
country: '#pc-country',
place: '#pc-place',
date: '#pc-date',
save: '#pc-save'
},
checkout: {
changePickupCenter: '#changePc',
pickupCenterValue: '#pcValue',
pickupCenterText: '#pcText',
pickupTime: '#pickuptime'
},
form: {
userid: 'userid',
username: 'realname',
channel: 'channel',
proid: 'proid',
skuid: 'skuid',
wareid: 'wareid',
num: 'num',
orderPayType: 'orderPayType',
distribution: 'distribution',
addressId: 'address',
mobile: 'mobile',
pcValue: 'pcValue',
pcTime: 'pcTime',
mctcode: 'mctcode',
mctapicode: 'mctapicode',
ispayonline: 'ispayonline',
remark:'remark',
submit: '#submitOrder',
isFromCart:'isFromCart',
pricePart:'pricePart',
pointPart:'pointPart',
acttype:'acttype',
freightPayType:'freightPayType'
}
};
this.url = {
shoppingCar: '/cart',
form: {
submitOrder: '/order/submitOrder',
payment: '/order/payment'
}
};
this.options = {
elements: {
isVirtualOrder: '#isVirtualOrder'
},
url: {
address: '/order/orderAddrListHtml'
},
channel: '0',
checkoutListElement: '#checkoutlist',
shoppingCartUrl: '/cart',
getCheckoutListUrl: '/api/checkout/getWebCheckoutList',
provinceElement: '#pc-province',
getProvinceHtmlUrl: '/comm/getProvinceHtml',
cityElement: '#pc-city',
getCityHtmlUrl: '/comm/getCityHtml',
countryElement: '#pc-country',
getCountryHtmlUrl: '/comm/getCountryHtml',
pickupCenterPlaceElement: '#pc-place',
getPickupCenter: '/comm/getPickupCenterHtml',
pcDateElement: 'pc-date',
changePickupCenterElement: '#changePickupCenter',
savePickupCenterElement: '#savePickupCenter',
getFreight: '/api/checkout/getFreight'
};
this.isFromCart = false;
}
SunboxCheckout.prototype = new SunboxBase();
SunboxCheckout.prototype.constructor = SunboxCheckout;
SunboxCheckout.prototype.init = function (parameters, channel) {
var instance = this;
if (channel) instance.options.channel = channel;
// 从cookie中获得要结算的商品
if (parameters) {
// 获得结算商品列表
instance.getCheckoutList(parameters);
} else {
instance.gotoShoppingCart();
}
}
SunboxCheckout.prototype.bindEvent = function () {
var instance = this;
// 绑定事件
$(instance.elements.checkout.changePickupCenter).on('click', function () {
// 打开选择自提点窗口
var skuid = $(this).data('skuid');
$(instance.elements.pickupCenter.save).data('skuid', skuid);
$(instance.elements.pickupCenter.modal).modal('show');
});
$(instance.elements.pickupCenter.province).on('change', function () {
// 选择省份刷新市
instance.getCityList($(this).val());
});
$(instance.elements.pickupCenter.city).on('change', function () {
// 选择市刷新区县
instance.getCountryList($(this).val());
});
$(instance.elements.pickupCenter.country).on('change', function () {
// 选择区县刷新自提点
instance.getPickupCenter();
});
$(instance.elements.pickupCenter.modal).on('show.bs.modal', function () {
// 自提点窗口打开事件
instance.showPickupCenter();
});
$(instance.elements.pickupCenter.save).on('click', function () {
// 保存自提点事件
instance.savePickupCenter();
});
$(instance.elements.form.submit).on('click', function () {
instance.submitOrder();
});
// 显示配送方式
$('span[data-exp]', '.checkout').on('click', function () {
var $this = $(this);
var id = $this.data('id');
var $exp = $this.parent().parent().next();
$('div[data-delivery]', $exp).addClass('hide');
$('#' + id, $exp).removeClass('hide');
});
// 默认选中第一个配送方式
$.each($('.checkout'), function (i, item) {
$('span[data-exp]', $(item)).first().trigger('click');
});
}
// 获取结算商品列表
SunboxCheckout.prototype.getCheckoutList = function (data) {
var instance = this;
if (data) {
jQuery.ajax({
url: instance.options.getCheckoutListUrl,
data:{data:data,isFromCart:instance.isFromCart},
dataType: 'html',
success: function (html) {
if (html) {
var index = html.indexOf('<input type="hidden" value="success">');
if (index > 0) {
var $el = $(instance.options.checkoutListElement);
if ($el) {
if ($el.hasClass('row')) {
if ($el.children.length > 0) {
$($el[0].children[0]).append(html);
} else {
$($el[0]).append(html);
}
instance.bindEvent();
if ($(instance.options.elements.isVirtualOrder).val() == 'false') {
instance.getAddressHtml();
} else {
$("#address-box").html("");
}
instance = null;
}
}
} else {
var errmsg = $(html).find('input[name="errmsg"]').val();
alert('提交订单失败:'+ errmsg);
}
} else {
instance.gotoShoppingCart();
}
},
error: function () {
},
complete: function () {
}
});
} else {
instance.gotoShoppingCart();
}
}
// 获取商品运费
SunboxCheckout.prototype.getFreight = function () {
var instance = this;
$(".freighttip").hide();
var freightIsSupport = $("#freightIsSupport").val();//是否支持运费
if (freightIsSupport != "true") {
return false;
}
var addressid = $(".currentAddr").attr("data-id");
if (!addressid) {
return false;//地址为空,直接返回
}
var delivers=[EnumList.DeliverType.ExpressPickUp.value,EnumList.DeliverType.Express.value,EnumList.DeliverType.Thrid.value];
var paramArray = [];
$.each($('.checkout'), function (i, item) {
// 循环获得每个商品
$.each($('tbody > tr', item), function (j, tr) {
var $delivery = $('td.sltDeliverType > div[data-type="exp"]', tr);
if ($delivery && $('.current', $delivery).length > 0) {
delivery = $('.current', $delivery);
}
var currentdelivery;
if (delivery && delivery.length == 0) {
return;
} else {
currentdelivery = delivery.data('id');
}
if(delivers.includes(parseInt(currentdelivery))){
var parameter = {};
var skuid = $(tr).data('skuid');
var num = $(tr).data('num');
var mctcode = $(tr).data('mctcode');
var mctapicode = $(tr).data('mctapicode');
parameter[instance.elements.form.skuid] = skuid ? skuid : '';
parameter[instance.elements.form.num] = num ? num : '';
parameter[instance.elements.form.distribution] = currentdelivery ? currentdelivery : '';
parameter["mctid"] = mctcode;
parameter[instance.elements.form.mctapicode] = mctapicode;
paramArray.push(parameter);
};
});
});
if (paramArray.length > 0) {
//调用运费接口
$.ajax({
url: instance.options.getFreight + "?addressid=" + addressid,
data: 'data=' + JSON.stringify(paramArray),
dataType: 'json',
success: function (data) {
if (data.result) {
var freighttotal = 0;
var freighttotalpoints = 0;
$.each(data.data, function (i, item) {
var $freight = $(".freight_" + i);
if ($freight.length != 0) {
var firTotal = 0,firTotalPoints = 0;
$.each(item, function (j, fir) {
firTotal = Number(firTotal) + Number(fir.freight);
firTotalPoints = Number(firTotalPoints) + Number(fir.freightPoint);
});
freighttotal = Number(freighttotal) + Number(firTotal);
freighttotalpoints = Number(freighttotalpoints) + Number(firTotalPoints);
$freight.data("freight",firTotal);
$freight.data("freightpoints",firTotalPoints);
var ispoint = $("#ckfreight").is(':checked');
if(ispoint){
$freight.html(freighttotalpoints + "积分");
}
else{
$freight.html("¥" + firTotal.toFixed(2));
}
}
else {
$freight.hide();
}
});
instance.getFreightSuccess(freighttotal,freighttotalpoints);
}
else {
instance.getFreightFailed();
alert(data.msg);
}
},
error: function () {
instance.getFreightFailed();
alert("运费计算异常");
}
});
}
else {
//直接设置运费为0
$.each($(".freight"), function (i, item) {
$(item).html("运费:¥0.00");
});
instance.getFreightSuccess(0,0,"");
}
}
//获取运费成功
SunboxCheckout.prototype.getFreightSuccess = function (freighttotal,freighttotalpoints) {
//设置提交按钮可用
var instance = this;
instance.isCansubmit = true;
$("#submitOrder").removeClass("bgGray");//移除遮罩样式
if(freighttotal > 0){
$(".freighttip").show();
}
var ispoint = $("#ckfreight").is(':checked');
//设置运费
$.each($(".freightTotals"), function (i, item) {
$(item).data("freight",freighttotal);
$(item).data("freightpoints",freighttotalpoints);
if(ispoint){
$(item).html(freighttotalpoints + "积分");
}
else{
$(item).html("¥" + freighttotal);
}
});
//设置订单总计
var pointTotal = $("#orderTotal").data("pointtotal");
var priceTotal = $("#orderTotal").data("pricetotal");
if(ispoint){
pointTotal = Number(pointTotal) + Number(freighttotalpoints);
}else{
priceTotal = Number(priceTotal) + Number(freighttotal);
}
var orderTotalinfo = "";
if(pointTotal > 0)
{
orderTotalinfo += pointTotal + "积分";
}
if(pointTotal > 0 && priceTotal >0){
orderTotalinfo += " + ";
}
if(priceTotal > 0)
{
orderTotalinfo += "¥" + priceTotal;
}
$("#orderTotal").html(orderTotalinfo);
instance.getDealersEnable(freighttotal);
}
//获取运费失败
SunboxCheckout.prototype.getFreightFailed = function () {
//设置提交按钮不可用
var instance = this;
instance.isCansubmit = false;
$("#submitOrder").addClass("bgGray");
$(".freighttip").hide();
var ispoint = $("#ckfreight").is(':checked');
//设置单个运费
$.each($(".freight"), function (i, item) {
$(item).data("freight","0.00");
$(item).data("freightpoints","0");
if(ispoint){
$(item).html("-积分");
}
else{
$(item).html("¥-");
}
});
//设置运费总计
$.each($(".freightTotals"), function (i, item) {
$(item).data("freight","0.00");
$(item).data("freightpoints","0");
if(ispoint){
$(item).html("-积分");
}
else{
$(item).html("¥-");
}
});
//设置订单总计
var pointTotal = $("#orderTotal").data("pointtotal");
var priceTotal = $("#orderTotal").data("pricetotal");
var orderTotalinfo = "";
if(pointTotal > 0)
{
orderTotalinfo += "-";
}
if(pointTotal > 0 && priceTotal >0){
orderTotalinfo += " + ";
}
if(priceTotal > 0)
{
orderTotalinfo += "¥-";
}
}
SunboxCheckout.prototype.getDealersEnable = function (freight){
if(Number(freight) > 0) {
var orgPriceTotal = $(".payWalletInfo").first().data("orgpricetotal");
var walletTotal = $(".payWalletInfo").first().data("balance");
if (Number(orgPriceTotal) <= Number(walletTotal)) {
if (Number(orgPriceTotal) + Number(freight) > Number(walletTotal)) {
$("#ckwallet").hide();
$(".walletshortinfo").text("余额不足");
}
else {
$("#ckwallet").show();
$(".walletshortinfo").text("使用额度支付");
}
}
}
}
// 提交订单
SunboxCheckout.prototype.submitOrder = function () {
var instance = this;
var userid = instance.getUserid();
var username = instance.getUsername();
var channel = instance.options.channel;
var addressid = $(".currentAddr").attr("data-id");
var paymentid = $('.v2_pay_btn.current').attr("data-id");
var isVirtualOrder = $(instance.options.elements.isVirtualOrder).val();
if (!userid) {
instance.showLogin();
return false;
}
var item = $(".cqphone");
var validphone = true;
$(".cqphone").each(function () {
var val = $(this).val();
var reg = /^1(3|4|5|7|8)\d{9}$/;
if ($.trim(val) != 'undefined' && $.trim(val) != null && $.trim(val) != '' && reg.test(val)) {
} else {
alert("请输入11位手机号");
$(this).focus();
validphone = false;
return false;
}
/*if(val==""){
alert("请输入手机号");
$(this).focus();
validphone= false;
}*/
}
);
if (!validphone) {
return false;
}
if (!addressid && isVirtualOrder == 'false') {
alert('请选择"收货地址"!');
return false;
}
if (!paymentid) {
alert('请选择"支付方式"!');
return false;
}
var acttype;
if(!acttype){
if($("#ckwallet").is(':checked')){
acttype = acttype = EnumList.ActType.Dealers.value;;
}
}
//运费的支付方式
var freightpaytype = EnumList.ProductPayType.CASH.value;
var freightIsSupport = $("#freightIsSupport").val();//是否支持运费
if (freightIsSupport == "true") {
if($("#ckfreight").is(':checked')){
freightpaytype = EnumList.ProductPayType.POINTS.value;
}
}
var invoiceType=$("input[name='invoiceTypePopup']:checked").val();
var purePoints = $("#purePoints").val();
if (purePoints == "true"){
invoiceType = -1;//纯积分支付不需要发票
}
//是否随货
var invoiceMode=0;
//发票抬头\发票内容
var invoicetitle= getCookie('invoiceTitlePopup');
var invoicecontent=$("input[name='invoiceContentPopup']:checked").val();
var invoicePhone=$("#invoicePhone").val();
var taxpayerNumber=$("#taxpayerNumber").val();
var status = true;
var paramArray = [];
var tag;
/* $.each($('.checkout'), function(i, item) {
var $tr = $('tbody > tr',item);
$.each($('tbody > tr',item), function(j, tr) {
var num = $(tr).data('num');
/!*var mctapicode = $(tr).data('mctapicode');
if(mctapicode==4){
if(num>3){
alert("抱歉,中石油电子充值卡单笔订单最大购买数量不得大于3!");
return tag;
}
}*!/
});
});*/
var delivery;
$.each($('.checkout'), function (i, item) {
status = true;
//留言信息
var remarkobj = $('tfoot input[name=remark]', item);
var remark = remarkobj.val();
var $tr = $('tbody > tr', item);
// 循环获得每个商品
var mobile = $('input[name=mobile]', item).val();
var pcValue = $('input[name=pcValue]', item).data('code');
var pcTime = $('input[name=pickupTime]', item).val();
$.each($('tbody > tr', item), function (j, tr) {
var $delivery = $('td.sltDeliverType > div[data-type="exp"]', tr);
if ($delivery && $('.current', $delivery).length > 0) {
delivery = $('.current', $delivery);
}
var currentdelivery;
if (delivery && delivery.length == 0) {
alert('请选择"配送方式"!');
status = false;
return status;
} else {
currentdelivery = delivery.data('id');
}
var parameter = {};
var proid = $(tr).data('proid');
var skuid = $(tr).data('skuid');
var num = $(tr).data('num');
var mctcode = $(tr).data('mctcode');
var mctapicode = $(tr).data('mctapicode');
var orderPayType = $(tr).data('paytype');
var pricePart = $(tr).data('pricepart');
var pointPart = $(tr).data('pointpart');
parameter[instance.elements.form.userid] = userid;
parameter[instance.elements.form.username] = username;
parameter[instance.elements.form.proid] = proid ? proid : '';
parameter[instance.elements.form.skuid] = skuid ? skuid : '';
parameter[instance.elements.form.num] = num ? num : '';
parameter[instance.elements.form.orderPayType] = orderPayType ? orderPayType : '';
parameter[instance.elements.form.distribution] = currentdelivery ? currentdelivery : '';
parameter[instance.elements.form.addressId] = addressid;
parameter[instance.elements.form.mobile] = mobile;
parameter[instance.elements.form.pcValue] = pcValue;
parameter[instance.elements.form.pcTime] = pcTime;
parameter[instance.elements.form.mctcode] = mctcode;
parameter[instance.elements.form.mctapicode] = mctapicode;
parameter[instance.elements.form.channel] = channel;
parameter[instance.elements.form.ispayonline] = paymentid;
parameter[instance.elements.form.remark] = remark;
parameter[instance.elements.form.isFromCart] = instance.isFromCart;
parameter[instance.elements.form.pricePart] = pricePart;
parameter[instance.elements.form.pointPart] = pointPart;
parameter[instance.elements.form.freightPayType] = freightpaytype;
parameter[instance.elements.form.acttype] = acttype;
paramArray.push(parameter);
});
});
var paramArray1 = [];
var parameter1 = {};
parameter1['invoicetype'] = invoiceType;
parameter1['invoicemode'] = invoiceMode;
parameter1['invoicetitle'] = invoicetitle;
parameter1['invoicecontent'] = invoicecontent;
parameter1['phone'] = invoicePhone;
parameter1['taxpayerNumber'] = taxpayerNumber;
paramArray1.push(parameter1);
if (status && paramArray.length > 0) {
//判断是否勾选服务条款
if($("#redRuleFlag").prop("checked")){
var paradata = {
data:JSON.stringify(paramArray),
invoiceData:JSON.stringify(paramArray1),
};
$.ajax({
url: instance.url.form.submitOrder,
data: paradata,
dataType: 'json',
beforeSend: function(){
//移除点击事件
$("#submitOrder").unbind();
$("#submitOrder").addClass("bgGray");
},
success: function(data) {
if (data) {
if (data.result) {
//document.location=instance.url.form.payment +"?data="+$.toJSON(data.rows)
instance.customForm(instance.url.form.payment, {
porderids: $.toJSON(data.rows),
paydata:data.data
});
} else {
alert(data.msg);
//添加点击事件
$(instance.elements.form.submit).on('click', function () {
instance.submitOrder();
});
//移除遮罩样式
$("#submitOrder").removeClass("bgGray");
}
}
},
error: function () {
//添加点击事件
$(instance.elements.form.submit).on('click', function () {
instance.submitOrder();
});
//移除遮罩样式
$("#submitOrder").removeClass("bgGray");
},
complete: function () {
//添加点击事件
$(instance.elements.form.submit).on('click', function () {
instance.submitOrder();
});
//移除遮罩样式
$("#submitOrder").removeClass("bgGray");
}
});
} else {
$("#redRuleFlag").focus();
alert("请同意协议并勾选");
}
}
}
// 返回购物车
SunboxCheckout.prototype.gotoShoppingCart = function () {
var instance = this;
alert('请选择结算的商品!');
// 跳转到购物车界面
window.location.href = instance.url.shoppingCar;
}
// 打开选择自提点窗口
SunboxCheckout.prototype.showPickupCenter = function () {
var instance = this;
$(instance.elements.pickupCenter.province).html('');
$(instance.elements.pickupCenter.city).html('');
$(instance.elements.pickupCenter.country).html('');
$(instance.elements.pickupCenter.place).html('');
$(instance.elements.pickupCenter.date).val('');
instance.getProvinceList();
}
// 保存自提点
SunboxCheckout.prototype.savePickupCenter = function () {
var instance = this;
if (!$(instance.elements.pickupCenter.place).val()) {
$(instance.elements.pickupCenter.place).focus();
instance.alert('请选择"自提点"!');
}
if (!$(instance.elements.pickupCenter.date).val()) {
$(instance.elements.pickupCenter.date).focus();
instance.alert('请选择"自提时间"!');
}
var placeValue = $(instance.elements.pickupCenter.place).val();
var placeText = $(instance.elements.pickupCenter.place).text();
var date = $(instance.elements.pickupCenter.date).val();
var skuid = $(instance.elements.pickupCenter.save).data('skuid');
var $productDiv = $('#' + skuid);
$(instance.elements.checkout.pickupCenterText, $productDiv).html(placeText);
$(instance.elements.checkout.pickupCenterValue, $productDiv).val(placeValue);
$(instance.elements.checkout.pickupTime, $productDiv).val(date);
$(instance.elements.pickupCenter.modal).modal('hide');
}
// 获得省份
SunboxCheckout.prototype.getProvinceList = function () {
var instance = this;
jQuery.ajax({
url: instance.options.getProvinceHtmlUrl,
dataType: 'html',
success: function (html) {
if (html) {
$(instance.options.provinceElement).html('');
$(instance.options.provinceElement).append(html);
$(instance.options.provinceElement + ' li:first').prop("selected", 'selected');
}
},
error: function () {
},
complete: function () {
}
});
}
// 获得市区
SunboxCheckout.prototype.getCityList = function (data) {
var instance = this;
jQuery.ajax({
url: instance.options.getCityHtmlUrl,
data: 'province=' + data,
dataType: 'html',
success: function (html) {
if (html) {
$(instance.options.cityElement).html('');
$(instance.options.cityElement).append(html);
$(instance.options.provinceElement + ' li:first').prop("selected", 'selected');
var cityid = $(instance.options.cityElement).val();
instance.getPickupCenter(cityid, countryid);
}
},
error: function () {
},
complete: function () {
}
});
}
// 获得区县
SunboxCheckout.prototype.getCountryList = function (data) {
var instance = this;
jQuery.ajax({
url: instance.options.getCountryHtmlUrl,
data: 'city=' + data,
dataType: 'html',
success: function (html) {
if (html) {
$(instance.options.countryElement).html('');
$(instance.options.countryElement).append(html);
$(instance.options.provinceElement + ' li:first').prop("selected", 'selected');
instance.getPickupCenter();
}
},
error: function () {
},
complete: function () {
}
});
}
// 获得自提点
SunboxCheckout.prototype.getPickupCenter = function () {
var instance = this;
var cityid = $(instance.options.cityElement).val();
var countryid = $(instance.options.countryElement).val();
if (isNaN(cityid)) cityid = 0;
if (isNaN(countryid)) countryid = 0;
jQuery.ajax({
url: instance.options.getCountryHtmlUrl,
data: 'cityid=' + cityid + '&countryid=' + countryid,
dataType: 'html',
success: function (html) {
if (html) {
$(instance.options.countryElement).html('');
$(instance.options.countryElement).append(html);
$(instance.options.provinceElement + ' li:first').prop("selected", 'selected');
}
},
error: function () {
},
complete: function () {
}
});
}
// 获得收货地址
SunboxCheckout.prototype.getAddressHtml = function () {
var instance = this;
$.ajax({
type: "post",
dataType: "text",
url: instance.options.url.address,
success: function (data) {
$("#address-box").html(data);
jQuery("#address-box").find("table tr").each(function (i) {
if (i > 2) {
jQuery(this).hide();
}
});
$("#showMoreAddress").show();
$("#receiveMsg").css("font-size", 15);
//计算运费
instance.getFreight();
}
});
}
/*$('#showMoreAddress').click(function(){
jQuery("#address-box").find("table tr").each(function(i){
jQuery(this).show();
});
$('#showMoreAddress').hide();
});*/
function showMoreAdd() {
jQuery("#address-box").find("table tr").each(function (i) {
jQuery(this).show();
});
$('#showMoreAddress').hide();
$("#foldMoreAddress").show();
}
function foldMoreAdd() {
jQuery("#addressTable").prepend(jQuery("#addressTable").find("tr.currentAddr"));
jQuery("#addressTable").find("tr.currentAddr").show();
jQuery("#address-box").find("table tr").each(function (i) {
if (i > 2) {
jQuery(this).hide();
}
});
$("#showMoreAddress").show();
$("#foldMoreAddress").hide();
}
/*回填手机号方法*/
function backphone(phone) {
jQuery.each(jQuery(".cqphone"), function (i, item) {
jQuery(item).val(phone);
});
}