add.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. var goods_list = [], selectedGoodsId = [], goods_id=[], coupon_id = [], addCoupon, currIndex;
  2. var laytpl, table, form, laydate, repeat_flag = false, //防重复标识
  3. currentDate = new Date(), //当前时间
  4. minDate = "";
  5. layui.use(['form', 'laydate', 'laytpl','util'], function() {
  6. form = layui.form,
  7. laydate = layui.laydate;
  8. laytpl = layui.laytpl;
  9. util = layui.util;
  10. form.render();
  11. renderTable(goods_list); // 初始化表格
  12. currentDate.setDate(currentDate.getDate() + 30); //当前时间+30之后的时间戳
  13. // 开始时间
  14. laydate.render({
  15. elem: '#start_time' ,//指定元素
  16. type: 'datetime',
  17. value: new Date(),
  18. done: function(value){
  19. minDate = value;
  20. reRender();
  21. }
  22. });
  23. //结束时间
  24. laydate.render({
  25. elem: '#end_time' ,//指定元素
  26. type: 'datetime',
  27. value: new Date(currentDate)
  28. });
  29. /**
  30. * 重新渲染结束时间
  31. * */
  32. function reRender(){
  33. $("#end_time").remove();
  34. $(".end-time").html('<input type="text" id="end_time" name="end_time" placeholder="请输入结束时间" lay-verify="required|time" class="layui-input len-mid" autocomplete="off"><i class=" iconrili iconfont calendar"></i>');
  35. laydate.render({
  36. elem: '#end_time',
  37. type: 'datetime',
  38. min: minDate
  39. });
  40. }
  41. //监听活动商品类型
  42. form.on('radio(manjian_type)', function(data){
  43. var value = data.value;
  44. if(value == 1){
  45. $(".goods_list").hide();
  46. }
  47. if(value == 2){
  48. $(".goods_list").show();
  49. }
  50. });
  51. form.on('radio(type)', function(data){
  52. var value = data.value;
  53. $('.level-item .type-' + value).removeClass('layui-hide').siblings('div').addClass('layui-hide');
  54. });
  55. /**
  56. * 表单验证
  57. */
  58. form.verify({
  59. len: function(value) {
  60. if (value.length > 25) {
  61. return "活动名称最多为25个字符!";
  62. }
  63. },
  64. time: function(value) {
  65. var now_time = (new Date()).getTime();
  66. var start_time = (new Date($("#start_time").val())).getTime();
  67. var end_time = (new Date(value)).getTime();
  68. if (now_time > end_time) {
  69. return '结束时间不能小于当前时间!'
  70. }
  71. if (start_time > end_time) {
  72. return '结束时间不能小于开始时间!';
  73. }
  74. },
  75. num: function(value) {
  76. if (value == '') {
  77. return;
  78. }
  79. if (value < 0) {
  80. return '数字不能小于0!';
  81. }
  82. if (value * 100 % 1 != 0) {
  83. return '数字最多保留两位小数!';
  84. }
  85. },
  86. manjian_money: function(value){
  87. var type = $('[name="type"]:checked').val();
  88. if (type == 0) {
  89. if (!/[\S]+/.test(value)) {
  90. return '请输入金额';
  91. }
  92. if (value < 0) {
  93. return '金额不能小于0!';
  94. }
  95. }
  96. },
  97. manjian_num: function(value){
  98. var type = $('[name="type"]:checked').val();
  99. if (type == 1) {
  100. if (!/[\S]+/.test(value)) {
  101. return '请输入数量';
  102. }
  103. if (value < 0) {
  104. return '数量不能小于0!';
  105. }
  106. }
  107. },
  108. coupon_num: function(value){
  109. if (!/[\S]+/.test(value)) {
  110. return '请输入优惠券赠送数量';
  111. }
  112. if (value < 1) {
  113. return '优惠券赠送数量不能少于一张';
  114. }
  115. },
  116. goods_num:function (value) {
  117. var manjian_type = $('[name="manjian_type"]:checked').val();
  118. if(manjian_type == 2){
  119. if(value == ''){
  120. return '请选择活动商品!';
  121. }
  122. }
  123. }
  124. });
  125. /**
  126. * 监听提交
  127. */
  128. form.on('submit(save)', function(data) {
  129. var rule = {},
  130. verify = true,
  131. type = $('[name="type"]:checked').val();
  132. $('.manjian-rule .level-item').each(function(i, e) {
  133. var limit = parseFloat($(this).find('.type-'+ type +' .layui-input').val()).toFixed(2);
  134. if (i > 0) {
  135. var prevLimit = $('.manjian-rule .level-item:eq('+ (i - 1) +') .type-'+ type +' .layui-input').val();
  136. if (parseFloat(prevLimit) >= parseFloat(limit)) {
  137. showErrMsg('优惠门槛需大于上一级优惠门槛', $(this).find('.type-'+ type +' .layui-input'));
  138. verify = false;
  139. return false;
  140. }
  141. }
  142. rule[limit] = {
  143. limit: limit
  144. };
  145. if ($(this).find('[value="discount_money"]').is(':checked')) {
  146. var discount_money = $(this).find('.discount-item.discount-money .layui-input').val();
  147. if (!/[\S]+/.test(discount_money)) {
  148. showErrMsg('请输入优惠金额', $(this).find('.discount-item.discount-money .layui-input'));
  149. verify = false;
  150. return false;
  151. }
  152. discount_money = parseFloat(discount_money);
  153. if (discount_money <= 0) {
  154. showErrMsg('优惠金额不能小于等于0', $(this).find('.discount-item.discount-money .layui-input'));
  155. verify = false;
  156. return false;
  157. }
  158. if (type == 0 && discount_money > parseFloat(limit)) {
  159. showErrMsg('优惠金额请勿超出优惠门槛', $(this).find('.discount-item.discount-money .layui-input'));
  160. verify = false;
  161. return false;
  162. }
  163. rule[limit].discount_money = discount_money;
  164. }
  165. if ($(this).find('[value="free_shipping"]').is(':checked')) {
  166. rule[limit].free_shipping = 1;
  167. }
  168. if ($(this).find('[value="point"]').is(':checked')) {
  169. var point = $(this).find('.discount-item.point .layui-input').val();
  170. rule[limit].point = point;
  171. if (!/[\S]+/.test(point)) {
  172. showErrMsg('请输入赠送积分数', $(this).find('.discount-item.point .layui-input'));
  173. verify = false;
  174. return false;
  175. }
  176. if (point <= 0) {
  177. showErrMsg('赠送积分数不能小于等于0', $(this).find('.discount-item.point .layui-input'));
  178. verify = false;
  179. return false;
  180. }
  181. }
  182. var coupon = [];
  183. var coupon_num = [];
  184. if ($(this).find('[value="coupon"]').is(':checked')) {
  185. $(this).find('tr[data-coupon]').each(function (i, e) {
  186. coupon.push($(e).attr('data-coupon'));
  187. });
  188. $(this).find(".coupon input[name='number']").each(function(j,item){
  189. coupon_num.push(item.value);
  190. });
  191. if (!coupon.length) {
  192. showErrMsg('请选择要赠送的优惠券');
  193. verify = false;
  194. return false;
  195. }
  196. rule[limit].coupon = coupon.toString();
  197. rule[limit].coupon_num = coupon_num.toString();
  198. }
  199. // if ($(this).find('[value="coupon"]').is(':checked')) {
  200. // var coupon = $(this).find('.coupon tr[data-coupon]').each(function (i, e) {coupon.push($(e).attr('data-coupon'));})
  201. //
  202. // if (coupon == undefined) {
  203. // showErrMsg('请选择要赠送的优惠券');
  204. // verify = false;
  205. // return false;
  206. // }
  207. // rule[limit].coupon = coupon;
  208. // }
  209. if (rule[limit].discount_money == undefined && rule[limit].free_shipping == undefined && rule[limit].point == undefined && rule[limit].coupon == undefined) {
  210. showErrMsg('请选择活动层级'+ (i + 1) +'的优惠内容');
  211. verify = false;
  212. return false;
  213. }
  214. });
  215. if (!verify) return;
  216. data.field.rule_json = JSON.stringify(rule);
  217. if(data.field.manjian_type != 1){
  218. if(data.field.goods_ids == ''){
  219. layer.msg("请选择商品");
  220. return;
  221. }
  222. }
  223. if (repeat_flag) return;
  224. repeat_flag = true;
  225. $.ajax({
  226. type: 'POST',
  227. url: ns.url("manjian://shop/manjian/add"),
  228. data: data.field,
  229. dataType: 'JSON',
  230. success: function (res) {
  231. repeat_flag = false;
  232. if (res.code == 0) {
  233. layer.confirm('添加成功', {
  234. title:'操作提示',
  235. btn: ['返回列表', '继续添加'],
  236. closeBtn: 0,
  237. yes: function(){
  238. location.href = ns.url("manjian://shop/manjian/lists")
  239. },
  240. btn2: function() {
  241. location.href = ns.url("manjian://shop/manjian/add")
  242. }
  243. });
  244. } else if (res.code == -10077){
  245. var key = res.data.key;
  246. layer.confirm('在同一活动时间内,部分商品已参加其他的满减活动', {
  247. title:'活动冲突提醒',
  248. btn: ['取消', '查看详情'],
  249. closeBtn: 0,
  250. btn1: function(){
  251. location.href = ns.url("manjian://shop/manjian/add");
  252. },
  253. btn2: function() {
  254. location.href = ns.url("manjian://shop/manjian/conflict", {"key": key});
  255. }
  256. });
  257. } else {
  258. layer.msg(res.message);
  259. }
  260. }
  261. });
  262. });
  263. function showErrMsg(msg, e){
  264. layer.msg(msg, { icon: 5, shift: 6 });
  265. if (e != undefined) {
  266. $(e).focus();
  267. }
  268. }
  269. form.on('submit(coupon-search)', function(data) {
  270. data.field.status = 1;
  271. $.ajax({
  272. type: 'POST',
  273. dataType: 'JSON',
  274. url: ns.url("coupon://shop/coupon/lists"),
  275. data: data.field,
  276. success: function(res) {
  277. repeat_flag = false;
  278. if (res.code == 0) {
  279. coupon_list=res.data;
  280. laytpl($("#couponListsearch").html()).render(coupon_list, function (html) {
  281. $(".layui-layer-content").html(html);
  282. if ($("tbody tr input:checked").length == $(".coupon-box tbody tr").length) {
  283. $("input[lay-filter='selectAll']").prop("checked", true);
  284. }
  285. form.render();
  286. });
  287. }else{
  288. layer.msg(res.message);
  289. }
  290. }
  291. })
  292. })
  293. });
  294. // 表格渲染
  295. function renderTable(goods_list) {
  296. //展示已知数据
  297. table = new Table({
  298. elem: '#selected_goods_list',
  299. page: false,
  300. limit: Number.MAX_VALUE,
  301. cols: [
  302. [{
  303. field: 'goods_name',
  304. title: '商品名称',
  305. unresize: 'false',
  306. width: '50%',
  307. templet: function(data) {
  308. var html = '';
  309. html += `
  310. <div class="goods-title">
  311. <div class="goods-img">
  312. <img src="${data.goods_image ? ns.img(data.goods_image.split(",")[0],'small') : ''}" alt="">
  313. </div>
  314. <p class="multi-line-hiding goods-name">${data.goods_name}</p>
  315. </div>
  316. `;
  317. return html;
  318. }
  319. }, {
  320. field: 'price',
  321. title: '商品价格(元)',
  322. unresize: 'false',
  323. align: 'right',
  324. width: '20%',
  325. templet: function(data) {
  326. return '¥' + data.price;
  327. }
  328. }, {
  329. field: 'goods_stock',
  330. title: '库存',
  331. unresize: 'false',
  332. align: 'center',
  333. width: '20%'
  334. }, {
  335. title: '操作',
  336. toolbar: '#operation',
  337. unresize: 'false',
  338. align:'right'
  339. }],
  340. ],
  341. data: goods_list,
  342. });
  343. }
  344. // 删除选中商品
  345. function delGoods(id) {
  346. var i, j;
  347. $.each(goods_list, function(index, item) {
  348. var goods_id = item.goods_id;
  349. if (id == Number(goods_id)) {
  350. i = index;
  351. }
  352. });
  353. goods_list.splice(i, 1);
  354. renderTable(goods_list);
  355. $.each(selectedGoodsId, function(index, item) {
  356. if (id == Number(item)) {
  357. j = index;
  358. }
  359. });
  360. selectedGoodsId.splice(j, 1);
  361. goods_id = selectedGoodsId;
  362. $("#goods_num").html(selectedGoodsId.length);
  363. $("input[name='goods_ids']").val(goods_id.toString());
  364. }
  365. /* 商品 */
  366. function addGoods(){
  367. goodsSelect(function (res) {
  368. if (!res.length) return false;
  369. goods_id = [];
  370. goods_list = [];
  371. for(var i=0;i<res.length;i++) {
  372. goods_id.push(res[i].goods_id);
  373. goods_list.push(res[i]);
  374. }
  375. renderTable(goods_list);
  376. $("input[name='goods_ids']").val(goods_id.toString());
  377. selectedGoodsId = goods_id;
  378. $("#goods_num").html(selectedGoodsId.length)
  379. }, selectedGoodsId, {mode: "spu"});
  380. goods_list.splice(0,goods_list.length);
  381. }
  382. function back() {
  383. location.href = ns.url("manjian://shop/manjian/lists");
  384. }
  385. // 添加优惠层级
  386. function addDiscountLevel(){
  387. var type = $('[name="type"]:checked').val();
  388. length = $('.discount-level .level-item').length;
  389. if (length == 5) {
  390. layer.msg('最多支持五个活动层级');
  391. return;
  392. }
  393. var template = `<div class="level-item">
  394. <div class="level-head">
  395. <label class="title">活动层级{{ d.length + 1 }}:</label>
  396. <a href="javascript:;" class="text-color" onclick="deleteLevel(this)">删除</a>
  397. </div>
  398. <div class="wrap">
  399. <div class="condition">
  400. <label class="layui-form-label"><span class="required">*</span>优惠门槛:</label>
  401. <div class="layui-input-block">
  402. <div class="type-0 {{ d.type != 0 ? 'layui-hide' : '' }}">
  403. <div class="layui-form-mid">满</div>
  404. <div class="layui-input-inline len-short">
  405. <input type="number" name="money" value="" lay-verify="manjian_money" placeholder="" autocomplete="off" class="layui-input len-short">
  406. </div>
  407. <div class="layui-form-mid">元</div>
  408. </div>
  409. <div class="type-1 {{ d.type != 1 ? 'layui-hide' : '' }}">
  410. <div class="layui-form-mid">满</div>
  411. <div class="layui-input-inline len-short">
  412. <input type="number" name="num" value="" lay-verify="manjian_num" placeholder="" autocomplete="off" class="layui-input len-short">
  413. </div>
  414. <div class="layui-form-mid">件</div>
  415. </div>
  416. </div>
  417. </div>
  418. <div class="content">
  419. <label class="layui-form-label"><span class="required">*</span>优惠内容:</label>
  420. <div class="layui-input-block">
  421. <div class="discount-item discount-money">
  422. <div>
  423. <input type="checkbox" name="discount_type" value="discount_money" class="input-checkbox" lay-skin="primary"><span>订单金额优惠</span>
  424. </div>
  425. <div class="discount-cont layui-hide">
  426. <div class="layui-form-mid">减</div>
  427. <div class="layui-input-inline len-short">
  428. <input type="number" value="" placeholder="" autocomplete="off" class="layui-input len-short">
  429. </div>
  430. <div class="layui-form-mid">元</div>
  431. </div>
  432. </div>
  433. <div class="discount-item">
  434. <div>
  435. <input type="checkbox" name="discount_type" value="free_shipping" class="input-checkbox" lay-skin="primary"><span>包邮</span>
  436. </div>
  437. <div class="word-aux" style="margin-left: 28px;margin-top: 0">
  438. <p>仅参与该活动的商品包邮,非整单包邮</p>
  439. </div>
  440. </div>
  441. <div class="discount-item point">
  442. <div>
  443. <input type="checkbox" name="discount_type" value="point" class="input-checkbox" lay-skin="primary"><span>送积分</span>
  444. </div>
  445. <div class="discount-cont layui-hide">
  446. <div class="layui-form-mid">送</div>
  447. <div class="layui-input-inline len-short">
  448. <input type="number" name="" value="" placeholder="" autocomplete="off" class="layui-input len-short">
  449. </div>
  450. <div class="layui-form-mid">积分</div>
  451. </div>
  452. </div>
  453. <div class="discount-item coupon">
  454. <div>
  455. <input type="checkbox" name="discount_type" value="coupon" class="input-checkbox" lay-skin="primary"><span>送优惠券</span>
  456. </div>
  457. <div class="discount-cont layui-hide">
  458. <div><a href="javascript:;" class="text-color select-coupon">选择优惠券</a></div>
  459. <div class="word-aux">
  460. <p>请确认优惠券数量是否充足,优惠券数量不足将导致赠送失败</p>
  461. </div>
  462. <div>
  463. <table class="layui-table" lay-skin="nob">
  464. <colgroup>
  465. <col width="30%">
  466. <col width="30%">
  467. <col width="20%">
  468. <col width="20%">
  469. </colgroup>
  470. <thead>
  471. <tr>
  472. <th>优惠券</th>
  473. <th>优惠内容</th>
  474. <th>赠券数</th>
  475. <th style="text-align:center;">操作</th>
  476. </tr>
  477. </thead>
  478. <tbody></tbody>
  479. </table>
  480. </div>
  481. </div>
  482. </div>
  483. </div>
  484. </div>
  485. </div>
  486. </div>`;
  487. laytpl(template).render({
  488. length: length,
  489. type: type
  490. }, function(string){
  491. $('.discount-level .level-item:last').after(string);
  492. form.render();
  493. });
  494. }
  495. // 选择优惠
  496. $('body').on('click', '.discount-item .layui-form-checkbox', function(e){
  497. if ($(this).prev('[name="discount_type"]').is(':checked')) {
  498. $(this).parents('.discount-item').find('.discount-cont').removeClass('layui-hide');
  499. } else {
  500. $(this).parents('.discount-item').find('.discount-cont').addClass('layui-hide');
  501. }
  502. });
  503. $('body').on('click', '.discount-item .select-coupon', function(e){
  504. currIndex = $(this).parents('.level-item').index();
  505. var data = {};
  506. data.coupon_id = coupon_id[currIndex] != undefined ? coupon_id[currIndex] : [];
  507. laytpl($("#couponList").html()).render(data, function(html) {
  508. coupon_list = layer.open({
  509. title: '优惠券列表',
  510. skin: 'layer-tips-class',
  511. type: 1,
  512. area: ['850px', '600px'],
  513. content: html,
  514. });
  515. if ($("tbody tr input:checked").length == $(".coupon-box tbody tr").length) {
  516. $("input[lay-filter='selectAll']").prop("checked", true);
  517. }
  518. form.render();
  519. });
  520. /**
  521. * 监听全选按钮
  522. */
  523. form.on('checkbox(selectAll)', function(data) {
  524. if (data.elem.checked) {
  525. $("tr .check-box input:checkbox").each(function(index) {
  526. $(this).prop("checked", true);
  527. });
  528. } else {
  529. $("tr .check-box input:checkbox").each(function() {
  530. $(this).prop("checked", false);
  531. });
  532. }
  533. form.render();
  534. });
  535. /**
  536. * 监听每一行的多选按钮
  537. */
  538. var len = $(".coupon-box tbody tr").length;
  539. for (var i = 0; i < len; i++) {
  540. form.on('checkbox(select' + i + ')', function(data) {
  541. if ($("tbody tr input:checked").length == len) {
  542. $("input[lay-filter='selectAll']").prop("checked", true);
  543. } else {
  544. $("input[lay-filter='selectAll']").prop("checked", false);
  545. }
  546. form.render();
  547. });
  548. }
  549. });
  550. function couponSelected() {
  551. if (!$("#goods tbody tr input:checked").length) {
  552. layer.msg('请选择优惠券');
  553. return;
  554. }
  555. layer.closeAll('page');
  556. coupon_id[currIndex] = [];
  557. $("#coupon_selected tbody").empty();
  558. var data = [];
  559. $("#goods tr input:checked").each(function(){
  560. var tr = $(this).parents('tr');
  561. coupon_id[currIndex].push(tr.find("#coupon_id").val());
  562. data.push({
  563. coupon_type_id: tr.find("#coupon_id").val(),
  564. coupon_name: tr.find(".title-content p").text(),
  565. at_least: tr.find('[name="at_least"]').val(),
  566. type: tr.find('[name="type"]').val(),
  567. discount: tr.find('[name="discount"]').val(),
  568. money: tr.find('[name="money"]').val()
  569. })
  570. });
  571. laytpl($("#addCoupon").html()).render(data, function(string){
  572. $('.level-item:eq('+ currIndex +') .discount-cont tbody').html(string);
  573. layer.closeAll();
  574. });
  575. }
  576. // 删除优惠层级
  577. function deleteLevel(e){
  578. $(e).parents('.level-item').remove();
  579. }
  580. // 删除优惠券
  581. function deleteCoupon(e,index){
  582. index = $(e).parents('.level-item').index();
  583. coupon_id[index].splice(index, 1);
  584. $(e).parents('tr').remove();
  585. }