compnt.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. export function showToast(message) {
  2. alert(message)
  3. }
  4. export function showAlert(message, hideButton, buttonLabel, hideTitle) {
  5. var title = '';
  6. if (hideTitle == undefined || hideTitle == false) {
  7. title = '温馨提示';
  8. }
  9. var $overlay = $('<div class="overlay overlay-alert"></div>');
  10. var $dialog = $(
  11. '<div class="dialog-alert">' +
  12. '<h1>' + title + '</h1>' +
  13. '<div class="content">' + message + '</div>' +
  14. '<div class="actions">' +
  15. ' <div class="btn">' + (buttonLabel == null || buttonLabel == "" ? "关闭" : buttonLabel) + '</div>' +
  16. '</div>' +
  17. '</div>');
  18. $("body").css("overflow", "hidden");
  19. $overlay.appendTo($(document.body));
  20. $dialog.appendTo($(document.body));
  21. if (hideButton == true) {
  22. $dialog.find('.btn').remove();
  23. }
  24. $dialog.close = function() {
  25. $("body").css("overflow", "auto");
  26. $overlay.remove();
  27. $dialog.remove();
  28. $dialog = null;
  29. }
  30. var def = $.Deferred();
  31. $dialog.find('.btn').on('click', function() {
  32. $dialog.close()
  33. def.resolve($dialog);
  34. })
  35. return def;
  36. }
  37. //去除温馨提示百度
  38. export function showAlertarxe(message, hideButton, buttonLabel, hideTitle) {
  39. var title = '';
  40. if (hideTitle == undefined || hideTitle == false) {
  41. title = '温馨提示';
  42. }
  43. var $overlay = $('<div class="overlay overlay-alert"></div>');
  44. var $dialog = $(
  45. '<div class="dialog-alert">' +
  46. // '<h1>' + title + '</h1>' +
  47. '<div class="content">' + message + '</div>' +
  48. '<div class="actions">' +
  49. ' <div class="btn btnsr">' + (buttonLabel == null || buttonLabel == "" ? "关闭" : buttonLabel) + '</div>' +
  50. '</div>' +
  51. '</div>');
  52. $("body").css("overflow", "hidden");
  53. $overlay.appendTo($(document.body));
  54. $dialog.appendTo($(document.body));
  55. if (hideButton == true) {
  56. $dialog.find('.btn').remove();
  57. }
  58. $dialog.close = function() {
  59. $("body").css("overflow", "auto");
  60. $overlay.remove();
  61. $dialog.remove();
  62. $dialog = null;
  63. }
  64. var def = $.Deferred();
  65. $dialog.find('.btn').on('click', function() {
  66. $dialog.close()
  67. def.resolve($dialog);
  68. })
  69. return def;
  70. }
  71. // 关闭后解除重复点击
  72. export function showAlerts(message, hideButton, buttonLabel, hideTitle) {
  73. var title = '';
  74. if (hideTitle == undefined || hideTitle == false) {
  75. title = '温馨提示';
  76. }
  77. var $overlay = $('<div class="overlay overlay-alert"></div>');
  78. var $dialog = $(
  79. '<div class="dialog-alert">' +
  80. '<h1>' + title + '</h1>' +
  81. '<div class="content">' + message + '</div>' +
  82. '<div class="actions">' +
  83. ' <div class="btn">' + (buttonLabel == null || buttonLabel == "" ? "关闭" : buttonLabel) + '</div>' +
  84. '</div>' +
  85. '</div>');
  86. $("body").css("overflow", "hidden");
  87. $overlay.appendTo($(document.body));
  88. $dialog.appendTo($(document.body));
  89. if (hideButton == true) {
  90. $dialog.find('.btn').remove();
  91. }
  92. $dialog.close = function() {
  93. $("body").css("overflow", "auto");
  94. $overlay.remove();
  95. $dialog.remove();
  96. $dialog = null;
  97. }
  98. var def = $.Deferred();
  99. $dialog.find('.btn').on('click', function() {
  100. $dialog.close()
  101. def.resolve($dialog);
  102. unlockClick();
  103. })
  104. return def;
  105. }
  106. export function showLoading(message) {
  107. if (window.$loading) {
  108. window.$loading.trigger('close');
  109. }
  110. $("body").css('overflow', 'hidden');
  111. var msg = (message || '处理中,请稍后...');
  112. var $overlay = $('<div class="overlay overlay-loading"></div>');
  113. var $loading = $(
  114. '<div class="loading">' +
  115. ' <div class="content">' + msg + '</div>' +
  116. '</div>');
  117. $overlay.appendTo($(document.body))
  118. $loading.appendTo($(document.body))
  119. $loading.on('close', function() {
  120. $("body").css('overflow', 'auto');
  121. $overlay.remove();
  122. $loading.remove();
  123. $loading = null;
  124. })
  125. window.$loading = $loading;
  126. return $loading;
  127. }
  128. export function hideLoading($loading) {
  129. if ($loading) {
  130. $loading.trigger('close');
  131. return;
  132. } else if (window.$loading) {
  133. window.$loading.trigger('close');
  134. return;
  135. }
  136. }
  137. /**
  138. * 防止重复点击
  139. * @param {} element
  140. */
  141. export function lockClick(element) {
  142. if ($(element || document.body).data('use') == '1') {
  143. return false;
  144. }
  145. $(element || document.body).data('use', '1');
  146. return true;
  147. }
  148. /**
  149. * 防止重复点击
  150. * @param {*} element
  151. */
  152. export function unlockClick(element) {
  153. $(element || document.body).data('use', '0');
  154. }
  155. /**
  156. * 保留2位小数,并且在在小数点后补0
  157. * @param {*} value
  158. */
  159. export function returnFloat(value) {
  160. var value = Math.round(parseFloat(value) * 100) / 100;
  161. var xsd = value.toString().split(".");
  162. if (xsd.length == 1) {
  163. value = value.toString() + ".00";
  164. return value;
  165. }
  166. if (xsd.length > 1) {
  167. if (xsd[1].length < 2) {
  168. value = value.toString() + "0";
  169. }
  170. return value;
  171. }
  172. }
  173. !(function() {
  174. /**
  175. * 计算今天剩余的毫秒
  176. */
  177. Date.remainMillis = function() {
  178. var now = new Date();
  179. return (23 - now.getHours()) * 60 * 60 * 1000 +
  180. (59 - now.getMinutes()) * 60 * 1000 +
  181. (59 - now.getSeconds()) * 1000;
  182. }
  183. /**
  184. * 计算本月剩余的天数
  185. */
  186. Date.remainMillisMonth = function() {
  187. var curDate = new Date();
  188. var curMonth = curDate.getMonth(); //当前月份 需要加1
  189. curDate.setMonth(curMonth + 1);
  190. curDate.setDate(0) // 关键
  191. curDate.getDate(); // 计算的当月总天数
  192. return curDate.getDate() - new Date().getDate()
  193. }
  194. /**
  195. * 将字符串转换为Date对象
  196. */
  197. Date.from = function(dateString) {
  198. var pattern = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/
  199. var str = dateString.replace(/-/g, '/');
  200. str = str.replace(pattern, '$1/$2/$3 $4:$5:$6')
  201. return new Date(str)
  202. }
  203. // Date.prototype.from = function (dateString) {
  204. // var pattern = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/
  205. // var str = dateString.replace(/-/g, '/');
  206. // str = str.replace(pattern, '$1/$2/$3 $4:$5:$6')
  207. // return new Date(str)
  208. // }
  209. /**
  210. * 对Date的扩展,将 Date 转化为指定格式的String
  211. * 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
  212. * 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
  213. * 例子:
  214. * (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
  215. * (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
  216. * author: meizz
  217. */
  218. Date.prototype.format = function(fmt) {
  219. var o = {
  220. "M+": this.getMonth() + 1,
  221. "d+": this.getDate(),
  222. "h+": this.getHours(),
  223. "m+": this.getMinutes(),
  224. "s+": this.getSeconds(),
  225. "q+": Math.floor((this.getMonth() + 3) / 3),
  226. "S": this.getMilliseconds()
  227. };
  228. if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  229. for (var k in o)
  230. if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  231. return fmt;
  232. }
  233. })();
  234. /**
  235. * 计算当天剩余毫秒数据
  236. */
  237. //export function getTodaySurplusMillis() {
  238. // var time1 = new Date();
  239. // var time2 = Date.from(time1.getFullYear() + '-' + (time1.getMonth() + 1) + '-' + time1.getDate() + ' 23:59:59');
  240. //
  241. // console.log(time2.getTime() - time1)
  242. // return time2.getTime() - time1.getTime();
  243. //}
  244. export function fixWechatBug() {
  245. if (navigator.userAgent.indexOf("MicroMessenger") >= 0) {
  246. //微信6.7.4及以后版本 ios12软键盘顶起页面后隐藏不回弹解决方案
  247. //打开嵌入的统一认证取号页面前,让原来的页面上下滚动。
  248. //统一认证的页面关闭以后,删除定时任务 clearInterval(window.capTimer)
  249. var currentPosition;
  250. var speed = 1; // 页面滚动距离
  251. window.capTimer = setInterval(function() {
  252. currentPosition = document.documentElement.scrollTop || document.body.scrollTop;
  253. currentPosition -= speed;
  254. window.top.scrollTo(0, currentPosition); //页面向上滚动
  255. currentPosition += speed; //speed变量
  256. window.top.scrollTo(0, currentPosition); //页面向下滚动
  257. }, 200);
  258. }
  259. }