recharge.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. data += '&group_ids='+getGroupList();
  37. $.ajax({
  38. url: '/admin/vip/admin/recharge/index',
  39. data: data,
  40. type: 'POST',
  41. timeout: 35000,
  42. success: function (data) {
  43. if (data.code == 1) {//成功
  44. Toastr.success(data.msg);
  45. } else {
  46. Toastr.error(data.msg);
  47. }
  48. },
  49. error: function (XMLHttpRequest, textStatus, errorThrown) {
  50. Toastr.error(XMLHttpRequest.message);
  51. },
  52. complete: function () {
  53. }
  54. });
  55. });
  56. $(document).on('click','.js_custom_goods_copy',function () {
  57. $.ajax({
  58. url: '/admin/vip/admin/recharge/copyGoods',
  59. data: null,
  60. type: 'POST',
  61. timeout: 35000,
  62. success:function(data){
  63. if (data.code == 1) {//成功
  64. Toastr.success(data.msg);
  65. } else {
  66. Toastr.error(data.msg);
  67. }
  68. }
  69. });
  70. })
  71. $(document).on('change','.copy-type',function () {
  72. var group_type = $(this).val();
  73. switch (group_type) {
  74. case "1":
  75. $('.group-content').hide();
  76. break;
  77. case "2":
  78. $('.group-content').show();
  79. break;
  80. default:
  81. break;
  82. }
  83. })
  84. $(document).on('click','.group-btn',function () {
  85. var groupIds = '';
  86. groupIds = getGroupList();
  87. var url = '/admin/vip/admin/group/select?not_in_ids='+groupIds;
  88. Fast.api.open(url,'选择分组',{callback:function (data) {
  89. var str = '';
  90. for (var i in data) {
  91. str +='<tr class="group-list" data-id="'+data[i]['id']+'"><td>'+data[i]['id']+'</td><td>'+data[i]['name']+'</td><td>'+data[i]['create_time']+'</td> <td>'+data[i]['update_time']+'</td> <td class="group-del">删除</td> </tr>';
  92. }
  93. $('.group-content table').append(str);
  94. }});
  95. });
  96. $(document).on('click','.group-del',function(){
  97. $(this).parent().remove();
  98. });
  99. $(document).on('click','.js_custom_goods_reset',function () {
  100. $.ajax({
  101. url: '/admin/vip/admin/recharge/reset',
  102. data: null,
  103. type: 'POST',
  104. timeout: 35000,
  105. success: function (data) {
  106. if (data.code == 1) {//成功
  107. Toastr.success(data.msg);
  108. $('.js_goods_item').each(function (i, e) {
  109. var cc = $(e).hasClass('js_add_goods_item');
  110. if (!cc) {
  111. e.remove();
  112. }
  113. });
  114. initGoodsList(data.data);
  115. } else {
  116. Toastr.error(data.msg);
  117. }
  118. },
  119. error: function (XMLHttpRequest, textStatus, errorThrown) {
  120. Toastr.error(XMLHttpRequest.message);
  121. },
  122. complete: function () {
  123. }
  124. });
  125. });
  126. //已充值/未充值tab切换
  127. $(document).on('click','#goods-list-tabs li',function () {
  128. //清除原有的充值列表节点
  129. $('.js_goods_list li').each(function () {
  130. if(!$(this).hasClass('js_add_goods_item')){
  131. $(this).remove();
  132. }
  133. });
  134. customGoodsListStr = Config.customGoodsList;
  135. customGoodsList = JSON.parse(customGoodsListStr);
  136. initGoodsList(customGoodsList);
  137. });
  138. /**
  139. * 获取关联的用户分组IDS
  140. * @returns {string}
  141. */
  142. function getGroupList() {
  143. var groupIds = '';
  144. var obj = $('.group-content table tr.group-list');
  145. obj.each(function (index,value) {
  146. groupIds += value.dataset.id+',';
  147. })
  148. return groupIds;
  149. }
  150. /**
  151. * 获取已选中的商品id
  152. * @returns {Array}
  153. */
  154. function getSelectedIds() {
  155. // formValueList = $('form').serializeArray();
  156. // selectedIds = [];
  157. // for (var i = 0; i < formValueList.length; i++) {
  158. // var item = formValueList[i];
  159. // if (item.name == 'row[goods_id][]') {
  160. // selectedIds.push(item.value);
  161. // }
  162. // }
  163. selectedIds = [];
  164. $('form li').each(function (i) {
  165. if(!$(this).hasClass('hide') && $(this).find('input').val()){
  166. selectedIds.push($(this).find('input').val());
  167. }
  168. });
  169. return selectedIds;
  170. }
  171. /**
  172. * 获取商品选择框的加载列表的API地址
  173. * @returns {string}
  174. */
  175. function getGoodsSelectUrl() {
  176. selectedIds = getSelectedIds();
  177. var show_type = getShowTypeValue();
  178. goodsSelectUrl = 'goods/select?business_line=0&recharge_show_type='+show_type;
  179. if (selectedIds.length > 0) {
  180. goodsSelectUrl += '&selected_ids=' + selectedIds.join('_');
  181. }
  182. return goodsSelectUrl;
  183. }
  184. /**
  185. * 初始化商品列表
  186. * @param list
  187. */
  188. function initGoodsList(list) {
  189. var data = {'row':{'fun':'get_custom_goods_list'}};
  190. $.ajax({
  191. url: '/admin/vip/admin/recharge/index',
  192. data: data,
  193. type: 'POST',
  194. timeout: 35000,
  195. success: function (data) {
  196. if (data.code == 1) {//成功
  197. var goodsList = data.data;
  198. console.log(goodsList);
  199. console.log(goodsList.length);
  200. $.each(goodsList,function (i,n) {
  201. createGoodsItem(n);
  202. })
  203. // for (i = 0; i < goodsList.length; i++) {
  204. // console.log(goodsList[i]);
  205. // if (goodsList[i].business_line == '0') {
  206. // console.log(1);
  207. // createGoodsItem(goodsList[i]);
  208. // }
  209. // }
  210. } else {
  211. Toastr.error(data.msg);
  212. }
  213. },
  214. error: function (XMLHttpRequest, textStatus, errorThrown) {
  215. Toastr.error(XMLHttpRequest.message);
  216. },
  217. complete: function () {
  218. }
  219. });
  220. }
  221. /**
  222. * 格式化商品单元格
  223. * @param goodsItem 商品对象
  224. * @returns {string}
  225. */
  226. function formatGoodsItem(goodsItem,hide) {
  227. var checked = goodsItem.default == '1' ? 'checked' : '';
  228. var htmlstr =
  229. '<div class="recl_info js_edit" '+hide+'>' +
  230. '<div class="recl_box">' +
  231. '<div class="recl_mark">' + goodsItem.icon + '</div>' +
  232. '<div class="recl_tit">' + goodsItem.show_type_text + '</div>' +
  233. '<div class="recl_money">' + goodsItem.title + '</div>' +
  234. '<div class="recl_des">' +
  235. '<strong>' + goodsItem.first_description + '</strong>' +
  236. '<span>' + goodsItem.second_description + '</span>' +
  237. '</div></div><input type="hidden" name="row[goods_id][]" value="' + goodsItem.id + '"></div> ' +
  238. '<div class="recl_operate">' +
  239. '<span style="float: left;line-height: 30px;"><input type="radio" name="row[default]" value="'+goodsItem.id+'" '+checked+'>用户默认勾选</span>'+
  240. '<a href="javascript:;" class="btn btn-xs btn-danger btn-delone js_del" title=""' +
  241. 'data-table-id="table" data-field-index="4" data-row-index="0"' +
  242. 'data-button-index="2"><i class="fa fa-trash"></i></a>' +
  243. '<a href="javascript:;" class="btn btn-xs btn-success js_move" title=""' +
  244. 'data-table-id="table" data-field-index="4" data-row-index="0"' +
  245. 'data-button-index="2"><i class="fa fa-arrows"></i></a>' +
  246. '</div>'
  247. ;
  248. return htmlstr;
  249. }
  250. /**
  251. * 创建商品
  252. * @param goodsItem
  253. */
  254. function createGoodsItem(goodsItem) {
  255. var show_type = getShowTypeValue();
  256. console.log(goodsItem);
  257. //已充值用户(全部用户/已充值用户/新用户/老用户)
  258. if(show_type == -1 && goodsItem.show_type == 2){
  259. htmlstr = formatGoodsItem(goodsItem,'hidden');
  260. htmlstr = '<li class="js_goods_item hide">' + htmlstr + '</li>';
  261. $('.js_add_goods_item').before(htmlstr);
  262. return;
  263. }
  264. //未充值用户(全部用户/未充值用户/新用户/老用户)
  265. else if(show_type == -2 && goodsItem.show_type == 1){
  266. htmlstr = formatGoodsItem(goodsItem,'hidden');
  267. htmlstr = '<li class="js_goods_item hide">' + htmlstr + '</li>';
  268. $('.js_add_goods_item').before(htmlstr);
  269. return;
  270. }
  271. else{
  272. htmlstr = formatGoodsItem(goodsItem,'');
  273. htmlstr = '<li class="js_goods_item">' + htmlstr + '</li>';
  274. $('.js_add_goods_item').before(htmlstr);
  275. return;
  276. }
  277. }
  278. /**
  279. * 获取当前tab标签选中value
  280. * @return (-1:已充值用户/-2:未充值用户)
  281. */
  282. function getShowTypeValue(){
  283. var show_type = -1;
  284. $("#goods-list-tabs li").each(function (obj) {
  285. if($(this).hasClass("active")){
  286. show_type = $(this).attr("data-value");
  287. return false;
  288. }
  289. });
  290. return show_type;
  291. }
  292. $(document).delegate('.js_move', 'click', function () {
  293. $(this).closest('.js_goods_item').addClass('js_goods_border');
  294. if ($('.js_goods_border').length == 2) {
  295. var list = $('.js_goods_item').toArray();
  296. var from = $('.js_goods_border').eq(0).index();
  297. var to = $('.js_goods_border').eq(1).index();
  298. var vfrom = list[from]
  299. var vto = list[to]
  300. list.splice(from,1,vto);
  301. list.splice(to,1,vfrom);
  302. $('.js_goods_list').html(list);
  303. $('.js_goods_border').removeClass('js_goods_border');
  304. }
  305. })
  306. // $(document).delegate('.js_goods_item', 'drop', function(event){
  307. // var list = $('.js_goods_item').toArray();
  308. // var to = $(event.currentTarget).index();
  309. // var from = event.originalEvent.dataTransfer.getData("index");
  310. // var vfrom = list[from]
  311. // var vto = list[to]
  312. // list.splice(from,1,vto);
  313. // list.splice(to,1,vfrom);
  314. // $('.js_goods_list').html(list);
  315. // })
  316. customGoodsListStr = Config.customGoodsList;
  317. customGoodsList = JSON.parse(customGoodsListStr);
  318. console.log(customGoodsList);
  319. initGoodsList(customGoodsList);
  320. var Controller = {
  321. index: function () {
  322. }
  323. };
  324. return Controller;
  325. });