import moment from 'moment'
|
const utils = {
|
checkCanMark(timestamp) {
|
if (!!timestamp) {
|
return new Date(timestamp).getTime() - (new Date()).getTime() <= 300000
|
}
|
return false
|
},
|
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, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
|
// return match;
|
// });
|
// newContent = newContent.replace(/<br[^>]*\/>/gi, '');
|
// newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"');
|
let newContent = html.replace(/<video[^>]*>/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;
|
}).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, 'max-width:100%;width:100%;').replace(/width:[^;]+;/gi,
|
'max-width:100%;width:100%;');
|
return match;
|
});
|
newContent = newContent.replace(/<br[^>]*\/>/gi, '');
|
newContent = newContent.replace(/\<video/gi,
|
'<video style="max-width:100%;width:100%;height:auto;display:inline-block;margin:10rpx auto;"').replace(
|
/\<img/gi,
|
'<img style="max-width:100%;width:100%;height:auto;display:inline-block;margin:10rpx auto;"');
|
|
newContent = newContent.replaceAll('style="">', ' mode="widthFix"/>')
|
newContent = newContent.replaceAll('style=""/>', ' />')
|
// newContent = newContent.replace('style=""/>', ' />')
|
// newContent = newContent.replace('<img ', '<img ')
|
// newContent = newContent.replace(' mode="widthFix"/>')
|
|
// return '<image style="max-width:200rpx;min-height:200rpx; height:auto;display:inline-block;margin:10rpx auto;" src="https://xy-go.oss-cn-beijing.aliyuncs.com/80/80a854925d49458ca945c85d6b3c8e99WechatIMG5.jpg" alt="" data-href="" mode="widthFix"/>'
|
// return '<img style="max-width:100px;width:50px;height:auto;display:inline-block;margin:10rpx auto;" src="https://xy-go.oss-cn-beijing.aliyuncs.com/80/80a854925d49458ca945c85d6b3c8e99WechatIMG5.jpg" alt="" data-href="" mode="widthFix"/>'
|
// "<p style="text-align: center;"><img src="https://xy-go.oss-cn-beijing.aliyuncs.com/80/80a854925d49458ca945c85d6b3c8e99WechatIMG5.jpg" alt="" data-href="" style=""><img src="https://xy-go.oss-cn-beijing.aliyuncs.com/60/60bff4a8d1c644bbbbcb0d7bec864e03WechatIMG6.jpg" alt="" data-href="" style=""></p><p style="text-align: center;"><img src="https://xy-go.oss-cn-beijing.aliyuncs.com/79/7979091cabdf4c54ae5a7d08a18f0570WechatIMG34.jpg" alt="" data-href="" style=""><img src="https://xy-go.oss-cn-beijing.aliyuncs.com/bb/bb8b18e8d7444dc0b918b2129158c97bWechatIMG20.jpg" alt="" data-href="" style="width: 281.50px;height: 284.69px;"></p>"
|
// return '<img style="max-width:100%;width:100%;height:auto;display:inline-block;margin:10rpx auto;" src="https://xy-go.oss-cn-beijing.aliyuncs.com/80/80a854925d49458ca945c85d6b3c8e99WechatIMG5.jpg" alt="" data-href="" mode="widthFix"/>'
|
// <img style="max-width:600rpx;width:600rpx;height:auto;display:inline-block;margin:10rpx auto;" src="https://xy-go.oss-cn-beijing.aliyuncs.com/60/60bff4a8d1c644bbbbcb0d7bec864e03WechatIMG6.jpg" alt="" data-href="" ><p style="text-align: center;"><img style="max-width:600rpx;width:600rpx;height:auto;display:inline-block;margin:10rpx auto;" src="https://xy-go.oss-cn-beijing.aliyuncs.com/79/7979091cabdf4c54ae5a7d08a18f0570WechatIMG34.jpg" alt="" data-href="" style=""><img style="max-width:600rpx;width:600rpx;height:auto;display:inline-block;margin:10rpx auto;" src="https://xy-go.oss-cn-beijing.aliyuncs.com/bb/bb8b18e8d7444dc0b918b2129158c97bWechatIMG20.jpg" alt="" data-href="" ></p>
|
return newContent;
|
// return html
|
},
|
getMin(time) {
|
if (!time) {
|
return '-'
|
}
|
if (time < 0) {
|
time = -time
|
}
|
if (time < 60) {
|
var t1 = parseFloat(time).toFixed(0)
|
return '00:' + (t1 < 10 ? '0' + t1 : t1) + ''
|
}
|
var min = parseInt(time / 60)
|
var t = (time - min * 60).toFixed(0)
|
// if(min<60){
|
return (min < 10 ? ('0' + min) : min) + ': ' + (t < 10 ? ('0' + t) : t)
|
// }
|
// else{
|
// var h = parseInt(min /60)
|
// t = (min - h*60).toFixed(0)
|
// return ( h<10?('0'+ h): h) + '小时' + (t<10?('0'+t):t)+""
|
|
// }
|
},
|
toDateByTime(timestamp) {
|
if (!!timestamp) {
|
return moment(timestamp).format('YYYY-MM-DD HH:mm')
|
}
|
},
|
toDateTime(date) {
|
if (date) {
|
return moment(date).format('YYYY-MM-DD HH:mm:ss')
|
}
|
return ''
|
},
|
toDateTime33(date) {
|
if (date) {
|
return moment(date).format('DD日HH:mm:ss')
|
}
|
return ''
|
},
|
toDateMin(date) {
|
if (date) {
|
return moment(date).format('YYYY-MM-DD HH:mm')
|
}
|
return ''
|
},
|
toDateMinOnly(date) {
|
if (date) {
|
return moment(date).format('HH:mm')
|
}
|
return ''
|
},
|
toDateSecOnly(date) {
|
if (date) {
|
return moment(date).format('HH:mm:ss')
|
}
|
return ''
|
},
|
toDateDay(date) {
|
if (date) {
|
return moment(date).format('MM-DD HH:mm:ss')
|
}
|
return ''
|
},
|
toDateSec(date) {
|
if (date) {
|
return moment(date).format('YYYY-MM-DD HH:mm:ss')
|
}
|
return ''
|
},
|
toYear(date) {
|
if (date) {
|
return moment(date).format('YYYY')
|
}
|
return ''
|
},
|
toDate(date) {
|
if (date) {
|
return moment(date).format('YYYY-MM-DD')
|
}
|
return ''
|
},
|
toMonth(date) {
|
if (date) {
|
return moment(date).format('MM-DD')
|
}
|
return ''
|
},
|
formatDate(date, format) {
|
if (date) {
|
return moment(date).format(format)
|
}
|
return ''
|
},
|
isTrueEmpty(str) {
|
if (str || str === 0 || str === false) {
|
return false
|
}
|
return true
|
},
|
// 是否为数字
|
isNumber(str) {
|
if (!utils.isTrueEmpty(str)) {
|
return !isNaN(str)
|
}
|
return false
|
},
|
// 是否为正数
|
isPositive(str) {
|
if (utils.isNumber(str)) {
|
if (str > 0) {
|
return true
|
}
|
}
|
return false
|
},
|
// 是否为负数
|
isNegative(str) {
|
if (utils.isNumber(str)) {
|
if (str < 0) {
|
return true
|
}
|
}
|
return false
|
},
|
// 是否是整数
|
isInteger(str) {
|
if (utils.isNumber(str)) {
|
if (str % 1 === 0) {
|
return true
|
}
|
}
|
return false
|
},
|
// 打电话
|
callPhone(phone) {
|
uni.makePhoneCall({
|
phoneNumber: phone
|
});
|
},
|
// 保留小数位数
|
toFixed(value, num) {
|
value = value.toFixed(num)
|
value = Number(value).toString()
|
return value
|
},
|
// 是否是闰年
|
isLeapYear(year) {
|
year = parseInt(year, 10);
|
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
|
return true;
|
} else {
|
return false;
|
}
|
},
|
// 手机号格式是否正确
|
isMobilePhone(value) {
|
const reg = /^1\d{10}$/
|
if (reg.test(value)) {
|
return true
|
}
|
return false
|
},
|
isTelephone(value) {
|
const reg =
|
/^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$/
|
if (reg.test(value)) {
|
return true
|
}
|
return false
|
},
|
// 邮箱格式是否正确
|
isEmail(value) {
|
const reg = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,}$/
|
if (reg.test(value)) {
|
return true
|
}
|
return false
|
},
|
isIdCard: (value) => {
|
const reg =
|
/^[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{3}([0-9]|X)$/
|
if (reg.test(value)) {
|
return true
|
}
|
return false
|
},
|
hex2rgb(color) {
|
color = color.toLowerCase()
|
let array = [];
|
for (let i = 1; i < 7; i += 2) {
|
array.push(parseInt("0x" + color.slice(i, i + 2)));
|
}
|
return array
|
},
|
delay(time) {
|
return new Promise((resolve) => {
|
setTimeout(() => {
|
resolve()
|
}, time)
|
})
|
},
|
// 显示textarea的值
|
toTextareaText(str) {
|
if (str) {
|
return str.replace(/\n/g, '<br/>')
|
}
|
return ''
|
},
|
// 获取天数
|
getDay: (year, month) => {
|
year = parseInt(year, 10);
|
month = parseInt(month, 10);
|
|
let dayNum = 0;
|
switch (month) {
|
case 1:
|
case 3:
|
case 5:
|
case 7:
|
case 8:
|
case 10:
|
case 12:
|
dayNum = 31;
|
break;
|
case 4:
|
case 6:
|
case 9:
|
case 11:
|
dayNum = 30;
|
break;
|
case 2:
|
dayNum = utils.isLeapYear(year) ? 29 : 28;
|
break;
|
}
|
return dayNum;
|
},
|
/**
|
* @description 铺平树形结构为一维数组
|
* @function flatten
|
* @param {Object|Object[]} arr 树节点或树节点数组
|
* @param {string} childKey=children 树子节点数组属性
|
* @returns {Object[]}
|
*/
|
flatten: (arr, childKey = 'children') => {
|
const flattenArray = (arr, childKey) => {
|
if (Array.isArray(arr))
|
return Array.prototype.concat.apply(
|
[],
|
arr.map((i) => flattenArray(i, childKey))
|
)
|
else if (arr.hasOwnProperty(childKey))
|
return [arr, ...flattenArray(arr[childKey], childKey)]
|
return [arr]
|
}
|
return flattenArray(arr, childKey)
|
},
|
}
|
|
// const patternString = {
|
// mobilePhone: '^1\\d{10}$',
|
// telePhone: '^((\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1})|(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1}))$',
|
// phone: '(^1\\d{10}$)|(^((\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1})|(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1}))$)',
|
// email: '^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\\.[a-zA-Z0-9]{2,}$',
|
// idCard: '^[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{3}([0-9]|X)$',
|
// nonnegativeInteger: '^[0-9]+?$',
|
// isAmount: '^(([0-9]+)|([0-9]+\.[0-9]{0,2}))$',
|
// }
|
|
// Vue.prototype.$utils = utils
|
// Vue.prototype.$patternString = patternString
|
|
export default utils
|