var selectedGoodsId = [], goods_id=[], coupon_id = [], addCoupon, currIndex; $.each(goods_list, function(index, item) { var id = item.goods_id; selectedGoodsId.push(id); goods_id.push(id); }); $("input[name='goods_ids']").val(goods_id.toString()); Object.values(manjian_info).forEach((item,index)=>{ if (item.coupon_data) { var arr = []; item.coupon_data.forEach((subItem,subIndex)=>{ arr.push(subItem.coupon_type_id); }); coupon_id.push(arr); } }); var table, form, laydate, laytpl, repeat_flag = false; //防重复标识 layui.use(['form', 'laydate', 'laytpl'], function() { form = layui.form, laydate = layui.laydate, laytpl = layui.laytpl; form.render(); renderTable(goods_list); // 初始化表格 laydate.render({ elem: '#start_time', type: 'datetime' }); laydate.render({ elem: '#end_time', type: 'datetime' }); //监听活动商品类型 form.on('radio(manjian_type)', function(data){ var value = data.value; if(value == 1){ $(".goods_list").hide(); } if(value == 2){ $(".goods_list").show(); } }); form.on('radio(type)', function(data){ var value = data.value; $('.level-item .type-' + value).removeClass('layui-hide').siblings('div').addClass('layui-hide'); }); /** * 表单验证 */ form.verify({ len: function(value) { if (value.length > 25) { return "活动名称最多为25个字符!"; } }, time: function(value) { var now_time = (new Date()).getTime(); var time = (new Date(value)).getTime(); if (time < now_time) { return '开始时间不能小于当前时间!'; } }, times: function(value) { var start_time = (new Date($("#start_time").val())).getTime(); var end_time = (new Date(value)).getTime(); if (start_time > end_time) { return '结束时间不能小于开始时间!'; } }, num: function(value) { if (value == '') { return; } if (value < 0) { return '数字不能小于0!'; } if (value * 100 % 1 != 0) { return '数字最多保留两位小数!'; } }, manjian_money: function(value){ var type = $('[name="type"]:checked').val(); if (type == 0) { if (!/[\S]+/.test(value)) { return '请输入金额'; } if (value < 0) { return '金额不能小于0!'; } } }, manjian_num: function(value){ var type = $('[name="type"]:checked').val(); if (type == 1) { if (!/[\S]+/.test(value)) { return '请输入数量'; } if (value < 0) { return '数量不能小于0!'; } } }, coupon_num: function(value){ if (!/[\S]+/.test(value)) { return '请输入优惠券赠送数量'; } if (value < 1) { return '优惠券赠送数量不能少于一张'; } }, goods_num:function (value) { var manjian_type = $('[name="manjian_type"]:checked').val(); if(manjian_type == 2){ if(value == 0){ return '请选择活动商品!'; } } } }); /** * 监听提交 */ form.on('submit(save)', function(data) { var rule = {}, verify = true, type = $('[name="type"]:checked').val(); $('.manjian-rule .level-item').each(function(i, e) { var limit = parseFloat($(this).find('.type-'+ type +' .layui-input').val()).toFixed(2); if (i > 0) { var prevLimit = $('.manjian-rule .level-item:eq('+ (i - 1) +') .type-'+ type +' .layui-input').val(); if (parseFloat(prevLimit) >= parseFloat(limit)) { showErrMsg('优惠门槛需大于上一级优惠门槛', $(this).find('.type-'+ type +' .layui-input')); verify = false; return false; } } rule[limit] = { limit: limit }; if ($(this).find('[value="discount_money"]').is(':checked')) { var discount_money = $(this).find('.discount-item.discount-money .layui-input').val(); if (!/[\S]+/.test(discount_money)) { showErrMsg('请输入优惠金额', $(this).find('.discount-item.discount-money .layui-input')); verify = false; return false; } discount_money = parseFloat(discount_money); if (discount_money <= 0) { showErrMsg('优惠金额不能小于等于0', $(this).find('.discount-item.discount-money .layui-input')); verify = false; return false; } if (type == 0 && discount_money > parseFloat(limit)) { showErrMsg('优惠金额请勿超出优惠门槛', $(this).find('.discount-item.discount-money .layui-input')); verify = false; return false; } rule[limit].discount_money = discount_money; } if ($(this).find('[value="free_shipping"]').is(':checked')) { rule[limit].free_shipping = 1; } if ($(this).find('[value="point"]').is(':checked')) { var point = $(this).find('.discount-item.point .layui-input').val(); rule[limit].point = point; if (!/[\S]+/.test(point)) { showErrMsg('请输入赠送积分数', $(this).find('.discount-item.point .layui-input')); verify = false; return false; } if (point <= 0) { showErrMsg('赠送积分数不能小于等于0', $(this).find('.discount-item.point .layui-input')); verify = false; return false; } } var coupon = []; var coupon_num = []; if ($(this).find('[value="coupon"]').is(':checked')) { $(this).find('tr[data-coupon]').each(function (i, e) { coupon.push($(e).attr('data-coupon')); }); $(this).find(".coupon input[name='number']").each(function(j,item){ coupon_num.push(item.value); }); if (!coupon.length) { showErrMsg('请选择要赠送的优惠券'); verify = false; return false; } rule[limit].coupon = coupon.toString(); rule[limit].coupon_num = coupon_num.toString(); } if (rule[limit].discount_money == undefined && rule[limit].free_shipping == undefined && rule[limit].point == undefined && rule[limit].coupon == undefined) { showErrMsg('请选择活动层级'+ (i + 1) +'的优惠内容'); verify = false; return false; } }); if (!verify) return; data.field.rule_json = JSON.stringify(rule); if (repeat_flag) return; repeat_flag = true; $.ajax({ type: 'POST', url: ns.url("manjian://shop/manjian/edit"), data: data.field, dataType: 'JSON', success: function (res) { repeat_flag = false; if (res.code == 0) { layer.confirm('编辑成功', { title:'操作提示', btn: ['返回列表', '继续操作'], yes: function(){ location.href = ns.url("manjian://shop/manjian/lists") }, btn2: function() { location.reload(); } }); } else if (res.code == -10077){ var key = res.data.key; layer.confirm('在同一活动时间内,部分商品已参加其他的满减活动', { title:'活动冲突提醒', btn: ['取消', '查看详情'], btn1: function(){ location.href = ns.url("manjian://shop/manjian/add"); }, btn2: function() { location.href = ns.url("manjian://shop/manjian/conflict", {"key": key}); } }); } else { layer.msg(res.message); } } }); }); function showErrMsg(msg, e){ layer.msg(msg, { icon: 5, shift: 6 }); if (e != undefined) { $(e).focus(); } } form.on('submit(coupon-search)', function(data) { couponTable.reload({ page: { curr: 1 }, where: data.field }) }) }); // 表格渲染 function renderTable(goods_list) { //展示已知数据 table = new Table({ elem: '#selected_sku_list', page: false, limit: Number.MAX_VALUE, cols: [ [{ field: 'goods_name', title: '商品名称', unresize: 'false', width: '50%', templet: function(data) { var html = ''; html += `
${data.goods_name}