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