global.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. function autoFlashHeight(){
  2. }
  3. $(window).resize(autoFlashHeight);
  4. $(function () {
  5. $('a').on('click', function () {
  6. if ($(this).hasClass('show-loading')) {
  7. showWaitingView();
  8. }
  9. });
  10. $('[data-toggle="tooltip"]').tooltip();
  11. $('[data-toggle="popover"]').popover();
  12. })
  13. /**
  14. * 动态请求数据
  15. * @param {String} url - 请求链接
  16. * @param data - 提交数据
  17. * @param {function} successCallback - 返回成功回调
  18. * @param {function} errorCallBack - 返回失败回调
  19. */
  20. const postData = function (url, data, successCallback, errorCallBack, showWaiting = true) {
  21. if (showWaiting) showWaitingView();
  22. $.ajax({
  23. type:"POST",
  24. url: url,
  25. data: {'data': JSON.stringify(data)},
  26. dataType: 'json',
  27. cache: false,
  28. timeout: 60000,
  29. beforeSend: function(xhr) {
  30. let csrfToken = Cookies.get('csrfToken_j');
  31. xhr.setRequestHeader('x-csrf-token', csrfToken);
  32. },
  33. success: function(result){
  34. if (result.err === 0) {
  35. if (successCallback) {
  36. successCallback(result.data);
  37. }
  38. } else if (result.err === 2) {
  39. toastr.error('error: ' + result.msg);
  40. setTimeout(function () {
  41. window.location.href = '/wap/login';
  42. },1000);
  43. } else {
  44. toastr.error('error: ' + result.msg);
  45. if (errorCallBack) {
  46. errorCallBack(result.msg);
  47. }
  48. }
  49. if (showWaiting) closeWaitingView();
  50. },
  51. error: function(jqXHR, textStatus, errorThrown){
  52. toastr.error('error: ' + textStatus + " " + errorThrown);
  53. if (errorCallBack) {
  54. errorCallBack();
  55. }
  56. if (showWaiting) closeWaitingView();
  57. }
  58. });
  59. };
  60. /**
  61. * 动态请求数据
  62. * @param {String} url - 请求链接
  63. * @param data - 提交数据
  64. * @param {function} successCallback - 返回成功回调
  65. * @param {function} errorCallBack - 返回失败回调
  66. */
  67. const postDataWithFile = function (url, formData, successCallback, errorCallBack, showWaiting = true) {
  68. if (showWaiting) showWaitingView();
  69. if (formData.getAll('file[]').length > 10) {
  70. toastr.error('文件数量不能多于10个');
  71. if (showWaiting) closeWaitingView();
  72. return
  73. }
  74. $.ajax({
  75. type:"POST",
  76. url: url,
  77. data: formData,
  78. dataType: 'json',
  79. cache: false,
  80. // 告诉jQuery不要去设置Content-Type请求头
  81. contentType: false,
  82. // 告诉jQuery不要去处理发送的数据
  83. processData: false,
  84. timeout: 60000,
  85. beforeSend: function(xhr) {
  86. let csrfToken = Cookies.get('csrfToken_j');
  87. xhr.setRequestHeader('x-csrf-token', csrfToken);
  88. },
  89. success: function(result){
  90. if (result.err === 0) {
  91. if (successCallback) {
  92. successCallback(result.data);
  93. }
  94. } else if (result.err === 2) {
  95. toastr.error('error: ' + result.msg);
  96. setTimeout(function () {
  97. window.location.href = '/wap/login';
  98. },1000);
  99. } else {
  100. toastr.error('error: ' + result.msg);
  101. if (errorCallBack) {
  102. errorCallBack();
  103. }
  104. }
  105. if (showWaiting) closeWaitingView();
  106. },
  107. error: function(jqXHR, textStatus, errorThrown){
  108. toastr.error('error: ' + textStatus + " " + errorThrown);
  109. if (errorCallBack) {
  110. errorCallBack();
  111. }
  112. if (showWaiting) closeWaitingView();
  113. }
  114. });
  115. };
  116. const postDataWithFileAsync = function (url, formData, showWaiting = true) {
  117. return new Promise(function (resolve, reject) {
  118. postDataWithFile(url, formData, result => {
  119. resolve(result);
  120. }, err => {
  121. reject(err);
  122. }, showWaiting);
  123. });
  124. };
  125. /**
  126. * 提示框
  127. *
  128. * @param string message
  129. * @param string type
  130. * @param string icon
  131. * @return void
  132. */
  133. function toast(message, type, icon) {
  134. var toast = $(".toast");
  135. toast.addClass(type);
  136. toast.children('.message').html(message);
  137. var iconClass = 'fa-' + icon;
  138. toast.children('.icon').addClass(iconClass);
  139. toast.fadeIn(500);
  140. setTimeout(function() {
  141. toast.fadeOut('fast');
  142. toast.children('.message').text('');
  143. toast.children('.icon').removeClass(iconClass);
  144. }, 3000);
  145. }
  146. /**
  147. * 获取url中参数
  148. * @param variable
  149. * @returns {*}
  150. */
  151. function getQueryVariable(variable) {
  152. var query = window.location.search.substring(1);
  153. var vars = query.split("&");
  154. for (var i=0;i<vars.length;i++) {
  155. var pair = vars[i].split("=");
  156. if(pair[0] == variable){return pair[1];}
  157. }
  158. return(false);
  159. }
  160. const zeroRange = 0.00000001;
  161. function checkZero(value) {
  162. return value === null || value === undefined || (_.isNumber(value) && Math.abs(value) < zeroRange);
  163. }
  164. function checkFieldChange(o, n) {
  165. return o == n || ((!o || o === '') && (n === ''));
  166. }
  167. /**
  168. * 设置本地缓存
  169. *
  170. * @param {String} key
  171. * @param {String|Number} value
  172. * @return {void}
  173. */
  174. function setLocalCache(key, value) {
  175. const storage = window.localStorage;
  176. if (!storage || key === '' || value === '') {
  177. return;
  178. }
  179. storage.setItem(key, value);
  180. }
  181. /**
  182. * 获取本地缓存
  183. *
  184. * @param {String} key
  185. * @return {String}
  186. */
  187. function getLocalCache(key) {
  188. const storage = window.localStorage;
  189. if (!storage || key === '') {
  190. return null;
  191. }
  192. return storage.getItem(key);
  193. }
  194. /**
  195. * 移除本地缓存
  196. * @param {String} key
  197. * @returns {Boolean}
  198. */
  199. function removeLocalCache(key) {
  200. const storage = window.localStorage;
  201. if (!storage || key === '') {
  202. return null;
  203. }
  204. return storage.removeItem(key);
  205. }
  206. function trimInvalidChar(str) {
  207. return $.trim(str).replace(/\n/g, '').replace(/\r/g, '').replace(/\t/g, '');
  208. }
  209. function cleanSymbols(str) {
  210. return $.trim(str).replace(/\\/g, '').replace(/\'/g, '').replace(/\"/g, '').replace(/\</g, '').replace(/\|/g, '');
  211. }
  212. //关闭等待窗口
  213. function closeWaitingView() {
  214. bShowWaiting = false;
  215. const time = parseInt(new Date());
  216. setTimeout(function () {
  217. var bgDiv = document.getElementById("bgDiv");
  218. var msgDiv = document.getElementById("msgDiv");
  219. //移除背景遮罩层div
  220. if(bgDiv != null){
  221. document.body.removeChild(bgDiv);
  222. }
  223. //移除中间信息提示层div
  224. if(msgDiv != null){
  225. document.body.removeChild(msgDiv);
  226. }
  227. }, Math.max(beginWaitingTime - time + 500, 0));
  228. }
  229. //显示等待窗口
  230. function showWaitingView() {
  231. // bShowWaiting = true;
  232. // setTimeout(function () {
  233. // if (!bShowWaiting) return;
  234. // beginWaitingTime = parseInt(new Date());
  235. // var msgw = 300; //提示窗口的宽度
  236. // var msgh = 100; //提示窗口的高度
  237. //
  238. // var sWidth, sHeight;
  239. // sWidth = document.body.clientWidth;
  240. // sHeight = document.body.clientHeight;
  241. //
  242. // //背景遮罩层div
  243. // var bgObj = document.createElement("div");
  244. // bgObj.setAttribute('id', 'bgDiv');
  245. // bgObj.style.zIndex = '9998';
  246. // bgObj.style.position = "absolute";
  247. // bgObj.style.top = "0px";
  248. // bgObj.style.background = "#888";
  249. // bgObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75";
  250. // bgObj.style.opacity = "0.6";
  251. // bgObj.style.left = "0px";
  252. // bgObj.style.width = sWidth + "px";
  253. // bgObj.style.height = sHeight + "px";
  254. // document.body.appendChild(bgObj);
  255. //
  256. // //信息提示层div
  257. // var msgObj = document.createElement("div");
  258. // msgObj.style.zIndex = '9999';
  259. // msgObj.setAttribute("id", "msgDiv");
  260. // msgObj.setAttribute("align", "center");
  261. // msgObj.style.position = "absolute";
  262. // msgObj.style.font = "12px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";
  263. // msgObj.style.width = msgw + "px";
  264. // msgObj.style.height = msgh + "px";
  265. // msgObj.style.top = (document.documentElement.scrollTop + (sHeight - msgh) / 2) + "px";
  266. // msgObj.style.left = (sWidth - msgw) / 2 + "px";
  267. // document.body.appendChild(msgObj);
  268. //
  269. // //中间等待图标
  270. // document.getElementById("msgDiv").innerHTML = '<i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i>';
  271. // }, 1000);
  272. beginWaitingTime = parseInt(new Date());
  273. var msgw = 300; //提示窗口的宽度
  274. var msgh = 100; //提示窗口的高度
  275. var sWidth, sHeight;
  276. sWidth = document.body.clientWidth;
  277. sHeight = document.body.clientHeight;
  278. //背景遮罩层div
  279. var bgObj = document.createElement("div");
  280. bgObj.setAttribute('id', 'bgDiv');
  281. bgObj.style.zIndex = '9998';
  282. bgObj.style.position = "absolute";
  283. bgObj.style.top = "0px";
  284. // bgObj.style.background = "#888";
  285. bgObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75";
  286. bgObj.style.opacity = "0.6";
  287. bgObj.style.left = "0px";
  288. bgObj.style.width = sWidth + "px";
  289. bgObj.style.height = sHeight + "px";
  290. document.body.appendChild(bgObj);
  291. //信息提示层div
  292. var msgObj = document.createElement("div");
  293. msgObj.style.zIndex = '9999';
  294. msgObj.setAttribute("id", "msgDiv");
  295. msgObj.setAttribute("align", "center");
  296. msgObj.style.position = "fixed";
  297. msgObj.style.font = "12px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";
  298. msgObj.style.width = msgw + "px";
  299. msgObj.style.height = msgh + "px";
  300. // msgObj.style.top = (document.documentElement.scrollTop + (sHeight - msgh) / 2) + "px";
  301. msgObj.style.top = "50%";
  302. msgObj.style.left = (sWidth - msgw) / 2 + "px";
  303. document.body.appendChild(msgObj);
  304. //中间等待图标
  305. document.getElementById("msgDiv").innerHTML = '<i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i>';
  306. }
  307. function transFormToChinese(num) {
  308. const changeNum = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
  309. const unit = ["", "十", "百", "千", "万"];
  310. num = parseInt(num);
  311. let getWan = (temp) => {
  312. let strArr = temp.toString().split("").reverse();
  313. let newNum = "";
  314. for (var i = 0; i < strArr.length; i++) {
  315. newNum = (i == 0 && strArr[i] == 0 ? "" : (i > 0 && strArr[i] == 0 && strArr[i - 1] == 0 ? "" : changeNum[strArr[i]] + (strArr[i] == 0 ? unit[0] : unit[i]))) + newNum;
  316. }
  317. return strArr.length === 2 && newNum.indexOf("一十") !== -1 ? newNum.replace('一十', '十') : newNum;
  318. }
  319. let overWan = Math.floor(num / 10000);
  320. let noWan = num % 10000;
  321. if (noWan.toString().length < 4) noWan = "0" + noWan;
  322. return overWan ? getWan(overWan) + "万" + getWan(noWan) : getWan(num);
  323. }