Products_Admin.js 22 KB

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