Coupons_Admin.js 16 KB

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