| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- export default {
- isType(obj) {
- //类型判断
- var regexp = /\s(\w+)\]/;
- var result = regexp.exec(Object.prototype.toString.call(obj))[1];
- return result;
- },
- dateFormat(time) {
- const t = new Date(time * 1000);
- // 日期格式
- const format = 'Y-m-d h:i:s';
- const year = t.getFullYear();
- // 由于 getMonth 返回值会比正常月份小 1
- const month = t.getMonth() + 1;
- const day = t.getDate();
- const hours = t.getHours();
- const minutes = t.getMinutes();
- const seconds = t.getSeconds();
- const hash = {
- Y: year,
- m: month,
- d: day,
- h: hours,
- i: minutes,
- s: seconds,
- };
- return format.replace(/\w/g, (o) => {
- return hash[o];
- });
- },
- replaceDate(str, flag) {
- //时间戳转换
- if (str) {
- str = str.toString().length == 10 ? str * 1000 : str;
- let now = str ? new Date(str) : new Date(),
- y = now.getFullYear(),
- m = now.getMonth() + 1,
- d = now.getDate();
- if (flag) {
- return y + '-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d);
- }
- return (
- y +
- '-' +
- (m < 10 ? '0' + m : m) +
- '-' +
- (d < 10 ? '0' + d : d) +
- ' ' +
- now.toTimeString().substr(0, 8)
- );
- } else {
- return null;
- }
- },
- replaceDateNoHMS(str) {
- //时间戳转换
- if (str) {
- str = str.toString().length == 10 ? str * 1000 : str;
- let now = str ? new Date(str) : new Date(),
- y = now.getFullYear(),
- m = now.getMonth() + 1,
- d = now.getDate();
- return y + '-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d);
- } else {
- return null;
- }
- },
- //时间转换
- setDate(time) {
- let data = '';
- if (Object.prototype.toString.call(time) === '[object Date]') {
- data = new Date(time).toLocaleDateString().replace(/\//g, '-');
- }
- return data;
- },
- uniquesObjs(obj) {
- //去除统一数组中相同的对象
- let uniques = [];
- let stringify = {};
- for (let i = 0; i < obj.length; i++) {
- let keys = Object.keys(obj[i]);
- keys.sort(function(a, b) {
- return Number(a) - Number(b);
- });
- let str = '';
- for (let j = 0; j < keys.length; j++) {
- str += JSON.stringify(keys[j]);
- str += JSON.stringify(obj[i][keys[j]]);
- }
- if (!stringify.hasOwnProperty(str)) {
- uniques.push(obj[i]);
- stringify[str] = true;
- }
- }
- uniques = uniques;
- return uniques;
- },
- throttle(func, wait) {
- //节流
- let timer = null;
- let fn = function() {
- if (!timer) {
- let args = Array.from(arguments);
- timer = setTimeout(() => {
- func.apply(this, args);
- timer = null;
- }, wait);
- }
- };
- return fn;
- },
- shake(func, t) {
- //防抖
- let fn = function() {
- let args = Array.from(arguments);
- clearTimeout(func.id);
- func.id = setTimeout(() => {
- func.apply(this, args);
- }, t);
- };
- return fn;
- },
- objToParams(obj) {
- let result = '';
- for (let i in obj) {
- result += `&${i}=${obj[i]}`;
- }
- result = '?' + result.substr(1, result.length - 1);
- return result;
- },
- computedForm(array, header) {
- const result = array.reduce((pre, cur) => {
- const data = header.filter(
- (row) => cur.is_show && row.serverName == cur.key
- );
- if (data && data.length == 1) {
- return pre.concat(data);
- } else {
- return pre;
- }
- }, []);
- return result;
- },
- computedHeader(array, header) {
- const result = array.reduce((pre, cur) => {
- const data = header.filter((row) => (cur.is_show && row.key == cur.key) && (row.title = cur.value));
- if (data && data.length == 1) {
- return pre.concat(data);
- } else {
- return pre;
- }
- }, []);
- return result;
- },
- };
|