util.js 1.96 KB
import axios from 'axios';

let util = {

};

util.title = function(title) {
    title = title ? title + ' - ufo后台管理' : 'ufo后台管理';
    window.document.title = title;
};


util.ajax = axios.create({
    timeout: 30000
});

function getQueryString(name) {
    let urlParams = new URLSearchParams(window.location.search);
    return urlParams.get(name);
}

util.getQueryString = getQueryString;

function getUrlQueryString(url, name) {
    if (!url) {
        return ''
    }

    let searchParams = url.split('?')[1]
    let urlParams = new URLSearchParams(searchParams);
    return urlParams.get(name) || ''
}

function getLink(url) {
    if (!url) {
        return ''
    }
    let urlObj = url.split('?')[0];
    return `${urlObj}`;
}

function qsParse(url) {
    if (!url) {
        return {};
    }

    let queryString = url.split('?')[1];

    if (!queryString) {
        return {};
    }

    const result = queryString.split('&').map(i => i.split('=')).reduce((t, [k, v = null]) => {
        if (v) {
            t[k] = v;
        }
        return t;
    }, {});

    return result;
}

function qsStringify(obj) {
    return Object.keys(obj).map((k) => {
        return `${k}=${obj[k]}`;
    }).join('&');
}

function isObjectEmpty(obj) {
    return Object.keys(obj).length === 0;
}

util.isObjectEmpty = isObjectEmpty;
util.qsParse = qsParse;
util.qsStringify = qsStringify;
util.getUrlQueryString = getUrlQueryString;
util.getLink = getLink;

function clone(aObject) {
    if (!aObject) {
        return aObject;
    }

    let bObject, v, k;

    bObject = Array.isArray(aObject) ? [] : {};

    for (k in aObject) {
        v = aObject[k];
        bObject[k] = (typeof v === "object") ? clone(v) : v;
    }
    return bObject;
}

util.clone = clone;

function jumpUrl(url, newTab = false) {
    if (newTab) {
        let win = window.open(url, '_blank');
        win.focus();
    } else {
        window.location.href = url;
    }
}

util.jumpUrl = jumpUrl;

export default util;