global.js 7.5 KB

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