global.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /*全局自适应高度*/
  2. /*全局自适应高度*/
  3. function autoFlashHeight(){
  4. function getObjHeight(select) {
  5. return select.length > 0 ? select.height() : 0;
  6. }
  7. /*侧栏高度*/
  8. var sBar1 = getObjHeight($(".sjs-bar-1"));
  9. var sBar2 = getObjHeight($(".sjs-bar-2"));
  10. var sBar3 = getObjHeight($(".sjs-bar-3"));
  11. var sBar4 = getObjHeight($(".sjs-bar-4"));
  12. var sBar5 = getObjHeight($(".sjs-bar-5"));
  13. /*侧栏高度*/
  14. var cHeader = getObjHeight($(".c-header"));
  15. var sBar = getObjHeight($(".sjs-bar"));
  16. var sBart = getObjHeight($(".sjs-bart"));
  17. var sBarz = getObjHeight($(".side-bar"));
  18. var pBarz = getObjHeight($(".toolsbar-f"));
  19. var bdtopc = getObjHeight($(".body-height-top"));
  20. var bcontent = getObjHeight($(".bcontent-wrap"));
  21. $(".sjs-height-0").height($(window).height()-cHeader-90+53);
  22. $(".sjs-height-1").height($(window).height()-cHeader-bcontent-90+53);
  23. $(".sjs-height-2").height($(window).height()-cHeader-sBarz-120);
  24. $(".sjs-height-3").height($(window).height()-cHeader-sBar-492+25);/*492*/
  25. $(".sjs-height-4").height($(window).height()-cHeader-pBarz-110+75);
  26. $(".sjs-height-5").height($(window).height()-cHeader-sBar-492+55);/*492*/
  27. $(".sp-wrap").height(bcontent-30);
  28. /*侧栏高度*/
  29. $(".sjs-sh-1").height($(window).height()-cHeader-sBar1-92+55);
  30. $(".sjs-sh-2").height($(window).height()-cHeader-sBar2-92+55);
  31. $(".sjs-sh-3").height($(window).height()-cHeader-sBar3-92+55);
  32. $(".sjs-sh-4").height($(window).height()-cHeader-sBar4-92+55);
  33. $(".sjs-sh-5").height($(window).height()-cHeader-sBar5-92+55);
  34. };
  35. $(window).resize(autoFlashHeight);
  36. /*全局自适应高度结束*/
  37. $(function(){
  38. /*侧滑*/
  39. $(".open-sidebar").click(function(){
  40. $(".slide-sidebar").animate({width:"800"}).addClass("open");
  41. });
  42. $("body").click(function(event){
  43. var e = event || window.event; //浏览器兼容性
  44. if(!$(event.target).is('a')) {
  45. var elem = event.target || e.srcElement;
  46. while (elem) { //循环判断至跟节点,防止点击的是div子元素
  47. if (elem.className == "open-sidebar" || elem.className == 'slide-sidebar open') {
  48. return false;
  49. }
  50. elem = elem.parentNode;
  51. }
  52. $(".slide-sidebar").animate({width:"0"}).removeClass("open")// 关闭处理
  53. }
  54. });
  55. /*侧滑*/
  56. /*工具提示*/
  57. $(function () {
  58. $('[data-toggle="tooltip"]').tooltip()
  59. });
  60. /*侧栏菜单*/
  61. $(".bg-nav > li > a").click(function() {
  62. var self = $(this);
  63. var subMenu = $(this).siblings('ul.sub-menu');
  64. if(subMenu.length > 0) {
  65. if(subMenu.is(":visible")) {
  66. self.find('.menu-arrow').removeClass('fa-angle-up').addClass('fa-angle-down');
  67. subMenu.slideUp('fast');
  68. self.parent().removeClass('active');
  69. }else{
  70. self.parent().addClass('active');
  71. self.find('.menu-arrow').removeClass('fa-angle-down').addClass('fa-angle-up');
  72. subMenu.slideDown('fast');
  73. }
  74. }
  75. });
  76. // 数据提交
  77. $("#submit-form").click(function() {
  78. $("#save-form").submit();
  79. });
  80. // modal弹窗拖动
  81. // $(document).on("show.bs.modal", ".modal", function() {
  82. // $(this).draggable({
  83. // handle: ".modal-header" // 只能点击头部拖动
  84. // });
  85. // $(this).css("overflow", "hidden");
  86. // });
  87. // 动态生成datepicker
  88. $('body').on('focus', ".datepicker-here", function (e) {
  89. if ($(this).data("datepicker") != "1") {
  90. $(this).datepicker();
  91. }
  92. $(this).data("datepicker", "1");
  93. });
  94. });
  95. /**
  96. * 提示框
  97. *
  98. * @param string message
  99. * @param string type
  100. * @param string icon
  101. * @return void
  102. */
  103. function toast(message, type, icon) {
  104. var toast = $(".toast");
  105. toast.addClass(type);
  106. toast.children('.message').html(message);
  107. var iconClass = 'fa-' + icon;
  108. toast.children('.icon').addClass(iconClass);
  109. toast.fadeIn(500);
  110. setTimeout(function() {
  111. toast.fadeOut('fast');
  112. toast.children('.message').text('');
  113. toast.children('.icon').removeClass(iconClass);
  114. }, 3000);
  115. }
  116. /**
  117. * 动态请求数据
  118. * @param {String} url - 请求链接
  119. * @param data - 提交数据
  120. * @param {function} successCallback - 返回成功回调
  121. * @param {function} errorCallBack - 返回失败回调
  122. */
  123. const postData = function (url, data, successCallback, errorCallBack, showWaiting = true) {
  124. if (showWaiting) showWaitingView();
  125. $.ajax({
  126. type:"POST",
  127. url: url,
  128. data: {'data': JSON.stringify(data)},
  129. dataType: 'json',
  130. cache: false,
  131. timeout: 60000,
  132. beforeSend: function(xhr) {
  133. let csrfToken = Cookies.get('csrfToken');
  134. xhr.setRequestHeader('x-csrf-token', csrfToken);
  135. },
  136. success: function(result){
  137. if (result.err === 0) {
  138. if (successCallback) {
  139. successCallback(result.data);
  140. }
  141. } else {
  142. toastr.error('error: ' + result.msg);
  143. if (errorCallBack) {
  144. errorCallBack(result.msg);
  145. }
  146. }
  147. if (showWaiting) closeWaitingView();
  148. },
  149. error: function(jqXHR, textStatus, errorThrown){
  150. toastr.error('error: ' + textStatus + " " + errorThrown);
  151. if (errorCallBack) {
  152. errorCallBack();
  153. }
  154. if (showWaiting) closeWaitingView();
  155. }
  156. });
  157. };
  158. /**
  159. * 动态请求数据(压缩数据)
  160. * @param {String} url - 请求链接
  161. * @param data - 提交数据
  162. * @param {function} successCallback - 返回成功回调
  163. * @param {function} errorCallBack - 返回失败回调
  164. */
  165. const postDataCompress = function (url, data, successCallback, errorCallBack, htype = 'progress') {
  166. if (htype === 'wait') showWaitingView();
  167. if (htype === 'progress') showProgress();
  168. $.ajax({
  169. type:"POST",
  170. url: url,
  171. data: {'data': LZString.compressToUTF16(JSON.stringify(data))},
  172. dataType: 'json',
  173. cache: false,
  174. timeout: 80000, // 导入清单Excel(10w行)预计需要时间
  175. beforeSend: function(xhr) {
  176. let csrfToken = Cookies.get('csrfToken');
  177. xhr.setRequestHeader('x-csrf-token', csrfToken);
  178. },
  179. success: function(result){
  180. if (htype === 'progress') doneProgress();
  181. if (result.err === 0) {
  182. if (successCallback) {
  183. successCallback(result.data);
  184. }
  185. } else {
  186. toastr.error('error: ' + result.msg);
  187. if (errorCallBack) {
  188. errorCallBack(result.msg);
  189. }
  190. }
  191. if (htype === 'wait') closeWaitingView();
  192. if (htype === 'progress') closeProgress();
  193. },
  194. error: function(jqXHR, textStatus, errorThrown){
  195. toastr.error('error: ' + textStatus + " " + errorThrown);
  196. if (errorCallBack) {
  197. errorCallBack();
  198. }
  199. if (htype === 'wait') closeWaitingView();
  200. if (htype === 'progress') closeProgress();
  201. }
  202. });
  203. };
  204. /**
  205. * 动态请求数据
  206. * @param {String} url - 请求链接
  207. * @param data - 提交数据
  208. * @param {function} successCallback - 返回成功回调
  209. * @param {function} errorCallBack - 返回失败回调
  210. */
  211. const postDataWithFile = function (url, formData, successCallback, errorCallBack, showWaiting = true) {
  212. if (showWaiting) showWaitingView();
  213. $.ajax({
  214. type:"POST",
  215. url: url,
  216. data: formData,
  217. dataType: 'json',
  218. cache: false,
  219. // 告诉jQuery不要去设置Content-Type请求头
  220. contentType: false,
  221. // 告诉jQuery不要去处理发送的数据
  222. processData: false,
  223. timeout: 5000,
  224. beforeSend: function(xhr) {
  225. let csrfToken = Cookies.get('csrfToken');
  226. xhr.setRequestHeader('x-csrf-token', csrfToken);
  227. },
  228. success: function(result){
  229. if (result.err === 0) {
  230. if (successCallback) {
  231. successCallback(result.data);
  232. }
  233. } else {
  234. toastr.error('error: ' + result.msg);
  235. if (errorCallBack) {
  236. errorCallBack();
  237. }
  238. }
  239. if (showWaiting) closeWaitingView();
  240. },
  241. error: function(jqXHR, textStatus, errorThrown){
  242. toastr.error('error: ' + textStatus + " " + errorThrown);
  243. if (errorCallBack) {
  244. errorCallBack();
  245. }
  246. if (showWaiting) closeWaitingView();
  247. }
  248. });
  249. };
  250. /**
  251. * 获取url中参数
  252. * @param variable
  253. * @returns {*}
  254. */
  255. function getQueryVariable(variable) {
  256. var query = window.location.search.substring(1);
  257. var vars = query.split("&");
  258. for (var i=0;i<vars.length;i++) {
  259. var pair = vars[i].split("=");
  260. if(pair[0] == variable){return pair[1];}
  261. }
  262. return(false);
  263. }
  264. const zeroRange = 0.00000001;
  265. function checkZero(value) {
  266. return value === null || value === undefined || (_.isNumber(value) && Math.abs(value) < zeroRange);
  267. }
  268. function checkFieldChange(o, n) {
  269. return o == n || ((!o || o === '') && (n === ''));
  270. }
  271. var bShowWaiting = false, beginWaitingTime;
  272. //关闭等待窗口
  273. function closeWaitingView() {
  274. bShowWaiting = false;
  275. const time = parseInt(new Date());
  276. setTimeout(function () {
  277. var bgDiv = document.getElementById("bgDiv");
  278. var msgDiv = document.getElementById("msgDiv");
  279. //移除背景遮罩层div
  280. if(bgDiv != null){
  281. document.body.removeChild(bgDiv);
  282. }
  283. //移除中间信息提示层div
  284. if(msgDiv != null){
  285. document.body.removeChild(msgDiv);
  286. }
  287. }, Math.max(beginWaitingTime - time + 1000, 0));
  288. }
  289. //显示等待窗口
  290. function showWaitingView() {
  291. bShowWaiting = true;
  292. setTimeout(function () {
  293. if (!bShowWaiting) return;
  294. beginWaitingTime = parseInt(new Date());
  295. var msgw = 300; //提示窗口的宽度
  296. var msgh = 100; //提示窗口的高度
  297. var sWidth, sHeight;
  298. sWidth = document.body.clientWidth;
  299. sHeight = document.body.clientHeight;
  300. //背景遮罩层div
  301. var bgObj = document.createElement("div");
  302. bgObj.setAttribute('id', 'bgDiv');
  303. bgObj.style.zIndex = '9998';
  304. bgObj.style.position = "absolute";
  305. bgObj.style.top = "0px";
  306. bgObj.style.background = "#888";
  307. bgObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75";
  308. bgObj.style.opacity = "0.6";
  309. bgObj.style.left = "0px";
  310. bgObj.style.width = sWidth + "px";
  311. bgObj.style.height = sHeight + "px";
  312. document.body.appendChild(bgObj);
  313. //信息提示层div
  314. var msgObj = document.createElement("div");
  315. msgObj.style.zIndex = '9999';
  316. msgObj.setAttribute("id", "msgDiv");
  317. msgObj.setAttribute("align", "center");
  318. msgObj.style.position = "absolute";
  319. msgObj.style.font = "12px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";
  320. msgObj.style.width = msgw + "px";
  321. msgObj.style.height = msgh + "px";
  322. msgObj.style.top = (document.documentElement.scrollTop + (sHeight - msgh) / 2) + "px";
  323. msgObj.style.left = (sWidth - msgw) / 2 + "px";
  324. document.body.appendChild(msgObj);
  325. //中间等待图标
  326. document.getElementById("msgDiv").innerHTML = '<i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i>';
  327. }, 1000);
  328. }
  329. let progressInterval;
  330. function showProgress() {
  331. var sWidth, sHeight;
  332. sWidth = document.body.clientWidth;
  333. sHeight = document.body.clientHeight;
  334. //背景遮罩层div
  335. var bgObj = document.createElement("div");
  336. bgObj.setAttribute('id', 'bgDiv');
  337. bgObj.style.zIndex = '9998';
  338. bgObj.style.position = "absolute";
  339. bgObj.style.top = "0px";
  340. bgObj.style.background = "#888";
  341. bgObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75";
  342. bgObj.style.opacity = "0.6";
  343. bgObj.style.left = "0px";
  344. bgObj.style.width = sWidth + "px";
  345. bgObj.style.height = sHeight + "px";
  346. document.body.appendChild(bgObj);
  347. //信息提示层div
  348. var msgObj = document.createElement("div");
  349. msgObj.classList.add('progress');
  350. msgObj.style.zIndex = '9999';
  351. msgObj.style.position = "absolute";
  352. msgObj.setAttribute("id", "progressDiv");
  353. msgObj.style.height = "2px";
  354. msgObj.style.width = "600px";
  355. msgObj.style.top = (document.documentElement.scrollTop + sHeight / 2) + "px";
  356. msgObj.style.left = (sWidth - 600) / 2 + "px";
  357. document.body.appendChild(msgObj);
  358. msgObj.innerHTML = '<div class="progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"></div>';
  359. const processObj = $('.progress-bar');
  360. let count = 0;
  361. progressInterval = setInterval(function () {
  362. const pos = parseInt(processObj.attr('aria-valuenow'));
  363. if (pos < 20) { // 1
  364. processObj.attr('aria-valuenow', pos + 2);
  365. processObj.width((pos + 2) + '%');
  366. } else if (pos < 40) { // 2
  367. processObj.attr('aria-valuenow', pos + 1);
  368. processObj.width((pos + 1) + '%');
  369. } else if (pos < 60) { // 4
  370. count += 1;
  371. if (count % 2 === 0) {
  372. processObj.attr('aria-valuenow', pos + 1);
  373. processObj.width((pos + 1) + '%');
  374. }
  375. } else if (pos < 80) { // 10
  376. count += 1;
  377. if (count % 5 === 0) {
  378. processObj.attr('aria-valuenow', pos + 1);
  379. processObj.width((pos + 1) + '%');
  380. }
  381. } else if (pos < 90) { // 15
  382. count += 1;
  383. if (count % 15 === 0) {
  384. processObj.attr('aria-valuenow', pos + 1);
  385. processObj.width((pos + 1) + '%');
  386. }
  387. } else if (pos < 95) { // 20
  388. count += 1;
  389. if (count % 40 === 0) {
  390. processObj.attr('aria-valuenow', pos + 1);
  391. processObj.width((pos + 1) + '%');
  392. }
  393. } else if (pos < 100) { // 30
  394. count += 1;
  395. if (count % 60 === 0) {
  396. processObj.attr('aria-valuenow', pos + 1);
  397. }
  398. }
  399. }, 100);
  400. }
  401. function doneProgress() {
  402. clearInterval(progressInterval);
  403. $('.progress-bar').attr('aria-valuenow', 100).width('100%');
  404. }
  405. function closeProgress() {
  406. setTimeout(function () {
  407. var bgDiv = document.getElementById("bgDiv");
  408. var msgDiv = document.getElementById("progressDiv");
  409. //移除背景遮罩层div
  410. if(bgDiv != null){
  411. document.body.removeChild(bgDiv);
  412. }
  413. //移除中间信息提示层div
  414. if(msgDiv != null){
  415. document.body.removeChild(msgDiv);
  416. }
  417. }, 500);
  418. }
  419. /**
  420. * 设置本地缓存
  421. *
  422. * @param {String} key
  423. * @param {String|Number} value
  424. * @return {void}
  425. */
  426. function setLocalCache(key, value) {
  427. const storage = window.localStorage;
  428. if (!storage || key === '' || value === '') {
  429. return;
  430. }
  431. storage.setItem(key, value);
  432. }
  433. /**
  434. * 获取本地缓存
  435. *
  436. * @param {String} key
  437. * @return {String}
  438. */
  439. function getLocalCache(key) {
  440. const storage = window.localStorage;
  441. if (!storage || key === '') {
  442. return null;
  443. }
  444. return storage.getItem(key);
  445. }
  446. /**
  447. * 移除本地缓存
  448. * @param {String} key
  449. * @returns {Boolean}
  450. */
  451. function removeLocalCache(key) {
  452. const storage = window.localStorage;
  453. if (!storage || key === '') {
  454. return null;
  455. }
  456. return storage.removeItem(key);
  457. }
  458. //params: 需要复制的对象(元素)
  459. function copyToClipboard( text ) {
  460. var isRTL = document.documentElement.getAttribute('dir') == 'rtl';
  461. var aux = document.createElement('textarea');// 创建元素用于复制
  462. // Prevent zooming on iOS
  463. aux.style.fontSize = '12pt';
  464. // Reset box model
  465. aux.style.border = '0';
  466. aux.style.padding = '0';
  467. aux.style.margin = '0';
  468. // Move element out of screen horizontally
  469. aux.style.position = 'absolute';
  470. aux.style[isRTL ? 'right' : 'left'] = '-9999px';
  471. // Move element to the same position vertically
  472. var yPosition = window.pageYOffset || document.documentElement.scrollTop;
  473. aux.style.top = yPosition + 'px';
  474. aux.setAttribute('readonly', '');
  475. // 设置元素内容
  476. aux.value = text;
  477. // 将元素插入页面进行调用
  478. document.body.appendChild(aux);
  479. // 复制内容
  480. aux.select();
  481. aux.setSelectionRange(0, text.length);
  482. // 将内容复制到剪贴板
  483. document.execCommand("copy", true);
  484. // 删除创建元素
  485. document.body.removeChild(aux);
  486. }
  487. function toastMessageUniq (obj) {
  488. if (!obj.msg || !obj.type) return;
  489. if (!obj.once) {
  490. switch (obj.type) {
  491. case 'error':
  492. toastr.error(obj.msg);
  493. break;
  494. case 'warning':
  495. toastr.warning(obj.msg);
  496. break;
  497. case 'info':
  498. toastr.info(obj.msg);
  499. break;
  500. case 'success':
  501. toastr.success(obj.msg);
  502. break;
  503. }
  504. obj.once = true;
  505. }
  506. }
  507. function trimInvalidChar(str) {
  508. return $.trim(str).replace('\n', '').replace('\r', '').replace('\t', '');
  509. }