Products_Admin.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. var clickFlag = true;
  2. var ExcelData;
  3. function ConfirmImport() {
  4. $.ajax({
  5. type: "POST",
  6. url: "/Admin/Products/Import?r=" + Math.random(1),
  7. data: "ExcelData=" + encodeURIComponent(JSON.stringify(ExcelData)),
  8. dataType: "text",
  9. success: function (data) {
  10. if (data == "success") {
  11. layer.msg("导入成功", { time: 2000 }, function () {
  12. window.location.reload();
  13. });
  14. } else {
  15. layer.msg(data);
  16. }
  17. }
  18. });
  19. }
  20. layui.config({
  21. base: '/layuiadmin/' //静态资源所在路径
  22. }).extend({
  23. myexcel: 'layui/lay/modules/excel',
  24. index: 'lib/index' //主入口模块
  25. }).use(['index', 'table', 'excel', 'laydate'], function () {
  26. var $ = layui.$
  27. , form = layui.form
  28. , table = layui.table;
  29. //- 筛选条件-日期
  30. var laydate = layui.laydate;
  31. //excel导入
  32. var excel = layui.excel;
  33. $('#ExcelFile').change(function (e) {
  34. var files = e.target.files;
  35. excel.importExcel(files, {}, function (data) {
  36. ExcelData = data[0].Sheet1;
  37. });
  38. });
  39. //监听单元格编辑
  40. table.on('edit(LAY-list-manage)', function (obj) {
  41. var value = obj.value //得到修改后的值
  42. , data = obj.data //得到所在行所有键值
  43. , field = obj.field; //得到字段
  44. if (field == "Sort") {
  45. $.ajax({
  46. type: "POST",
  47. url: "/Admin/Products/Sort?r=" + Math.random(1),
  48. data: "Id=" + data.Id + "&Sort=" + value,
  49. dataType: "text",
  50. success: function (data) {
  51. }
  52. });
  53. }
  54. });
  55. //列表数据
  56. table.render({
  57. elem: '#LAY-list-manage'
  58. , url: '/Admin/Products/IndexData' //模拟接口
  59. , cols: [[
  60. { type: 'checkbox', fixed: 'left' }
  61. , { field: 'Id', fixed: 'left', title: 'ID', width: 80, sort: true, unresize: true }
  62. , { field: 'ProductName', width: 200, title: '商品名称', sort: true }
  63. , { field: 'Status', width: 200, title: '状态', sort: true }
  64. , { field: 'Stock', width: 200, title: '库存', sort: true }
  65. , { field: 'BuyCount', width: 200, title: '已购买数', sort: true }
  66. , { field: 'Price', width: 200, title: '单价', sort: true }
  67. // , { field: 'Integral', width: 200, title: '抵扣积分', sort: true }
  68. // , { field: 'MemberPrice', width: 200, title: '创客价', sort: true }
  69. // , { field: 'UserIntegral', width: 200, title: '创客抵扣积分', sort: true }
  70. // , { field: 'Status', width: 200, title: '状态', sort: true }
  71. , { field: 'Sort', fixed: 'right', title: '排序', width: 80, edit: 'text' }
  72. , { title: '操作', width: 250, align: 'center', fixed: 'right', toolbar: '#table-list-tools' }
  73. ]]
  74. , page: true
  75. , limit: 30
  76. , height: 'full-' + String($('.layui-card-header').height() + 130)
  77. , text: '对不起,加载出现异常!'
  78. , done: function (res, curr, count) {
  79. $(".layui-none").text("无数据");
  80. }
  81. });
  82. //监听工具条
  83. table.on('tool(LAY-list-manage)', function (obj) {
  84. var data = obj.data;
  85. if (obj.event === 'del') {
  86. var index = layer.confirm('确定要删除吗?删除后不能恢复!', function (index) {
  87. $.ajax({
  88. type: "POST",
  89. url: "/Admin/Products/Delete?r=" + Math.random(1),
  90. data: "Id=" + data.Id,
  91. dataType: "text",
  92. success: function (data) {
  93. if (data == "success") {
  94. obj.del();
  95. layer.close(index);
  96. } else {
  97. parent.layer.msg(data);
  98. }
  99. }
  100. });
  101. });
  102. } else if (obj.event === 'edit') {
  103. var tr = $(obj.tr);
  104. var perContent = layer.open({
  105. type: 2
  106. , title: '商品信息-编辑'
  107. , content: 'Edit?Id=' + data.Id + '&right=' + right
  108. , maxmin: true
  109. , area: ['500px', '450px']
  110. , btn: ['确定', '取消']
  111. , yes: function (index, layero) {
  112. var iframeWindow = window['layui-layer-iframe' + index]
  113. , submitID = 'LAY-list-front-submit'
  114. , submit = layero.find('iframe').contents().find('#' + submitID);
  115. setTimeout(function () {
  116. layero.find('iframe').contents().find('.layui-tab-item').each(function (i) {
  117. var errObj = $(this).find('.layui-form-danger');
  118. if (errObj.length > 0) {
  119. iframeWindow.element.tabChange('mytabbar', String(i + 1));
  120. submit.click();
  121. }
  122. });
  123. }, 300);
  124. //内容
  125. iframeWindow.Contentsedit.sync();
  126. //图集
  127. iframeWindow.checkPics("DetailPicPath");
  128. //监听提交
  129. iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
  130. if (clickFlag) {
  131. clickFlag = false;
  132. var field = data.field; //获取提交的字段
  133. var userdata = "";
  134. for (var prop in field) {
  135. userdata += prop + "=" + encodeURIComponent(field[prop]) + "&";
  136. }
  137. //提交 Ajax 成功后,静态更新表格中的数据
  138. //$.ajax({});
  139. $.ajax({
  140. type: "POST",
  141. url: "/Admin/Products/Edit?r=" + Math.random(1),
  142. data: userdata,
  143. dataType: "text",
  144. success: function (data) {
  145. clickFlag = true;
  146. layer.close(index); //关闭弹层
  147. if (data == "success") {
  148. table.reload('LAY-list-manage'); //数据刷新
  149. } else {
  150. layer.msg(data);
  151. }
  152. }
  153. });
  154. }
  155. });
  156. submit.trigger('click');
  157. }
  158. , success: function (layero, index) {
  159. }
  160. });
  161. layer.full(perContent);
  162. } else if (obj.event === 'norm') {
  163. var tr = $(obj.tr);
  164. var perContent = layer.open({
  165. type: 2
  166. , title: '商品规格'
  167. , content: '/Admin/ProductNorm/Edit?Id=' + data.Id + '&MerchantId=' + data.MerchantId + '&right=' + right
  168. , maxmin: true
  169. , area: ['800px', '450px']
  170. , btn: ['确定', '取消']
  171. , yes: function (index, layero) {
  172. var iframeWindow = window['layui-layer-iframe' + index]
  173. , submitID = 'LAY-list-front-submit'
  174. , submit = layero.find('iframe').contents().find('#' + submitID);
  175. layero.find('iframe').contents().find("#ItemList").val(JSON.stringify(iframeWindow.app.itemList));
  176. layero.find('iframe').contents().find("#DetailList").val(JSON.stringify(iframeWindow.app.detailList));
  177. //监听提交
  178. iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
  179. var field = data.field; //获取提交的字段
  180. var userdata = "";
  181. for (var prop in field) {
  182. userdata += prop + "=" + encodeURIComponent(field[prop]) + "&";
  183. }
  184. //提交 Ajax 成功后,静态更新表格中的数据
  185. //$.ajax({});
  186. $.ajax({
  187. type: "POST",
  188. url: "/Admin/ProductNorm/EditPost?r=" + Math.random(1),
  189. data: userdata,
  190. dataType: "text",
  191. success: function (data) {
  192. layer.close(index); //关闭弹层
  193. if (data == "success") {
  194. table.reload('LAY-list-manage'); //数据刷新
  195. } else {
  196. layer.msg(data);
  197. }
  198. }
  199. });
  200. });
  201. submit.trigger('click');
  202. }
  203. , success: function (layero, index) {
  204. }
  205. });
  206. layer.full(perContent);
  207. }
  208. });
  209. //监听搜索
  210. form.on('submit(LAY-list-front-search)', function (data) {
  211. var field = data.field;
  212. //执行重载
  213. table.reload('LAY-list-manage', {
  214. where: field,
  215. page: {
  216. curr: 1
  217. }
  218. });
  219. });
  220. form.on('submit(LAY-list-front-searchall)', function (data) {
  221. table.reload('LAY-list-manage', {
  222. where: null,
  223. page: {
  224. curr: 1
  225. }
  226. });
  227. });
  228. //事件
  229. var active = {
  230. batchdel: function () {
  231. var checkStatus = table.checkStatus('LAY-list-manage')
  232. , data = checkStatus.data; //得到选中的数据
  233. if (data.length < 1) {
  234. parent.layer.msg("请选择要删除的项");
  235. } else {
  236. var ids = "";
  237. $.each(data, function (index, value) {
  238. ids += data[index].Id + ",";
  239. });
  240. ids = ids.substring(0, ids.length - 1);
  241. var index = layer.confirm('确定要删除吗?删除后不能恢复!', function (index) {
  242. $.ajax({
  243. type: "POST",
  244. url: "/Admin/Products/Delete?r=" + Math.random(1),
  245. data: "Id=" + ids,
  246. dataType: "text",
  247. success: function (data) {
  248. layer.close(index);
  249. if (data == "success") {
  250. table.reload('LAY-list-manage');
  251. } else {
  252. layer.msg(data);
  253. }
  254. }
  255. });
  256. });
  257. }
  258. }
  259. , add: function () {
  260. var perContent = layer.open({
  261. type: 2
  262. , title: '商品信息-添加'
  263. , content: 'Add?right=' + right
  264. , maxmin: true
  265. , area: ['500px', '450px']
  266. , btn: ['确定', '取消']
  267. , yes: function (index, layero) {
  268. var iframeWindow = window['layui-layer-iframe' + index]
  269. , submitID = 'LAY-list-front-submit'
  270. , submit = layero.find('iframe').contents().find('#' + submitID);
  271. setTimeout(function () {
  272. layero.find('iframe').contents().find('.layui-tab-item').each(function (i) {
  273. var errObj = $(this).find('.layui-form-danger');
  274. if (errObj.length > 0) {
  275. iframeWindow.element.tabChange('mytabbar', String(i + 1));
  276. submit.click();
  277. }
  278. });
  279. }, 300);
  280. //内容
  281. iframeWindow.Contentsedit.sync();
  282. //图集
  283. iframeWindow.checkPics("DetailPicPath");
  284. //监听提交
  285. iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
  286. if (clickFlag) {
  287. clickFlag = false;
  288. var field = data.field; //获取提交的字段
  289. var userdata = "";
  290. for (var prop in field) {
  291. userdata += prop + "=" + encodeURIComponent(field[prop]) + "&";
  292. }
  293. //提交 Ajax 成功后,静态更新表格中的数据
  294. //$.ajax({});
  295. $.ajax({
  296. type: "POST",
  297. url: "/Admin/Products/Add?r=" + Math.random(1),
  298. data: userdata,
  299. dataType: "text",
  300. success: function (data) {
  301. clickFlag = true;
  302. layer.close(index); //关闭弹层
  303. if (data == "success") {
  304. table.reload('LAY-list-manage'); //数据刷新
  305. } else {
  306. layer.msg(data);
  307. }
  308. }
  309. });
  310. }
  311. });
  312. submit.trigger('click');
  313. }
  314. });
  315. layer.full(perContent);
  316. }
  317. , ImportData: function () {
  318. layer.open({
  319. type: 2,
  320. title: '导入',
  321. maxmin: false,
  322. area: ['460px', '180px'],
  323. content: $('#excelForm'),
  324. cancel: function () {
  325. }
  326. });
  327. }
  328. , ExportExcel: function () {
  329. var userdata = '';
  330. $(".layuiadmin-card-header-auto input").each(function (i) {
  331. userdata += $(this).attr('name') + '=' + encodeURIComponent($(this).val()) + '&';
  332. });
  333. $(".layuiadmin-card-header-auto select").each(function (i) {
  334. userdata += $(this).attr('name') + '=' + encodeURIComponent($(this).val()) + '&';
  335. });
  336. $.ajax({
  337. type: "GET",
  338. url: "/Admin/Products/ExportExcel?r=" + Math.random(1),
  339. data: userdata,
  340. dataType: "json",
  341. success: function (data) {
  342. data.Obj.unshift(data.Fields);
  343. excel.exportExcel(data.Obj, data.Info, 'xlsx');
  344. }
  345. });
  346. }
  347. , Open: function () {
  348. var checkStatus = table.checkStatus('LAY-list-manage')
  349. , data = checkStatus.data; //得到选中的数据
  350. if (data.length < 1) {
  351. parent.layer.msg("请选择要开启的项");
  352. } else {
  353. var ids = "";
  354. $.each(data, function (index, value) {
  355. ids += data[index].Id + ",";
  356. });
  357. ids = ids.substring(0, ids.length - 1);
  358. var index = layer.confirm('确定要开启吗?', function (index) {
  359. $.ajax({
  360. type: "POST",
  361. url: "/Admin/Products/Open?r=" + Math.random(1),
  362. data: "Id=" + ids,
  363. dataType: "text",
  364. success: function (data) {
  365. layer.close(index);
  366. if (data == "success") {
  367. table.reload('LAY-list-manage');
  368. } else {
  369. layer.msg(data);
  370. }
  371. }
  372. });
  373. });
  374. }
  375. }
  376. , Close: function () {
  377. var checkStatus = table.checkStatus('LAY-list-manage')
  378. , data = checkStatus.data; //得到选中的数据
  379. if (data.length < 1) {
  380. parent.layer.msg("请选择要关闭的项");
  381. } else {
  382. var ids = "";
  383. $.each(data, function (index, value) {
  384. ids += data[index].Id + ",";
  385. });
  386. ids = ids.substring(0, ids.length - 1);
  387. var index = layer.confirm('确定要关闭吗?', function (index) {
  388. $.ajax({
  389. type: "POST",
  390. url: "/Admin/Products/Close?r=" + Math.random(1),
  391. data: "Id=" + ids,
  392. dataType: "text",
  393. success: function (data) {
  394. layer.close(index);
  395. if (data == "success") {
  396. table.reload('LAY-list-manage');
  397. } else {
  398. layer.msg(data);
  399. }
  400. }
  401. });
  402. });
  403. }
  404. }
  405. , UsingToSpe: function () {
  406. var checkStatus = table.checkStatus('LAY-list-manage')
  407. , data = checkStatus.data; //得到选中的数据
  408. if (data.length < 1) {
  409. parent.layer.msg("请选择要引用的项");
  410. } else {
  411. var ids = "";
  412. $.each(data, function (index, value) {
  413. ids += data[index].Id + ",";
  414. });
  415. ids = ids.substring(0, ids.length - 1);
  416. layer.open({
  417. type: 2
  418. , title: '商品引用'
  419. , content: '/Admin/Articles/OperateSpecial?Kind=Using'
  420. , maxmin: false
  421. , area: ['400px', '650px']
  422. , btn: ['确定', '取消']
  423. , yes: function (index, layero) {
  424. var iframeWindow = window['layui-layer-iframe' + index]
  425. , submitID = 'LAY-list-front-submit'
  426. , submit = layero.find('iframe').contents().find('#' + submitID);
  427. //var ColListCheckedData = iframeWindow.tree.getChecked('ColListTreeID');
  428. //iframeWindow.ids = "";
  429. //iframeWindow.getChildren(ColListCheckedData);
  430. //if (iframeWindow.ids.indexOf(',') < 0) {
  431. // parent.layer.msg("请选择要引用的栏目");
  432. // return;
  433. //}
  434. //layero.find('iframe').contents().find("#ColList").val(iframeWindow.ids.substring(0, iframeWindow.ids.length - 1));
  435. var checkcount = layero.find('iframe').contents().find('input[type=checkbox][name=ColLists]:checked').length;
  436. if (checkcount < 1) {
  437. parent.layer.msg("请选择要引用的栏目");
  438. return;
  439. }
  440. var ColList = '';
  441. layero.find('iframe').contents().find('input[type=checkbox][name=ColLists]:checked').each(function (i) {
  442. ColList += $(this).val() + ',';
  443. });
  444. if (ColList != '') {
  445. ColList = ColList.substring(0, ColList.length - 1);
  446. }
  447. //监听提交
  448. iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
  449. var field = data.field; //获取提交的字段
  450. var userdata = "";
  451. for (var prop in field) {
  452. userdata += prop + "=" + encodeURIComponent(field[prop]) + "&";
  453. }
  454. $.ajax({
  455. type: "POST",
  456. url: "/Admin/Products/UseToSpecial?r=" + Math.random(1),
  457. data: userdata + "&ids=" + ids + "&ColList=" + ColList,
  458. dataType: "text",
  459. success: function (data) {
  460. layer.close(index); //关闭弹层
  461. if (data == "success") {
  462. layer.msg("引用成功");
  463. } else {
  464. layer.msg(data);
  465. }
  466. }
  467. });
  468. });
  469. submit.trigger('click');
  470. }
  471. });
  472. }
  473. }
  474. };
  475. $('.layui-btn').on('click', function () {
  476. var type = $(this).data('type');
  477. active[type] ? active[type].call(this) : '';
  478. });
  479. });