微信小程序-----工具类

// 请求根地址
const rootDocment = 'https://';
// post请求
post: (url, data, cb) => {
  wx.request({
    url: rootDocment + url,
    data: data,
    method: 'post',
    header: { 'Content-Type': 'application/x-www-form-urlencoded' },
    complete: (res) => {
      if (res.statusCode == 200) {
        return typeof cb == "function" && cb(res.data)
      } else {
        wx.showToast({
          title: '请求错误,错误码:' + res.statusCode,
          icon: 'none'
        })
      }
    }
  })
}
// get请求
get: (url, data, cb) => {
  wx.request({
    url: rootDocment + url,
    data: data,
    method: 'get',
    complete: (res) => {
      if (res.statusCode == 200) {
        return typeof cb == "function" && cb(res.data)
      } else {
        wx.showToast({
          title: '请求错误,错误码:' + res.statusCode,
          icon: 'none'
        })
      }
    }
  })
}
// 成功信息
successMsg: (msg, duration, image) => {
  msg = msg || 'success!';
  duration = duration || 1000;
  image = image || '';
  wx.showToast({
    title: msg,
    icon: 'success',
    duration: duration,
    image: image
  })
}
// 失败消息
errorMsg: (msg, duration, image) => {
  msg = msg || 'error!';
  duration = duration || 1000;
  image = image || '';
  wx.showToast({
    title: msg,
    icon: 'none',
    duration: duration,
    image: image
  })
}
/**
 * 时间戳或时间 格式化
 * 两个参数:
 * 第一个参数:可选,时间或时间戳,默认为当前时间。第二个参数:可选,转换的格式,默认为YYYY-MM-DD hh:mm:ss。
 * 不补0:YYYY-M-D h:m:s 2018-1-1 0:0:0
 * 加入星期:YYYY-MM-DD www hh:mm:ss 2018-01-01 星期一 00:00:00
 * 加入周:YYYY-MM-DD ww hh:mm:ss 2018-01-01 周一 00:00:00
 */
formatTime: (date, fmt) => {
  date = date || new Date();
  date = ((date instanceof Date) || (typeof date) == 'number') ? new Date(date) : new Date();
  fmt = fmt || 'YYYY-MM-DD hh:mm:ss';
  var obj = {
    'Y': date.getFullYear(),
    'M': date.getMonth() + 1,
    'D': date.getDate(),
    'w': date.getDay(),
    'h': date.getHours(),
    'm': date.getMinutes(),
    's': date.getSeconds(),
  }, week = ['日', '一', '二', '三', '四', '五', '六'];
  for (var i in obj) {
    fmt = fmt.replace(new RegExp(i + '+', 'g'), function (e) {
      var itemStr = obj[i] + '';
      if (i == 'w') return (e.length > 2 ? '星期' : '周') + week[itemStr];
      for (var j = 0, len = itemStr.length; j < e.length - len; j++) itemStr = '0' + itemStr;
      return itemStr;
    });
  }
  return fmt;
}
/**
 * 时间戳转换为时分秒
 * 两个参数:
 * 第一个参数:可选,秒数,默认为0。第二个参数:可选,转换的格式,默认为hh: mm: ss。
 * 不补0 h: m: s 0:0:0
 * 加入天:DD天hh: mm: ss 0606:06:06
 */
formatTimestamp: (date, fmt) => {
  date = date / 1000 || 0;
  fmt = fmt || 'hh:mm:ss';
  var obj;
  function setObj(h, m) {
    obj = {
      'D': parseInt(date / 60 / 60 / 24),
      'h': h == 1 ? parseInt(date / 60 / 60) % 24 : parseInt(date / 60 / 60),
      'm': m == 1 ? parseInt(date / 60) % 60 : parseInt(date / 60),
      's': parseInt(date) % 60,
    }
  }
  fmt.indexOf('h') == -1 ? setObj(0, 0) : (fmt.indexOf('D') == -1 ? setObj(0, 1) : setObj(1, 1));
  for (var i in obj) {
    fmt = fmt.replace(new RegExp(i + '+', 'g'), function (e) {
      var itemStr = obj[i] + '';
      for (var j = 0, len = itemStr.length; j < e.length - len; j++) itemStr = '0' + itemStr;
      return itemStr;
    });
  }
  return fmt;
}
module.exports = {
  post: post,
  get: get,
  successMsg: successMSg,
  errorMsg: errorMsg,
  formatTime: formatTime,
  formatTimestamp: formatTimestamp
}
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值