goods.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
  2. var Controller = {
  3. index: function () {
  4. // 初始化表格参数配置
  5. Table.api.init({
  6. extend: {
  7. index_url: 'qingdongams/product/goods/index' + location.search,
  8. add_url: 'qingdongams/product/goods/add',
  9. edit_url: 'qingdongams/product/goods/edit',
  10. detail_url: 'qingdongams/product/goods/detail',
  11. multi_url: 'qingdongams/product/goods/multi',
  12. import_url: 'qingdongams/product/goods/import',
  13. table: 'qingdongams_goods',
  14. }
  15. });
  16. var isselect=false;
  17. var pselect = true;
  18. var q = {};
  19. location.search.replace(/([^?&=]+)=([^&]+)/g,(_,k,v)=>q[k]=v);
  20. if (q.isselect == 1) {
  21. isselect= true;
  22. pselect = false;
  23. }
  24. var table = $("#table");
  25. // 初始化表格
  26. table.bootstrapTable({
  27. url: $.fn.bootstrapTable.defaults.extend.index_url,
  28. pk: 'id',
  29. sortName: 'id',
  30. columns: [
  31. [
  32. {checkbox: true},
  33. {
  34. field : 'name', title : '商品名称', operate : 'like', fixedColumns : true, formatter : function (value, row, index) {
  35. return "<a href='javascript:void(0);' data-id='" + row.id + "' class='show-goods' >" + value + "</a>";
  36. }
  37. },
  38. {
  39. field: 'type.name', title: '商品分类', operate: false
  40. },
  41. {field: 'type_id', title: __('商品分类'), searchList: $.getJSON("qingdongams/product/goods/get_product_type"),visible:false},
  42. {field: 'cost_price', title: __('采购价'), operate: 'LIKE'},
  43. {field: 'unit', title: __('单位'), operate: 'LIKE'},
  44. {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
  45. {
  46. field: 'operate2', title: __('Operate'),visible:isselect, events: {
  47. 'click .btn-chooseone': function (e, value, row, index) {
  48. Fast.api.close(row);
  49. },
  50. }, formatter: function () {
  51. return '<a href="javascript:;" class="btn btn-danger btn-chooseone btn-xs"><i class="fa fa-check"></i> ' + __('Choose') + '</a>';
  52. }
  53. },
  54. {
  55. field: 'operate', title: __('Operate'),table: table,visible:pselect,
  56. events: Table.api.events.operate, formatter: Table.api.formatter.buttons,
  57. buttons: [
  58. {
  59. name: 'edit',
  60. text: __('编辑'),
  61. title: __('编辑'),
  62. extend:'data-area=["90%","90%"]',
  63. classname: 'records btn-dialog',
  64. url: 'qingdongams/product/goods/edit',
  65. },
  66. {
  67. name: 'detail',
  68. text: __('详情'),
  69. title: __('详情'),
  70. extend:'data-area=["95%","95%"]',
  71. classname: 'records btn-dialog',
  72. url: 'qingdongams/product/goods/detail',
  73. },
  74. ]
  75. },
  76. ]
  77. ],
  78. search:false,
  79. //启用普通表单搜索
  80. commonSearch : true,
  81. searchFormVisible:true,
  82. showSearch : true,
  83. pageSize : 10,
  84. onLoadSuccess: function () {
  85. // 这里就是数据渲染结束后的回调函数
  86. $(".btn-add").data("area", ["90%", "90%"]);
  87. $(".btn-edit").data("area", ["90%", "90%"]);
  88. }
  89. });
  90. $(document).on('click', '.show-goods', function (data) {
  91. var area = [$(window).width() > 1200 ? '1200px' : '95%', $(window).height() > 800 ? '800px' : '95%'];
  92. var options = {
  93. shadeClose : false,
  94. shade : [0.3, '#393D49'],
  95. area : area,
  96. refresh:true,
  97. callback : function (value) {
  98. //在回调函数里可以调用你的业务代码实现前端的各种逻辑和效果
  99. },
  100. end:function (){
  101. $('.btn-refresh').trigger('click');
  102. }
  103. };
  104. Fast.api.open($.fn.bootstrapTable.defaults.extend.detail_url + "?ids=" + $(this).data('id'), '商品详情', options);
  105. });
  106. // 为表格绑定事件
  107. Table.api.bindevent(table);
  108. },
  109. recyclebin: function () {
  110. // 初始化表格参数配置
  111. Table.api.init({
  112. extend: {
  113. 'dragsort_url': ''
  114. }
  115. });
  116. var table = $("#table");
  117. // 初始化表格
  118. table.bootstrapTable({
  119. url: 'qingdongams/product/goods/recyclebin' + location.search,
  120. pk: 'id',
  121. sortName: 'id',
  122. columns: [
  123. [
  124. {checkbox: true},
  125. {field: 'id', title: __('Id')},
  126. {field: 'name', title: __('Name'), align: 'left'},
  127. {
  128. field: 'deletetime',
  129. title: __('Deletetime'),
  130. operate: 'RANGE',
  131. addclass: 'datetimerange',
  132. formatter: Table.api.formatter.datetime
  133. },
  134. {
  135. field: 'operate',
  136. width: '130px',
  137. title: __('Operate'),
  138. table: table,
  139. events: Table.api.events.operate,
  140. buttons: [
  141. {
  142. name: 'Restore',
  143. text: __('Restore'),
  144. classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
  145. icon: 'fa fa-rotate-left',
  146. url: 'qingdongams/product/goods/restore',
  147. refresh: true
  148. },
  149. {
  150. name: 'Destroy',
  151. text: __('Destroy'),
  152. classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
  153. icon: 'fa fa-times',
  154. url: 'qingdongams/product/goods/destroy',
  155. refresh: true
  156. }
  157. ],
  158. formatter: Table.api.formatter.operate
  159. }
  160. ]
  161. ]
  162. });
  163. // 为表格绑定事件
  164. Table.api.bindevent(table);
  165. },
  166. add: function () {
  167. $(document).on('change','input[name="row[specs_type]"]',function (){
  168. var specs = $(this).val();
  169. if(specs == 1){
  170. $('.specesinfo').show();
  171. $('.goodsinfo').hide();
  172. }else{
  173. $('.specesinfo').hide();
  174. $('.goodsinfo').show();
  175. }
  176. });
  177. Controller.api.bindevent();
  178. },
  179. edit: function () {
  180. //后续操作
  181. var ids = $('#ids').val();
  182. var specs_id=$("#specs_id").val();
  183. var price=$('#c-price').val();
  184. $.post('qingdongams/product/goods/get_specs_detail',{specs_id:specs_id,ids:ids},function (data){
  185. if(data.code == 1){
  186. var result=[];
  187. var specslist='';
  188. $.each(data.data,function (index,params){
  189. params.type=params.name;
  190. params.name='['+params.name+']';
  191. params.price=price;
  192. params.remarks="";
  193. params.avatar="";
  194. params.index=index;
  195. result.push(params);
  196. var dataparams=JSON.stringify(params);
  197. if(params.check && params.check == 1){
  198. specslist+='<a class="label label-default label-specs label-info" data-params=\''+dataparams+'\' data-id="'+index+'" data-show="1" checked="checked">'+params.name+'</a> ';
  199. }else{
  200. specslist+='<a class="label label-default label-specs" data-params=\''+dataparams+'\' data-id="'+index+'" data-show="0" >'+params.name+'</a> ';
  201. }
  202. });
  203. $('#specslist').html(specslist);
  204. }
  205. },'json');
  206. $(document).on('change','input[name="row[specs_type]"]',function (){
  207. var specs = $(this).val();
  208. if(specs == 1){
  209. $('.specesinfo').show();
  210. $('.goodsinfo').hide();
  211. }else{
  212. $('.specesinfo').hide();
  213. $('.goodsinfo').show();
  214. }
  215. });
  216. Controller.api.bindevent();
  217. },
  218. import: function () {
  219. Controller.api.bindevent();
  220. },
  221. detail: function () {
  222. // 初始化表格参数配置
  223. Table.api.init({});
  224. var id = $('#ids').val();
  225. //产品信息
  226. var table1 = $("#operation_product");
  227. // 初始化表格
  228. table1.bootstrapTable({
  229. url : 'qingdongams/product/goods/get_product?goods_id=' +id,
  230. pk : 'id',
  231. sortName : 'id',
  232. toolbar: '#toolbar_product',
  233. operate : false,
  234. commonSearch : false,
  235. search : false,
  236. visible : false,
  237. showColumns : false,
  238. showExport : false,
  239. showToggle : false,
  240. columns : [
  241. [
  242. {
  243. field : 'producttype.name', title : '产品分类'
  244. },
  245. {
  246. field : 'name', title : '产品名称', operate : 'like', fixedColumns : true, formatter : function (value, row, index) {
  247. return "<a href='javascript:void(0);' class='btn-dialog' " +
  248. "data-url='qingdongams/product/product/detail/ids/"+row.id+"' data-shade='0.3' data-title='产品详情' >" + value + "</a>";
  249. }
  250. },
  251. {field : 'num', title : '产品编码', operate : 'like'},
  252. {field: 'img', title: '产品图片', operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
  253. {field : 'unit', title : '产品单位', operate : false},
  254. {field : 'price', title : '零售价', operate : false},
  255. {field : 'wholesale', title : '批发价', operate : false},
  256. {field : 'cost_price', title : '采购价', operate : false},
  257. ]
  258. ]
  259. });
  260. // 为表格绑定事件
  261. Table.api.bindevent(table1);
  262. //编辑
  263. $(document).on('click','.btn-edit',function () {
  264. var id=$('#ids').val();
  265. Fast.api.open("qingdongams/product/goods/edit?ids="+id, "编辑",{
  266. shadeClose: false,
  267. shade: false,
  268. maxmin: false,
  269. moveOut: false,
  270. scrollbars:false,
  271. 'area':['90%','90%'],
  272. callback:function () {
  273. location.reload()
  274. }
  275. });
  276. });
  277. $(document).on('click', ".btn-del", function () {//删除
  278. var id = $('#ids').val();
  279. Layer.confirm('确定删除当前商品吗?', {
  280. btn : ['确定', '取消'],
  281. title : '提示',
  282. }, function (index, layero) {
  283. Fast.api.ajax("qingdongams/product/goods/del?ids=" + id, function (data, ret) {
  284. if (ret.code == 1) {
  285. Layer.close(index);
  286. parent.location.reload();
  287. }
  288. }, function (data, ret) {
  289. });
  290. });
  291. });
  292. Controller.api.bindevent();
  293. },
  294. api: {
  295. bindevent: function () {
  296. $(document).on("fa.event.appendfieldlist", '.fieldlist', function(){
  297. //通用的表单组件事件绑定和组件渲染
  298. Form.events.selectpage($("form"));
  299. Form.events.plupload($("form"));
  300. });
  301. $("#specs_id").data("eSelect", function(){
  302. //后续操作
  303. var specs_id=$("#specs_id").val();
  304. var price=$('#c-price').val();
  305. $.post('qingdongams/product/goods/get_specs_detail',{specs_id:specs_id},function (data){
  306. if(data.code == 1){
  307. var result=[];
  308. var specslist='';
  309. $.each(data.data,function (index,params){
  310. params.type=params.name;
  311. params.name='['+params.name+']';
  312. params.price=price;
  313. params.remarks="";
  314. params.avatar="";
  315. params.index=index;
  316. result.push(params);
  317. var dataparams=JSON.stringify(params);
  318. specslist+='<a class="label label-default label-specs" data-params=\''+dataparams+'\' data-id="'+index+'" data-show="0" >'+params.name+'</a> ';
  319. });
  320. $('#specslist').html(specslist);
  321. }
  322. },'json');
  323. });
  324. $(document).on('click','.label-specs',function (){
  325. var is_show=$(this).attr('data-show');
  326. var product=$('textarea[name="row[product]"]').val();
  327. if(is_show == 1){
  328. $(this).attr({'data-show':0});
  329. $(this).removeClass('label-info');
  330. var id=$(this).data('id');
  331. product=JSON.parse(product);
  332. var result=[];
  333. $.each(product,function (index,params){
  334. if(params.index != id){
  335. result.push(params);
  336. }
  337. });
  338. result=JSON.stringify(result);
  339. $('textarea[name="row[product]"]').val(result);
  340. $(".fieldlist").trigger("fa.event.refreshfieldlist");
  341. $('textarea[name="row[product]"]').trigger("fa.event.refreshfieldlist");
  342. }else{
  343. $(this).addClass('label-info');
  344. $(this).attr({'data-show':1});
  345. var params=$(this).data('params');
  346. product=JSON.parse(product);
  347. product.push(params);
  348. var res=JSON.stringify(product);
  349. $('textarea[name="row[product]"]').val(res);
  350. $(".fieldlist").trigger("fa.event.refreshfieldlist");
  351. $('textarea[name="row[product]"]').trigger("fa.event.refreshfieldlist");
  352. }
  353. });
  354. Form.api.bindevent($("form[role=form]"));
  355. }
  356. }
  357. };
  358. return Controller;
  359. });