viprecharge.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
  2. var max_goods_count = 6;
  3. $(document).on('click', '.js_add_goods', function () {
  4. selectedIds = getSelectedIds();
  5. if (selectedIds.length >= max_goods_count) {
  6. Layer.alert('最多可以选择' + max_goods_count + '个商品');
  7. return false;
  8. }
  9. _goodsSelectUrl = getGoodsSelectUrl();
  10. Fast.api.open(_goodsSelectUrl, '选择商品', {
  11. callback: function (data) {
  12. createGoodsItem(data[0]);
  13. }
  14. })
  15. });
  16. $(document).on('click', '.js_edit', function (e) {
  17. $editBtn = $(this);
  18. _goodsSelectUrl = getGoodsSelectUrl();
  19. Fast.api.open(_goodsSelectUrl, '选择商品', {
  20. callback: function (data) {
  21. var goodsItem = data[0];
  22. htmlstr = formatGoodsItem(goodsItem);
  23. $editBtn.parents('.js_goods_item').html(htmlstr);
  24. }
  25. })
  26. });
  27. $(document).on('click', '.js_del', function () {
  28. $delBtn = $(this);
  29. layer.confirm('您确认移除商品吗?', {title: "移除商品"}, function (index) {
  30. layer.close(index);
  31. $delBtn.parents('.js_goods_item').remove();
  32. });
  33. });
  34. $(document).on('click', '.js_custom_goods_submit', function () {
  35. var data = $('.js_form_custom_goods').serialize();
  36. $.ajax({
  37. url: '/admin/viprecharge/index',
  38. data: data,
  39. type: 'POST',
  40. timeout: 35000,
  41. success: function (data) {
  42. if (data.code == 1) {//成功
  43. Toastr.success(data.msg);
  44. } else {
  45. Toastr.error(data.msg);
  46. }
  47. },
  48. error: function (XMLHttpRequest, textStatus, errorThrown) {
  49. Toastr.error(XMLHttpRequest.message);
  50. },
  51. complete: function () {
  52. }
  53. });
  54. });
  55. $(document).on('click','.js_custom_goods_reset',function () {
  56. $.ajax({
  57. url: '/admin/viprecharge/reset',
  58. data: null,
  59. type: 'POST',
  60. timeout: 35000,
  61. success: function (data) {
  62. if (data.code == 1) {//成功
  63. Toastr.success(data.msg);
  64. $('.js_goods_item').each(function (i, e) {
  65. var cc = $(e).hasClass('js_add_goods_item');
  66. if (!cc) {
  67. e.remove();
  68. }
  69. });
  70. initGoodsList(data.data);
  71. } else {
  72. Toastr.error(data.msg);
  73. }
  74. },
  75. error: function (XMLHttpRequest, textStatus, errorThrown) {
  76. Toastr.error(XMLHttpRequest.message);
  77. },
  78. complete: function () {
  79. }
  80. });
  81. });
  82. //已充值/未充值tab切换
  83. $(document).on('click','#goods-list-tabs li',function () {
  84. //清除原有的充值列表节点
  85. $('.js_goods_list li').each(function () {
  86. if(!$(this).hasClass('js_add_goods_item')){
  87. $(this).remove();
  88. }
  89. });
  90. customGoodsListStr = Config.customGoodsList;
  91. customGoodsList = JSON.parse(customGoodsListStr);
  92. initGoodsList(customGoodsList);
  93. });
  94. $(document).on('click','.js_custom_goods_sync',function () {
  95. $.ajax({
  96. url: '/admin/vip/admin/recharge/syncvipRecharge',
  97. type: 'POST',
  98. timeout: 35000,
  99. success: function (data) {
  100. if (data.code == 0) {//成功
  101. layer.alert('同步成功');
  102. } else {
  103. layer.alert('同步失败');
  104. }
  105. },
  106. error: function (XMLHttpRequest, textStatus, errorThrown) {
  107. Toastr.error(XMLHttpRequest.message);
  108. },
  109. complete: function () {
  110. }
  111. });
  112. })
  113. /**
  114. * 获取已选中的商品id
  115. * @returns {Array}
  116. */
  117. function getSelectedIds() {
  118. // formValueList = $('form').serializeArray();
  119. // selectedIds = [];
  120. // for (var i = 0; i < formValueList.length; i++) {
  121. // var item = formValueList[i];
  122. // if (item.name == 'row[goods_id][]') {
  123. // selectedIds.push(item.value);
  124. // }
  125. // }
  126. selectedIds = [];
  127. $('form li').each(function (i) {
  128. if(!$(this).hasClass('hide') && $(this).find('input').val()){
  129. selectedIds.push($(this).find('input').val());
  130. }
  131. });
  132. console.log(selectedIds)
  133. return selectedIds;
  134. }
  135. /**
  136. * 获取商品选择框的加载列表的API地址
  137. * @returns {string}
  138. */
  139. function getGoodsSelectUrl() {
  140. selectedIds = getSelectedIds();
  141. var show_type = getShowTypeValue();
  142. goodsSelectUrl = 'goods/select?business_line=0&recharge_show_type='+show_type;
  143. if (selectedIds.length > 0) {
  144. goodsSelectUrl += '&selected_ids=' + selectedIds.join('_');
  145. }
  146. return goodsSelectUrl;
  147. }
  148. /**
  149. * 初始化商品列表
  150. * @param list
  151. */
  152. function initGoodsList(list) {
  153. var data = {'row':{'fun':'get_custom_goods_list'}};
  154. $.ajax({
  155. url: '/admin/viprecharge/index',
  156. data: data,
  157. type: 'POST',
  158. timeout: 35000,
  159. success: function (data) {
  160. if (data.code == 1) {//成功
  161. var goodsList = data.data;
  162. for (i = 0; i < goodsList.length; i++) {
  163. if (goodsList[i].business_line == '0') {
  164. createGoodsItem(goodsList[i]);
  165. }
  166. }
  167. } else {
  168. Toastr.error(data.msg);
  169. }
  170. },
  171. error: function (XMLHttpRequest, textStatus, errorThrown) {
  172. Toastr.error(XMLHttpRequest.message);
  173. },
  174. complete: function () {
  175. }
  176. });
  177. }
  178. /**
  179. * 格式化商品单元格
  180. * @param goodsItem 商品对象
  181. * @returns {string}
  182. */
  183. function formatGoodsItem(goodsItem,hide) {
  184. var checked = goodsItem.default == '1' ? 'checked' : '';
  185. var htmlstr =
  186. '<div class="recl_info js_edit" '+hide+'>' +
  187. '<div class="recl_box">' +
  188. '<div class="recl_mark">' + goodsItem.icon + '</div>' +
  189. '<div class="recl_tit">' + goodsItem.show_type_text + '</div>' +
  190. '<div class="recl_money">' + goodsItem.title + '</div>' +
  191. '<div class="recl_des">' +
  192. '<strong>' + goodsItem.first_description + '</strong>' +
  193. '<span>' + goodsItem.second_description + '</span>' +
  194. '</div></div><input type="hidden" name="row[goods_id][]" value="' + goodsItem.id + '"></div> ' +
  195. '<div class="recl_operate">' +
  196. '<span style="float: left;line-height: 30px;"><input type="radio" name="row[default]" value="'+goodsItem.id+'" '+checked+'>用户默认勾选</span>'+
  197. '<a href="javascript:;" class="btn btn-xs btn-danger btn-delone js_del" title=""' +
  198. 'data-table-id="table" data-field-index="4" data-row-index="0"' +
  199. 'data-button-index="2"><i class="fa fa-trash"></i></a>' +
  200. '<a href="javascript:;" class="btn btn-xs btn-success js_move" title=""' +
  201. 'data-table-id="table" data-field-index="4" data-row-index="0"' +
  202. 'data-button-index="2"><i class="fa fa-arrows"></i></a>' +
  203. '</div>'
  204. ;
  205. return htmlstr;
  206. }
  207. /**
  208. * 创建商品
  209. * @param goodsItem
  210. */
  211. function createGoodsItem(goodsItem) {
  212. var show_type = getShowTypeValue();
  213. //已充值用户(全部用户/已充值用户/新用户/老用户)
  214. if(show_type == -1 && goodsItem.show_type == 2){
  215. htmlstr = formatGoodsItem(goodsItem,'hidden');
  216. htmlstr = '<li class="js_goods_item hide">' + htmlstr + '</li>';
  217. $('.js_add_goods_item').before(htmlstr);
  218. return;
  219. }
  220. //未充值用户(全部用户/未充值用户/新用户/老用户)
  221. else if(show_type == -2 && goodsItem.show_type == 1){
  222. htmlstr = formatGoodsItem(goodsItem,'hidden');
  223. htmlstr = '<li class="js_goods_item hide">' + htmlstr + '</li>';
  224. $('.js_add_goods_item').before(htmlstr);
  225. return;
  226. }
  227. else{
  228. htmlstr = formatGoodsItem(goodsItem,'');
  229. htmlstr = '<li class="js_goods_item">' + htmlstr + '</li>';
  230. $('.js_add_goods_item').before(htmlstr);
  231. return;
  232. }
  233. }
  234. /**
  235. * 获取当前tab标签选中value
  236. * @return (-1:已充值用户/-2:未充值用户)
  237. */
  238. function getShowTypeValue(){
  239. var show_type = -1;
  240. $("#goods-list-tabs li").each(function (obj) {
  241. if($(this).hasClass("active")){
  242. show_type = $(this).attr("data-value");
  243. return false;
  244. }
  245. });
  246. return show_type;
  247. }
  248. $(document).delegate('.js_move', 'click', function () {
  249. $(this).closest('.js_goods_item').addClass('js_goods_border');
  250. if ($('.js_goods_border').length == 2) {
  251. var list = $('.js_goods_item').toArray();
  252. var from = $('.js_goods_border').eq(0).index();
  253. var to = $('.js_goods_border').eq(1).index();
  254. var vfrom = list[from]
  255. var vto = list[to]
  256. list.splice(from,1,vto);
  257. list.splice(to,1,vfrom);
  258. $('.js_goods_list').html(list);
  259. $('.js_goods_border').removeClass('js_goods_border');
  260. }
  261. })
  262. // $(document).delegate('.js_goods_item', 'drop', function(event){
  263. // var list = $('.js_goods_item').toArray();
  264. // var to = $(event.currentTarget).index();
  265. // var from = event.originalEvent.dataTransfer.getData("index");
  266. // var vfrom = list[from]
  267. // var vto = list[to]
  268. // list.splice(from,1,vto);
  269. // list.splice(to,1,vfrom);
  270. // $('.js_goods_list').html(list);
  271. // })
  272. customGoodsListStr = Config.customGoodsList;
  273. customGoodsList = JSON.parse(customGoodsListStr);
  274. initGoodsList(customGoodsList);
  275. var Controller = {
  276. index: function () {
  277. }
  278. };
  279. return Controller;
  280. });