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

微信小程序工具类

浏览量:397 时间: 来源:苏__
// 请求根地址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}

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

热门模板

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎