微信小程序工具类
// 请求根地址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 06天06: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}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。

