goods_selector.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. define(["biz"], function(biz) {
  2. model = {};
  3. model.multi = false;
  4. model.callback = '';
  5. model.ele = {};
  6. model.listenPool = [1];
  7. model.selectedPool = {};
  8. model.merchid = 0;
  9. model.no_merchid = 0;
  10. model.url = function(routes, merch) {
  11. if (merch) {
  12. var url = './merchant.php?c=site&a=entry&m=lionfish_comshop&do=admin&controller=' + routes.replace(/\//ig, '.')
  13. } else {
  14. var url = './index.php?c=site&a=entry&m=lionfish_comshop&do=admin&controller=' + routes.replace(/\//ig, '.')
  15. }
  16. return url
  17. };
  18. model.post_url = model.url('util.goods_selector', model.merchid);
  19. model.open = function(callback, type, merchid, multi, api_url, selected_ids, no_merchid) {
  20. model.merchid = merchid;
  21. model.no_merchid = no_merchid;
  22. model.post_url = model.url('util.goods_selector', merchid);
  23. var elename = 'goods_selector';
  24. if (api_url && api_url.length > 0) {
  25. model.post_url = api_url
  26. }
  27. if (multi) {
  28. model.multi = true
  29. } else {
  30. model.multi = false
  31. }
  32. if (type == 'creditshop') {
  33. model.post_url += '&creditshop=1'
  34. } else if (type == 'group') {
  35. model.post_url += '&group=1'
  36. }
  37. if (merchid) {
  38. model.post_url += '&merchid=' + merchid
  39. }
  40. model.callback = callback;
  41. if (typeof(model.callback) == 'string') {
  42. var url = model.url('util.goods_selector.js', merchid);
  43. model.name = elename;
  44. $('#goods-selector-modal').remove();
  45. $('body').append('<div id="goods-selector-modal"></div>');
  46. $.ajax({
  47. url: url,
  48. async: false,
  49. success: function(htm) {
  50. $('#goods-selector-modal').empty().html(htm);
  51. return
  52. }
  53. });
  54. model.ele = $("#goods_selector_" + model.name);
  55. model.mask = model.ele.find(".modal");
  56. model.close1 = model.ele.find(".modal").find(".close");
  57. model.modal = model.ele.find(".modal").find(".modal-dialog");
  58. model.close2 = model.ele.find(".modal").find(".modal-footer").find(".btn");
  59. model.s2id_autogen1 = model.ele.find("#s2id_autogen1");
  60. model.select2drop = model.ele.find("#select2-drop");
  61. model.select2result = model.ele.find(".select2-result");
  62. model.select2chosen = model.ele.find("#select2-chosen-2");
  63. model.$search = model.ele.find(".search");
  64. model.$goodsgroup = 0;
  65. model.getpage(1);
  66. model.mask.css("display", "block");
  67. setTimeout("model.mask.addClass('in')");
  68. var json = model.ele.find("textarea[name=" + model.name + "]").html();
  69. if (model.isJSON(json)) {
  70. model.selectedPool = JSON.parse(json)
  71. } else {
  72. model.selectedPool = {};
  73. model.ele.find("textarea[name=" + model.name + "]").html("")
  74. }
  75. model.listen();
  76. if (selected_ids && selected_ids.length > 0) {
  77. $.each(selected_ids, function(i, v) {
  78. model.selectedPool[v] = {
  79. id: v
  80. };
  81. model.selectStatus()
  82. })
  83. }
  84. }
  85. };
  86. model.init = function() {
  87. var textareas = $(".goods-selector-textarea");
  88. $.each(textareas, function(i, v) {
  89. var obj = $(v);
  90. var json = obj.html();
  91. model.name = obj.attr("name");
  92. if (model.isJSON(json)) {
  93. model.selectedPool = JSON.parse(json)
  94. } else {
  95. model.selectedPool = {};
  96. obj.html("")
  97. }
  98. model.put_selected_to_list()
  99. });
  100. $(document).on("click", ".goods-selector-cancel", function() {
  101. model.name = $(this).parent().parent().parent().data("name");
  102. var id = $(this).data("id");
  103. model.del(id);
  104. model.ele = $("#goods_selector_" + model.name);
  105. var json = model.ele.find("textarea[name=" + model.name + "]").html();
  106. if (model.isJSON(json)) {
  107. model.selectedPool = JSON.parse(json)
  108. } else {
  109. model.selectedPool = {};
  110. model.ele.find("textarea[name=" + model.name + "]").html("")
  111. }
  112. delete model.selectedPool[id];
  113. model.saveSelected();
  114. model.name = model.ele = undefined
  115. });
  116. $(document).on("click", ".goods-selector-op", function() {
  117. model.name = $(this).parent().parent().parent().data("name");
  118. var name = model.name;
  119. model.ele = $("#goods_selector_" + model.name);
  120. model.option_switch = $(model.ele).attr("data-switch");
  121. var href = $(this).attr('href');
  122. href = href.replace('nooption=undefined', 'nooption=' + model.option_switch);
  123. $(this).attr('href', href);
  124. var goodsid = $(this).data("id");
  125. var thismodal = $("#goods-selector-opmodal-" + goodsid);
  126. var json = $("#goods_selector_" + name).find(".goods-selector-textarea").html();
  127. if (model.isJSON(json)) {
  128. model.selectedPool = JSON.parse(json)
  129. } else {
  130. model.selectedPool = {};
  131. model.ele.find("textarea[name=" + model.name + "]").html("")
  132. }
  133. var goods = model.selectedPool[goodsid]
  134. });
  135. $(document).on("click", ".goods-selector-op-option", function() {
  136. model.ele = $("#goods_selector_" + model.name);
  137. var goodsid = $(this).data("id");
  138. var checked = $("#goods-selector-opmodal-" + goodsid).find(".option-item:checked");
  139. var options = {};
  140. $.each(checked, function(i, v) {
  141. var thisobj = $(v);
  142. var input = thisobj.parent().parent().find("input").not(".option-item");
  143. var column = {};
  144. $.each(input, function(j, k) {
  145. column[$(k).attr("name")] = $(k).val()
  146. });
  147. var obj = {
  148. id: thisobj.val(),
  149. marketprice: thisobj.data("price"),
  150. title: thisobj.parent().parent().find("td:nth-child(1)").text(),
  151. stock: thisobj.parent().parent().find("td:nth-child(3)").text(),
  152. column: column
  153. };
  154. options[thisobj.val()] = obj
  155. });
  156. var json = model.ele.find("textarea[name=" + model.name + "]").html();
  157. if (model.isJSON(json)) {
  158. model.selectedPool = JSON.parse(json)
  159. } else {
  160. model.selectedPool = {};
  161. model.ele.find("textarea[name=" + model.name + "]").html("")
  162. }
  163. model.selectedPool[goodsid]["options"] = options;
  164. model.saveSelected();
  165. model.name = undefined
  166. });
  167. $(document).on("click", ".goods-selector-op-goods", function() {
  168. model.ele = $("#goods_selector_" + model.name);
  169. var goodsid = $(this).data("id");
  170. var json = model.ele.find("textarea[name=" + model.name + "]").html();
  171. if (model.isJSON(json)) {
  172. model.selectedPool = JSON.parse(json)
  173. } else {
  174. model.selectedPool = {};
  175. model.ele.find("textarea[name=" + model.name + "]").html("")
  176. }
  177. var column = {};
  178. var input = $("#goods-selector-opmodal-" + goodsid).find("input");
  179. $.each(input, function(j, k) {
  180. column[$(k).attr("name")] = $(k).val()
  181. });
  182. model.selectedPool[goodsid]["column"] = column;
  183. model.saveSelected()
  184. });
  185. model.p = $(".goods-selector-open");
  186. model.p.click(function() {
  187. var url = $(this).data('url');
  188. var type = $(this).data('type');
  189. if (type == 'creditshop') {
  190. model.post_url += '&creditshop=1'
  191. } else if (type == 'group') {
  192. model.post_url += '&group=1'
  193. }
  194. var merchid = $(this).data('merchid');
  195. if (merchid) {
  196. model.post_url += '&merchid=' + merchid
  197. }
  198. var elename = $(this).attr("data-name");
  199. model.callback = $(this).data('callback');
  200. if (model.callback) {
  201. var url = model.url('util.goods_selector.js', merchid);
  202. if (model.loaded) {} else {
  203. $('body').append('<div id="goods-selector-modal"></div>');
  204. $.ajax({
  205. url: url,
  206. async: false,
  207. success: function(htm) {
  208. $('#goods-selector-modal').empty().html(htm);
  209. model.loaded = 1;
  210. return
  211. }
  212. })
  213. }
  214. }
  215. model.name = elename;
  216. model.ele = $("#goods_selector_" + model.name);
  217. model.mask = model.ele.find(".modal");
  218. model.close1 = model.ele.find(".modal").find(".close");
  219. model.modal = model.ele.find(".modal").find(".modal-dialog");
  220. model.close2 = model.ele.find(".modal").find(".modal-footer").find(".btn");
  221. model.s2id_autogen1 = model.ele.find("#s2id_autogen1");
  222. model.select2drop = model.ele.find("#select2-drop");
  223. model.select2result = model.ele.find(".select2-result");
  224. model.select2chosen = model.ele.find("#select2-chosen-2");
  225. model.$search = model.ele.find(".search");
  226. model.$goodsgroup = 0;
  227. model.getpage(1);
  228. model.mask.css("display", "block");
  229. setTimeout("model.mask.addClass('in')");
  230. var json = model.ele.find("textarea[name=" + model.name + "]").html();
  231. if (model.isJSON(json)) {
  232. model.selectedPool = JSON.parse(json)
  233. } else {
  234. model.selectedPool = {};
  235. model.ele.find("textarea[name=" + model.name + "]").html("")
  236. }
  237. if (model.listenPool.indexOf(model.name) < 0) {
  238. model.listen()
  239. }
  240. })
  241. };
  242. model.listen = function() {
  243. $(document).keypress(function(e) {
  244. if (e.which == 13 && model.ele != undefined) {
  245. model.jumpnow(1);
  246. return false
  247. }
  248. });
  249. model.listenPool.push(model.name);
  250. $(model.modal).on("click", ".pager-nav", function() {
  251. var num = Number($(this).attr("page"));
  252. model.jumpnow(num)
  253. });
  254. $(model.modal).on("change", ".page-raduis", function() {
  255. var num = Number($(this).val());
  256. $(this).parent().next("li").find("a").attr("page", num)
  257. });
  258. $(model.modal).on("click", ".selectit", function() {
  259. var goods = {};
  260. goods = $(this).data("json");
  261. if (model.callback) {
  262. goods.act = 1;
  263. eval(model.callback + "(goods)");
  264. if (!model.multi) {
  265. $('.modal.in').find('.close').trigger('click')
  266. } else {
  267. $(this).removeClass("selectit").removeClass("label-primary").addClass("cancelit").addClass("label-danger").text("取消")
  268. }
  269. }
  270. model.selectedPool[goods.id] = goods;
  271. $(this).removeClass("selectit").removeClass("label-primary").addClass("cancelit").addClass("label-danger").text("取消");
  272. model.put(goods.id)
  273. });
  274. $(model.modal).on("click", ".cancelit", function() {
  275. var goods = {};
  276. goods = $(this).data("json");
  277. delete model.selectedPool[goods.id];
  278. if (model.multi) {
  279. goods.act = 0;
  280. eval(model.callback + "(goods)")
  281. }
  282. $(this).removeClass("cancelit").removeClass("label-danger").addClass("selectit").addClass("label-primary").text("选择");
  283. model.del(goods.id)
  284. });
  285. $(model.mask).click(function() {
  286. model.mask.removeClass("in");
  287. setTimeout("model.mask.css('display','none');", 150);
  288. model.s2id_autogen1.css("border", "1px solid #efefef");
  289. model.select2drop.hide();
  290. model.saveSelected()
  291. });
  292. $(model.modal).click(function(event) {
  293. var e = window.event || event;
  294. if (e.stopPropagation) {
  295. e.stopPropagation()
  296. } else {
  297. e.cancelBubble = true
  298. }
  299. model.select2drop.hide();
  300. model.mask.css("display", "block");
  301. model.s2id_autogen1.css("border", "1px solid #efefef")
  302. });
  303. $(model.close1).click(function(event) {
  304. var e = window.event || event;
  305. if (e.stopPropagation) {
  306. e.stopPropagation()
  307. } else {
  308. e.cancelBubble = true
  309. }
  310. model.mask.removeClass("in");
  311. setTimeout("model.mask.css('display','none');", 150);
  312. model.saveSelected()
  313. });
  314. $(model.close2).click(function(event) {
  315. var e = window.event || event;
  316. if (e.stopPropagation) {
  317. e.stopPropagation()
  318. } else {
  319. e.cancelBubble = true
  320. }
  321. model.mask.removeClass("in");
  322. setTimeout("model.mask.css('display','none');", 150);
  323. model.saveSelected()
  324. });
  325. $(model.s2id_autogen1).click(function() {
  326. var e = window.event || event;
  327. if (e.stopPropagation) {
  328. e.stopPropagation()
  329. } else {
  330. e.cancelBubble = true
  331. }
  332. model.select2drop.show();
  333. $(this).css("border", "1px solid #44abf7 ");
  334. $(this).css("border-bottom", "0")
  335. });
  336. $(model.select2drop).click(function() {
  337. var e = window.event || event;
  338. if (e.stopPropagation) {
  339. e.stopPropagation()
  340. } else {
  341. e.cancelBubble = true
  342. }
  343. model.select2drop.show()
  344. });
  345. model.ele.find('.fenlei').find('select').change(function() {
  346. var e = window.event || event;
  347. if (e.stopPropagation) {
  348. e.stopPropagation()
  349. } else {
  350. e.cancelBubble = true
  351. }
  352. model.select2drop.hide();
  353. model.goodsgroup = $(this).val();
  354. model.select2chosen.html($(this).find("div").html());
  355. model.s2id_autogen1.css("border", "1px solid #efefef")
  356. });
  357. $(model.select2result).hover(function() {
  358. $(this).addClass("select2-highlighted")
  359. }, function() {
  360. $(this).removeClass("select2-highlighted")
  361. })
  362. };
  363. model.put_selected_to_list = function() {
  364. $.each(model.selectedPool, function(i, v) {
  365. model.put(v.id)
  366. });
  367. model.selectedPool = {};
  368. model.name = undefined
  369. };
  370. model.put = function(id) {
  371. model.option_switch = $(model.ele).data("switch");
  372. var url = "./index.php?c=site&a=entry&m=lionfish_comshop&do=admin&r=util.goods_selector.op&id=" + id + "&nooption=" + model.option_switch + "&column=" + encodeURI($("#goods-selected-list-" + model.name).attr("data-column"));
  373. if (model.option_switch == 0) {
  374. url += "&nooption=1"
  375. }
  376. var set_color = "btn-danger";
  377. var htm = '<tr id="goods-selected-goods' + id + '"> <td><img src="' + model.selectedPool[id].thumb + '" style="width: 40px;height: 40px;border: solid #ccc 1px"></td> <td><p class="title">' + model.selectedPool[id].title + '</p> <p class="text text-danger">¥' + model.selectedPool[id].marketprice + "</p></td> <td>" + '' + '</td> <td><a data-toggle="ajaxModal" href="' + url + '" class="btn ' + set_color + ' btn-sm goods-selector-op" data-id="' + model.selectedPool[id].id + '">商品设置</a> <a class="btn btn-default btn-sm goods-selector-cancel" data-id="' + id + '">取消</a></td> </tr>';
  378. $("#goods-selected-list-" + model.name).append(htm)
  379. };
  380. model.del = function(id) {
  381. $("#goods-selected-list-" + model.name).find("#goods-selected-goods" + id).remove()
  382. };
  383. model.selectStatus = function() {
  384. var selectBtn = model.ele.find(".selectit");
  385. $.each(selectBtn, function(i, v) {
  386. var obj = $(v);
  387. var thisid = obj.data("id");
  388. if (model.selectedPool[thisid] !== undefined) {
  389. obj.removeClass("selectit").removeClass("label-primary").addClass("cancelit").addClass("label-danger").text("取消")
  390. }
  391. })
  392. };
  393. model.isJSON = function(str) {
  394. if (typeof str == "string") {
  395. try {
  396. var obj = JSON.parse(str);
  397. if (str.indexOf("{") > -1) {
  398. return true
  399. } else {
  400. return false
  401. }
  402. } catch (e) {
  403. console.log("隐藏域数据格式不能解析,已清空,请重新选择:" + e);
  404. return false
  405. }
  406. }
  407. return false
  408. };
  409. model.saveSelected = function() {
  410. model.ele.find("textarea[name=" + model.name + "]").html(JSON.stringify(model.selectedPool));
  411. model.selectedPool = {}
  412. };
  413. model.jumpnow = function(page) {
  414. model.keyword = model.$search.val();
  415. model.getpage(page, model.keyword, model.goodsgroup)
  416. };
  417. model.getpage = function(page, keywords, goodsgroup) {
  418. if (!page > 0) {
  419. page = 1
  420. }
  421. if (keywords == undefined) {
  422. model.$search.val("")
  423. }
  424. var condition = model.ele.find("where").text();
  425. $.ajax({
  426. url: model.post_url,
  427. type: "post",
  428. data: {
  429. data: {},
  430. page: page,
  431. keywords: keywords,
  432. goodsgroup: goodsgroup,
  433. condition: condition,
  434. no_merchid: model.no_merchid
  435. },
  436. success: function(htm) {
  437. model.ele.find(".content").empty().html(htm);
  438. model.selectStatus()
  439. },
  440. })
  441. };
  442. return model
  443. });