工具类

作者: 奇怪的双子座 | 来源:发表于2018-10-17 14:30 被阅读0次

    import router from '@/router'

    let util = {

    }

    /**

    *

    * @param {*转化的时间格式} fmt

    * @param {*日期时间对象} date

    * @return 返回格式化后的字符串:yyyy-MM-dd HH:mm:ss 

    */

    util.formateDate = function (fmt, date) { //author: meizz 

      var o = {

        "M+": date.getMonth() + 1, //月份 

        "d+": date.getDate(), //日 

        "h+": date.getHours(), //小时 

        "m+": date.getMinutes(), //分 

        "s+": date.getSeconds(), //秒 

        "q+": Math.floor((date.getMonth() + 3) / 3), //季度 

        "S": date.getMilliseconds() //毫秒 

      };

      if (/(y+)/.test(fmt))

        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));

      for (var k in o)

        if (new RegExp("(" + k + ")").test(fmt))

          fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));

      return fmt;

    }

    /**

    *

    * @param {*时间差值毫秒} EndTimeMsg

    * @return {*返回时分秒}

    */

    util.timeRun = function (EndTimeMsg) {

      let h = Math.floor(EndTimeMsg / 60 / 60);

      let m = Math.floor((EndTimeMsg - h * 60 * 60) / 60);

      let s = Math.floor((EndTimeMsg - h * 60 * 60 - m * 60));

      if (h > 0) {

        return h + "小时" + m + "分" + s + '秒'

      } else {

        return m + "分" + s + "秒"

      }

    }

    // 对Date的扩展,将 Date 转化为指定格式的String

    // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,

    // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)

    // 例子:

    // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423

    // (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18

    Date.prototype.Format = function (fmt) {

      var o = {

        "M+": this.getMonth() + 1, //月份

        "d+": this.getDate(), //日

        "h+": this.getHours(), //小时

        "m+": this.getMinutes(), //分

        "s+": this.getSeconds(), //秒

        "q+": Math.floor((this.getMonth() + 3) / 3), //季度

        "S": this.getMilliseconds() //毫秒

      };

      if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));

      for (var k in o)

        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));

      return fmt;

    }

    /**

    * 获取时间差转化为时分秒

    * @param {现在时间毫秒} nowTime

    * @param {*之前时间毫秒} beformTime

    * @param {*倒计时总时间} alltime

    * @return {*返回时分秒}

    */

    util.differenctTime = function (nowTime, endTime) {

    ////console.log(nowTime + "**********" + endTime + "**********");

      if ((nowTime - endTime) > 0) {

        return false;

      } else {

        return this.timeRun((endTime - nowTime) / 1000)

      }

    }

    util.loading = {

    show: function () {

        //alert(1)

        dd.ready(function(){

        dd.device.notification.showPreloader({

          text: "使劲加载中..", //loading显示的字符,空表示不显示文字

          showIcon: true, //是否显示icon,默认true。Android无此参数。

          onSuccess: function (result) {

            },

          onFail: function (err) {

          }

        })

        })

    },

      hide: function () {

        dd.ready(function(){

        dd.device.notification.hidePreloader({

          onSuccess: function (result) {

            /*{}*/

          },

          onFail: function (err) {}

        })

        })

        }

    }

    //设置标题

    util.setTitle = function (name) {

      dd.ready(function () {

        dd.biz.navigation.setTitle({

          title: name

        });

      })

    }

    //设置右上角的按钮

    util.setRight = function (show, control, msg, successCallBack, failCallBack) {

      // if(!show){

      //  control = false;

      //  msg = '';

      //  successCallBack=function(){//console.log('succ');};

      //  failCallBack=function(){//console.log('err');};

      // }

      dd.ready(function () {

        dd.biz.navigation.setRight({    

            show: show ? true : false, //控制按钮显示, true 显示, false 隐藏, 默认true

              control: control, //是否控制点击事件,true 控制,false 不控制, 默认false

              text: msg, //控制显示文本,空字符串表示显示默认文本

              onSuccess: successCallBack,

              onFail: failCallBack

        });

      })

    }

    // util.listenName=null;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

    util.setLeft = function (thisobj,callBack) {

      if (this.isAndroid()) {

        listenName =function(e) {

    ////console.log("点击返回按钮了");

          // // 在这里处理你的业务逻辑

          // if (event == -1) { //如果传进来-1表明回退历史纪录  否则阻止默认回退行为,并重写

          if (callBack) {

            e.preventDefault();

            callBack(thisobj);

          } else {

            e.preventDefault();

            dd.ready(function () {

              dd.biz.navigation.goBack({

                onSuccess: function (result) {

                  /*result结构

                  {}

                  */

                },

                onFail: function (err) {

                  ////console.log(err);

                }

              })

            })

          }

        };

        // //console.log(getEventListeners(document.querySelector('backbutton')));

        document.addEventListener('backbutton',listenName,false);

      } else {

        // callBack();

        console.log("thatObj");

        dd.ready(function () {

          let canControl=false;

          if (callBack) {

            canControl=true;

          };

          dd.biz.navigation.setLeft({

            control: canControl, //是否控制点击事件,true 控制,false 不控制, 默认false

            text: '', //控制显示文本,空字符串表示显示默认文本

            onSuccess: function (result) {

              if (callBack) {

                callBack(thisobj)

              }else{

                dd.ready(function () {

                  dd.biz.navigation.goBack({

                    onSuccess: function (result) {

                      /*result结构

                      {}

                      */

                    },

                    onFail: function (err) {

                      ////console.log(err);

                    }

                  })

                })

              }

              /*

              {}

              */

              //如果control为true,则onSuccess将在发生按钮点击事件被回调

            },

            onFail: function (err) {}

          });

        })

      }

    }

    util.isAndroid = function () {

      let u = navigator.userAgent;

      let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端

      return isAndroid ? true : false

    }

    util.getUrlParameter = function getUrlParameter(sParam) {

      let sPageURL = decodeURIComponent(window.location.search.substring(1)),

        sURLVariables = sPageURL.split('&'),

        sParameterName,

        i;

      for (i = 0; i < sURLVariables.length; i++) {

        sParameterName = sURLVariables[i].split('=');

        if (sParameterName[0] === sParam) {

          return sParameterName[1] === undefined ? true : sParameterName[1];

        }

      }

    };

    export default util

    相关文章

      网友评论

          本文标题:工具类

          本文链接:https://www.haomeiwen.com/subject/uhulzftx.html