import moment from 'moment'; const app = getApp(); const 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(':')}` } const getWeekByDate = dates => { let show_day = new Array('周日', '周一', '周二', '周三', '周四', '周五', '周六'); let date = new Date(dates); date.setDate(date.getDate()); let day = date.getDay(); return show_day[day]; } const formatNumber = n => { n = n.toString() return n[1] ? n : `0${n}` } function formatDate(date) { var year = date.getFullYear(); var month = date.getMonth() + 1; var day = date.getDate(); if (month < 10) month = "0" + month; if (day < 10) day = "0" + day; return [year, month, day].map(formatNumber).join('-'); } //时间戳转日期,falg:true表示只要年月日,part: year month date function toDate(number, flag, part) { var n = number; var date = new Date(parseInt(n) * 1000); var y = date.getFullYear(); var m = date.getMonth() + 1; m = m < 10 ? ('0' + m) : m; var d = date.getDate(); d = d < 10 ? ('0' + d) : d; var h = date.getHours(); h = h < 10 ? ('0' + h) : h; var minute = date.getMinutes(); var second = date.getSeconds(); minute = minute < 10 ? ('0' + minute) : minute; second = second < 10 ? ('0' + second) : second; if (flag) { if (part == "year") { return y; } else if (part == "month") { return m; } else if (part == "date") { return n; } return y + '-' + m + '-' + d; } return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second; } //当前日期加减天数,falg:true表示只要年月日 function mathChangeDate(date, method, days, flag) { //method:'+' || '-' //ios不解析带'-'的日期格式,要转成'/',不然Nan,切记 var dateVal = date.replace(/-/g, '/'); var timestamp = Date.parse(dateVal); if (method == '+') { timestamp = timestamp / 1000 + 24 * 60 * 60 * days; } else if (method == '-') { timestamp = timestamp / 1000 - 24 * 60 * 60 * days; } return toDate(timestamp, flag); } /** * 封封微信的的request */ function request(url, data = {}, method = "GET") { var url=url; // console.log(app.globalData); let isUat=wx.getStorageSync('isUat'); if(!isUat){ // url=url.replace('uat.',''); } return new Promise(function (resolve, reject) { wx.request({ url: url, data: data, method: method, header: { 'Content-Type': 'application/json', 'token': wx.getStorageSync('token') }, success: function (res) { if (res.statusCode == 200) { if (res.data.errno == 501||res.data.errno == 401) { wx.hideLoading(); // 清除登录相关内容 try { wx.removeStorageSync('userInfo'); wx.removeStorageSync('token'); } catch (e) { // Do something when catch error } // 切换到登录页面 // wx.redirectTo({ // url: '/pages/temp/login/login' // }); wx.redirectTo({ url: '/pages/index/index' }); } else { resolve(res.data); } } else { reject(res.errMsg); } }, fail: function (err) { reject(err) } }) }); } function showErrorToast(msg) { wx.showToast({ title: msg, image: '/static/images/icon_error.png' }) } function formatweek(date) { let week = date.getDay(); let weekArr = ''; switch (week) { case 1: weekArr = '周一'; break; case 2: weekArr = '周二'; break; case 3: weekArr = '周三'; break; case 4: weekArr = '周四'; break; case 5: weekArr = '周五'; break; case 6: weekArr = '周六'; break; case 0: weekArr = '周日'; break; } return weekArr; } function getDateList(date, interval, length) { let dateList = []; let len = (length ? length : 7); for (let i = 0; i < len; i++) { let datestamp = new Date(date.getTime() + i * 60 * 60 * 1000 * 24 * interval); let dateItem = { week: formatweek(datestamp), date: formetdate(datestamp), id: new Date(datestamp).getDay() }; dateList.push(dateItem); } return dateList; } function dateDifference(end) { // //比较2个日期 // console.log(start + ",," + end); // var stime = new Date(start).getTime(); var stime = new Date().getTime(); console.log(end.replace(/-/g, "/")); var etime = new Date(end.replace(/-/g, "/")).getTime(); var usedTime = etime - stime; //两个时间戳相差的毫秒数 var days = Math.floor(usedTime / (24 * 3600 * 1000)); //计算出小时数 var leave1 = usedTime % (24 * 3600 * 1000); //计算天数后剩余的毫秒数 var hours = Math.floor(leave1 / (3600 * 1000)); //计算相差分钟数 var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数 var minutes = Math.floor(leave2 / (60 * 1000)); if (days == 0 && hours == 0) { return minutes + "分钟"; } if (days == 0) { return hours + "时" + minutes + "分钟"; } var time = days + "天" + hours + "时" + minutes + "分钟"; // var time = days; return time; } function formetdate(date) { var year = date.getFullYear(); var month = date.getMonth() + 1; var day = date.getDate(); return [year, month, day].map(formatNumber).join('-'); } function getYearMonth(str,method){ let arr = str.split('-'); let year = arr[0]; //获取当前日期的年份 let month = arr[1]; //获取当前日期的月份 let total=0; if (method == '+') { total=Number(year*12+Number(month)+1).toFixed(0); } else if (method == '-') { total=Number(year*12+Number(month)-1).toFixed(0); } let year2=parseInt(total/12); let month2=total%12; if(month2==0){ year2=parseInt(total/12)-1; month2=12; } if (month2 < 10) { month2 = '0' + month2; } let str2 = year2 + '-' + month2 ; return str2; } function getWeek(day) { let week=moment(day).format("W"); return week; } function validIdCard(value) { //15位和18位身份证号码的正则表达式 var regIdCard = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/; //如果通过该验证,说明身份证格式正确,但准确性还需计算 if (regIdCard.test(value)) { if (value.length == 18) { var idCardWi = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); //将前17位加权因子保存在数组里 var idCardY = new Array(1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2); //这是除以11后,可能产生的11位余数、验证码,也保存成数组 var idCardWiSum = 0; //用来保存前17位各自乖以加权因子后的总和 for (var i = 0; i < 17; i++) { idCardWiSum += value.substring(i, i + 1) * idCardWi[i]; } var idCardMod = idCardWiSum % 11;//计算出校验码所在数组的位置 var idCardLast = value.substring(17);//得到最后一位身份证号码 //如果等于2,则说明校验码是10,身份证号码最后一位应该是X if (idCardMod == 2) { if (idCardLast == "X" || idCardLast == "x") { return true; } else { // callback(new Error("身份证格式不正确!")); return false; } } else { //用计算出的验证码与最后一位身份证号码匹配,如果一致,说明通过,否则是无效的身份证号码 if (idCardLast == idCardY[idCardMod]) { return true; } else { // return false; } } } } else { //alert("身份证格式不正确!"); return false; } } module.exports = { formatTime, formatDate, request, mathChangeDate, showErrorToast, getWeekByDate, formatweek, getDateList, dateDifference, formetdate, toDate, getYearMonth, getWeek, validIdCard }