Products_Admin.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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', title: '商品名称', sort: true }
  63. , { field: 'Stock', title: '库存', sort: true }
  64. , { field: 'BuyCount', title: '已购买数', sort: true }
  65. , { field: 'Price', title: '价格', sort: true }
  66. , { field: 'Integral', title: '抵扣积分', sort: true }
  67. , { field: 'MemberPrice', title: '会员价', sort: true }
  68. , { field: 'UserIntegral', title: '会员抵扣积分', sort: true }
  69. , { field: 'Status', 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-220'
  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. });
  215. });
  216. form.on('submit(LAY-list-front-searchall)', function (data) {
  217. table.reload('LAY-list-manage', {
  218. where: null
  219. });
  220. });
  221. //事件
  222. var active = {
  223. batchdel: function () {
  224. var checkStatus = table.checkStatus('LAY-list-manage')
  225. , data = checkStatus.data; //得到选中的数据
  226. if (data.length < 1) {
  227. parent.layer.msg("请选择要删除的项");
  228. } else {
  229. var ids = "";
  230. $.each(data, function (index, value) {
  231. ids += data[index].Id + ",";
  232. });
  233. ids = ids.substring(0, ids.length - 1);
  234. var index = layer.confirm('确定要删除吗?删除后不能恢复!', function (index) {
  235. $.ajax({
  236. type: "POST",
  237. url: "/Admin/Products/Delete?r=" + Math.random(1),
  238. data: "Id=" + ids,
  239. dataType: "text",
  240. success: function (data) {
  241. layer.close(index);
  242. if (data == "success") {
  243. table.reload('LAY-list-manage');
  244. } else {
  245. layer.msg(data);
  246. }
  247. }
  248. });
  249. });
  250. }
  251. }
  252. , add: function () {
  253. var perContent = layer.open({
  254. type: 2
  255. , title: '商品信息-添加'
  256. , content: 'Add?right=' + right
  257. , maxmin: true
  258. , area: ['500px', '450px']
  259. , btn: ['确定', '取消']
  260. , yes: function (index, layero) {
  261. var iframeWindow = window['layui-layer-iframe' + index]
  262. , submitID = 'LAY-list-front-submit'
  263. , submit = layero.find('iframe').contents().find('#' + submitID);
  264. setTimeout(function () {
  265. layero.find('iframe').contents().find('.layui-tab-item').each(function (i) {
  266. var errObj = $(this).find('.layui-form-danger');
  267. if (errObj.length > 0) {
  268. iframeWindow.element.tabChange('mytabbar', String(i + 1));
  269. submit.click();
  270. }
  271. });
  272. }, 300);
  273. //内容
  274. iframeWindow.Contentsedit.sync();
  275. //图集
  276. iframeWindow.checkPics("DetailPicPath");
  277. //监听提交
  278. iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
  279. if (clickFlag) {
  280. clickFlag = false;
  281. var field = data.field; //获取提交的字段
  282. var userdata = "";
  283. for (var prop in field) {
  284. userdata += prop + "=" + encodeURIComponent(field[prop]) + "&";
  285. }
  286. //提交 Ajax 成功后,静态更新表格中的数据
  287. //$.ajax({});
  288. $.ajax({
  289. type: "POST",
  290. url: "/Admin/Products/Add?r=" + Math.random(1),
  291. data: userdata,
  292. dataType: "text",
  293. success: function (data) {
  294. clickFlag = true;
  295. layer.close(index); //关闭弹层
  296. if (data == "success") {
  297. table.reload('LAY-list-manage'); //数据刷新
  298. } else {
  299. layer.msg(data);
  300. }
  301. }
  302. });
  303. }
  304. });
  305. submit.trigger('click');
  306. }
  307. });
  308. layer.full(perContent);
  309. }
  310. , ImportData: function () {
  311. layer.open({
  312. type: 2,
  313. title: '导入',
  314. maxmin: false,
  315. area: ['460px', '180px'],
  316. content: $('#excelForm'),
  317. cancel: function () {
  318. }
  319. });
  320. }
  321. , ExportExcel: function () {
  322. var userdata = '';
  323. $(".layuiadmin-card-header-auto input").each(function (i) {
  324. userdata += $(this).attr('name') + '=' + encodeURIComponent($(this).val()) + '&';
  325. });
  326. $(".layuiadmin-card-header-auto select").each(function (i) {
  327. userdata += $(this).attr('name') + '=' + encodeURIComponent($(this).val()) + '&';
  328. });
  329. $.ajax({
  330. type: "GET",
  331. url: "/Admin/Products/ExportExcel?r=" + Math.random(1),
  332. data: userdata,
  333. dataType: "json",
  334. success: function (data) {
  335. data.Obj.unshift(data.Fields);
  336. excel.exportExcel(data.Obj, data.Info, 'xlsx');
  337. }
  338. });
  339. }
  340. , Open: function () {
  341. var checkStatus = table.checkStatus('LAY-list-manage')
  342. , data = checkStatus.data; //得到选中的数据
  343. if (data.length < 1) {
  344. parent.layer.msg("请选择要开启的项");
  345. } else {
  346. var ids = "";
  347. $.each(data, function (index, value) {
  348. ids += data[index].Id + ",";
  349. });
  350. ids = ids.substring(0, ids.length - 1);
  351. var index = layer.confirm('确定要开启吗?', function (index) {
  352. $.ajax({
  353. type: "POST",
  354. url: "/Admin/Products/Open?r=" + Math.random(1),
  355. data: "Id=" + ids,
  356. dataType: "text",
  357. success: function (data) {
  358. layer.close(index);
  359. if (data == "success") {
  360. table.reload('LAY-list-manage');
  361. } else {
  362. layer.msg(data);
  363. }
  364. }
  365. });
  366. });
  367. }
  368. }
  369. , Close: function () {
  370. var checkStatus = table.checkStatus('LAY-list-manage')
  371. , data = checkStatus.data; //得到选中的数据
  372. if (data.length < 1) {
  373. parent.layer.msg("请选择要关闭的项");
  374. } else {
  375. var ids = "";
  376. $.each(data, function (index, value) {
  377. ids += data[index].Id + ",";
  378. });
  379. ids = ids.substring(0, ids.length - 1);
  380. var index = layer.confirm('确定要关闭吗?', function (index) {
  381. $.ajax({
  382. type: "POST",
  383. url: "/Admin/Products/Close?r=" + Math.random(1),
  384. data: "Id=" + ids,
  385. dataType: "text",
  386. success: function (data) {
  387. layer.close(index);
  388. if (data == "success") {
  389. table.reload('LAY-list-manage');
  390. } else {
  391. layer.msg(data);
  392. }
  393. }
  394. });
  395. });
  396. }
  397. }
  398. , UsingToSpe: function () {
  399. var checkStatus = table.checkStatus('LAY-list-manage')
  400. , data = checkStatus.data; //得到选中的数据
  401. if (data.length < 1) {
  402. parent.layer.msg("请选择要引用的项");
  403. } else {
  404. var ids = "";
  405. $.each(data, function (index, value) {
  406. ids += data[index].Id + ",";
  407. });
  408. ids = ids.substring(0, ids.length - 1);
  409. layer.open({
  410. type: 2
  411. , title: '商品引用'
  412. , content: '/Admin/Articles/OperateSpecial?Kind=Using'
  413. , maxmin: false
  414. , area: ['400px', '650px']
  415. , btn: ['确定', '取消']
  416. , yes: function (index, layero) {
  417. var iframeWindow = window['layui-layer-iframe' + index]
  418. , submitID = 'LAY-list-front-submit'
  419. , submit = layero.find('iframe').contents().find('#' + submitID);
  420. //var ColListCheckedData = iframeWindow.tree.getChecked('ColListTreeID');
  421. //iframeWindow.ids = "";
  422. //iframeWindow.getChildren(ColListCheckedData);
  423. //if (iframeWindow.ids.indexOf(',') < 0) {
  424. // parent.layer.msg("请选择要引用的栏目");
  425. // return;
  426. //}
  427. //layero.find('iframe').contents().find("#ColList").val(iframeWindow.ids.substring(0, iframeWindow.ids.length - 1));
  428. var checkcount = layero.find('iframe').contents().find('input[type=checkbox][name=ColLists]:checked').length;
  429. if (checkcount < 1) {
  430. parent.layer.msg("请选择要引用的栏目");
  431. return;
  432. }
  433. var ColList = '';
  434. layero.find('iframe').contents().find('input[type=checkbox][name=ColLists]:checked').each(function (i) {
  435. ColList += $(this).val() + ',';
  436. });
  437. if (ColList != '') {
  438. ColList = ColList.substring(0, ColList.length - 1);
  439. }
  440. //监听提交
  441. iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
  442. var field = data.field; //获取提交的字段
  443. var userdata = "";
  444. for (var prop in field) {
  445. userdata += prop + "=" + encodeURIComponent(field[prop]) + "&";
  446. }
  447. $.ajax({
  448. type: "POST",
  449. url: "/Admin/Products/UseToSpecial?r=" + Math.random(1),
  450. data: userdata + "&ids=" + ids + "&ColList=" + ColList,
  451. dataType: "text",
  452. success: function (data) {
  453. layer.close(index); //关闭弹层
  454. if (data == "success") {
  455. layer.msg("引用成功");
  456. } else {
  457. layer.msg(data);
  458. }
  459. }
  460. });
  461. });
  462. submit.trigger('click');
  463. }
  464. });
  465. }
  466. }
  467. };
  468. $('.layui-btn').on('click', function () {
  469. var type = $(this).data('type');
  470. active[type] ? active[type].call(this) : '';
  471. });
  472. });