global.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. // 动态生成datepicker
  90. $('body').on('focus', ".datepicker-here", function (e) {
  91. if ($(this).data("datepicker") != "1") {
  92. $(this).datepicker();
  93. }
  94. $(this).data("datepicker", "1");
  95. });
  96. });
  97. /**
  98. * 提示框
  99. *
  100. * @param string message
  101. * @param string type
  102. * @param string icon
  103. * @return void
  104. */
  105. function toast(message, type, icon) {
  106. var toast = $(".toast");
  107. toast.addClass(type);
  108. toast.children('.message').html(message);
  109. var iconClass = 'fa-' + icon;
  110. toast.children('.icon').addClass(iconClass);
  111. toast.fadeIn(500);
  112. setTimeout(function() {
  113. toast.fadeOut('fast');
  114. toast.children('.message').text('');
  115. toast.children('.icon').removeClass(iconClass);
  116. }, 3000);
  117. }
  118. /**
  119. * 动态请求数据
  120. * @param {String} url - 请求链接
  121. * @param data - 提交数据
  122. * @param {function} successCallback - 返回成功回调
  123. * @param {function} errorCallBack - 返回失败回调
  124. */
  125. const postData = function (url, data, successCallback, errorCallBack, showWaiting = false) {
  126. if (showWaiting) showWaitingView();
  127. $.ajax({
  128. type:"POST",
  129. url: url,
  130. data: {'data': JSON.stringify(data)},
  131. dataType: 'json',
  132. cache: false,
  133. timeout: 40000,
  134. beforeSend: function(xhr) {
  135. let csrfToken = Cookies.get('csrfToken');
  136. xhr.setRequestHeader('x-csrf-token', csrfToken);
  137. },
  138. success: function(result){
  139. if (result.err === 0) {
  140. if (successCallback) {
  141. successCallback(result.data);
  142. }
  143. } else {
  144. toastr.error('error: ' + result.msg);
  145. if (errorCallBack) {
  146. errorCallBack(result.msg);
  147. }
  148. }
  149. if (showWaiting) closeWaitingView();
  150. },
  151. error: function(jqXHR, textStatus, errorThrown){
  152. toastr.error('error: ' + textStatus + " " + errorThrown);
  153. if (errorCallBack) {
  154. errorCallBack();
  155. }
  156. if (showWaiting) closeWaitingView();
  157. }
  158. });
  159. };
  160. /**
  161. * 动态请求数据(压缩数据)
  162. * @param {String} url - 请求链接
  163. * @param data - 提交数据
  164. * @param {function} successCallback - 返回成功回调
  165. * @param {function} errorCallBack - 返回失败回调
  166. */
  167. const postDataCompress = function (url, data, successCallback, errorCallBack, showWaiting = false) {
  168. if (showWaiting) showWaitingView();
  169. $.ajax({
  170. type:"POST",
  171. url: url,
  172. data: {'data': LZString.compressToUTF16(JSON.stringify(data))},
  173. dataType: 'json',
  174. cache: false,
  175. timeout: 60000, // 导入清单Excel(10w行)预计需要时间
  176. beforeSend: function(xhr) {
  177. let csrfToken = Cookies.get('csrfToken');
  178. xhr.setRequestHeader('x-csrf-token', csrfToken);
  179. },
  180. success: function(result){
  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 (showWaiting) closeWaitingView();
  192. },
  193. error: function(jqXHR, textStatus, errorThrown){
  194. toastr.error('error: ' + textStatus + " " + errorThrown);
  195. if (errorCallBack) {
  196. errorCallBack();
  197. }
  198. if (showWaiting) closeWaitingView();
  199. }
  200. });
  201. };
  202. /**
  203. * 动态请求数据
  204. * @param {String} url - 请求链接
  205. * @param data - 提交数据
  206. * @param {function} successCallback - 返回成功回调
  207. * @param {function} errorCallBack - 返回失败回调
  208. */
  209. const postDataWithFile = function (url, formData, successCallback, errorCallBack, showWaiting = false) {
  210. if (showWaiting) showWaitingView();
  211. $.ajax({
  212. type:"POST",
  213. url: url,
  214. data: formData,
  215. dataType: 'json',
  216. cache: false,
  217. // 告诉jQuery不要去设置Content-Type请求头
  218. contentType: false,
  219. // 告诉jQuery不要去处理发送的数据
  220. processData: false,
  221. timeout: 5000,
  222. beforeSend: function(xhr) {
  223. let csrfToken = Cookies.get('csrfToken');
  224. xhr.setRequestHeader('x-csrf-token', csrfToken);
  225. },
  226. success: function(result){
  227. if (result.err === 0) {
  228. if (successCallback) {
  229. successCallback(result.data);
  230. }
  231. } else {
  232. toastr.error('error: ' + result.msg);
  233. if (errorCallBack) {
  234. errorCallBack();
  235. }
  236. }
  237. if (showWaiting) closeWaitingView();
  238. },
  239. error: function(jqXHR, textStatus, errorThrown){
  240. toastr.error('error: ' + textStatus + " " + errorThrown);
  241. if (errorCallBack) {
  242. errorCallBack();
  243. }
  244. if (showWaiting) closeWaitingView();
  245. }
  246. });
  247. };
  248. /**
  249. * 获取url中参数
  250. * @param variable
  251. * @returns {*}
  252. */
  253. function getQueryVariable(variable) {
  254. var query = window.location.search.substring(1);
  255. var vars = query.split("&");
  256. for (var i=0;i<vars.length;i++) {
  257. var pair = vars[i].split("=");
  258. if(pair[0] == variable){return pair[1];}
  259. }
  260. return(false);
  261. }
  262. const zeroRange = 0.00000001;
  263. function checkZero(value) {
  264. return _.isNumber(value) && Math.abs(value) < zeroRange;
  265. }
  266. function checkFieldChange(o, n) {
  267. return o == n || ((!o || o === '') && (n === ''));
  268. }
  269. //关闭等待窗口
  270. function closeWaitingView() {
  271. var bgDiv = document.getElementById("bgDiv");
  272. var msgDiv = document.getElementById("msgDiv");
  273. //移除背景遮罩层div
  274. if(bgDiv != null){
  275. document.body.removeChild(bgDiv);
  276. }
  277. //移除中间信息提示层div
  278. if(msgDiv != null){
  279. document.body.removeChild(msgDiv);
  280. }
  281. }
  282. //显示等待窗口
  283. function showWaitingView() {
  284. var msgw = 300; //提示窗口的宽度
  285. var msgh = 100; //提示窗口的高度
  286. var sWidth, sHeight;
  287. sWidth = document.body.clientWidth;
  288. sHeight = document.body.clientHeight;
  289. //背景遮罩层div
  290. var bgObj = document.createElement("div");
  291. bgObj.setAttribute('id', 'bgDiv');
  292. bgObj.style.zIndex = '9998';
  293. bgObj.style.position = "absolute";
  294. bgObj.style.top = "0px";
  295. bgObj.style.background = "#888";
  296. bgObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75";
  297. bgObj.style.opacity = "0.6";
  298. bgObj.style.left = "0px";
  299. bgObj.style.width = sWidth + "px";
  300. bgObj.style.height = sHeight + "px";
  301. document.body.appendChild(bgObj);
  302. //信息提示层div
  303. var msgObj = document.createElement("div");
  304. msgObj.style.zIndex = '9999';
  305. msgObj.setAttribute("id", "msgDiv");
  306. msgObj.setAttribute("align", "center");
  307. msgObj.style.position = "absolute";
  308. msgObj.style.font = "12px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";
  309. msgObj.style.width = msgw + "px";
  310. msgObj.style.height = msgh + "px";
  311. msgObj.style.top = (document.documentElement.scrollTop + (sHeight - msgh) / 2) + "px";
  312. msgObj.style.left = (sWidth - msgw) / 2 + "px";
  313. document.body.appendChild(msgObj);
  314. //中间等待图标
  315. document.getElementById("msgDiv").innerHTML = '<i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i>';
  316. }
  317. /**
  318. * 设置本地缓存
  319. *
  320. * @param {String} key
  321. * @param {String|Number} value
  322. * @return {void}
  323. */
  324. function setLocalCache(key, value) {
  325. const storage = window.localStorage;
  326. if (!storage || key === '' || value === '') {
  327. return;
  328. }
  329. storage.setItem(key, value);
  330. }
  331. /**
  332. * 获取本地缓存
  333. *
  334. * @param {String} key
  335. * @return {String}
  336. */
  337. function getLocalCache(key) {
  338. const storage = window.localStorage;
  339. if (!storage || key === '') {
  340. return null;
  341. }
  342. return storage.getItem(key);
  343. }
  344. /**
  345. * 移除本地缓存
  346. * @param {String} key
  347. * @returns {Boolean}
  348. */
  349. function removeLocalCache(key) {
  350. const storage = window.localStorage;
  351. if (!storage || key === '') {
  352. return null;
  353. }
  354. return storage.removeItem(key);
  355. }
  356. //params: 需要复制的对象(元素)
  357. function copyToClipboard( text ) {
  358. var isRTL = document.documentElement.getAttribute('dir') == 'rtl';
  359. var aux = document.createElement('textarea');// 创建元素用于复制
  360. // Prevent zooming on iOS
  361. aux.style.fontSize = '12pt';
  362. // Reset box model
  363. aux.style.border = '0';
  364. aux.style.padding = '0';
  365. aux.style.margin = '0';
  366. // Move element out of screen horizontally
  367. aux.style.position = 'absolute';
  368. aux.style[isRTL ? 'right' : 'left'] = '-9999px';
  369. // Move element to the same position vertically
  370. var yPosition = window.pageYOffset || document.documentElement.scrollTop;
  371. aux.style.top = yPosition + 'px';
  372. aux.setAttribute('readonly', '');
  373. // 设置元素内容
  374. aux.value = text;
  375. // 将元素插入页面进行调用
  376. document.body.appendChild(aux);
  377. // 复制内容
  378. aux.select();
  379. aux.setSelectionRange(0, text.length);
  380. // 将内容复制到剪贴板
  381. document.execCommand("copy", true);
  382. // 删除创建元素
  383. document.body.removeChild(aux);
  384. }