first commit
This commit is contained in:
189
common/utils.js
Normal file
189
common/utils.js
Normal file
@@ -0,0 +1,189 @@
|
||||
import httpConfig from "@/common/http/config"
|
||||
export default {
|
||||
//时间格式化
|
||||
formatDate (formats, timestamp) {
|
||||
var formats = formats || 'Y-M-D';
|
||||
var myDate = timestamp ? new Date(timestamp) : new Date();
|
||||
var year = myDate.getFullYear();
|
||||
var month = formatDigit(myDate.getMonth() + 1);
|
||||
var day = formatDigit(myDate.getDate());
|
||||
var hour = formatDigit(myDate.getHours());
|
||||
var minute = formatDigit(myDate.getMinutes());
|
||||
var second = formatDigit(myDate.getSeconds());
|
||||
return formats.replace(/Y|M|D|h|m|s/g, function(matches) {
|
||||
return ({
|
||||
Y: year,
|
||||
M: month,
|
||||
D: day,
|
||||
h: hour,
|
||||
m: minute,
|
||||
s: second
|
||||
})[matches];
|
||||
});
|
||||
// 小于10补0
|
||||
function formatDigit (n) {
|
||||
return n.toString().replace(/^(\d)$/, '0$1');
|
||||
}
|
||||
},
|
||||
// 正则验证
|
||||
checkStr(str,type) {
|
||||
switch (type){
|
||||
case 'mobile': //电话
|
||||
return /^1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(str)
|
||||
case 'card': //身份证
|
||||
return /^[\d]{6}(19|20)?[\d]{2}(0[1-9]|1[0-2])(0[1-9]|1[0-9]|2[0-9]|3[0-1])[0-9]{3}[xX\d]$/.test(str)
|
||||
case 'email': //邮箱
|
||||
return /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/.test(str)
|
||||
case 'qq': //qq
|
||||
return /^[1-9][0-9]{4,10}$/.test(str)
|
||||
case 'postcode': //邮编
|
||||
return /^(0[1-7]|1[0-356]|2[0-7]|3[0-6]|4[0-7]|5[1-7]|6[1-7]|7[0-5]|8[013-6])\d{4}$/.test(str)
|
||||
case 'int': //整数
|
||||
return /^-?\d+$/.test(str)
|
||||
case 'posint': //正整数
|
||||
return /^\d+$/.test(str)
|
||||
case 'negint': //负整数
|
||||
return /^-\d+$/.test(str)
|
||||
case 'num': //数字
|
||||
return /^-?\d*\.?\d+$/.test(str)
|
||||
case 'posnum': //正数
|
||||
return /^\d*\.?\d+$/.test(str)
|
||||
case 'negnum': //负数
|
||||
return /^-\d*\.?\d+$/.test(str)
|
||||
case 'nposint': //非0正整数
|
||||
return /^\+?[1-9][0-9]*$/.test(str)
|
||||
case 'nnegint': //非0负整数
|
||||
return /^\-?[1-9][0-9]*$/.test(str)
|
||||
case 'price': //价格
|
||||
return /(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/.test(str)
|
||||
case 'money': //金额(小数点2位)
|
||||
return /^\d*(?:\.\d{0,2})?$/.test(str);
|
||||
default:
|
||||
return true
|
||||
}
|
||||
},
|
||||
// 当前日期加固定天数
|
||||
getNumDay (num) {
|
||||
const currentDate = new Date();
|
||||
currentDate.setDate(currentDate.getDate() + num);
|
||||
return currentDate.toISOString();
|
||||
},
|
||||
// 判断是否再有效期
|
||||
isDateValid (startDate, endDate, date) {
|
||||
var currentDate = new Date();
|
||||
if (date) currentDate = new Date(date);
|
||||
return currentDate > new Date(startDate + ' 00:00:00') && currentDate < new Date(endDate + ' 23:59:59');
|
||||
},
|
||||
// 判断指定日期是否小于今天
|
||||
isDateLessThanToday (date, nowDate) {
|
||||
var currentDate = new Date();
|
||||
if (nowDate) currentDate = new Date(nowDate);
|
||||
return currentDate < new Date(date + ' 23:59:59');
|
||||
},
|
||||
// 获取指定日期为周几
|
||||
getDayOfWeek (dateString) {
|
||||
const days = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
|
||||
const date = new Date(dateString);
|
||||
return days[date.getDay()];
|
||||
},
|
||||
// 获取指定日期的三天
|
||||
getDateList (dateStr) {
|
||||
const date = new Date(dateStr);
|
||||
let nextThreeDays = [];
|
||||
for (let i = 0; i < 3; i++) {
|
||||
const day = new Date(date);
|
||||
day.setDate(day.getDate() + i);
|
||||
nextThreeDays.push(day.toISOString().split('T')[0]);
|
||||
}
|
||||
return nextThreeDays;
|
||||
},
|
||||
// 获取指定日期的上一天几下一天
|
||||
getNewDate (str, isPurchaseDate, beforeDay) {
|
||||
const date = new Date(str);
|
||||
let day = '';
|
||||
if (isPurchaseDate == '1') {
|
||||
let now = new Date();
|
||||
day = new Date(now.setDate(now.getDate() + beforeDay))
|
||||
} else day = new Date();
|
||||
if (date.toISOString().split('T')[0] == day.toISOString().split('T')[0]) {// 选择的当天
|
||||
return this.getDateList(date)
|
||||
} else {// 选择的之后日期
|
||||
let arr = [];
|
||||
const res = new Date(date);
|
||||
let prev = new Date(res.setDate(res.getDate() - 1));
|
||||
let next = new Date(res.setDate(res.getDate() + 2));
|
||||
arr = [prev.toISOString().split('T')[0], date.toISOString().split('T')[0], next.toISOString().split('T')[0]]
|
||||
return arr;
|
||||
}
|
||||
},
|
||||
// 截取年月日
|
||||
splitDate (str) {
|
||||
let newstr = str.substr(0, 10);
|
||||
let arr = newstr.split('-');
|
||||
return arr[0] + '年' + arr[1] + '月' + arr[2] + '日 ' + str.substr(11, str.length - 1);
|
||||
},
|
||||
// 截取年月
|
||||
splitDates (str) {
|
||||
let newstr = str.substr(0, 10);
|
||||
let arr = newstr.split('-');
|
||||
return arr[0] + '年' + arr[1] + '月';
|
||||
},
|
||||
// 数组分类
|
||||
resetArr (arr, key) {
|
||||
let tempArr = [];// 分类属性项
|
||||
var newArr = [];
|
||||
arr.forEach((i, index) => {
|
||||
if (tempArr.indexOf(i[key]) === -1) {
|
||||
newArr.push({
|
||||
time: i[key],
|
||||
data: [i]
|
||||
});
|
||||
tempArr.push(i[key]);
|
||||
} else {
|
||||
newArr[tempArr.indexOf(i[key])].data.push(i)
|
||||
}
|
||||
});
|
||||
return newArr;
|
||||
},
|
||||
setImgUrl (image) {
|
||||
return image ? (httpConfig.url + image) : ''
|
||||
},
|
||||
replaceImg (a) {
|
||||
let returnstr = a;
|
||||
if (a) {
|
||||
// a 为富文本的字符串内容,为了测试,只写了img标签
|
||||
var b = /<img [^>]*src=['"]([^'"]+)[^>]*>/g;// img 标签取src里面内容的正则
|
||||
var s = a.match(b);// 取到所有img标签 放到数组 s里面
|
||||
if (s) {
|
||||
for (var i = 0; i < s.length; i++) {
|
||||
var srcImg = s[i].replace(b, '$1');//取src面的内容
|
||||
if (srcImg.slice(0, 4) == 'http' || srcImg.slice(0, 5) == 'https' || srcImg.slice(0, 10) == 'data:image') {
|
||||
//若src前4位置或者前5位是http、https则不做任何修改
|
||||
} else {
|
||||
//修改富文本字符串内容 img标签src 相对路径改为绝对路径
|
||||
let str = srcImg.substr(9, srcImg.length - 1);
|
||||
returnstr = returnstr.replace(new RegExp(srcImg, 'g'), httpConfig.url + str);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
console.log(returnstr)
|
||||
return returnstr;
|
||||
},
|
||||
getDistance(lat1, lng1, lat2, lng2) {
|
||||
var radLat1 = toRadians(lat1);
|
||||
var radLat2 = toRadians(lat2);
|
||||
var deltaLat = radLat1 - radLat2;
|
||||
var deltaLng = toRadians(lng1) - toRadians(lng2);
|
||||
var dis = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(deltaLat / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(deltaLng / 2), 2)));
|
||||
let dd = (dis * 6378137).toFixed(0);
|
||||
if(+dd>1000){
|
||||
return (dd/1000).toFixed(2)+'km';
|
||||
}else{
|
||||
return dd+'m';
|
||||
}
|
||||
function toRadians(d) {
|
||||
return d * Math.PI / 180;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user