global.js 18 KB

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