/**
- 输出指定格式的日期
- @param {any} dateObj new Date()
- @param {any} format 'yyyy-MM-dd hh:mm:ss'
- @returns
*/
function dateToString(dateObj, format) {
var z = {
y: dateObj.getFullYear(),
M: dateObj.getMonth() + 1,
d: dateObj.getDate(),
h: dateObj.getHours(),
m: dateObj.getMinutes(),
s: dateObj.getSeconds()
};
return format.replace(/(y+|M+|d+|h+|m+|s+)/g, function (v) {
return ((v.length > 1 ? '0' : '') + eval('z.' + v.slice(-1))).slice(-(v.length > 2 ? v.length : 2))
});
}
function axiosRequest(url, type, data = {}, timeout = 100000, needContentType = true) {
return new Promise(function (resolve, reject) {
let requestObj = {
method: type,
url: url,
params: data,
data: data,
responseType: 'json',
maxContentLength: 1073741824, // 单位 bytes 这里为1G
headers: {
'Content-Type': 'application/json'
},
timeout: timeout || 100000
}
// 如果是get请求,需要对参数序列化函数进行改造,因为会过滤掉null值得参数
if (_.isEqual(type.toLowerCase(), 'get')) {
requestObj.paramsSerializer = function (params) {
let str = '';
for (let key in params) {
str += key + '=' + encodeURIComponent(params[key]) + '&';
}
return str.slice(0, str.length - 1);
}
}
axios(requestObj).then((response) => {
resolve(response.data);
}).catch(function (error) {
console.error(error);
reject(error);
});
})
}
import _ from 'lodash';
// 克隆自身属性
function cloneDeep(obj) {
var o;
if (typeof obj === 'object') {
if (obj === null) {
o = null;
} else {
if (obj instanceof Array) {
o = [];
for (var i = 0, len = obj.length; i < len; i++) {
o.push(cloneDeep(obj[i]));
}
} else {
o = {};
for (var k in obj) {
if (obj.hasOwnProperty(k)) {
o[k] = cloneDeep(obj[k]);
}
}
}
}
} else {
o = obj;
}
return o;
}
// 克隆指定的自身属性
function cloneDeepWithKeys(obj, keys) {
var o;
if (typeof obj === 'object') {
if (obj === null) {
o = null;
} else {
if (obj instanceof Array) {
o = [];
for (var i = 0, len = obj.length; i < len; i++) {
o.push(cloneDeepWithKeys(obj[i], keys));
}
} else {
o = {};
for (var k in obj) {
if (_.indexOf(keys, k) !== -1 && obj.hasOwnProperty(k)) {
o[k] = cloneDeep(obj[k]);
}
}
}
}
} else {
o = obj;
}
return o;
}
// 判断是否为空
function isEmpty(obj) {
if (obj === undefined || obj === '') {
return true;
}
if (_.isBoolean(obj) || _.isNumber(obj) || _.isDate(obj) || _.isRegExp(obj) || _.isFunction(obj) || _.isElement(obj) || _.isArguments(obj)) {
return false;
}
if ((typeof obj) === 'undefined') {
return true;
}
return _.isEmpty(obj);
}
function toPercent(point) {
var str = Number(point * 100).toFixed(1);
str += '%';
return str;
}
function toPoint(percent) {
var str = percent.replace('%', '');
str = str / 100;
return str;
}
function getCookie(cName) {
if (document.cookie.length > 0) {
var cStart = document.cookie.indexOf(cName + '=')
if (cStart != -1) {
cStart = cStart + cName.length + 1
var cEnd = document.cookie.indexOf(';', cStart)
if (cEnd == -1) cEnd = document.cookie.length
return unescape(document.cookie.substring(cStart, cEnd))
}
}
return ''
}
function setCookie(cName, value, expiredays) {
var exdate = new Date()
exdate.setDate(exdate.getDate() + expiredays)
document.cookie = cName + '=' + escape(value) +
((expiredays == null) ? '' : ';expires=' + exdate.toGMTString())
}
/**
-
查找对象中是否包含某个属性的某个值(全等)
*/
function findPropertyDeepEqual(obj, proName, value) {
if (obj == null) {
return false;
}// 首先判断是不是对象
if (typeof obj == 'object') {
// 再判断是不是数组
if (Array.isArray(obj)) {
for (let i = 0; i < obj.length; i++) {
if (findPropertyDeepEqual(obj[i], proName, value)) {
return true;
}
}
} else {
// 首先判断这个对象里的这个属性
if (obj[proName] != null && obj[proName] == value) {
return true;
}let keys = Object.keys(obj); for (let i = 0; i < keys.length; i++) { if (findPropertyDeepEqual(obj[keys[i]], proName, value)) { return true; } } } return false;
} else {
return false;
}
}
/**
-
查找对象中是否包含某个属性的某个值(包含)
*/
function findPropertyDeepIncludes(obj, proName, value) {
if (obj == null) {
return false;
}// 首先判断是不是对象
if (typeof obj == 'object') {
// 再判断是不是数组
if (Array.isArray(obj)) {
for (let i = 0; i < obj.length; i++) {
if (findPropertyDeepIncludes(obj[i], proName, value)) {
return true;
}
}
} else {
// 首先判断这个对象里的这个属性
if (obj[proName] != null && _.includes(obj[proName], value)) {
return true;
}let keys = Object.keys(obj); for (let i = 0; i < keys.length; i++) { if (findPropertyDeepIncludes(obj[keys[i]], proName, value)) { return true; } } } return false;
} else {
return false;
}
}
/**
- 交换数组中两个元素的位置,这个会直接改变传入的数组
- @param {any} array
- @param {any} index1
- @param {any} index2
*/
function swapElement(array, index1, index2) {
[array[index1], array[index2]] = [array[index2], array[index1]];
}
export default {
cloneDeep: cloneDeep,
cloneDeepWithKeys: cloneDeepWithKeys,
isEmpty: isEmpty,
toPercent: toPercent,
toPoint: toPoint,
setCookie: setCookie,
getCookie: getCookie,
findPropertyDeepEqual: findPropertyDeepEqual,
findPropertyDeepIncludes: findPropertyDeepIncludes,
swapElement: swapElement
}
网友评论