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: 'qingdong/product/goods/index' + location.search,
  8. add_url: 'qingdong/product/goods/add',
  9. edit_url: 'qingdong/product/goods/edit',
  10. detail_url: 'qingdong/product/goods/detail',
  11. multi_url: 'qingdong/product/goods/multi',
  12. import_url: 'qingdong/product/goods/import',
  13. table: 'qingdong_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("qingdong/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: 'qingdong/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: 'qingdong/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. console.log(value);
  100. },
  101. end:function (){
  102. $('.btn-refresh').trigger('click');
  103. }
  104. };
  105. Fast.api.open($.fn.bootstrapTable.defaults.extend.detail_url + "?ids=" + $(this).data('id'), '商品详情', options);
  106. });
  107. // 为表格绑定事件
  108. Table.api.bindevent(table);
  109. },
  110. recyclebin: function () {
  111. // 初始化表格参数配置
  112. Table.api.init({
  113. extend: {
  114. 'dragsort_url': ''
  115. }
  116. });
  117. var table = $("#table");
  118. // 初始化表格
  119. table.bootstrapTable({
  120. url: 'qingdong/product/goods/recyclebin' + location.search,
  121. pk: 'id',
  122. sortName: 'id',
  123. columns: [
  124. [
  125. {checkbox: true},
  126. {field: 'id', title: __('Id')},
  127. {field: 'name', title: __('Name'), align: 'left'},
  128. {
  129. field: 'deletetime',
  130. title: __('Deletetime'),
  131. operate: 'RANGE',
  132. addclass: 'datetimerange',
  133. formatter: Table.api.formatter.datetime
  134. },
  135. {
  136. field: 'operate',
  137. width: '130px',
  138. title: __('Operate'),
  139. table: table,
  140. events: Table.api.events.operate,
  141. buttons: [
  142. {
  143. name: 'Restore',
  144. text: __('Restore'),
  145. classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
  146. icon: 'fa fa-rotate-left',
  147. url: 'qingdong/product/goods/restore',
  148. refresh: true
  149. },
  150. {
  151. name: 'Destroy',
  152. text: __('Destroy'),
  153. classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
  154. icon: 'fa fa-times',
  155. url: 'qingdong/product/goods/destroy',
  156. refresh: true
  157. }
  158. ],
  159. formatter: Table.api.formatter.operate
  160. }
  161. ]
  162. ]
  163. });
  164. // 为表格绑定事件
  165. Table.api.bindevent(table);
  166. },
  167. add: function () {
  168. $(document).on('change','input[name="row[is_specs]"]',function (){
  169. var specs = $(this).val();
  170. if(specs == 1){
  171. $('.specesinfo').show();
  172. $('.goodsinfo').hide();
  173. }else{
  174. $('.specesinfo').hide();
  175. $('.goodsinfo').show();
  176. }
  177. });
  178. Controller.api.bindevent();
  179. },
  180. edit: function () {
  181. //后续操作
  182. var ids = $('#ids').val();
  183. var specs_id=$("#specs_id").val();
  184. var price=$('#c-price').val();
  185. console.log(specs_id);
  186. $.post('qingdong/product/goods/get_specs_detail',{specs_id:specs_id,ids:ids},function (data){
  187. if(data.code == 1){
  188. var result=[];
  189. var specslist='';
  190. $.each(data.data,function (index,params){
  191. params.type=params.name;
  192. params.name='['+params.name+']';
  193. params.price=price;
  194. params.remarks="";
  195. params.avatar="";
  196. params.index=index;
  197. result.push(params);
  198. var dataparams=JSON.stringify(params);
  199. if(params.check && params.check == 1){
  200. specslist+='<a class="label label-default label-specs label-info" data-params=\''+dataparams+'\' data-id="'+index+'" data-show="1" checked="checked">'+params.name+'</a> ';
  201. }else{
  202. specslist+='<a class="label label-default label-specs" data-params=\''+dataparams+'\' data-id="'+index+'" data-show="0" >'+params.name+'</a> ';
  203. }
  204. });
  205. $('#specslist').html(specslist);
  206. }
  207. },'json');
  208. $(document).on('change','input[name="row[is_specs]"]',function (){
  209. var specs = $(this).val();
  210. if(specs == 1){
  211. $('.specesinfo').show();
  212. $('.goodsinfo').hide();
  213. }else{
  214. $('.specesinfo').hide();
  215. $('.goodsinfo').show();
  216. }
  217. });
  218. Controller.api.bindevent();
  219. },
  220. import: function () {
  221. Controller.api.bindevent();
  222. },
  223. detail: function () {
  224. // 初始化表格参数配置
  225. Table.api.init({});
  226. var id = $('#ids').val();
  227. //产品信息
  228. var table1 = $("#operation_product");
  229. // 初始化表格
  230. table1.bootstrapTable({
  231. url : 'qingdong/product/goods/get_product?goods_id=' +id,
  232. pk : 'id',
  233. sortName : 'id',
  234. toolbar: '#toolbar_product',
  235. operate : false,
  236. commonSearch : false,
  237. search : false,
  238. visible : false,
  239. showColumns : false,
  240. showExport : false,
  241. showToggle : false,
  242. columns : [
  243. [
  244. {
  245. field : 'producttype.name', title : '产品分类'
  246. },
  247. {
  248. field : 'name', title : '产品名称', operate : 'like', fixedColumns : true, formatter : function (value, row, index) {
  249. return "<a href='javascript:void(0);' class='btn-dialog' " +
  250. "data-url='qingdong/product/product/detail/ids/"+row.id+"' data-shade='0.3' data-title='产品详情' >" + value + "</a>";
  251. }
  252. },
  253. {field : 'num', title : '产品编码', operate : 'like'},
  254. {field: 'img', title: '产品图片', operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
  255. {field : 'unit', title : '产品单位', operate : false},
  256. {field : 'price', title : '零售价', operate : false},
  257. {field : 'wholesale', title : '批发价', operate : false},
  258. {field : 'cost_price', title : '采购价', operate : false},
  259. ]
  260. ]
  261. });
  262. // 为表格绑定事件
  263. Table.api.bindevent(table1);
  264. //编辑
  265. $(document).on('click','.btn-edit',function () {
  266. var id=$('#ids').val();
  267. Fast.api.open("qingdong/product/goods/edit?ids="+id, "编辑",{
  268. shadeClose: false,
  269. shade: false,
  270. maxmin: false,
  271. moveOut: false,
  272. scrollbars:false,
  273. 'area':['90%','90%'],
  274. callback:function () {
  275. location.reload()
  276. }
  277. });
  278. });
  279. $(document).on('click', ".btn-del", function () {//删除
  280. var id = $('#ids').val();
  281. Layer.confirm('确定删除当前商品吗?', {
  282. btn : ['确定', '取消'],
  283. title : '提示',
  284. }, function (index, layero) {
  285. Fast.api.ajax("qingdong/product/goods/del?ids=" + id, function (data, ret) {
  286. if (ret.code == 1) {
  287. Layer.close(index);
  288. parent.location.reload();
  289. }
  290. }, function (data, ret) {
  291. });
  292. });
  293. });
  294. Controller.api.bindevent();
  295. },
  296. api: {
  297. bindevent: function () {
  298. $(document).on("fa.event.appendfieldlist", '.fieldlist', function(){
  299. //通用的表单组件事件绑定和组件渲染
  300. Form.events.selectpage($("form"));
  301. Form.events.plupload($("form"));
  302. });
  303. $("#specs_id").data("eSelect", function(){
  304. //后续操作
  305. var specs_id=$("#specs_id").val();
  306. var price=$('#c-price').val();
  307. $.post('qingdong/product/goods/get_specs_detail',{specs_id:specs_id},function (data){
  308. if(data.code == 1){
  309. var result=[];
  310. var specslist='';
  311. $.each(data.data,function (index,params){
  312. params.type=params.name;
  313. params.name='['+params.name+']';
  314. params.price=price;
  315. params.remarks="";
  316. params.avatar="";
  317. params.index=index;
  318. result.push(params);
  319. var dataparams=JSON.stringify(params);
  320. specslist+='<a class="label label-default label-specs" data-params=\''+dataparams+'\' data-id="'+index+'" data-show="0" >'+params.name+'</a> ';
  321. });
  322. $('#specslist').html(specslist);
  323. }
  324. },'json');
  325. });
  326. $(document).on('click','.label-specs',function (){
  327. var is_show=$(this).attr('data-show');
  328. var product=$('textarea[name="row[product]"]').val();
  329. if(is_show == 1){
  330. $(this).attr({'data-show':0});
  331. $(this).removeClass('label-info');
  332. var id=$(this).data('id');
  333. product=JSON.parse(product);
  334. var result=[];
  335. $.each(product,function (index,params){
  336. if(params.index != id){
  337. result.push(params);
  338. }
  339. });
  340. result=JSON.stringify(result);
  341. $('textarea[name="row[product]"]').val(result);
  342. $(".fieldlist").trigger("fa.event.refreshfieldlist");
  343. $('textarea[name="row[product]"]').trigger("fa.event.refreshfieldlist");
  344. }else{
  345. $(this).addClass('label-info');
  346. $(this).attr({'data-show':1});
  347. var params=$(this).data('params');
  348. product=JSON.parse(product);
  349. product.push(params);
  350. var res=JSON.stringify(product);
  351. $('textarea[name="row[product]"]').val(res);
  352. $(".fieldlist").trigger("fa.event.refreshfieldlist");
  353. $('textarea[name="row[product]"]').trigger("fa.event.refreshfieldlist");
  354. }
  355. });
  356. Form.api.bindevent($("form[role=form]"));
  357. }
  358. }
  359. };
  360. return Controller;
  361. });