global.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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);
  22. $(".sjs-height-1").height($(window).height()-cHeader-bcontent-90);
  23. $(".sjs-height-2").height($(window).height()-cHeader-sBarz-110);
  24. $(".sjs-height-3").height($(window).height()-cHeader-sBar-492);
  25. $(".sjs-height-4").height($(window).height()-cHeader-pBarz-110);
  26. $(".sjs-height-5").height($(window).height()-cHeader-sBart-555);
  27. $(".sjs-height-6").height($(window).height()-cHeader-bdtopc-156);
  28. $(".sjs-height-7").height($(window).height()-cHeader-sBar-110);
  29. $(".sp-wrap").height(bcontent-40);
  30. /*侧栏高度*/
  31. $(".sjs-sh-1").height($(window).height()-cHeader-sBar1-92);
  32. $(".sjs-sh-2").height($(window).height()-cHeader-sBar2-92);
  33. $(".sjs-sh-3").height($(window).height()-cHeader-sBar3-92);
  34. $(".sjs-sh-4").height($(window).height()-cHeader-sBar4-92);
  35. $(".sjs-sh-5").height($(window).height()-cHeader-sBar5-92);
  36. };
  37. $(window).resize(autoFlashHeight);
  38. /*全局自适应高度结束*/
  39. $(function(){
  40. /*侧滑*/
  41. $(".open-sidebar").click(function(){
  42. $(".slide-sidebar").animate({width:"800"}).addClass("open");
  43. });
  44. $("body").click(function(event){
  45. var e = event || window.event; //浏览器兼容性
  46. if(!$(event.target).is('a')) {
  47. var elem = event.target || e.srcElement;
  48. while (elem) { //循环判断至跟节点,防止点击的是div子元素
  49. if (elem.className == "open-sidebar" || elem.className == 'slide-sidebar open') {
  50. return false;
  51. }
  52. elem = elem.parentNode;
  53. }
  54. $(".slide-sidebar").animate({width:"0"}).removeClass("open")// 关闭处理
  55. }
  56. });
  57. /*侧滑*/
  58. /*工具提示*/
  59. $(function () {
  60. $('[data-toggle="tooltip"]').tooltip()
  61. });
  62. /*侧栏菜单*/
  63. $(".bg-nav > li > a").click(function() {
  64. var self = $(this);
  65. var subMenu = $(this).siblings('ul.sub-menu');
  66. if(subMenu.length > 0) {
  67. if(subMenu.is(":visible")) {
  68. self.find('.menu-arrow').removeClass('fa-angle-up').addClass('fa-angle-down');
  69. subMenu.slideUp('fast');
  70. self.parent().removeClass('active');
  71. }else{
  72. self.parent().addClass('active');
  73. self.find('.menu-arrow').removeClass('fa-angle-down').addClass('fa-angle-up');
  74. subMenu.slideDown('fast');
  75. }
  76. }
  77. });
  78. // 数据提交
  79. $("#submit-form").click(function() {
  80. $("#save-form").submit();
  81. });
  82. // modal弹窗拖动
  83. // $(document).on("show.bs.modal", ".modal", function() {
  84. // $(this).draggable({
  85. // handle: ".modal-header" // 只能点击头部拖动
  86. // });
  87. // $(this).css("overflow", "hidden");
  88. // });
  89. });
  90. /**
  91. * 提示框
  92. *
  93. * @param string message
  94. * @param string type
  95. * @param string icon
  96. * @return void
  97. */
  98. function toast(message, type, icon) {
  99. var toast = $(".toast");
  100. toast.addClass(type);
  101. toast.children('.message').html(message);
  102. var iconClass = 'fa-' + icon;
  103. toast.children('.icon').addClass(iconClass);
  104. toast.fadeIn(500);
  105. setTimeout(function() {
  106. toast.fadeOut('fast');
  107. toast.children('.message').text('');
  108. toast.children('.icon').removeClass(iconClass);
  109. }, 3000);
  110. }
  111. /**
  112. * 动态请求数据
  113. * @param {String} url - 请求链接
  114. * @param data - 提交数据
  115. * @param {function} successCallback - 返回成功回调
  116. * @param {function} errorCallBack - 返回失败回调
  117. */
  118. const postData = function (url, data, successCallback, errorCallBack, showWaiting = false) {
  119. if (showWaiting) showWaitingView();
  120. $.ajax({
  121. type:"POST",
  122. url: url,
  123. data: {'data': JSON.stringify(data)},
  124. dataType: 'json',
  125. cache: false,
  126. timeout: 40000,
  127. beforeSend: function(xhr) {
  128. let csrfToken = Cookies.get('csrfToken');
  129. xhr.setRequestHeader('x-csrf-token', csrfToken);
  130. },
  131. success: function(result){
  132. if (result.err === 0) {
  133. if (successCallback) {
  134. successCallback(result.data);
  135. }
  136. } else {
  137. toast('error: ' + result.msg, 'error', 'exclamation-circle');
  138. if (errorCallBack) {
  139. errorCallBack(result.msg);
  140. }
  141. }
  142. if (showWaiting) closeWaitingView();
  143. },
  144. error: function(jqXHR, textStatus, errorThrown){
  145. toast('error ' + textStatus + " " + errorThrown, 'error', 'exclamation-circle');
  146. if (errorCallBack) {
  147. errorCallBack();
  148. }
  149. if (showWaiting) closeWaitingView();
  150. }
  151. });
  152. };
  153. /**
  154. * 动态请求数据(压缩数据)
  155. * @param {String} url - 请求链接
  156. * @param data - 提交数据
  157. * @param {function} successCallback - 返回成功回调
  158. * @param {function} errorCallBack - 返回失败回调
  159. */
  160. const postDataCompress = function (url, data, successCallback, errorCallBack, showWaiting = false) {
  161. if (showWaiting) showWaitingView();
  162. $.ajax({
  163. type:"POST",
  164. url: url,
  165. data: {'data': LZString.compressToUTF16(JSON.stringify(data))},
  166. dataType: 'json',
  167. cache: false,
  168. timeout: 60000, // 导入清单Excel(10w行)预计需要时间
  169. beforeSend: function(xhr) {
  170. let csrfToken = Cookies.get('csrfToken');
  171. xhr.setRequestHeader('x-csrf-token', csrfToken);
  172. },
  173. success: function(result){
  174. if (result.err === 0) {
  175. if (successCallback) {
  176. successCallback(result.data);
  177. }
  178. } else {
  179. toast('error: ' + result.msg, 'error', 'exclamation-circle');
  180. if (errorCallBack) {
  181. errorCallBack(result.msg);
  182. }
  183. }
  184. if (showWaiting) closeWaitingView();
  185. },
  186. error: function(jqXHR, textStatus, errorThrown){
  187. toast('error ' + textStatus + " " + errorThrown, 'error', 'exclamation-circle');
  188. if (errorCallBack) {
  189. errorCallBack();
  190. }
  191. if (showWaiting) closeWaitingView();
  192. }
  193. });
  194. };
  195. /**
  196. * 动态请求数据
  197. * @param {String} url - 请求链接
  198. * @param data - 提交数据
  199. * @param {function} successCallback - 返回成功回调
  200. * @param {function} errorCallBack - 返回失败回调
  201. */
  202. const postDataWithFile = function (url, formData, successCallback, errorCallBack, showWaiting = false) {
  203. if (showWaiting) showWaitingView();
  204. $.ajax({
  205. type:"POST",
  206. url: url,
  207. data: formData,
  208. dataType: 'json',
  209. cache: false,
  210. // 告诉jQuery不要去设置Content-Type请求头
  211. contentType: false,
  212. // 告诉jQuery不要去处理发送的数据
  213. processData: false,
  214. timeout: 5000,
  215. beforeSend: function(xhr) {
  216. let csrfToken = Cookies.get('csrfToken');
  217. xhr.setRequestHeader('x-csrf-token', csrfToken);
  218. },
  219. success: function(result){
  220. if (result.err === 0) {
  221. if (successCallback) {
  222. successCallback(result.data);
  223. }
  224. } else {
  225. toast('error: ' + result.msg, 'error', 'exclamation-circle');
  226. if (errorCallBack) {
  227. errorCallBack();
  228. }
  229. }
  230. if (showWaiting) closeWaitingView();
  231. },
  232. error: function(jqXHR, textStatus, errorThrown){
  233. toast('error ' + textStatus + " " + errorThrown, 'error', 'exclamation-circle');
  234. if (errorCallBack) {
  235. errorCallBack();
  236. }
  237. if (showWaiting) closeWaitingView();
  238. }
  239. });
  240. };
  241. /**
  242. * 获取url中参数
  243. * @param variable
  244. * @returns {*}
  245. */
  246. function getQueryVariable(variable) {
  247. var query = window.location.search.substring(1);
  248. var vars = query.split("&");
  249. for (var i=0;i<vars.length;i++) {
  250. var pair = vars[i].split("=");
  251. if(pair[0] == variable){return pair[1];}
  252. }
  253. return(false);
  254. }
  255. const zeroRange = 0.00000001;
  256. function checkZero(value) {
  257. return _.isNumber(value) && Math.abs(value) < zeroRange;
  258. }
  259. function checkFieldChange(o, n) {
  260. return o == n || ((!o || o === '') && (n === ''));
  261. }
  262. //关闭等待窗口
  263. function closeWaitingView() {
  264. var bgDiv = document.getElementById("bgDiv");
  265. var msgDiv = document.getElementById("msgDiv");
  266. //移除背景遮罩层div
  267. if(bgDiv != null){
  268. document.body.removeChild(bgDiv);
  269. }
  270. //移除中间信息提示层div
  271. if(msgDiv != null){
  272. document.body.removeChild(msgDiv);
  273. }
  274. }
  275. //显示等待窗口
  276. function showWaitingView() {
  277. var msgw = 300; //提示窗口的宽度
  278. var msgh = 100; //提示窗口的高度
  279. var sWidth, sHeight;
  280. sWidth = document.body.clientWidth;
  281. sHeight = document.body.clientHeight;
  282. //背景遮罩层div
  283. var bgObj = document.createElement("div");
  284. bgObj.setAttribute('id', 'bgDiv');
  285. bgObj.style.zIndex = '9998';
  286. bgObj.style.position = "absolute";
  287. bgObj.style.top = "0px";
  288. bgObj.style.background = "#888";
  289. bgObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75";
  290. bgObj.style.opacity = "0.6";
  291. bgObj.style.left = "0px";
  292. bgObj.style.width = sWidth + "px";
  293. bgObj.style.height = sHeight + "px";
  294. document.body.appendChild(bgObj);
  295. //信息提示层div
  296. var msgObj = document.createElement("div");
  297. msgObj.style.zIndex = '9999';
  298. msgObj.setAttribute("id", "msgDiv");
  299. msgObj.setAttribute("align", "center");
  300. msgObj.style.position = "absolute";
  301. msgObj.style.font = "12px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";
  302. msgObj.style.width = msgw + "px";
  303. msgObj.style.height = msgh + "px";
  304. msgObj.style.top = (document.documentElement.scrollTop + (sHeight - msgh) / 2) + "px";
  305. msgObj.style.left = (sWidth - msgw) / 2 + "px";
  306. document.body.appendChild(msgObj);
  307. //中间等待图标
  308. document.getElementById("msgDiv").innerHTML = '<i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i>';
  309. }
  310. /**
  311. * 设置本地缓存
  312. *
  313. * @param {String} key
  314. * @param {String|Number} value
  315. * @return {void}
  316. */
  317. function setLocalCache(key, value) {
  318. const storage = window.localStorage;
  319. if (!storage || key === '' || value === '') {
  320. return;
  321. }
  322. storage.setItem(key, value);
  323. }
  324. /**
  325. * 获取本地缓存
  326. *
  327. * @param {String} key
  328. * @return {String}
  329. */
  330. function getLocalCache(key) {
  331. const storage = window.localStorage;
  332. if (!storage || key === '') {
  333. return null;
  334. }
  335. return storage.getItem(key);
  336. }
  337. /**
  338. * 移除本地缓存
  339. * @param {String} key
  340. * @returns {Boolean}
  341. */
  342. function removeLocalCache(key) {
  343. const storage = window.localStorage;
  344. if (!storage || key === '') {
  345. return null;
  346. }
  347. return storage.removeItem(key);
  348. }