function Fn() {}
Fn.prototype = {
  each: function(data, callback) {
    for (var x in data) {
      callback(x, data[x]);
    }
  },
  //配合picker使用
  getformatTime: function (code, is_now, date ) {
    if (is_now){var date = new Date();}
    if (date){
      var year = date.getFullYear()
      var month = date.getMonth() + 1
      var day = date.getDate()
      var hour = date.getHours()
      var minute = date.getMinutes()
      var second = date.getSeconds()
      switch (code) {
        case 'date':
          return [year, month, day].map(this.formatNumber).join('-');
          break;
        case 'time':
          return [hour, minute, second].map(this.formatNumber).join(':');
          break;
      }
    }
  },
  Countdown: function (endtime){
    if (parseInt(endtime)>0){
      var Countdowndata = new Object;
      var daysms = 24 * 60 * 60 * 1000;
      var hoursms = 60 * 60 * 1000;
      var Secondms = 60 * 1000;
      var microsecond = 1000;
      var time = new Date();
      var leftTime = Math.floor(endtime * 1000) - Math.floor(Date.parse(time));
      if (leftTime > 0) {
        var Hour = Math.floor(leftTime / daysms);
        leftTime -= Hour * daysms;
        var Minute = Math.floor(leftTime / hoursms);
        leftTime -= Minute * hoursms;
        var Second = Math.floor(leftTime / Secondms);
        leftTime -= Second * Secondms;
        var dSecs = Math.floor(leftTime / microsecond);
        Countdowndata.status = true;
        Countdowndata.Hour = this.formate(Hour);
        Countdowndata.Minute = this.formate(Minute);
        Countdowndata.Second = this.formate(Second);
        Countdowndata.dSecs = this.formate(dSecs);
      } else {
        Countdowndata.status = false;
      }
      return Countdowndata;
    }
  },
  formate: function (time) {
    if (time >= 10) {
      return time;
    } else {
      return "0" + time;
    }
  },
  formatTime: function(date) {
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()
    var hour = date.getHours()
    var minute = date.getMinutes()
    var second = date.getSeconds()
    return [year, month, day].map(this.formatNumber).join('/') + ' ' + [hour, minute, second].map(this.formatNumber).join(':')
  },
  inArray: function (search, array){
    for (var i in array) {
      if (array[i] == search) {
        return true;
      }
    }
    return false;
  },
  inArrayS: function (search, array) {
    for (var i in array) {
      if (array[i] == search) {
        return i;
      }
    }
    return -1;
  },
  isContained: function (a, b) {//是否被包含,是返回true,不是返回false
    if (!(a instanceof Array) || !(b instanceof Array)) return false;
    if (a.length < b.length) return false;
    var aStr = a.toString();
    for (var i = 0, len = b.length; i < len; i++) {
      if (aStr.indexOf(b[i]) == -1) return false;
    }
    return true;
  },
  formatNumber: function(n) {
    n = n.toString()
    return n[1] ? n : '0' + n
  },
  //格式化金额
  formatmoney: function (price, len){
    len = len > 0 && len <= 20 ? len : 2;
    if (price <= 0)
      price = 0;
    price = parseFloat((price + "").replace(/[^\d\.-]/g, "")).toFixed(len) + "";
    var l = price.split(".")[0].split("").reverse(), r = price.split(".")[1];
    var t = "";
    for (var i = 0; i < l.length; i++) {
      t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
    }
    var re = t.split("").reverse().join("") + "." + r;
    return re.replace("-,", "-");
  },
  //将对象转化为&链接的字符串
  ObjectToStr: function (obj) {
    if ( typeof (obj) != 'object' || !obj)
      return true;
    var newstr = '';
    for (var i in obj) {
      newstr += '&' + i + '=' + obj[i];
    }
    return newstr;
  },
  //将&链接的字符串转化为对象
  StrToObject: function (str) {
    if ( typeof (str) != 'string')
      return true;
    var s = str.split("&");
    var newaction = new Object;
    for (var i = 0; i < s.length; i++) {
      var d = s[i].split("=");
      if (d[1])
        newaction[d[0]] = d[1];
    }
    return newaction;
  },
  //判断对象是否为空
  isEmptyObject: function (obj) {
    for (var key in obj) {
      return false;//返回false，不为空对象
    　　}
    　　return true;//返回true，为空对象
  },
  //Hanchao：实例变量   data：ajax请求数据  带分页带参数
  //固定数据源 为list 参数 param{page:1,...}
  init_page_list :function(HC, list) {
    var len = 0;
    if (Object.prototype.toString.call(list) === '[object Array]') {
      len = list.length;
    } else {
      var arr = Object.keys(list);
      len = arr.length;
    }
    if (HC.data.param.page == 1) {
      HC.setData({
        list: []
      })
    } 
    var initlist = HC.data.list;
    for (var i = 0; i < len; i++) {
      if (list[i]) {
        initlist.push(list[i]);
      }
    }
    HC.setData({
      list: initlist
    })
  },
  init_page_empty_list:function (HC, msg) {
    if (HC.data.param.page == 1){
      HC.setData({
        list: []
      })
      HC.setData({
        hasMore: false,
        dataNull: false
      });
    }else{
      if (HC.data.list!=''){
        HC.setData({
          hasMore: true,
          dataNull: true
        });
      }
    }
  },
  init_vue_append_list: function (HC, list){
    var initlist = HC.data.list;
    for (var i in list) {
      if (list[i]) {
        initlist.push(list[i]);
      }
    }
    HC.setData({
      list: initlist
    })
  },
  /*获取当前页url*/
  getCurrentPageUrl:function (){
    var pages = getCurrentPages()    //获取加载的页面
    var currentPage = pages[pages.length - 1]    //获取当前页面的对象
    var url = currentPage.route    //当前页面url
    return url
  },
  /*获取当前页带参数的url*/
  getCurrentPageUrlWithArgs:function() {
    var pages = getCurrentPages()    //获取加载的页面
    var currentPage = pages[pages.length - 1]    //获取当前页面的对象
    var url = currentPage.route    //当前页面url
    var options = currentPage.options    //如果要获取url中所带的参数可以查看options
    //拼接url的参数
    var urlWithArgs = url + '?'
    for (var key in options) {
      var value = options[key]
      urlWithArgs += key + '=' + value + '&'
    }
    urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1)
    return urlWithArgs
  },
  isEmail: function (strEmail){
      if (strEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1){
        return true;
      }else{
        return false;
      }
  },
  /*打开地图所需参数*/
  getopenmapinfo: function (lat,lon) {
    if (!lat) lat = 23.099994;
    if (!lon) lon = 113.324520;
    return {
      markers: [{
        iconPath: "/images/red.png",
        id: 0,
        latitude: lat,
        longitude: lon,
        width: 40,
        height: 20,
      }],
      param:{
        latitude: lat,
        longitude: lon,
        scale: 14,
        style: "width: 100%; height: 200px;"
      }
    }
  },
  /*判断是否为空*/
  is_empty: function (obj) {
    if (!obj || obj == '') return true;
    if (typeof obj === "string") {
      obj = obj.replace(/\s*/g, "");//移除字符串中所有 ''
      if (obj === '') return true;
    }
    return (Array.isArray(obj) && obj.length === 0) || (Object.prototype.isPrototypeOf(obj) && Object.keys(obj).length === 0);
  }
}
var fn = new Fn();
module.exports = fn