global.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. $(function () {
  2. $('a').on('click', function () {
  3. if ($(this).hasClass('show-loading')) {
  4. showWaitingView();
  5. }
  6. });
  7. $('[data-toggle="tooltip"]').tooltip();
  8. $('[data-toggle="popover"]').popover();
  9. })
  10. /**
  11. * 动态请求数据
  12. * @param {String} url - 请求链接
  13. * @param data - 提交数据
  14. * @param {function} successCallback - 返回成功回调
  15. * @param {function} errorCallBack - 返回失败回调
  16. */
  17. const postData = function (url, data, successCallback, errorCallBack, showWaiting = true) {
  18. if (showWaiting) showWaitingView();
  19. $.ajax({
  20. type:"POST",
  21. url: url,
  22. data: {'data': JSON.stringify(data)},
  23. dataType: 'json',
  24. cache: false,
  25. timeout: 60000,
  26. beforeSend: function(xhr) {
  27. let csrfToken = Cookies.get('csrfToken');
  28. xhr.setRequestHeader('x-csrf-token', csrfToken);
  29. },
  30. success: function(result){
  31. if (result.err === 0) {
  32. if (successCallback) {
  33. successCallback(result.data);
  34. }
  35. } else {
  36. toastr.error('error: ' + result.msg);
  37. if (errorCallBack) {
  38. errorCallBack(result.msg);
  39. }
  40. }
  41. if (showWaiting) closeWaitingView();
  42. },
  43. error: function(jqXHR, textStatus, errorThrown){
  44. toastr.error('error: ' + textStatus + " " + errorThrown);
  45. if (errorCallBack) {
  46. errorCallBack();
  47. }
  48. if (showWaiting) closeWaitingView();
  49. }
  50. });
  51. };
  52. /**
  53. * 提示框
  54. *
  55. * @param string message
  56. * @param string type
  57. * @param string icon
  58. * @return void
  59. */
  60. function toast(message, type, icon) {
  61. var toast = $(".toast");
  62. toast.addClass(type);
  63. toast.children('.message').html(message);
  64. var iconClass = 'fa-' + icon;
  65. toast.children('.icon').addClass(iconClass);
  66. toast.fadeIn(500);
  67. setTimeout(function() {
  68. toast.fadeOut('fast');
  69. toast.children('.message').text('');
  70. toast.children('.icon').removeClass(iconClass);
  71. }, 3000);
  72. }
  73. /**
  74. * 获取url中参数
  75. * @param variable
  76. * @returns {*}
  77. */
  78. function getQueryVariable(variable) {
  79. var query = window.location.search.substring(1);
  80. var vars = query.split("&");
  81. for (var i=0;i<vars.length;i++) {
  82. var pair = vars[i].split("=");
  83. if(pair[0] == variable){return pair[1];}
  84. }
  85. return(false);
  86. }
  87. const zeroRange = 0.00000001;
  88. function checkZero(value) {
  89. return value === null || value === undefined || (_.isNumber(value) && Math.abs(value) < zeroRange);
  90. }
  91. function checkFieldChange(o, n) {
  92. return o == n || ((!o || o === '') && (n === ''));
  93. }
  94. /**
  95. * 设置本地缓存
  96. *
  97. * @param {String} key
  98. * @param {String|Number} value
  99. * @return {void}
  100. */
  101. function setLocalCache(key, value) {
  102. const storage = window.localStorage;
  103. if (!storage || key === '' || value === '') {
  104. return;
  105. }
  106. storage.setItem(key, value);
  107. }
  108. /**
  109. * 获取本地缓存
  110. *
  111. * @param {String} key
  112. * @return {String}
  113. */
  114. function getLocalCache(key) {
  115. const storage = window.localStorage;
  116. if (!storage || key === '') {
  117. return null;
  118. }
  119. return storage.getItem(key);
  120. }
  121. /**
  122. * 移除本地缓存
  123. * @param {String} key
  124. * @returns {Boolean}
  125. */
  126. function removeLocalCache(key) {
  127. const storage = window.localStorage;
  128. if (!storage || key === '') {
  129. return null;
  130. }
  131. return storage.removeItem(key);
  132. }
  133. function trimInvalidChar(str) {
  134. return $.trim(str).replace(/\n/g, '').replace(/\r/g, '').replace(/\t/g, '');
  135. }
  136. function cleanSymbols(str) {
  137. return $.trim(str).replace(/\\/g, '').replace(/\'/g, '').replace(/\"/g, '').replace(/\</g, '').replace(/\|/g, '');
  138. }
  139. //关闭等待窗口
  140. function closeWaitingView() {
  141. bShowWaiting = false;
  142. const time = parseInt(new Date());
  143. setTimeout(function () {
  144. var bgDiv = document.getElementById("bgDiv");
  145. var msgDiv = document.getElementById("msgDiv");
  146. //移除背景遮罩层div
  147. if(bgDiv != null){
  148. document.body.removeChild(bgDiv);
  149. }
  150. //移除中间信息提示层div
  151. if(msgDiv != null){
  152. document.body.removeChild(msgDiv);
  153. }
  154. }, Math.max(beginWaitingTime - time + 500, 0));
  155. }
  156. //显示等待窗口
  157. function showWaitingView() {
  158. // bShowWaiting = true;
  159. // setTimeout(function () {
  160. // if (!bShowWaiting) return;
  161. // beginWaitingTime = parseInt(new Date());
  162. // var msgw = 300; //提示窗口的宽度
  163. // var msgh = 100; //提示窗口的高度
  164. //
  165. // var sWidth, sHeight;
  166. // sWidth = document.body.clientWidth;
  167. // sHeight = document.body.clientHeight;
  168. //
  169. // //背景遮罩层div
  170. // var bgObj = document.createElement("div");
  171. // bgObj.setAttribute('id', 'bgDiv');
  172. // bgObj.style.zIndex = '9998';
  173. // bgObj.style.position = "absolute";
  174. // bgObj.style.top = "0px";
  175. // bgObj.style.background = "#888";
  176. // bgObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75";
  177. // bgObj.style.opacity = "0.6";
  178. // bgObj.style.left = "0px";
  179. // bgObj.style.width = sWidth + "px";
  180. // bgObj.style.height = sHeight + "px";
  181. // document.body.appendChild(bgObj);
  182. //
  183. // //信息提示层div
  184. // var msgObj = document.createElement("div");
  185. // msgObj.style.zIndex = '9999';
  186. // msgObj.setAttribute("id", "msgDiv");
  187. // msgObj.setAttribute("align", "center");
  188. // msgObj.style.position = "absolute";
  189. // msgObj.style.font = "12px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";
  190. // msgObj.style.width = msgw + "px";
  191. // msgObj.style.height = msgh + "px";
  192. // msgObj.style.top = (document.documentElement.scrollTop + (sHeight - msgh) / 2) + "px";
  193. // msgObj.style.left = (sWidth - msgw) / 2 + "px";
  194. // document.body.appendChild(msgObj);
  195. //
  196. // //中间等待图标
  197. // document.getElementById("msgDiv").innerHTML = '<i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i>';
  198. // }, 1000);
  199. beginWaitingTime = parseInt(new Date());
  200. var msgw = 300; //提示窗口的宽度
  201. var msgh = 100; //提示窗口的高度
  202. var sWidth, sHeight;
  203. sWidth = document.body.clientWidth;
  204. sHeight = document.body.clientHeight;
  205. //背景遮罩层div
  206. var bgObj = document.createElement("div");
  207. bgObj.setAttribute('id', 'bgDiv');
  208. bgObj.style.zIndex = '9998';
  209. bgObj.style.position = "absolute";
  210. bgObj.style.top = "0px";
  211. // bgObj.style.background = "#888";
  212. bgObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75";
  213. bgObj.style.opacity = "0.6";
  214. bgObj.style.left = "0px";
  215. bgObj.style.width = sWidth + "px";
  216. bgObj.style.height = sHeight + "px";
  217. document.body.appendChild(bgObj);
  218. //信息提示层div
  219. var msgObj = document.createElement("div");
  220. msgObj.style.zIndex = '9999';
  221. msgObj.setAttribute("id", "msgDiv");
  222. msgObj.setAttribute("align", "center");
  223. msgObj.style.position = "fixed";
  224. msgObj.style.font = "12px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";
  225. msgObj.style.width = msgw + "px";
  226. msgObj.style.height = msgh + "px";
  227. // msgObj.style.top = (document.documentElement.scrollTop + (sHeight - msgh) / 2) + "px";
  228. msgObj.style.top = "50%";
  229. msgObj.style.left = (sWidth - msgw) / 2 + "px";
  230. document.body.appendChild(msgObj);
  231. //中间等待图标
  232. document.getElementById("msgDiv").innerHTML = '<i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i>';
  233. }