util.js
1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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;