global.js 7.8 KB

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