edit.js 16 KB

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