const objectUtil = require('../../utils/object'); const toolUtil = require('../../utils/tool');

//模态框
function showModal(title, content, confirm, cancel) {
  wx.showModal({
      title: title,
      content: content,
      showCancel: true,
      success(res) {
          if (res.confirm) {
              confirm(confirm)
          } else if (res.cancel) {
              if (cancel) {
                  cancel(cancel)
              }
          }
      },
  })
}
//提示框
function showToast(title, icon, duration, success) {
  wx.showToast({
      title: title,
      icon: icon,
      duration: duration ? duration : 2000,
      mask: true,
      success: function (res) {
          if (success) {
              success(res)
          }
      },
      fail: function () { },
  })
}
//加载框
function showLoading(title) {
  if (title) {
      wx.showLoading({
          title: title,
          mask: true,
          success: () => { },
          fail: () => {
              wx.hideLoading()
          }
      })
  } else {
      wx.showLoading({
          title: '加载中...',
          mask: true,
          success: () => { },
          fail: () => {
              wx.hideLoading()
          }
      })
  }
}
//手机号脱敏
function formatterPhone(value) {
  var len = value.length;
  var xx = value.substring(3, len - 4);
  var values = value.replace(xx, "****");
  return values;
}
//身份证脱敏
function formatterIDCard(value) {
  var len = value.length;
  var xx = value.substring(5, len - 4);
  var values = value.replace(xx, "**********");
  return values;
}
//节流
function throttle(fn, interval) {
  var enterTime = 0; //触发的时间
  var gapTime = interval || 2000; //间隔时间,如果interval不传,则默认2000ms
  return function () {
      var context = this;
      var backTime = new Date(); //第一次函数return即触发的时间
      if (backTime - enterTime > gapTime) {
          fn.call(context, arguments);
          enterTime = backTime; //赋值给第一次触发的时间,这样就保存了第二次触发的时间
      }
  };
}
//防抖
function debounce(fn, interval) {
  var timer;
  var gapTime = interval || 2000; //间隔时间,如果interval不传,则默认2000ms
  return function () {
      clearTimeout(timer);
      var context = this;
      var args = arguments; //保存此处的arguments,因为setTimeout是全局的,arguments不是防抖函数需要的。
      timer = setTimeout(function () {
          fn.call(context, args);
      }, gapTime);
  };
}
//格式化时间 返回2024/03/02 14:23:32这种格式
function 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(':')}`
}
function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : `0${n}`
}
//字符串转数字,默认两位
function string2Number(str) {
  return Number.parseInt(str).toFixed(2);
}
//请求封装
function requestUrl(url, data, method) {
  let promise = new Promise((resolve, reject) => {
      wx.request({
          url: url,
          data: data,
          method: method,
          header: {
              'token': wx.getStorageSync('token')
          },
          success: res => {
              resolve(res.data);
          },
          fail: res => {
              reject(res.data);
          },
      })
  })
  return promise
}
module.exports = {
  throttle,
  debounce: debounce,
  showModal,
  showToast,
  showLoading,
  requestUrl,
  string2Number,
  formatTime,
  formatterPhone,
  formatterIDCard
};

  

// 非对象模式的判断数据为不为空
export const isDataNotEmpty = function(checkObj) {
    console.log(JSON.stringify(checkObj))
    if (checkObj === 0) {
        return true
    }
    // Checke if it is ""、 undefined、 null 、NaN、 []
    if (checkObj) {
        return true
    }
    return false
}
const objectUtil = require('../../utils/object');
const toolUtil = require('../../utils/tool');

 

posted on 2024-08-05 11:45  海阔天空123  阅读(7)  评论(0编辑  收藏  举报

导航