global.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. const postDataWithFileAsync = function (url, formData, showWaiting = true) {
  114. return new Promise(function (resolve, reject) {
  115. postDataWithFile(url, formData, result => {
  116. resolve(result);
  117. }, err => {
  118. reject(err);
  119. }, showWaiting);
  120. });
  121. };
  122. /**
  123. * 提示框
  124. *
  125. * @param string message
  126. * @param string type
  127. * @param string icon
  128. * @return void
  129. */
  130. function toast(message, type, icon) {
  131. var toast = $(".toast");
  132. toast.addClass(type);
  133. toast.children('.message').html(message);
  134. var iconClass = 'fa-' + icon;
  135. toast.children('.icon').addClass(iconClass);
  136. toast.fadeIn(500);
  137. setTimeout(function() {
  138. toast.fadeOut('fast');
  139. toast.children('.message').text('');
  140. toast.children('.icon').removeClass(iconClass);
  141. }, 3000);
  142. }
  143. /**
  144. * 获取url中参数
  145. * @param variable
  146. * @returns {*}
  147. */
  148. function getQueryVariable(variable) {
  149. var query = window.location.search.substring(1);
  150. var vars = query.split("&");
  151. for (var i=0;i<vars.length;i++) {
  152. var pair = vars[i].split("=");
  153. if(pair[0] == variable){return pair[1];}
  154. }
  155. return(false);
  156. }
  157. const zeroRange = 0.00000001;
  158. function checkZero(value) {
  159. return value === null || value === undefined || (_.isNumber(value) && Math.abs(value) < zeroRange);
  160. }
  161. function checkFieldChange(o, n) {
  162. return o == n || ((!o || o === '') && (n === ''));
  163. }
  164. /**
  165. * 设置本地缓存
  166. *
  167. * @param {String} key
  168. * @param {String|Number} value
  169. * @return {void}
  170. */
  171. function setLocalCache(key, value) {
  172. const storage = window.localStorage;
  173. if (!storage || key === '' || value === '') {
  174. return;
  175. }
  176. storage.setItem(key, value);
  177. }
  178. /**
  179. * 获取本地缓存
  180. *
  181. * @param {String} key
  182. * @return {String}
  183. */
  184. function getLocalCache(key) {
  185. const storage = window.localStorage;
  186. if (!storage || key === '') {
  187. return null;
  188. }
  189. return storage.getItem(key);
  190. }
  191. /**
  192. * 移除本地缓存
  193. * @param {String} key
  194. * @returns {Boolean}
  195. */
  196. function removeLocalCache(key) {
  197. const storage = window.localStorage;
  198. if (!storage || key === '') {
  199. return null;
  200. }
  201. return storage.removeItem(key);
  202. }
  203. function trimInvalidChar(str) {
  204. return $.trim(str).replace(/\n/g, '').replace(/\r/g, '').replace(/\t/g, '');
  205. }
  206. function cleanSymbols(str) {
  207. return $.trim(str).replace(/\\/g, '').replace(/\'/g, '').replace(/\"/g, '').replace(/\</g, '').replace(/\|/g, '');
  208. }
  209. //关闭等待窗口
  210. function closeWaitingView() {
  211. bShowWaiting = false;
  212. const time = parseInt(new Date());
  213. setTimeout(function () {
  214. var bgDiv = document.getElementById("bgDiv");
  215. var msgDiv = document.getElementById("msgDiv");
  216. //移除背景遮罩层div
  217. if(bgDiv != null){
  218. document.body.removeChild(bgDiv);
  219. }
  220. //移除中间信息提示层div
  221. if(msgDiv != null){
  222. document.body.removeChild(msgDiv);
  223. }
  224. }, Math.max(beginWaitingTime - time + 500, 0));
  225. }
  226. //显示等待窗口
  227. function showWaitingView() {
  228. // bShowWaiting = true;
  229. // setTimeout(function () {
  230. // if (!bShowWaiting) return;
  231. // beginWaitingTime = parseInt(new Date());
  232. // var msgw = 300; //提示窗口的宽度
  233. // var msgh = 100; //提示窗口的高度
  234. //
  235. // var sWidth, sHeight;
  236. // sWidth = document.body.clientWidth;
  237. // sHeight = document.body.clientHeight;
  238. //
  239. // //背景遮罩层div
  240. // var bgObj = document.createElement("div");
  241. // bgObj.setAttribute('id', 'bgDiv');
  242. // bgObj.style.zIndex = '9998';
  243. // bgObj.style.position = "absolute";
  244. // bgObj.style.top = "0px";
  245. // bgObj.style.background = "#888";
  246. // bgObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75";
  247. // bgObj.style.opacity = "0.6";
  248. // bgObj.style.left = "0px";
  249. // bgObj.style.width = sWidth + "px";
  250. // bgObj.style.height = sHeight + "px";
  251. // document.body.appendChild(bgObj);
  252. //
  253. // //信息提示层div
  254. // var msgObj = document.createElement("div");
  255. // msgObj.style.zIndex = '9999';
  256. // msgObj.setAttribute("id", "msgDiv");
  257. // msgObj.setAttribute("align", "center");
  258. // msgObj.style.position = "absolute";
  259. // msgObj.style.font = "12px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";
  260. // msgObj.style.width = msgw + "px";
  261. // msgObj.style.height = msgh + "px";
  262. // msgObj.style.top = (document.documentElement.scrollTop + (sHeight - msgh) / 2) + "px";
  263. // msgObj.style.left = (sWidth - msgw) / 2 + "px";
  264. // document.body.appendChild(msgObj);
  265. //
  266. // //中间等待图标
  267. // document.getElementById("msgDiv").innerHTML = '<i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i>';
  268. // }, 1000);
  269. beginWaitingTime = parseInt(new Date());
  270. var msgw = 300; //提示窗口的宽度
  271. var msgh = 100; //提示窗口的高度
  272. var sWidth, sHeight;
  273. sWidth = document.body.clientWidth;
  274. sHeight = document.body.clientHeight;
  275. //背景遮罩层div
  276. var bgObj = document.createElement("div");
  277. bgObj.setAttribute('id', 'bgDiv');
  278. bgObj.style.zIndex = '9998';
  279. bgObj.style.position = "absolute";
  280. bgObj.style.top = "0px";
  281. // bgObj.style.background = "#888";
  282. bgObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75";
  283. bgObj.style.opacity = "0.6";
  284. bgObj.style.left = "0px";
  285. bgObj.style.width = sWidth + "px";
  286. bgObj.style.height = sHeight + "px";
  287. document.body.appendChild(bgObj);
  288. //信息提示层div
  289. var msgObj = document.createElement("div");
  290. msgObj.style.zIndex = '9999';
  291. msgObj.setAttribute("id", "msgDiv");
  292. msgObj.setAttribute("align", "center");
  293. msgObj.style.position = "fixed";
  294. msgObj.style.font = "12px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";
  295. msgObj.style.width = msgw + "px";
  296. msgObj.style.height = msgh + "px";
  297. // msgObj.style.top = (document.documentElement.scrollTop + (sHeight - msgh) / 2) + "px";
  298. msgObj.style.top = "50%";
  299. msgObj.style.left = (sWidth - msgw) / 2 + "px";
  300. document.body.appendChild(msgObj);
  301. //中间等待图标
  302. document.getElementById("msgDiv").innerHTML = '<i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i>';
  303. }