@ajaxjs/util 1.1.3 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +65 -0
- package/dist/index.esm.js +210 -432
- package/dist/index.esm.js.map +1 -0
- package/dist/index.umd.js +213 -435
- package/dist/index.umd.js.map +1 -0
- package/dist/src/date_format.d.ts +9 -0
- package/dist/src/index.d.ts +5 -5
- package/dist/src/utils.d.ts +38 -0
- package/dist/src/{core/xhr.d.ts → xhr.d.ts} +22 -9
- package/dist/src/xhr_fetch.d.ts +17 -0
- package/package.json +41 -41
- package/dist/src/core/cookies.d.ts +0 -18
- package/dist/src/core/dom.d.ts +0 -17
- package/dist/src/core/utils.d.ts +0 -51
- package/dist/src/core/xhr-config.d.ts +0 -22
package/dist/index.esm.js
CHANGED
|
@@ -1,51 +1,115 @@
|
|
|
1
|
+
function getQueryParam(variable, isParent = false) {
|
|
2
|
+
const query = (isParent ? parent.location : window.location).search.substring(1);
|
|
3
|
+
const vars = query.split("&");
|
|
4
|
+
for (let i = 0; i < vars.length; i++) {
|
|
5
|
+
const pair = vars[i].split("=");
|
|
6
|
+
if (pair[0] == variable)
|
|
7
|
+
return pair[1];
|
|
8
|
+
}
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
1
11
|
/**
|
|
2
|
-
*
|
|
12
|
+
* 函数节流
|
|
3
13
|
*
|
|
4
|
-
* @
|
|
5
|
-
* @
|
|
14
|
+
* @author https://www.cnblogs.com/moqiutao/p/6875955.html
|
|
15
|
+
* @param fn
|
|
16
|
+
* @param delay
|
|
17
|
+
* @param mustRunDelay
|
|
6
18
|
*/
|
|
7
|
-
function
|
|
8
|
-
let
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
19
|
+
function throttle(fn, delay, mustRunDelay) {
|
|
20
|
+
let timer, t_start;
|
|
21
|
+
return function () {
|
|
22
|
+
let t_curr = +new Date();
|
|
23
|
+
window.clearTimeout(timer);
|
|
24
|
+
if (!t_start)
|
|
25
|
+
t_start = t_curr;
|
|
26
|
+
if (t_curr - t_start >= mustRunDelay) {
|
|
27
|
+
fn.apply(this, arguments);
|
|
28
|
+
t_start = t_curr;
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
let args = arguments;
|
|
32
|
+
timer = window.setTimeout(() => fn.apply(this, args), delay);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
14
35
|
}
|
|
15
36
|
/**
|
|
16
|
-
*
|
|
37
|
+
* 并行和串行任务
|
|
17
38
|
*
|
|
18
|
-
* @
|
|
19
|
-
* @param
|
|
39
|
+
* @author https://segmentfault.com/a/1190000013265925
|
|
40
|
+
* @param arr
|
|
41
|
+
* @param finnaly
|
|
20
42
|
*/
|
|
21
|
-
function
|
|
22
|
-
let
|
|
23
|
-
exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);
|
|
43
|
+
function parallel(arr, _finally) {
|
|
44
|
+
let fn, index = 0;
|
|
24
45
|
// @ts-ignore
|
|
25
|
-
|
|
46
|
+
let statusArr = Array(arr.length).fill().map(() => ({ isActive: false, data: null }));
|
|
47
|
+
let isFinished = () => statusArr.every((item) => item.isActive === true);
|
|
48
|
+
let resolve = function (index) {
|
|
49
|
+
return function (data) {
|
|
50
|
+
statusArr[index].data = data;
|
|
51
|
+
statusArr[index].isActive = true;
|
|
52
|
+
let isFinish = isFinished();
|
|
53
|
+
if (isFinish) {
|
|
54
|
+
let datas = statusArr.map((item) => item.data);
|
|
55
|
+
_finally(datas);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
while ((fn = arr.shift())) {
|
|
60
|
+
fn(resolve(index)); // 给 resolve 函数追加参数,可以使用 bind 函数实现,这里使用了柯里化
|
|
61
|
+
index++;
|
|
62
|
+
}
|
|
26
63
|
}
|
|
27
64
|
/**
|
|
28
|
-
*
|
|
65
|
+
* 通用的打开下载对话框方法,没有测试过具体兼容性
|
|
66
|
+
* https://www.cnblogs.com/liuxianan/p/js-download.html
|
|
67
|
+
*
|
|
68
|
+
* ref 这应该是你见过的最全前端下载总结 https://juejin.cn/post/6844903763359039501
|
|
69
|
+
*
|
|
70
|
+
* @param url 下载地址,也可以是一个blob对象,必选
|
|
71
|
+
* @param saveName 保存文件名,可选
|
|
29
72
|
*/
|
|
30
|
-
function
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
73
|
+
function openDownloadDialog(url, saveName) {
|
|
74
|
+
if (typeof url == 'object' && url instanceof Blob)
|
|
75
|
+
url = URL.createObjectURL(url); // 创建blob地址
|
|
76
|
+
const aLink = document.createElement('a');
|
|
77
|
+
aLink.href = url;
|
|
78
|
+
aLink.download = saveName || ''; // HTML5新增的属性,指定保存文件名,可以不要后缀,注意,file:///模式下不会生效
|
|
79
|
+
let event;
|
|
80
|
+
if (window.MouseEvent)
|
|
81
|
+
event = new MouseEvent('click');
|
|
82
|
+
else {
|
|
83
|
+
event = document.createEvent('MouseEvents');
|
|
84
|
+
event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
|
|
85
|
+
}
|
|
86
|
+
aLink.dispatchEvent(event);
|
|
87
|
+
}
|
|
88
|
+
function myHTMLInclude() {
|
|
89
|
+
let a, file, xhttp;
|
|
90
|
+
const z = document.getElementsByTagName("*");
|
|
91
|
+
for (let i = 0; i < z.length; i++) {
|
|
92
|
+
if (z[i].getAttribute("w3-include-html")) {
|
|
93
|
+
a = z[i].cloneNode(false);
|
|
94
|
+
file = z[i].getAttribute("w3-include-html");
|
|
95
|
+
xhttp = new XMLHttpRequest();
|
|
96
|
+
xhttp.onreadystatechange = function () {
|
|
97
|
+
if (xhttp.readyState == 4 && xhttp.status == 200) {
|
|
98
|
+
a.removeAttribute("w3-include-html");
|
|
99
|
+
a.innerHTML = xhttp.responseText;
|
|
100
|
+
// @ts-ignore
|
|
101
|
+
z[i].parentNode.replaceChild(a, z[i]);
|
|
102
|
+
myHTMLInclude();
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
if (file) {
|
|
106
|
+
xhttp.open("GET", file, false);
|
|
107
|
+
xhttp.send();
|
|
108
|
+
}
|
|
109
|
+
return;
|
|
38
110
|
}
|
|
39
111
|
}
|
|
40
112
|
}
|
|
41
|
-
|
|
42
|
-
var cookies = /*#__PURE__*/Object.freeze({
|
|
43
|
-
__proto__: null,
|
|
44
|
-
delCookie: delCookie,
|
|
45
|
-
getCookie: getCookie,
|
|
46
|
-
setCookie: setCookie
|
|
47
|
-
});
|
|
48
|
-
|
|
49
113
|
/**
|
|
50
114
|
* 向父级元素递归搜索
|
|
51
115
|
*
|
|
@@ -68,56 +132,48 @@ function up(_el, tagName, className) {
|
|
|
68
132
|
}
|
|
69
133
|
return null;
|
|
70
134
|
}
|
|
71
|
-
/**
|
|
72
|
-
* 加载脚本
|
|
73
|
-
*
|
|
74
|
-
* @param url 脚本地址
|
|
75
|
-
* @param id 脚本元素 id,可选的
|
|
76
|
-
* @param cb 回调函数,可选的
|
|
77
|
-
*/
|
|
78
|
-
function loadScript(url, id, cb) {
|
|
79
|
-
let script = document.createElement("script");
|
|
80
|
-
script.src = url;
|
|
81
|
-
if (cb)
|
|
82
|
-
script.onload = cb;
|
|
83
|
-
if (id)
|
|
84
|
-
script.id = id;
|
|
85
|
-
document.getElementsByTagName("head")[0].appendChild(script);
|
|
86
|
-
}
|
|
87
135
|
|
|
88
|
-
var
|
|
136
|
+
var utils = /*#__PURE__*/Object.freeze({
|
|
89
137
|
__proto__: null,
|
|
90
|
-
|
|
138
|
+
getQueryParam: getQueryParam,
|
|
139
|
+
myHTMLInclude: myHTMLInclude,
|
|
140
|
+
openDownloadDialog: openDownloadDialog,
|
|
141
|
+
parallel: parallel,
|
|
142
|
+
throttle: throttle,
|
|
91
143
|
up: up
|
|
92
144
|
});
|
|
93
145
|
|
|
94
146
|
/**
|
|
95
|
-
*
|
|
96
|
-
*/
|
|
97
|
-
/**
|
|
98
|
-
* 是否调试模式中
|
|
147
|
+
* 格式化日期时间
|
|
99
148
|
*
|
|
100
|
-
*
|
|
149
|
+
* @param {string} format 日期格式化模板
|
|
150
|
+
* @returns {string} 格式化后的日期字符串
|
|
151
|
+
* @this {Date} 调用此方法的Date对象
|
|
152
|
+
* @example
|
|
153
|
+
* // 返回类似 "2023-12-25 14:30:00"
|
|
154
|
+
* new Date().format("yyyy-MM-dd hh:mm:ss");
|
|
155
|
+
* // 返回类似 "23-12-25"
|
|
156
|
+
* new Date().format("yy-MM-dd");
|
|
157
|
+
* // 返回类似 "2023年12月25日"
|
|
158
|
+
* new Date().format("yyyy年MM月dd日");
|
|
101
159
|
*
|
|
102
|
-
*
|
|
160
|
+
* 格式化模板说明:
|
|
161
|
+
* - yyyy: 四位年份
|
|
162
|
+
* - yy: 两位年份
|
|
163
|
+
* - MM: 两位月份(01-12)
|
|
164
|
+
* - M: 一位月份(1-12)
|
|
165
|
+
* - dd: 两位日期(01-31)
|
|
166
|
+
* - d: 一位日期(1-31)
|
|
167
|
+
* - hh: 两位小时(00-23)
|
|
168
|
+
* - h: 一位小时(0-23)
|
|
169
|
+
* - mm: 两位分钟(00-59)
|
|
170
|
+
* - m: 一位分钟(0-59)
|
|
171
|
+
* - ss: 两位秒钟(00-59)
|
|
172
|
+
* - s: 一位秒钟(0-59)
|
|
173
|
+
* - q: 季度(1-4)
|
|
174
|
+
* - S: 毫秒(000-999)
|
|
103
175
|
*/
|
|
104
|
-
function
|
|
105
|
-
// @ts-ignore
|
|
106
|
-
return process.env.NODE_ENV === 'development';
|
|
107
|
-
}
|
|
108
|
-
function isDev() {
|
|
109
|
-
let currentHostname = window.location.hostname;
|
|
110
|
-
// 判断主机名是否是内网地址
|
|
111
|
-
return (currentHostname.startsWith('192.168.') || currentHostname.startsWith('10.') || currentHostname === 'localhost');
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* 日期格式化。详见博客文章:http://blog.csdn.net/zhangxin09/archive/2011/01/01/6111294.aspx
|
|
115
|
-
* e.g: new Date().format("yyyy-MM-dd hh:mm:ss")
|
|
116
|
-
*
|
|
117
|
-
* @param {String} format
|
|
118
|
-
* @return {String}
|
|
119
|
-
*/
|
|
120
|
-
function dateFormat(format) {
|
|
176
|
+
function formatDate(format) {
|
|
121
177
|
let $1, o = {
|
|
122
178
|
"M+": this.getMonth() + 1, // 月份,从0开始算
|
|
123
179
|
"d+": this.getDate(), // 日期
|
|
@@ -128,13 +184,15 @@ function dateFormat(format) {
|
|
|
128
184
|
"q+": Math.floor((this.getMonth() + 3) / 3),
|
|
129
185
|
"S": this.getMilliseconds() // 千秒
|
|
130
186
|
};
|
|
131
|
-
if (/(y+)/.test(format))
|
|
132
|
-
// @ts-ignore
|
|
133
|
-
$1 = RegExp.$1, format = format.replace($1, String(this.getFullYear()).substr(4 - $1));
|
|
134
187
|
let key, value;
|
|
188
|
+
if (/(y+)/.test(format)) {
|
|
189
|
+
$1 = RegExp.$1,
|
|
190
|
+
format = format.replace($1, String(this.getFullYear()).substr(4 - $1));
|
|
191
|
+
}
|
|
135
192
|
for (key in o) { // 如果没有指定该参数,则子字符串将延续到 stringvar 的最后。
|
|
136
193
|
if (new RegExp("(" + key + ")").test(format)) {
|
|
137
194
|
$1 = RegExp.$1,
|
|
195
|
+
// @ts-ignore
|
|
138
196
|
value = String(o[key]),
|
|
139
197
|
value = $1.length == 1 ? value : ("00" + value).substr(value.length),
|
|
140
198
|
format = format.replace($1, value);
|
|
@@ -144,382 +202,102 @@ function dateFormat(format) {
|
|
|
144
202
|
}
|
|
145
203
|
/**
|
|
146
204
|
* 日期格式化
|
|
147
|
-
* @author meizz
|
|
148
|
-
* @param date 日期,必须为 Date 类型
|
|
149
|
-
* @param fmt 格式模板
|
|
150
|
-
* @returns 格式化后的字符串
|
|
151
|
-
*/
|
|
152
|
-
function dateFormat2(date, fmt) {
|
|
153
|
-
let o = {
|
|
154
|
-
"M+": date.getMonth() + 1, // 月份
|
|
155
|
-
"d+": date.getDate(), // 日
|
|
156
|
-
"h+": date.getHours(), // 小时
|
|
157
|
-
"m+": date.getMinutes(), // 分
|
|
158
|
-
"s+": date.getSeconds(), // 秒
|
|
159
|
-
"q+": Math.floor((date.getMonth() + 3) / 3), // 季度
|
|
160
|
-
"S": date.getMilliseconds() // 毫秒
|
|
161
|
-
};
|
|
162
|
-
if (/(y+)/.test(fmt))
|
|
163
|
-
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
|
|
164
|
-
for (var k in o)
|
|
165
|
-
if (new RegExp("(" + k + ")").test(fmt)) {
|
|
166
|
-
let obj = (RegExp.$1.length == 1) ? o[k] : ("00" + o[k]).substr(("" + o[k]).length);
|
|
167
|
-
// @ts-ignore
|
|
168
|
-
fmt = fmt.replace(RegExp.$1, obj);
|
|
169
|
-
}
|
|
170
|
-
return fmt;
|
|
171
|
-
}
|
|
172
|
-
/**
|
|
173
|
-
* 并行和串行任务
|
|
174
205
|
*
|
|
175
|
-
* @
|
|
176
|
-
* @param
|
|
177
|
-
* @
|
|
206
|
+
* @param {date}
|
|
207
|
+
* @param {string} format 日期格式化模板
|
|
208
|
+
* @returns {string} 格式化后的日期字符串
|
|
178
209
|
*/
|
|
179
|
-
function
|
|
180
|
-
|
|
181
|
-
// @ts-ignore
|
|
182
|
-
let statusArr = Array(arr.length).fill().map(() => ({ isActive: false, data: null }));
|
|
183
|
-
let isFinished = function () {
|
|
184
|
-
return statusArr.every((item) => item.isActive === true);
|
|
185
|
-
};
|
|
186
|
-
let resolve = function (index) {
|
|
187
|
-
return function (data) {
|
|
188
|
-
statusArr[index].data = data;
|
|
189
|
-
statusArr[index].isActive = true;
|
|
190
|
-
let isFinish = isFinished();
|
|
191
|
-
if (isFinish) {
|
|
192
|
-
let datas = statusArr.map((item) => item.data);
|
|
193
|
-
_finally(datas);
|
|
194
|
-
}
|
|
195
|
-
};
|
|
196
|
-
};
|
|
197
|
-
// @ts-ignore
|
|
198
|
-
while ((fn = arr.shift())) {
|
|
199
|
-
fn(resolve(index)); // 给 resolve 函数追加参数,可以使用 bind 函数实现,这里使用了柯里化
|
|
200
|
-
index++;
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
/**
|
|
204
|
-
* 函数节流
|
|
205
|
-
*
|
|
206
|
-
* @author https://www.cnblogs.com/moqiutao/p/6875955.html
|
|
207
|
-
* @param fn
|
|
208
|
-
* @param delay
|
|
209
|
-
* @param mustRunDelay
|
|
210
|
-
*/
|
|
211
|
-
function throttle(fn, delay, mustRunDelay) {
|
|
212
|
-
var timer, t_start;
|
|
213
|
-
return function () {
|
|
214
|
-
var t_curr = +new Date();
|
|
215
|
-
window.clearTimeout(timer);
|
|
216
|
-
if (!t_start)
|
|
217
|
-
t_start = t_curr;
|
|
218
|
-
if (t_curr - t_start >= mustRunDelay) {
|
|
219
|
-
// @ts-ignore
|
|
220
|
-
fn.apply(this, arguments);
|
|
221
|
-
t_start = t_curr;
|
|
222
|
-
}
|
|
223
|
-
else {
|
|
224
|
-
var args = arguments;
|
|
225
|
-
// @ts-ignore
|
|
226
|
-
timer = window.setTimeout(() => fn.apply(this, args), delay);
|
|
227
|
-
}
|
|
228
|
-
};
|
|
210
|
+
function dateFormat(date, format) {
|
|
211
|
+
return formatDate.call(new Date(date), format);
|
|
229
212
|
}
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
*
|
|
233
|
-
* @param {string} text
|
|
234
|
-
*/
|
|
235
|
-
function copyToClipboard(text) {
|
|
236
|
-
if (navigator.clipboard)
|
|
237
|
-
navigator.clipboard.writeText(text); // clipboard api 复制
|
|
238
|
-
else {
|
|
239
|
-
let textarea = document.createElement('textarea');
|
|
240
|
-
document.body.appendChild(textarea); // 隐藏此输入框
|
|
241
|
-
textarea.style.position = 'fixed';
|
|
242
|
-
textarea.style.clip = 'rect(0 0 0 0)';
|
|
243
|
-
textarea.style.top = '10px';
|
|
244
|
-
textarea.value = text; // 赋值
|
|
245
|
-
textarea.select(); // 选中
|
|
246
|
-
document.execCommand('copy', true); // 复制
|
|
247
|
-
document.body.removeChild(textarea); // 移除输入框
|
|
248
|
-
}
|
|
213
|
+
function now(format = 'yyyy-MM-dd hh:mm') {
|
|
214
|
+
return formatDate.call(new Date(), format);
|
|
249
215
|
}
|
|
250
216
|
|
|
251
|
-
var
|
|
217
|
+
var date_format = /*#__PURE__*/Object.freeze({
|
|
252
218
|
__proto__: null,
|
|
253
|
-
copyToClipboard: copyToClipboard,
|
|
254
219
|
dateFormat: dateFormat,
|
|
255
|
-
|
|
256
|
-
isDebug: isDebug,
|
|
257
|
-
isDev: isDev,
|
|
258
|
-
parallel: parallel,
|
|
259
|
-
throttle: throttle
|
|
220
|
+
now: now
|
|
260
221
|
});
|
|
261
222
|
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
const
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
/**
|
|
275
|
-
* 设置全局请求的 head 参数
|
|
276
|
-
*
|
|
277
|
-
* @param param
|
|
278
|
-
*/
|
|
279
|
-
function setBaseHeadParams(params) {
|
|
280
|
-
if (BASE_HEAD_PARAMS === null)
|
|
281
|
-
BASE_HEAD_PARAMS = {};
|
|
282
|
-
Object.assign(BASE_HEAD_PARAMS, params);
|
|
283
|
-
}
|
|
284
|
-
/**
|
|
285
|
-
*
|
|
286
|
-
* @param getOrDel
|
|
287
|
-
* @param url
|
|
288
|
-
* @param cb
|
|
289
|
-
* @param params
|
|
290
|
-
* @param cfg
|
|
291
|
-
*/
|
|
292
|
-
function getOrDel(getOrDel, url, cb, params, cfg = DEFAULT_XHR_CFG) {
|
|
293
|
-
let xhr = initXhr(cfg);
|
|
294
|
-
if (params != null) {
|
|
295
|
-
if (url.indexOf('?') != -1)
|
|
296
|
-
url += '&' + toParams(params);
|
|
297
|
-
else
|
|
298
|
-
url += '?' + toParams(params);
|
|
223
|
+
function request(api, method, bodyData, callback, header) {
|
|
224
|
+
let headers = {};
|
|
225
|
+
if (header)
|
|
226
|
+
for (const key in header)
|
|
227
|
+
headers[key] = header[key];
|
|
228
|
+
method = method.toUpperCase();
|
|
229
|
+
let body = bodyData;
|
|
230
|
+
if (bodyData && (method === 'POST' || method === 'PUT')) {
|
|
231
|
+
if (headers['Content-Type'] == 'application/json')
|
|
232
|
+
body = JSON.stringify(bodyData);
|
|
233
|
+
else if (headers['Content-Type'] == "application/x-www-form-urlencoded")
|
|
234
|
+
body = json2formParams(bodyData);
|
|
299
235
|
}
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
responseHandle(this, cb, cfg);
|
|
322
|
-
};
|
|
323
|
-
if (BASE_HEAD_PARAMS) // 设置自定义请求头
|
|
324
|
-
for (let key in BASE_HEAD_PARAMS)
|
|
325
|
-
xhr.setRequestHeader(key, BASE_HEAD_PARAMS[key]);
|
|
326
|
-
// 此方法必须在 open() 方法和 send() 之间调用
|
|
327
|
-
if (!cfg.contentType) // 如未设置,默认为表单请求
|
|
328
|
-
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
329
|
-
else
|
|
330
|
-
xhr.setRequestHeader("Content-Type", cfg.contentType);
|
|
331
|
-
let _params = typeof params != 'string' ? toParams(params) : params;
|
|
332
|
-
if (_params)
|
|
333
|
-
xhr.send(_params);
|
|
334
|
-
else
|
|
335
|
-
xhr.send();
|
|
336
|
-
}
|
|
337
|
-
/**
|
|
338
|
-
*
|
|
339
|
-
* @param url
|
|
340
|
-
* @param cb
|
|
341
|
-
* @param params
|
|
342
|
-
* @param cfg
|
|
343
|
-
*/
|
|
344
|
-
function xhr_post_upload(url, cb, params, cfg = DEFAULT_XHR_CFG) {
|
|
345
|
-
let xhr = initXhr(cfg);
|
|
346
|
-
xhr.open('post', url, true);
|
|
347
|
-
xhr.onreadystatechange = function () {
|
|
348
|
-
responseHandle(this, cb, cfg);
|
|
349
|
-
};
|
|
350
|
-
if (BASE_HEAD_PARAMS) // 设置自定义请求头
|
|
351
|
-
for (let key in BASE_HEAD_PARAMS)
|
|
352
|
-
xhr.setRequestHeader(key, BASE_HEAD_PARAMS[key]);
|
|
353
|
-
// 什么 Content-Type 都不设置
|
|
354
|
-
xhr.send(params);
|
|
355
|
-
}
|
|
356
|
-
/**
|
|
357
|
-
* XHR GET 请求
|
|
358
|
-
*
|
|
359
|
-
* @param url 请求地址
|
|
360
|
-
* @param cb 回调函数 @example (json: {}, text: string) => void;
|
|
361
|
-
* @param params 参数,必填,如无填空字符串 "";参数类型是json;参数值会进行 URL 编码,最后附加到 QueryString 中
|
|
362
|
-
* @param cfg 配置,可选的
|
|
363
|
-
*/
|
|
364
|
-
function xhr_get(url, cb, params, cfg = DEFAULT_XHR_CFG) {
|
|
365
|
-
getOrDel('get', url, cb, params, cfg);
|
|
366
|
-
}
|
|
367
|
-
/**
|
|
368
|
-
* XHR DELETE 请求
|
|
369
|
-
*
|
|
370
|
-
* @param url 请求地址
|
|
371
|
-
* @param cb 回调函数 @example (json: {}, text: string) => void;
|
|
372
|
-
* @param params 参数,必填,如无填空字符串 "";参数类型是json;参数值会进行 URL 编码,最后附加到 QueryString 中
|
|
373
|
-
* @param cfg 配置,可选的
|
|
374
|
-
*/
|
|
375
|
-
function xhr_del(url, cb, params, cfg = DEFAULT_XHR_CFG) {
|
|
376
|
-
getOrDel('delete', url, cb, params, cfg);
|
|
377
|
-
}
|
|
378
|
-
/**
|
|
379
|
-
* XHR POST 请求
|
|
380
|
-
*
|
|
381
|
-
* @param url 请求地址
|
|
382
|
-
* @param cb 回调函数 @example (json: {}, text: string) => void;
|
|
383
|
-
* @param params 参数,必填,如无填空字符串 "";参数类型可以是字符串或 json;参数值会进行 URL 编码
|
|
384
|
-
* @param cfg 配置,可选的
|
|
385
|
-
*/
|
|
386
|
-
function xhr_post(url, cb, params, cfg = DEFAULT_XHR_CFG) {
|
|
387
|
-
postOrPut('post', url, cb, params, cfg);
|
|
388
|
-
}
|
|
389
|
-
/**
|
|
390
|
-
* XHR PUT 请求
|
|
391
|
-
*
|
|
392
|
-
* @param url 请求地址
|
|
393
|
-
* @param cb 回调函数 @example (json: {}, text: string) => void;
|
|
394
|
-
* @param params 参数,必填,如无填空字符串 "";参数类型可以是字符串或 json;参数值会进行 URL 编码
|
|
395
|
-
* @param cfg 配置,可选的
|
|
396
|
-
*/
|
|
397
|
-
function xhr_put(url, cb, params, cfg = DEFAULT_XHR_CFG) {
|
|
398
|
-
postOrPut('put', url, cb, params, cfg);
|
|
236
|
+
fetch(api, {
|
|
237
|
+
method,
|
|
238
|
+
headers: headers,
|
|
239
|
+
body,
|
|
240
|
+
credentials: 'include'
|
|
241
|
+
}).then(response => {
|
|
242
|
+
if (response.status === 404)
|
|
243
|
+
throw new Error('Not found 404: ' + api);
|
|
244
|
+
else if (response.status === 500)
|
|
245
|
+
throw new Error('Server error: ' + api);
|
|
246
|
+
else if (!response.ok)
|
|
247
|
+
throw new Error(`Unexpected status: ${response.status}`);
|
|
248
|
+
return response.json();
|
|
249
|
+
})
|
|
250
|
+
.then(data => {
|
|
251
|
+
if (callback)
|
|
252
|
+
callback(data); // 调用回调
|
|
253
|
+
})
|
|
254
|
+
.catch(error => {
|
|
255
|
+
console.error('Network error when fetching from: ' + api, error); // 网络错误时才会 reject Promise
|
|
256
|
+
});
|
|
399
257
|
}
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
*/
|
|
406
|
-
function initXhr(cfg) {
|
|
407
|
-
let xhr = new XMLHttpRequest();
|
|
408
|
-
if (cfg && cfg.timeout) {
|
|
409
|
-
xhr.timeout = cfg.timeout;
|
|
410
|
-
xhr.ontimeout = (e) => console.error('系统异常,XHR 连接服务超时');
|
|
258
|
+
function json2formParams(params) {
|
|
259
|
+
const pairs = [];
|
|
260
|
+
for (const [key, value] of Object.entries(params)) {
|
|
261
|
+
if (value !== null && value !== undefined && typeof value !== 'function')
|
|
262
|
+
pairs.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`);
|
|
411
263
|
}
|
|
412
|
-
|
|
413
|
-
xhr.withCredentials = true;
|
|
414
|
-
return xhr;
|
|
264
|
+
return pairs.join('&');
|
|
415
265
|
}
|
|
416
266
|
/**
|
|
417
|
-
*
|
|
267
|
+
* HTTP GET 请求
|
|
418
268
|
*
|
|
419
|
-
* @param
|
|
269
|
+
* @param api API 地址
|
|
270
|
+
* @param callback
|
|
271
|
+
* @param header
|
|
420
272
|
*/
|
|
421
|
-
function
|
|
422
|
-
|
|
423
|
-
if (xhr.status <= 400)
|
|
424
|
-
msg = '请求参数错误或者权限不足。';
|
|
425
|
-
else if (xhr.status <= 500)
|
|
426
|
-
msg = '服务端异常。';
|
|
427
|
-
else
|
|
428
|
-
msg = `未知异常,HTTP code:${xhr.status}。`;
|
|
429
|
-
if (!xhr.responseText)
|
|
430
|
-
msg += " 服务端返回空的字符串!";
|
|
431
|
-
console.error(msg, xhr.responseText);
|
|
273
|
+
function get(api, callback, header) {
|
|
274
|
+
request(api, 'GET', null, callback, header);
|
|
432
275
|
}
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
*
|
|
436
|
-
* @param xhr
|
|
437
|
-
* @param cb
|
|
438
|
-
* @param cfg
|
|
439
|
-
*/
|
|
440
|
-
function responseHandle(xhr, cb, cfg) {
|
|
441
|
-
if (xhr.readyState == 4) {
|
|
442
|
-
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {
|
|
443
|
-
let text = xhr.responseText;
|
|
444
|
-
let json;
|
|
445
|
-
if (!text)
|
|
446
|
-
console.warn('服务端没有返回任何字符串');
|
|
447
|
-
switch (cfg.parseContentType) {
|
|
448
|
-
case 'text':
|
|
449
|
-
break;
|
|
450
|
-
case 'xml':
|
|
451
|
-
json = xhr.responseXML;
|
|
452
|
-
break;
|
|
453
|
-
case 'json':
|
|
454
|
-
default:
|
|
455
|
-
try {
|
|
456
|
-
json = JSON.parse(text);
|
|
457
|
-
}
|
|
458
|
-
catch (e) {
|
|
459
|
-
console.error('解析 JSON 时候发生错误,非法 JSON');
|
|
460
|
-
console.warn(e);
|
|
461
|
-
}
|
|
462
|
-
}
|
|
463
|
-
cb && cb(json, text);
|
|
464
|
-
}
|
|
465
|
-
else
|
|
466
|
-
errHandle(xhr);
|
|
467
|
-
}
|
|
276
|
+
function post(api, bodyData, callback, header) {
|
|
277
|
+
request(api, 'POST', bodyData, callback, Object.assign({ 'Content-Type': 'application/json' }, header));
|
|
468
278
|
}
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
*
|
|
472
|
-
* @param {Object} param JSON 对象
|
|
473
|
-
* @returns URL 参数列表
|
|
474
|
-
*/
|
|
475
|
-
function toParams(param) {
|
|
476
|
-
let result = "";
|
|
477
|
-
for (let name in param) {
|
|
478
|
-
if (typeof param[name] != "function")
|
|
479
|
-
result += "&" + name + "=" + encodeURIComponent(param[name]);
|
|
480
|
-
}
|
|
481
|
-
return result.substring(1);
|
|
279
|
+
function postForm(api, bodyData, callback, header) {
|
|
280
|
+
request(api, 'POST', bodyData, callback, Object.assign({ 'Content-Type': 'application/x-www-form-urlencoded' }, header));
|
|
482
281
|
}
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
*
|
|
486
|
-
* @param val
|
|
487
|
-
* @returns
|
|
488
|
-
*/
|
|
489
|
-
function getQuery(val) {
|
|
490
|
-
const w = location.hash.indexOf('?');
|
|
491
|
-
const query = location.hash.substring(w + 1);
|
|
492
|
-
let vars = query.split('&');
|
|
493
|
-
for (let i = 0; i < vars.length; i++) {
|
|
494
|
-
const pair = vars[i].split('=');
|
|
495
|
-
if (pair[0] == val)
|
|
496
|
-
return pair[1];
|
|
497
|
-
}
|
|
498
|
-
return '';
|
|
282
|
+
function put(api, bodyData, callback, header) {
|
|
283
|
+
request(api, 'PUT', bodyData, callback, Object.assign({ 'Content-Type': 'application/json' }, header));
|
|
499
284
|
}
|
|
500
|
-
function
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
callback && callback();
|
|
506
|
-
}
|
|
507
|
-
else
|
|
508
|
-
self.$Message.warning(j.message || '获取数据失败');
|
|
509
|
-
};
|
|
285
|
+
function putForm(api, bodyData, callback, header) {
|
|
286
|
+
request(api, 'PUT', bodyData, callback, Object.assign({ 'Content-Type': 'application/x-www-form-urlencoded' }, header));
|
|
287
|
+
}
|
|
288
|
+
function del(api, callback, header) {
|
|
289
|
+
request(api, 'DELETE', null, callback, header);
|
|
510
290
|
}
|
|
511
291
|
|
|
512
|
-
var
|
|
292
|
+
var xhr_fetch = /*#__PURE__*/Object.freeze({
|
|
513
293
|
__proto__: null,
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
xhr_post: xhr_post,
|
|
521
|
-
xhr_post_upload: xhr_post_upload,
|
|
522
|
-
xhr_put: xhr_put
|
|
294
|
+
del: del,
|
|
295
|
+
get: get,
|
|
296
|
+
post: post,
|
|
297
|
+
postForm: postForm,
|
|
298
|
+
put: put,
|
|
299
|
+
putForm: putForm
|
|
523
300
|
});
|
|
524
301
|
|
|
525
|
-
export {
|
|
302
|
+
export { date_format as DateFormat, utils as Utils, xhr_fetch as Xhr, xhr_fetch as XhrFetch };
|
|
303
|
+
//# sourceMappingURL=index.esm.js.map
|