global.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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', 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. * @param {String} url - 请求链接
  60. * @param data - 提交数据
  61. * @param {function} successCallback - 返回成功回调
  62. * @param {function} errorCallBack - 返回失败回调
  63. */
  64. const postDataWithFile = function (url, formData, successCallback, errorCallBack, showWaiting = true) {
  65. if (showWaiting) showWaitingView();
  66. if (formData.getAll('file[]').length > 10) {
  67. toastr.error('文件数量不能多于10个');
  68. if (showWaiting) closeWaitingView();
  69. return
  70. }
  71. $.ajax({
  72. type:"POST",
  73. url: url,
  74. data: formData,
  75. dataType: 'json',
  76. cache: false,
  77. // 告诉jQuery不要去设置Content-Type请求头
  78. contentType: false,
  79. // 告诉jQuery不要去处理发送的数据
  80. processData: false,
  81. timeout: 60000,
  82. beforeSend: function(xhr) {
  83. let csrfToken = Cookies.get('csrfToken_j');
  84. xhr.setRequestHeader('x-csrf-token', csrfToken);
  85. },
  86. success: function(result){
  87. if (result.err === 0) {
  88. if (successCallback) {
  89. successCallback(result.data);
  90. }
  91. } else if (result.err === 2) {
  92. toastr.error('error: ' + result.msg);
  93. setTimeout(function () {
  94. window.location.href = '/wap/login';
  95. },1000);
  96. } else {
  97. toastr.error('error: ' + result.msg);
  98. if (errorCallBack) {
  99. errorCallBack();
  100. }
  101. }
  102. if (showWaiting) closeWaitingView();
  103. },
  104. error: function(jqXHR, textStatus, errorThrown){
  105. toastr.error('error: ' + textStatus + " " + errorThrown);
  106. if (errorCallBack) {
  107. errorCallBack();
  108. }
  109. if (showWaiting) closeWaitingView();
  110. }
  111. });
  112. };
  113. /**
  114. * 提示框
  115. *
  116. * @param string message
  117. * @param string type
  118. * @param string icon
  119. * @return void
  120. */
  121. function toast(message, type, icon) {
  122. var toast = $(".toast");
  123. toast.addClass(type);
  124. toast.children('.message').html(message);
  125. var iconClass = 'fa-' + icon;
  126. toast.children('.icon').addClass(iconClass);
  127. toast.fadeIn(500);
  128. setTimeout(function() {
  129. toast.fadeOut('fast');
  130. toast.children('.message').text('');
  131. toast.children('.icon').removeClass(iconClass);
  132. }, 3000);
  133. }
  134. /**
  135. * 获取url中参数
  136. * @param variable
  137. * @returns {*}
  138. */
  139. function getQueryVariable(variable) {
  140. var query = window.location.search.substring(1);
  141. var vars = query.split("&");
  142. for (var i=0;i<vars.length;i++) {
  143. var pair = vars[i].split("=");
  144. if(pair[0] == variable){return pair[1];}
  145. }
  146. return(false);
  147. }
  148. const zeroRange = 0.00000001;
  149. function checkZero(value) {
  150. return value === null || value === undefined || (_.isNumber(value) && Math.abs(value) < zeroRange);
  151. }
  152. function checkFieldChange(o, n) {
  153. return o == n || ((!o || o === '') && (n === ''));
  154. }
  155. /**
  156. * 设置本地缓存
  157. *
  158. * @param {String} key
  159. * @param {String|Number} value
  160. * @return {void}
  161. */
  162. function setLocalCache(key, value) {
  163. const storage = window.localStorage;
  164. if (!storage || key === '' || value === '') {
  165. return;
  166. }
  167. storage.setItem(key, value);
  168. }
  169. /**
  170. * 获取本地缓存
  171. *
  172. * @param {String} key
  173. * @return {String}
  174. */
  175. function getLocalCache(key) {
  176. const storage = window.localStorage;
  177. if (!storage || key === '') {
  178. return null;
  179. }
  180. return storage.getItem(key);
  181. }
  182. /**
  183. * 移除本地缓存
  184. * @param {String} key
  185. * @returns {Boolean}
  186. */
  187. function removeLocalCache(key) {
  188. const storage = window.localStorage;
  189. if (!storage || key === '') {
  190. return null;
  191. }
  192. return storage.removeItem(key);
  193. }
  194. function trimInvalidChar(str) {
  195. return $.trim(str).replace(/\n/g, '').replace(/\r/g, '').replace(/\t/g, '');
  196. }
  197. function cleanSymbols(str) {
  198. return $.trim(str).replace(/\\/g, '').replace(/\'/g, '').replace(/\"/g, '').replace(/\</g, '').replace(/\|/g, '');
  199. }
  200. //关闭等待窗口
  201. function closeWaitingView() {
  202. bShowWaiting = false;
  203. const time = parseInt(new Date());
  204. setTimeout(function () {
  205. var bgDiv = document.getElementById("bgDiv");
  206. var msgDiv = document.getElementById("msgDiv");
  207. //移除背景遮罩层div
  208. if(bgDiv != null){
  209. document.body.removeChild(bgDiv);
  210. }
  211. //移除中间信息提示层div
  212. if(msgDiv != null){
  213. document.body.removeChild(msgDiv);
  214. }
  215. }, Math.max(beginWaitingTime - time + 500, 0));
  216. }
  217. //显示等待窗口
  218. function showWaitingView() {
  219. // bShowWaiting = true;
  220. // setTimeout(function () {
  221. // if (!bShowWaiting) return;
  222. // beginWaitingTime = parseInt(new Date());
  223. // var msgw = 300; //提示窗口的宽度
  224. // var msgh = 100; //提示窗口的高度
  225. //
  226. // var sWidth, sHeight;
  227. // sWidth = document.body.clientWidth;
  228. // sHeight = document.body.clientHeight;
  229. //
  230. // //背景遮罩层div
  231. // var bgObj = document.createElement("div");
  232. // bgObj.setAttribute('id', 'bgDiv');
  233. // bgObj.style.zIndex = '9998';
  234. // bgObj.style.position = "absolute";
  235. // bgObj.style.top = "0px";
  236. // bgObj.style.background = "#888";
  237. // bgObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75";
  238. // bgObj.style.opacity = "0.6";
  239. // bgObj.style.left = "0px";
  240. // bgObj.style.width = sWidth + "px";
  241. // bgObj.style.height = sHeight + "px";
  242. // document.body.appendChild(bgObj);
  243. //
  244. // //信息提示层div
  245. // var msgObj = document.createElement("div");
  246. // msgObj.style.zIndex = '9999';
  247. // msgObj.setAttribute("id", "msgDiv");
  248. // msgObj.setAttribute("align", "center");
  249. // msgObj.style.position = "absolute";
  250. // msgObj.style.font = "12px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";
  251. // msgObj.style.width = msgw + "px";
  252. // msgObj.style.height = msgh + "px";
  253. // msgObj.style.top = (document.documentElement.scrollTop + (sHeight - msgh) / 2) + "px";
  254. // msgObj.style.left = (sWidth - msgw) / 2 + "px";
  255. // document.body.appendChild(msgObj);
  256. //
  257. // //中间等待图标
  258. // document.getElementById("msgDiv").innerHTML = '<i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i>';
  259. // }, 1000);
  260. beginWaitingTime = parseInt(new Date());
  261. var msgw = 300; //提示窗口的宽度
  262. var msgh = 100; //提示窗口的高度
  263. var sWidth, sHeight;
  264. sWidth = document.body.clientWidth;
  265. sHeight = document.body.clientHeight;
  266. //背景遮罩层div
  267. var bgObj = document.createElement("div");
  268. bgObj.setAttribute('id', 'bgDiv');
  269. bgObj.style.zIndex = '9998';
  270. bgObj.style.position = "absolute";
  271. bgObj.style.top = "0px";
  272. // bgObj.style.background = "#888";
  273. bgObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75";
  274. bgObj.style.opacity = "0.6";
  275. bgObj.style.left = "0px";
  276. bgObj.style.width = sWidth + "px";
  277. bgObj.style.height = sHeight + "px";
  278. document.body.appendChild(bgObj);
  279. //信息提示层div
  280. var msgObj = document.createElement("div");
  281. msgObj.style.zIndex = '9999';
  282. msgObj.setAttribute("id", "msgDiv");
  283. msgObj.setAttribute("align", "center");
  284. msgObj.style.position = "fixed";
  285. msgObj.style.font = "12px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";
  286. msgObj.style.width = msgw + "px";
  287. msgObj.style.height = msgh + "px";
  288. // msgObj.style.top = (document.documentElement.scrollTop + (sHeight - msgh) / 2) + "px";
  289. msgObj.style.top = "50%";
  290. msgObj.style.left = (sWidth - msgw) / 2 + "px";
  291. document.body.appendChild(msgObj);
  292. //中间等待图标
  293. document.getElementById("msgDiv").innerHTML = '<i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i>';
  294. }