util.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. const formatTime = date => {
  2. const year = date.getFullYear()
  3. const month = date.getMonth() + 1
  4. const day = date.getDate()
  5. const hour = date.getHours()
  6. const minute = date.getMinutes()
  7. const second = date.getSeconds()
  8. return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
  9. }
  10. const formatNumber = n => {
  11. n = n.toString()
  12. return n[1] ? n : `0${n}`
  13. }
  14. const urlToObj = function(url) {
  15. let obj = {}
  16. let str = url.slice(url.indexOf('?') + 1)
  17. let arr = str.split('&')
  18. for(let i = 0 ; i < arr.length ; i ++) {
  19. let arr_temp = arr[i].split('=')
  20. obj[arr_temp[0]] = arr_temp[1]
  21. }
  22. return obj
  23. }
  24. const removeCss = function(content) {
  25. let reg=/(style|class)="[^"]+"/gi;
  26. let img=/<img[^>]+>/gi;
  27. let res;
  28. if(img.test(content))
  29. {
  30. res = content.match(img);
  31. for(let i=0;i<res.length;i++){
  32. console.log(res[i].replace(reg,""));
  33. content=content.replace(res[i],res[i].replace(reg,""));
  34. }
  35. }
  36. // (/\<img/gi, '<img style="max-width:100%;height:auto;display:block;"')
  37. return content.replace(/\<img/gi, '<img class="richImg" ');
  38. }
  39. const getPrePage = function(content) {
  40. let pages=getCurrentPages()
  41. return pages[pages.length-1]
  42. }
  43. const objToParam = function(param) {
  44. if (Object.prototype.toString.call(param) !== '[object Object]') {
  45. return '';
  46. }
  47. let queryParam = '';
  48. for (let key in param) {
  49. if (param.hasOwnProperty(key)) {
  50. let value = param[key];
  51. queryParam += toQueryPair(key, value);
  52. }
  53. }
  54. return queryParam;
  55. }
  56. let callBackList = [];
  57. let timer_;
  58. /**
  59. * 开启定时器
  60. */
  61. const startTimer = function() {
  62. if(!timer_) {
  63. timer_ = setInterval(() => {
  64. if(callBackList.length == 0) {
  65. console.log('倒计时结束 关闭定时器');
  66. clearInterval(timer_);
  67. timer_ = null;
  68. }
  69. callBackList.forEach((item,index) => {
  70. item.currentTime--
  71. var h = parseInt(item.currentTime / 3600); //小时
  72. var m = parseInt(item.currentTime / 60 % 60); //分钟
  73. var s = parseInt(item.currentTime % 60); //当前的秒
  74. let obj = {
  75. h,m,s,
  76. currentTime:item.currentTime
  77. }
  78. item.fn(obj);
  79. if(item.currentTime <= 0) {
  80. callBackList.splice(index,1);
  81. }
  82. });
  83. }, 1000);
  84. }
  85. }
  86. const addTime = function(callback,currentTime) {
  87. if(currentTime) {
  88. callBackList.push({fn: callback,currentTime});
  89. console.log('第'+callBackList.length+'个倒计时开启');
  90. if(!timer_) {
  91. startTimer()
  92. }
  93. }
  94. }
  95. const removeTime = function(fn) {
  96. for( var i = 0 ; i < callBackList.length ; i ++) {
  97. let item = callBackList[i];
  98. if(item.fn == fn) {
  99. console.log('清除倒计时~');
  100. callBackList.splice(i,1);
  101. }
  102. }
  103. }
  104. function toQueryPair(key, value) {
  105. if (typeof value == 'undefined') {
  106. return `${key}=&`;
  107. }
  108. return `${key}=${value}&`;
  109. }
  110. module.exports = {
  111. formatTime,
  112. urlToObj,
  113. removeCss,
  114. getPrePage,
  115. objToParam,
  116. addTime,
  117. removeTime
  118. }