SysAdmin_Admin.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. var ExcelData,ExcelKind;
  2. function ConfirmImport() {
  3. $.ajax({
  4. type: "POST",
  5. url: "/Admin/SysAdmin/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("导入成功", { time: 2000 }, function () {
  11. window.location.reload();
  12. });
  13. } else {
  14. layer.msg(data);
  15. }
  16. }
  17. });
  18. }
  19. var excel;
  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. var layLastLoginDate = laydate.render({
  32. elem: '#LastLoginDate',
  33. trigger: 'click',
  34. type: 'datetime',
  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. layLastLoginDate.hint('日期范围请不要超过1个月');
  42. setTimeout(function () {
  43. $(".laydate-btns-confirm").addClass("laydate-disabled");
  44. }, 1);
  45. }
  46. } else {
  47. op = false;
  48. layLastLoginDate.hint('日期范围请不要超过1个月');
  49. setTimeout(function () {
  50. $(".laydate-btns-confirm").addClass("laydate-disabled");
  51. }, 1);
  52. }
  53. if (op) {
  54. $('#LastLoginDate').val(value);
  55. }
  56. }
  57. });
  58. //excel导入
  59. 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. ,data = obj.data //得到所在行所有键值
  70. ,field = obj.field; //得到字段
  71. if(field == "Sort"){
  72. $.ajax({
  73. type: "POST",
  74. url: "/Admin/SysAdmin/Sort?r=" + Math.random(1),
  75. data: "Id=" + data.Id + "&Sort=" + value,
  76. dataType: "text",
  77. success: function (data) {
  78. }
  79. });
  80. }
  81. });
  82. //列表数据
  83. table.render({
  84. elem: '#LAY-list-manage'
  85. , url: '/Admin/SysAdmin/IndexData' //模拟接口
  86. , cols: [[
  87. { type: 'checkbox', fixed: 'left' }
  88. , {field:'Id', fixed: 'left', title:'ID', width:80, sort: true, unresize: true}
  89. ,{field:'AdminName', width: 200, title:'用户名', sort: true}
  90. ,{field:'RealName', width: 200, title:'名称', sort: true}
  91. ,{field:'Role', width: 200, title:'角色', sort: true}
  92. ,{field:'LastLoginDate', width: 200, title:'最后登录时间', sort: true}
  93. , {field:'Sort', fixed: 'right', title:'排序', width:80, edit: 'text'}
  94. , { title: '操作', align: 'center', fixed: 'right', toolbar: '#table-list-tools' }
  95. ]]
  96. , where: {
  97. }
  98. , page: true
  99. , limit: 30
  100. , height: 'full-220'
  101. , text: '对不起,加载出现异常!'
  102. , done: function (res, curr, count) {
  103. $(".layui-none").text("无数据");
  104. }
  105. });
  106. //监听工具条
  107. table.on('tool(LAY-list-manage)', function (obj) {
  108. var data = obj.data;
  109. if (obj.event === 'del') {
  110. var index = layer.confirm('确定要删除吗?删除后不能恢复!', function (index) {
  111. $.ajax({
  112. type: "POST",
  113. url: "/Admin/SysAdmin/Delete?r=" + Math.random(1),
  114. data: "Id=" + data.Id,
  115. dataType: "text",
  116. success: function (data) {
  117. if (data == "success") {
  118. obj.del();
  119. layer.close(index);
  120. } else {
  121. parent.layer.msg(data);
  122. }
  123. }
  124. });
  125. });
  126. } else if (obj.event === 'edit') {
  127. var tr = $(obj.tr);
  128. var perContent = layer.open({
  129. type: 2
  130. , title: '后台管理员-编辑'
  131. , content: 'Edit?Id=' + data.Id + ''
  132. , maxmin: true
  133. , area: ['500px', '450px']
  134. , btn: ['确定', '取消']
  135. , yes: function (index, layero) {
  136. var iframeWindow = window['layui-layer-iframe' + index]
  137. , submitID = 'LAY-list-front-submit'
  138. , submit = layero.find('iframe').contents().find('#' + submitID);
  139. setTimeout(function () {
  140. layero.find('iframe').contents().find('.layui-tab-item').each(function (i) {
  141. var errObj = $(this).find('.layui-form-danger');
  142. if (errObj.length > 0) {
  143. iframeWindow.element.tabChange('mytabbar', String(i + 1));
  144. submit.click();
  145. }
  146. });
  147. }, 300);
  148. //监听提交
  149. iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
  150. var field = data.field; //获取提交的字段
  151. var userdata = "";
  152. for (var prop in field) {
  153. userdata += prop + "=" + encodeURIComponent(field[prop]) + "&";
  154. }
  155. //提交 Ajax 成功后,静态更新表格中的数据
  156. //$.ajax({});
  157. $.ajax({
  158. type: "POST",
  159. url: "/Admin/SysAdmin/Edit?r=" + Math.random(1),
  160. data: userdata,
  161. dataType: "text",
  162. success: function (data) {
  163. layer.close(index); //关闭弹层
  164. if (data == "success") {
  165. table.reload('LAY-list-manage'); //数据刷新
  166. } else {
  167. layer.msg(data);
  168. }
  169. }
  170. });
  171. });
  172. submit.trigger('click');
  173. }
  174. , success: function (layero, index) {
  175. }
  176. });
  177. layer.full(perContent);
  178. }
  179. });
  180. //监听搜索
  181. form.on('submit(LAY-list-front-search)', function (data) {
  182. var field = data.field;
  183. //执行重载
  184. table.reload('LAY-list-manage', {
  185. where: field
  186. });
  187. });
  188. form.on('submit(LAY-list-front-searchall)', function (data) {
  189. table.reload('LAY-list-manage', {
  190. where: null
  191. });
  192. });
  193. //事件
  194. var active = {
  195. batchdel: function () {
  196. var checkStatus = table.checkStatus('LAY-list-manage')
  197. , data = checkStatus.data; //得到选中的数据
  198. if (data.length < 1) {
  199. parent.layer.msg("请选择要删除的项");
  200. } else {
  201. var ids = "";
  202. $.each(data, function (index, value) {
  203. ids += data[index].Id + ",";
  204. });
  205. ids = ids.substring(0, ids.length - 1);
  206. var index = layer.confirm('确定要删除吗?删除后不能恢复!', function (index) {
  207. $.ajax({
  208. type: "POST",
  209. url: "/Admin/SysAdmin/Delete?r=" + Math.random(1),
  210. data: "Id=" + ids,
  211. dataType: "text",
  212. success: function (data) {
  213. layer.close(index);
  214. if (data == "success") {
  215. table.reload('LAY-list-manage');
  216. } else {
  217. layer.msg(data);
  218. }
  219. }
  220. });
  221. });
  222. }
  223. }
  224. , add: function () {
  225. var perContent = layer.open({
  226. type: 2
  227. , title: '后台管理员-添加'
  228. , content: 'Add'
  229. , maxmin: true
  230. , area: ['500px', '450px']
  231. , btn: ['确定', '取消']
  232. , yes: function (index, layero) {
  233. var iframeWindow = window['layui-layer-iframe' + index]
  234. , submitID = 'LAY-list-front-submit'
  235. , submit = layero.find('iframe').contents().find('#' + submitID);
  236. setTimeout(function () {
  237. layero.find('iframe').contents().find('.layui-tab-item').each(function (i) {
  238. var errObj = $(this).find('.layui-form-danger');
  239. if (errObj.length > 0) {
  240. iframeWindow.element.tabChange('mytabbar', String(i + 1));
  241. submit.click();
  242. }
  243. });
  244. }, 300);
  245. //监听提交
  246. iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
  247. var field = data.field; //获取提交的字段
  248. var userdata = "";
  249. for (var prop in field) {
  250. userdata += prop + "=" + encodeURIComponent(field[prop]) + "&";
  251. }
  252. //提交 Ajax 成功后,静态更新表格中的数据
  253. //$.ajax({});
  254. $.ajax({
  255. type: "POST",
  256. url: "/Admin/SysAdmin/Add?r=" + Math.random(1),
  257. data: userdata,
  258. dataType: "text",
  259. success: function (data) {
  260. layer.close(index); //关闭弹层
  261. if (data == "success") {
  262. table.reload('LAY-list-manage'); //数据刷新
  263. } else {
  264. layer.msg(data);
  265. }
  266. }
  267. });
  268. });
  269. submit.trigger('click');
  270. }
  271. });
  272. layer.full(perContent);
  273. }
  274. , ImportData: function () {
  275. ExcelKind = 1;
  276. layer.open({
  277. type: 1,
  278. title: '导入',
  279. maxmin: false,
  280. area: ['460px', '280px'],
  281. content: $('#excelForm'),
  282. cancel: function () {
  283. }
  284. });
  285. $("#excelTemp").html('<a href="/excelfile/模板文件.xlsx">点击下载模板文件</a>');
  286. }
  287. , ExportExcel: function () {
  288. var userdata = '';
  289. $(".layuiadmin-card-header-auto input").each(function (i) {
  290. userdata += $(this).attr('name') + '=' + encodeURIComponent($(this).val()) + '&';
  291. });
  292. $(".layuiadmin-card-header-auto select").each(function (i) {
  293. userdata += $(this).attr('name') + '=' + encodeURIComponent($(this).val()) + '&';
  294. });
  295. $.ajax({
  296. type: "GET",
  297. url: "/Admin/SysAdmin/ExportExcel?r=" + Math.random(1),
  298. data: userdata,
  299. dataType: "json",
  300. success: function (data) {
  301. data.Obj.unshift(data.Fields);
  302. excel.exportExcel(data.Obj, data.Info, 'xlsx');
  303. }
  304. });
  305. }
  306. , Open: function () {
  307. var checkStatus = table.checkStatus('LAY-list-manage')
  308. , data = checkStatus.data; //得到选中的数据
  309. if(data.length < 1){
  310. parent.layer.msg("请选择要开启的项");
  311. }else{
  312. var ids = "";
  313. $.each(data, function (index, value) {
  314. ids += data[index].Id + ",";
  315. });
  316. ids = ids.substring(0, ids.length - 1);
  317. var index = layer.confirm('确定要开启吗?', function (index) {
  318. $.ajax({
  319. type: "POST",
  320. url: "/Admin/SysAdmin/Open?r=" + Math.random(1),
  321. data: "Id=" + ids,
  322. dataType: "text",
  323. success: function (data) {
  324. layer.close(index);
  325. if (data == "success") {
  326. table.reload('LAY-list-manage');
  327. } else {
  328. layer.msg(data);
  329. }
  330. }
  331. });
  332. });
  333. }
  334. }
  335. , Close: function () {
  336. var checkStatus = table.checkStatus('LAY-list-manage')
  337. , data = checkStatus.data; //得到选中的数据
  338. if(data.length < 1){
  339. parent.layer.msg("请选择要关闭的项");
  340. }else{
  341. var ids = "";
  342. $.each(data, function (index, value) {
  343. ids += data[index].Id + ",";
  344. });
  345. ids = ids.substring(0, ids.length - 1);
  346. var index = layer.confirm('确定要关闭吗?', function (index) {
  347. $.ajax({
  348. type: "POST",
  349. url: "/Admin/SysAdmin/Close?r=" + Math.random(1),
  350. data: "Id=" + ids,
  351. dataType: "text",
  352. success: function (data) {
  353. layer.close(index);
  354. if (data == "success") {
  355. table.reload('LAY-list-manage');
  356. } else {
  357. layer.msg(data);
  358. }
  359. }
  360. });
  361. });
  362. }
  363. }
  364. };
  365. $('.layui-btn').on('click', function () {
  366. var type = $(this).data('type');
  367. active[type] ? active[type].call(this) : '';
  368. });
  369. });