123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- const formatNumber = n => {
- n = n.toString()
- return n[1] ? n : '0' + n
- }
- const formatTime = (date, word = false, datestring = '/', timestring = ":") => {
- if(date.constructor==String){
- var date = new Date(date.replace(/-/g, '/'));
- }
- 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()
- if (word == false) {
- return [year, month, day].map(formatNumber).join(datestring) + ' ' + [hour, minute, second].map(formatNumber).join(
- timestring)
- }
- return [year].map(formatNumber) + '年' + [month].map(formatNumber) + '月' + [day].map(formatNumber) + '日' + ' ' + [
- hour
- ].map(formatNumber) + ':' + [minute].map(formatNumber) + ':' + [second].map(formatNumber)
- }
- const formatSpotDayHourMinute = (date, word = false, datestring = '/', timestring = ":") => {
- if(date.constructor==String){
- var date = new Date(date.replace(/-/g, '/'));
- }
- const year = date.getFullYear()
- const month = date.getMonth() + 1
- const day = date.getDate()
- const hour = date.getHours()
- const minute = date.getMinutes()
- if (word == false) {
- return [year, month, day].map(formatNumber).join(datestring) + ' ' + [hour, minute].map(formatNumber).join(
- timestring)
- }
- return [year].map(formatNumber) + '年' + [month].map(formatNumber) + '月' + [day].map(formatNumber) + '日' + ' ' + [
- hour
- ].map(formatNumber) + ':' + [minute].map(formatNumber)
- }
- const formatTimeDay = (date, word = false, datestring = '/') => {
- if(date.constructor==String){
- var date = new Date(date.replace(/-/g, '/'));
- }
- const year = date.getFullYear()
- const month = date.getMonth() + 1
- const day = date.getDate()
- if (word == false) {
- return [year, month, day].map(formatNumber).join(datestring)
- }
- return [year].map(formatNumber) + '年' + [month].map(formatNumber) + '月' + [day].map(formatNumber) + '日'
- }
- const formatWordsYM = (date, word = false, datestring = '/') => {
- if(date.constructor==String){
- var date = new Date(date.replace(/-/g, '/'));
- }
- const year = date.getFullYear()
- const month = date.getMonth() + 1
- if (word == false) {
- return [year, month].map(formatNumber).join(datestring)
- }
- return [year].map(formatNumber) + '年' + [month].map(formatNumber) + '月'
- }
- const formatWordsYD = (date, word = false, datestring = '/') => {
- if(date.constructor==String){
- var date = new Date(date.replace(/-/g, '/'));
- }
- const month = date.getMonth() + 1
- const day = date.getDate()
- if (word == false) {
- return [month, day].map(formatNumber).join(datestring)
- }
- return [month].map(formatNumber) + '月' + [day].map(formatNumber) + '日'
- }
- const formatTimeHourMonth = (date, word = false, timestring = ":") => {
- if(date.constructor==String){
- var date = new Date(date.replace(/-/g, '/'));
- }
- const hour = date.getHours()
- const minute = date.getMinutes()
- if (word == false) {
- return [hour, minute].map(formatNumber).join(timestring)
- }
- return [hour].map(formatNumber) + '时' + [minute].map(formatNumber) + '分'
- }
- function getfullDateStr(dayCount = 0,word=false,datestring="-") {
- var dd = new Date();
- dd.setDate(dd.getDate() + dayCount);
- const year = dd.getFullYear()
- const month = dd.getMonth() + 1
- const day = dd.getDate()
- if(!word){
- return [year, month, day].map(formatNumber).join(datestring)
- }
- return [year].map(formatNumber) + '年' + [month].map(formatNumber) + '月' + [day].map(formatNumber) + '日'
- }
- function daysBetween(sDate1, sDate2) {
-
- var time1 = (new Date(sDate1)).getTime();
- var time2 = (new Date(sDate2)).getTime();
- var nDays = parseInt((time2 - time1) / 1000 / 3600 / 24);
- return nDays;
- };
- function getDateStr(dayCount = 0) {
- var dd = new Date();
- dd.setDate(dd.getDate() + dayCount);
- var m = dd.getMonth() + 1;
- var d = dd.getDate();
- return `${m}月${d<10?'0'+d:d}日`
- }
- const formatTimeHours = (timespan,word=false) => {
- if(timespan.constructor==String){
- var dateTimeStamp = new Date(timespan.replace(/-/g, '/'));
- }else{
- var dateTimeStamp = timespan
- }
- var minute = 1000 * 60;
- var hour = minute * 60;
- var day = hour * 24;
- var result = '';
- var now = new Date().getTime();
- var diffValue = now - dateTimeStamp;
- if (diffValue < 0) {
- console.log("时间不对劲,服务器创建时间与当前时间不同步");
- return result = "刚刚";
- }
- var dayC = diffValue / day;
- var hourC = diffValue / hour;
- var minC = diffValue / minute;
- if (parseInt(dayC) > 7) {
- if(word==false){
- result = "" + formatSpotDayHourMinute(dateTimeStamp,false,'-',':')
- }else{
- result = "" + formatSpotDayHourMinute(dateTimeStamp,true)
- }
- } else if (parseInt(dayC) > 1) {
- result = "" + parseInt(dayC) + "天前" + ' ' + formatTimeHourMonth(dateTimeStamp,word);
- } else if (parseInt(dayC) == 1) {
- result = "昨天" + ' ' + formatTimeHourMonth(dateTimeStamp,word);
- } else if (hourC >= 1) {
- result = "" + parseInt(hourC) + "小时前";
- } else if (minC >= 5) {
- result = "" + parseInt(minC) + "分钟前";
- } else {
- result = "刚刚";
- }
- return result;
- };
- function getWeekDateStr(dayCount = 0) {
-
- var weekStr = "周" + "日一二三四五六".charAt((new Date().getDay() + dayCount) % 7);
- return weekStr;
- }
- function isPhone(val) {
- const regexp = /^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/;
- return regexp.test(val)
-
-
-
- }
- function isIDcard(value) {
- if (!code || !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(value)) {
- return true
- }
- }
- function formatRichText(html) {
- let newContent = html.replace(/<img[^>]*>/gi, function (match, capture) {
- match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
- match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
- match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
- return match;
- });
- newContent = newContent.replace(/style="[^"]+"/gi, function (match, capture) {
- match = match.replace(/width:[^;]+;/gi, 'width:100%;').replace(/width:[^;]+;/gi, 'width:100%;');
- return match;
- });
- newContent = newContent.replace(/<br[^>]*\/>/gi, '');
- newContent = newContent.replace(/\<img/gi, '<img style="width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"');
- return newContent;
- };
- function toImageBase64(url) {
- return 'data:image/jpg;base64,' + wx.getFileSystemManager().readFileSync(url, "base64")
- }
- function RandomNumBoth(Min, Max) {
- var Range = Max - Min;
- var Rand = Math.random();
- var num = Min + Math.round(Rand * Range);
- return num;
- }
- function randomName(len) {
- len = len || 23;
- var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
- var maxPos = chars.length;
- var str = '';
- for (var i = 0; i < len; i++) {
- str += chars.charAt(Math.floor(Math.random() * maxPos));
- }
- return new Date().getTime() + str;
- }
- function buildColor() {
- var color = "#" + Math.floor(Math.random() * 0xFFFFFF).toString(16).padStart(6, "0");
- return color;
- }
- function path(val){
- return '/static/images/'+val
- }
- function formatPhone(val) {
- if(val.length==11){
- const matches = /^(\d{3})(\d{4})(\d{4})$/.exec(val)
- if (matches) {
- return matches[1] + '-' + matches[2] + '-' + matches[3]
- }
- }else{
- return val
- }
-
- }
- const testPwd = (val) => {
- const regexp = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,}$/;
- return regexp.test(val)
- }
- module.exports = {
- formatWordsYM: formatWordsYM,
- formatTime: formatTime,
- formatTimeDay: formatTimeDay,
- formatSpotDayHourMinute: formatSpotDayHourMinute,
- formatWordsYD: formatWordsYD,
- formatTimeHourMonth: formatTimeHourMonth,
- formatTimeHours:formatTimeHours,
- daysBetween: daysBetween,
- isPhone: isPhone,
- isIDcard: isIDcard,
- formatRichText,
- formatNumber: formatNumber,
- toImageBase64: toImageBase64,
- getDateStr: getDateStr,
- getWeekDateStr: getWeekDateStr,
- getfullDateStr: getfullDateStr,
- RandomNumBoth: RandomNumBoth,
- randomName: randomName,
- buildColor:buildColor,
- path:path,
- formatPhone,
- testPwd
- }
|