global.js 7.4 KB

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