You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
duoji-frontend/src/utils/index.js

270 lines
8.3 KiB
JavaScript

6 years ago
export default {
// 时间戳转换
format: (time) => {
var days = parseInt(time / (1000 * 60 * 60 * 24));
var hours = parseInt((time % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = parseInt((time % (1000 * 60 * 60)) / (1000 * 60));
var seconds = (time % (1000 * 60)) / 1000;
return days + " 天 " + hours + " 小时 " + minutes + " 分钟 " + seconds + " 秒 " ;
},
//计算字符串长度英文1个字符中文2个字符
computedStrLen (str) {
var len = 0;
for (var i = 0; i < str.length; i++) {
var c = str.charCodeAt(i);
//单字节加1
if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
len++;
} else {
len += 2;
}
}
return len;
},
/**
* 对象的深度拷贝
* @param data 需要拷贝的元数据
* @return {any} 返回拷贝后的新数据
*/
deepClone(data) {
const type = this.getType(data);
let obj;
if (type === 'array') {
obj = [];
} else if (type === 'object') {
obj = {};
} else {
//不再具有下一层次
return data;
}
if (type === 'array') {
for (let i = 0, len = data.length; i < len; i++) {
obj.push(this.deepClone(data[i]));
}
} else if (type === 'object') {
for (let key in data) {
obj[key] = this.deepClone(data[key]);
}
}
const constructor = data.constructor;
if (constructor) {
return Object.assign(new constructor(), obj);
}
return obj;
},
cleanData(data) {
let _data = {};
for (let key in data) {
if(Object.prototype.toString.call(data[key]) === '[object Object]') {
_data[key] = null;
} else if (data[key] instanceof Array) {
_data[key] = [];
} else {
_data[key] = undefined;
}
}
return _data;
},
/**
* 判断对象类型
* @param {Object} object
* @return {String} object type
*/
getType(object) {
var toString = Object.prototype.toString;
var map = {
'[object Boolean]': 'boolean',
'[object Number]': 'number',
'[object String]': 'string',
'[object Function]': 'function',
'[object Array]': 'array',
'[object Date]': 'date',
'[object RegExp]': 'regExp',
'[object Undefined]': 'undefined',
'[object Null]': 'null',
'[object Object]': 'object'
};
if (object instanceof Element) {
return 'element';
}
return map[toString.call(object)];
},
// 下载
downloadFile (fileName, blob) {
if (window.navigator.msSaveBlob) {
// for ie10 and later
try {
window.navigator.msSaveBlob(blob, fileName);
}
catch(e) {
console.log(e);
}
} else {
// 只有 Firefox 和 Chrome 支持 download 属性。
const elink = document.createElement('a')
elink.download = fileName
elink.style.display = 'none'
elink.href = window.URL.createObjectURL(blob)
document.body.appendChild(elink)
elink.click()
URL.revokeObjectURL(elink.href) // 释放URL 对象
document.body.removeChild(elink)
}
},
/*
* 根据文件名的尾缀 返回文件类型
* @param {any} fileName 文件名
*/
getFileType(fileName) {
// 后缀获取
let suffix = '';
// 获取类型结果
let result = '';
try {
const flieArr = fileName.split('.');
suffix = flieArr[flieArr.length - 1];
} catch (err) {
suffix = '';
}
// fileName无后缀返回 false
if (!suffix) { return false; }
suffix = suffix.toLocaleLowerCase();
// 图片格式
const imglist = ['png', 'jpg', 'jpeg', 'bmp', 'gif'];
// 进行图片匹配
result = imglist.find(item => item === suffix);
if (result) {
return 'image';
}
// 匹配txt
const txtlist = ['txt'];
result = txtlist.find(item => item === suffix);
if (result) {
return 'txt';
}
// 匹配 excel
const excelist = ['xls', 'xlsx'];
result = excelist.find(item => item === suffix);
if (result) {
return 'excel';
}
// 匹配 word
const wordlist = ['doc', 'docx'];
result = wordlist.find(item => item === suffix);
if (result) {
return 'word';
}
// 匹配 pdf
const pdflist = ['pdf'];
result = pdflist.find(item => item === suffix);
if (result) {
return 'pdf';
}
// 匹配 ppt
const pptlist = ['ppt', 'pptx'];
result = pptlist.find(item => item === suffix);
if (result) {
return 'ppt';
}
// 匹配 视频
const videolist = ['mp4', 'm2v', 'mkv', 'rmvb', 'wmv', 'avi', 'flv', 'mov', 'm4v'];
result = videolist.find(item => item === suffix);
if (result) {
return 'video';
}
// 匹配 音频
const radiolist = ['mp3', 'wav', 'wmv'];
result = radiolist.find(item => item === suffix);
if (result) {
return 'radio';
}
// 其他 文件类型
return 'other';
},
// 千位分隔符
numFormat(num){
var res = num.toString().replace(/\d+/, (n) => { // 先提取整数部分
return n.replace(/(\d)(?=(\d{3})+$)/g, ($1) => {
return $1 + ",";
});
})
return res;
},
reg: {
required: [{
required: true,
message: '必填'
}],
phone: [{
pattern: /^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/,
message: '请填写正确的手机号码'
}],
shouldSelectProduction:[{
required: true,
message: '请选择所属产品'
}],
equipmentId:[{
required: true,
pattern: /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,8}$/,
message: '设备id必须为8位数字字母组合'
}],
morethanTwo:[{
min: 2,
message: '长度至少为2个字符'
}],
morethanFour:[{
min: 4,
message: '长度至少为4个字符'
}],
positiveInteger: [{
pattern: /^[1-9]\d*$/,
message: '请填写正整数'
}],
integer: [{
pattern: /^[0-9]\d*$/,
message: '请填写非负整数'
}],
price: [{
pattern: /(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/,
message: '请填写正确的钱数'
}],
long: [{
pattern: /^(-?\d+)(\.\d+)?$/,
message: '请填写正确的数值'
}],
space: [{
pattern: /^\S*$/,
message: '请勿输入空格'
}],
lengthMax: [{
max: 200,
message: '超出最大字数限制'
}],
absLong: [{
pattern: /^(\d+)(\.\d+)?$/,
message: '请填写大于等于的0的数字'
}],
long1: [{
pattern: /(^[1-9]\d*(\.\d{0,1})?$)|(^0(\.\d{0,1})?$)/,
message: '保留一位小数'
}],
long2: [{
pattern: /(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/,
message: '保留两位小数'
}],
date: [{
pattern: /^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/,
message: '日期格式应为2018-01-01'
}],
coordinates: [{
pattern: /^(\\-|\\+)?\d+(\.\d+)?(,(\\-|\+)?\d+(\.\d+)?)$/,
message: '格式错误,例:‘经度’,‘纬度’'
}],
email:[{
pattern: /^([a-zA-Z0-9\\_\\.\\-])+\\@(([a-zA-Z0-9\\-])+\.)+([a-zA-Z0-9]{2,4})+$/,
message: '请填写正确的邮箱'
}]
},
}