123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- const formatTime = date => {
- const year = date.getFullYear()
- const month = date.getMonth() + 1
- const day = date.getDate()
- const hour = date.getHours()
- const minute = date.getMinutes()
- const second = date.getSeconds()
- return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
- }
- const formatNumber = n => {
- n = n.toString()
- return n[1] ? n : `0${n}`
- }
- const urlToObj = function(url) {
- let obj = {}
- let str = url.slice(url.indexOf('?') + 1)
- let arr = str.split('&')
- for(let i = 0 ; i < arr.length ; i ++) {
- let arr_temp = arr[i].split('=')
- obj[arr_temp[0]] = arr_temp[1]
- }
- return obj
- }
- const removeCss = function(content) {
- let reg=/(style|class)="[^"]+"/gi;
- let img=/<img[^>]+>/gi;
- let res;
- if(img.test(content))
- {
- res = content.match(img);
- for(let i=0;i<res.length;i++){
- console.log(res[i].replace(reg,""));
- content=content.replace(res[i],res[i].replace(reg,""));
- }
- }
- // (/\<img/gi, '<img style="max-width:100%;height:auto;display:block;"')
- return content.replace(/\<img/gi, '<img class="richImg" ');
- }
- const getPrePage = function(content) {
- let pages=getCurrentPages()
- return pages[pages.length-1]
- }
- const objToParam = function(param) {
- if (Object.prototype.toString.call(param) !== '[object Object]') {
- return '';
- }
- let queryParam = '';
- for (let key in param) {
- if (param.hasOwnProperty(key)) {
- let value = param[key];
- queryParam += toQueryPair(key, value);
- }
- }
- return queryParam;
- }
- let callBackList = [];
- let timer_;
- /**
- * 开启定时器
- */
- const startTimer = function() {
- if(!timer_) {
- timer_ = setInterval(() => {
- if(callBackList.length == 0) {
- console.log('倒计时结束 关闭定时器');
- clearInterval(timer_);
- timer_ = null;
- }
- callBackList.forEach((item,index) => {
- item.currentTime--
- var h = parseInt(item.currentTime / 3600); //小时
- var m = parseInt(item.currentTime / 60 % 60); //分钟
- var s = parseInt(item.currentTime % 60); //当前的秒
- let obj = {
- h,m,s,
- currentTime:item.currentTime
- }
- item.fn(obj);
- if(item.currentTime <= 0) {
- callBackList.splice(index,1);
- }
- });
- }, 1000);
- }
- }
- const addTime = function(callback,currentTime) {
- if(currentTime) {
- callBackList.push({fn: callback,currentTime});
- console.log('第'+callBackList.length+'个倒计时开启');
- if(!timer_) {
- startTimer()
- }
- }
- }
- const removeTime = function(fn) {
- for( var i = 0 ; i < callBackList.length ; i ++) {
- let item = callBackList[i];
- if(item.fn == fn) {
- console.log('清除倒计时~');
- callBackList.splice(i,1);
- }
- }
- }
- function toQueryPair(key, value) {
- if (typeof value == 'undefined') {
- return `${key}=&`;
- }
- return `${key}=${value}&`;
- }
- module.exports = {
- formatTime,
- urlToObj,
- removeCss,
- getPrePage,
- objToParam,
- addTime,
- removeTime
- }
|