member_cluster.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. var surplus_coupon = [], // 剩余数据
  2. selected_coupon = [], // 被选中数据
  3. selected_coupon_id = [], // 被选中id
  4. temp_coupon = [], //临时数据
  5. clusterId = '';
  6. function renderCoupon(couponIds, cluster_id) {
  7. var couponIds_arr = [];
  8. if (couponIds) {
  9. couponIds_arr = couponIds.split(",");
  10. }
  11. selected_coupon_id = couponIds_arr;
  12. clusterId = cluster_id;
  13. $.ajax({
  14. type: "POST",
  15. data: {
  16. page_size: 0,
  17. status: 1
  18. },
  19. url: ns.url('coupon://shop/coupon/lists'),
  20. dataType: 'JSON',
  21. success: function(data) {
  22. surplus_coupon = get_data_by_ids(data.data.list, 'all', couponIds).all; // 得到剩余的优惠券数据
  23. selected_coupon = get_data_by_ids(data.data.list, 'selected', couponIds).selected;
  24. compile_new_data();
  25. }
  26. })
  27. }
  28. /**
  29. * 获取优惠券数据
  30. */
  31. function get_data_by_ids(data, obj, ids) {
  32. var id_arr = [], temp = {};
  33. if (ids) {
  34. var arr = ids.split(",");
  35. $.each(arr, function(i, id) {
  36. id = parseInt(id);
  37. id_arr.push(id);
  38. })
  39. }
  40. var temp_all = [], temp_selected = [];
  41. $.each(data, function(index, item) {
  42. if (id_arr.length > 0) {
  43. var index = id_arr.indexOf(item.coupon_type_id);
  44. if (index == -1) {
  45. temp_all.push(item);
  46. } else {
  47. temp_selected.push(item);
  48. }
  49. } else {
  50. temp_all.push(item);
  51. }
  52. temp.all = temp_all;
  53. temp.selected = temp_selected;
  54. })
  55. return temp;
  56. }
  57. /**
  58. * 渲染列表数据
  59. */
  60. function compile_new_data() {
  61. var surplus_html = compile_list(surplus_coupon, 'all');
  62. $(".coupon-modal .all-coupon .box").html(surplus_html);
  63. var selected_html = compile_list(selected_coupon, 'selected');
  64. $(".coupon-modal .selected-coupon .box").html(selected_html);
  65. }
  66. /**
  67. * 渲染数据
  68. */
  69. function compile_list(temp_list, obj) {
  70. var html = '<ul>';
  71. $.each(temp_list, function(index, item) {
  72. var selected_html = obj == 'selected' ? 'selected' : '';
  73. html += '<li class="' + selected_html + '" data-selected="' + index + '" data-id="' + item.coupon_type_id + '">';
  74. html += '<div class="coupon-box">';
  75. if (item.type == 'reward') {
  76. html += '<div class="coupon-money">¥' + item.money + '</div>';
  77. } else {
  78. html += '<div class="coupon-money">' + item.discount + '折</div>';
  79. }
  80. html += '<div class="coupon-name">' + item.coupon_name + '</div>';
  81. if (item.validity_type == 0) {
  82. html += '<div class="coupon-time">失效日期:' + ns.time_to_date(item.end_time) + '</div>';
  83. } else {
  84. html += '<div class="coupon-time">领取后,' + item.fixed_term + '天有效</div>';
  85. }
  86. if (obj == 'selected') {
  87. html += `<div class="give-num">
  88. <span>发放数量</span>
  89. <input type="number" class="layui-input len-short num" value="1" max="99">
  90. </div>`
  91. }
  92. html += '</div>';
  93. if (obj == 'selected') {
  94. html += '<div class="coupon-delete">×</div>'
  95. }
  96. html += '</li>';
  97. })
  98. html += '</ul>';
  99. return html;
  100. }
  101. /**
  102. * 选中与取消数据 先改变数据 再重新渲染
  103. */
  104. temp_coupon = [];
  105. $("body").on('click', '.layui-layer .coupon-list.all-coupon ul li .coupon-box', function() {
  106. var li = $(this).parent();
  107. if ($(this).hasClass("left-selected")) {
  108. $(this).removeClass("left-selected");
  109. var index = '';
  110. $.each(temp_coupon, function(i, item) {
  111. if (item == li.attr('data-id')) {
  112. index = i;
  113. }
  114. })
  115. temp_coupon.splice(index, 1)
  116. } else {
  117. $(this).addClass("left-selected");
  118. temp_coupon.push(li.attr('data-id'))
  119. }
  120. });
  121. // 添加优惠券
  122. $("body").on('click', '.layui-layer .coupon-modal .add', function() {
  123. var ids = [];
  124. ids = selected_coupon_id.concat(temp_coupon);
  125. temp_coupon = [];
  126. renderCoupon(ids.toString(), clusterId);
  127. // compile_new_data();
  128. });
  129. // 删除优惠券
  130. $("body").on('click', '.layui-layer .coupon-modal .coupon-delete', function() {
  131. var id = $(this).parents("li").attr("data-id");
  132. var ind = '';
  133. $.each(selected_coupon_id, function(index, item) {
  134. if (id == parseInt(item)) {
  135. ind = index;
  136. }
  137. })
  138. selected_coupon_id.splice(ind, 1);
  139. var ids = [];
  140. ids = selected_coupon_id;
  141. renderCoupon(ids.toString(), clusterId);
  142. });
  143. /**
  144. * 保存操作
  145. */
  146. var isRepeat = false;
  147. $("body").on("click", ".modal-operation .save-btn", function () {
  148. var coupon_data = [], isReturn = false;
  149. $('.coupon-modal .selected-coupon .selected').each(function () {
  150. var num = $(this).find('.num').val();
  151. if (!/[\S]+/.test(num)) {
  152. layer.msg('请输入发放数量'); isReturn = true;
  153. return false;
  154. }
  155. if (num%1 != 0) {
  156. layer.msg('发放数量格式错误'); isReturn = true;
  157. return false;
  158. }
  159. if (num <= 0) {
  160. layer.msg('发放数量不能小于等于0'); isReturn = true;
  161. return false;
  162. }
  163. coupon_data.push({
  164. coupon_type_id: $(this).attr('data-id'),
  165. num: num
  166. })
  167. })
  168. if (isReturn || isRepeat) return;
  169. if (!coupon_data.length) {
  170. layer.msg('请选择要发放的优惠券');
  171. return;
  172. }
  173. isRepeat = true;
  174. $.ajax({
  175. type: "POST",
  176. data: {
  177. cluster_id : clusterId,
  178. coupon_data: JSON.stringify(coupon_data)
  179. },
  180. url: ns.url('shop/membercluster/sendCoupon'),
  181. dataType: 'JSON',
  182. success: function(res) {
  183. isRepeat = false;
  184. layer.close(layer_coupon);
  185. layer.msg(res.message);
  186. }
  187. })
  188. });