一、两种时间格式校验
1.精确到年、月、日、时、分、秒
function formatTime(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(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':') }
2.用“-”连接年月日
function formatTime2(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(formatNumber).join('-') } /*小于两位数的处理*/ function formatNumber(n) { n = n.toString() return n[1] ? n : '0' + n }
二、加载显示数据之前 “请稍后”等待提示和关闭提示
function showLoading(){ console.log("showloading-----"); wx.showToast({ title: '请稍后', icon: 'loading', duration: 5000 }); }
function hideLoading(){
wx.hideToast();
}
三、取消、确认操作以及网络异常提示框
// 取消确认提示框 function showModal(text){ wx.showModal({ title: '提示', content: text, showCancel:false, success: function(res) { if (res.confirm) { } } }) } // 网络异常提示框 function showFailModal(){ wx.showModal({ title: '提示', content: '网络异常,请检查网络', success: function(res) { if (res.confirm) { console.log('用户点击确定') } } }) }
四、根据id去本地缓存中的products中匹配到p_title
function getProductNameById(id){ var products = wx.getStorageSync('products'); /**本地缓存中的products对象**/ for(var i=0;i<products.length;i++){ var product=products[i]; if(product.p_id==id){ return product.p_title; } } }
五、根据id去本地缓存中的products中匹配product
function getProductById(id){ var products = wx.getStorageSync('products'); /**本地缓存中的products对象**/ for(var i=0;i<products.length;i++){ var product=products[i]; if(product.p_id==id){ return product; } } }
function su(){ } function com(){ }
六、生成唯一标识符方法(登录时先在本地缓存中获取,若没有,则按照方法去生成一个全局唯一标识符)
function uuid(len, radix) { var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split(''); var uuid = [], i; radix = radix || chars.length; if (len) { for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix]; } else { var r; uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'; uuid[14] = '4'; // Fill in random data. At i==19 set the high bits of clock sequence as for (i = 0; i < 36; i++) { if (!uuid[i]) { r = 0 | Math.random()*16; uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r]; } } } return uuid.join(''); }
总结:将上面的方法都放在util.js中,然后在其中最后写一个暴露接口的方法,方便其他地方调用,如下
module.exports = {
formatTime: formatTime,
formatTime2: formatTime2,
showLoading:showLoading,
hideLoading:hideLoading,
showFailModal:showFailModal,
getProductNameById:getProductNameById,
getProductById:getProductById,
showModal:showModal,
uuid:uuid,
su:su,
com:com
}