@ajaxjs/util 1.1.2 → 1.1.3
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/package.json +6 -4
- package/src/core/cookies.ts +0 -43
- package/src/core/dom.ts +0 -47
- package/src/core/utils.ts +0 -179
- package/src/core/xhr-config.ts +0 -25
- package/src/core/xhr.ts +0 -296
- package/src/index.ts +0 -6
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ajaxjs/util",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.3",
|
|
4
4
|
"description": "Core JS Utils",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"dev": "rollup -w -c",
|
|
@@ -8,15 +8,17 @@
|
|
|
8
8
|
},
|
|
9
9
|
"main": "dist/index.umd.js",
|
|
10
10
|
"module": "dist/index.esm.js",
|
|
11
|
-
"types": "dist/index.d.ts",
|
|
11
|
+
"types": "dist/src/index.d.ts",
|
|
12
12
|
"type": "module",
|
|
13
13
|
"files": [
|
|
14
|
-
"src/",
|
|
15
14
|
"dist/"
|
|
16
15
|
],
|
|
17
16
|
"repository": {
|
|
18
17
|
"type": "git",
|
|
19
|
-
"url": "https://github.com/lightweight-component/js"
|
|
18
|
+
"url": "https://github.com/lightweight-component/js/tree/main/util"
|
|
19
|
+
},
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/lightweight-component/js/issues"
|
|
20
22
|
},
|
|
21
23
|
"keywords": [
|
|
22
24
|
"ajaxjs",
|
package/src/core/cookies.ts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 获取某个 Cookie
|
|
3
|
-
*
|
|
4
|
-
* @param name 键名称
|
|
5
|
-
* @returns 值
|
|
6
|
-
*/
|
|
7
|
-
export function getCookie(name: string): string | null {
|
|
8
|
-
let arr: RegExpMatchArray, reg: RegExp = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
|
|
9
|
-
// @ts-ignore
|
|
10
|
-
if (arr = document.cookie.match(reg))
|
|
11
|
-
return unescape(arr[2]);
|
|
12
|
-
else
|
|
13
|
-
return null;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* 设置某个 Cookie
|
|
18
|
-
*
|
|
19
|
-
* @param name 键名称
|
|
20
|
-
* @param value 值
|
|
21
|
-
*/
|
|
22
|
-
export function setCookie(name: string, value: string): void {
|
|
23
|
-
let days: number = 2, exp: Date = new Date();
|
|
24
|
-
exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);
|
|
25
|
-
// @ts-ignore
|
|
26
|
-
document.cookie = name + "=" + escape(value) + ";path=/;expires=" + exp.toGMTString();
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* 清空 Cookie
|
|
31
|
-
*/
|
|
32
|
-
export function delCookie(): void {
|
|
33
|
-
let keys = document.cookie.match(/[^ =;]+(?==)/g);
|
|
34
|
-
|
|
35
|
-
if (keys) {
|
|
36
|
-
let date: string = new Date(0).toUTCString();
|
|
37
|
-
for (let i = keys.length; i--;) {
|
|
38
|
-
document.cookie = keys[i] + '=0;path=/;expires=' + date; // 清除当前域名下的,例如:m.ratingdog.cn
|
|
39
|
-
document.cookie = keys[i] + '=0;path=/;domain=' + document.domain + ';expires=' + date; // 清除当前域名下的,例如 .m.ratingdog.cn
|
|
40
|
-
document.cookie = keys[i] + '=0;path=/;domain=' + location.host + ';expires=' + date; // 清除一级域名下的或指定的,例如 .ratingdog.cn
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
package/src/core/dom.ts
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 向父级元素递归搜索
|
|
3
|
-
*
|
|
4
|
-
* @param _el 当前所在元素
|
|
5
|
-
* @param tagName 目标标签名称
|
|
6
|
-
* @param className 目标元素样式类
|
|
7
|
-
* @returns 目标元素,找不到为 null
|
|
8
|
-
*/
|
|
9
|
-
export function up(_el: Element, tagName: string, className: string): Element | null {
|
|
10
|
-
if (tagName && className)
|
|
11
|
-
throw '只能任选一种参数,不能同时传';
|
|
12
|
-
|
|
13
|
-
let el: Element = _el.parentNode as Element;
|
|
14
|
-
tagName = tagName && tagName.toUpperCase();
|
|
15
|
-
|
|
16
|
-
while (el) {
|
|
17
|
-
if (tagName && el.tagName == tagName)
|
|
18
|
-
return el;
|
|
19
|
-
|
|
20
|
-
if (className && el.className && ~el.className.indexOf(className))
|
|
21
|
-
return el;
|
|
22
|
-
|
|
23
|
-
el = <Element>el.parentNode;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
return null;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* 加载脚本
|
|
31
|
-
*
|
|
32
|
-
* @param url 脚本地址
|
|
33
|
-
* @param id 脚本元素 id,可选的
|
|
34
|
-
* @param cb 回调函数,可选的
|
|
35
|
-
*/
|
|
36
|
-
export function loadScript(url: string, id?: string, cb?: (ev: Event) => any): void {
|
|
37
|
-
let script: HTMLScriptElement = document.createElement("script");
|
|
38
|
-
script.src = url;
|
|
39
|
-
|
|
40
|
-
if (cb)
|
|
41
|
-
script.onload = cb;
|
|
42
|
-
|
|
43
|
-
if (id)
|
|
44
|
-
script.id = id;
|
|
45
|
-
|
|
46
|
-
document.getElementsByTagName("head")[0].appendChild(script);
|
|
47
|
-
}
|
package/src/core/utils.ts
DELETED
|
@@ -1,179 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 通用工具类
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* 是否调试模式中
|
|
9
|
-
*
|
|
10
|
-
* 打包成组件之后不能用
|
|
11
|
-
*
|
|
12
|
-
* @returns
|
|
13
|
-
*/
|
|
14
|
-
export function isDebug(): boolean {
|
|
15
|
-
// @ts-ignore
|
|
16
|
-
return process.env.NODE_ENV === 'development';
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function isDev(): boolean {
|
|
20
|
-
let currentHostname: string = window.location.hostname;
|
|
21
|
-
|
|
22
|
-
// 判断主机名是否是内网地址
|
|
23
|
-
return (currentHostname.startsWith('192.168.') || currentHostname.startsWith('10.') || currentHostname === 'localhost');
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* 日期格式化。详见博客文章:http://blog.csdn.net/zhangxin09/archive/2011/01/01/6111294.aspx
|
|
28
|
-
* e.g: new Date().format("yyyy-MM-dd hh:mm:ss")
|
|
29
|
-
*
|
|
30
|
-
* @param {String} format
|
|
31
|
-
* @return {String}
|
|
32
|
-
*/
|
|
33
|
-
export function dateFormat(this: Date, format: string): string {
|
|
34
|
-
let $1, o: any = {
|
|
35
|
-
"M+": this.getMonth() + 1, // 月份,从0开始算
|
|
36
|
-
"d+": this.getDate(), // 日期
|
|
37
|
-
"h+": this.getHours(), // 小时
|
|
38
|
-
"m+": this.getMinutes(), // 分钟
|
|
39
|
-
"s+": this.getSeconds(), // 秒钟
|
|
40
|
-
// 季度 quarter
|
|
41
|
-
"q+": Math.floor((this.getMonth() + 3) / 3),
|
|
42
|
-
"S": this.getMilliseconds() // 千秒
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
if (/(y+)/.test(format))
|
|
46
|
-
// @ts-ignore
|
|
47
|
-
$1 = RegExp.$1, format = format.replace($1, String(this.getFullYear()).substr(4 - $1));
|
|
48
|
-
|
|
49
|
-
let key: string, value: string;
|
|
50
|
-
for (key in o) { // 如果没有指定该参数,则子字符串将延续到 stringvar 的最后。
|
|
51
|
-
if (new RegExp("(" + key + ")").test(format)) {
|
|
52
|
-
$1 = RegExp.$1,
|
|
53
|
-
value = String(o[key]),
|
|
54
|
-
value = $1.length == 1 ? value : ("00" + value).substr(value.length),
|
|
55
|
-
format = format.replace($1, value);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return format;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* 日期格式化
|
|
64
|
-
* @author meizz
|
|
65
|
-
* @param date 日期,必须为 Date 类型
|
|
66
|
-
* @param fmt 格式模板
|
|
67
|
-
* @returns 格式化后的字符串
|
|
68
|
-
*/
|
|
69
|
-
export function dateFormat2(date: Date, fmt: string): string {
|
|
70
|
-
let o: { [key: string]: number } = {
|
|
71
|
-
"M+": date.getMonth() + 1, // 月份
|
|
72
|
-
"d+": date.getDate(), // 日
|
|
73
|
-
"h+": date.getHours(), // 小时
|
|
74
|
-
"m+": date.getMinutes(), // 分
|
|
75
|
-
"s+": date.getSeconds(), // 秒
|
|
76
|
-
"q+": Math.floor((date.getMonth() + 3) / 3),// 季度
|
|
77
|
-
"S": date.getMilliseconds() // 毫秒
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
if (/(y+)/.test(fmt))
|
|
81
|
-
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
|
|
82
|
-
|
|
83
|
-
for (var k in o)
|
|
84
|
-
if (new RegExp("(" + k + ")").test(fmt)) {
|
|
85
|
-
let obj = (RegExp.$1.length == 1) ? o[k] : ("00" + o[k]).substr(("" + o[k]).length);
|
|
86
|
-
// @ts-ignore
|
|
87
|
-
fmt = fmt.replace(RegExp.$1, obj);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
return fmt;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* 并行和串行任务
|
|
95
|
-
*
|
|
96
|
-
* @author https://segmentfault.com/a/1190000013265925
|
|
97
|
-
* @param arr
|
|
98
|
-
* @param finnaly
|
|
99
|
-
*/
|
|
100
|
-
export function parallel(arr: [], _finally: Function) {
|
|
101
|
-
let fn: Function, index: number = 0;
|
|
102
|
-
// @ts-ignore
|
|
103
|
-
let statusArr = Array(arr.length).fill().map(() => ({ isActive: false, data: null }));
|
|
104
|
-
|
|
105
|
-
let isFinished = function () {
|
|
106
|
-
return statusArr.every((item: any) => item.isActive === true);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
let resolve = function (index: number): Function {
|
|
110
|
-
return function (data: any) {
|
|
111
|
-
statusArr[index].data = data;
|
|
112
|
-
statusArr[index].isActive = true;
|
|
113
|
-
let isFinish = isFinished();
|
|
114
|
-
|
|
115
|
-
if (isFinish) {
|
|
116
|
-
let datas = statusArr.map((item: any) => item.data);
|
|
117
|
-
|
|
118
|
-
_finally(datas);
|
|
119
|
-
}
|
|
120
|
-
};
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
// @ts-ignore
|
|
124
|
-
while ((fn = arr.shift())) {
|
|
125
|
-
fn(resolve(index));// 给 resolve 函数追加参数,可以使用 bind 函数实现,这里使用了柯里化
|
|
126
|
-
index++;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* 函数节流
|
|
132
|
-
*
|
|
133
|
-
* @author https://www.cnblogs.com/moqiutao/p/6875955.html
|
|
134
|
-
* @param fn
|
|
135
|
-
* @param delay
|
|
136
|
-
* @param mustRunDelay
|
|
137
|
-
*/
|
|
138
|
-
export function throttle(fn: Function, delay: number, mustRunDelay: number): Function {
|
|
139
|
-
var timer: number, t_start: number;
|
|
140
|
-
|
|
141
|
-
return function () {
|
|
142
|
-
var t_curr = +new Date();
|
|
143
|
-
window.clearTimeout(timer);
|
|
144
|
-
|
|
145
|
-
if (!t_start)
|
|
146
|
-
t_start = t_curr;
|
|
147
|
-
|
|
148
|
-
if (t_curr - t_start >= mustRunDelay) {
|
|
149
|
-
// @ts-ignore
|
|
150
|
-
fn.apply(this, arguments);
|
|
151
|
-
t_start = t_curr;
|
|
152
|
-
} else {
|
|
153
|
-
var args = arguments;
|
|
154
|
-
// @ts-ignore
|
|
155
|
-
timer = window.setTimeout(() => fn.apply(this, args), delay);
|
|
156
|
-
}
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* 复制文字到剪切板
|
|
162
|
-
*
|
|
163
|
-
* @param {string} text
|
|
164
|
-
*/
|
|
165
|
-
export function copyToClipboard(text: string): void {
|
|
166
|
-
if (navigator.clipboard)
|
|
167
|
-
navigator.clipboard.writeText(text); // clipboard api 复制
|
|
168
|
-
else {
|
|
169
|
-
let textarea = document.createElement('textarea');
|
|
170
|
-
document.body.appendChild(textarea); // 隐藏此输入框
|
|
171
|
-
textarea.style.position = 'fixed';
|
|
172
|
-
textarea.style.clip = 'rect(0 0 0 0)';
|
|
173
|
-
textarea.style.top = '10px';
|
|
174
|
-
textarea.value = text; // 赋值
|
|
175
|
-
textarea.select(); // 选中
|
|
176
|
-
document.execCommand('copy', true); // 复制
|
|
177
|
-
document.body.removeChild(textarea); // 移除输入框
|
|
178
|
-
}
|
|
179
|
-
}
|
package/src/core/xhr-config.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* XHR 请求配置
|
|
3
|
-
*/
|
|
4
|
-
export interface XhrConfig {
|
|
5
|
-
/**
|
|
6
|
-
* 请求头里面的 Content-Type 字段
|
|
7
|
-
*/
|
|
8
|
-
contentType?: string;
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* XHR 响应结果的类型
|
|
12
|
-
*/
|
|
13
|
-
parseContentType?: 'text' | 'xml' | 'json';
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* 超时时间,单位是毫秒
|
|
17
|
-
* 设为 0 适合不控制超时
|
|
18
|
-
*/
|
|
19
|
-
timeout?: number;
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* 是否跨域
|
|
23
|
-
*/
|
|
24
|
-
withCredentials?: boolean;
|
|
25
|
-
}
|
package/src/core/xhr.ts
DELETED
|
@@ -1,296 +0,0 @@
|
|
|
1
|
-
import { XhrConfig } from './xhr-config';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 默认的请求配置
|
|
5
|
-
*/
|
|
6
|
-
const DEFAULT_XHR_CFG: XhrConfig = {
|
|
7
|
-
timeout: 5000,
|
|
8
|
-
withCredentials: false,
|
|
9
|
-
parseContentType: 'json'
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* 处理响应的回调函数
|
|
14
|
-
*/
|
|
15
|
-
type XhrCallback = (json: {}, text: string) => void;
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* 全局请求的 head 参数
|
|
19
|
-
*/
|
|
20
|
-
let BASE_HEAD_PARAMS: { [key: string]: any } | null = null;
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* 设置全局请求的 head 参数
|
|
24
|
-
*
|
|
25
|
-
* @param param
|
|
26
|
-
*/
|
|
27
|
-
export function setBaseHeadParams(params: any): void {
|
|
28
|
-
if (BASE_HEAD_PARAMS === null)
|
|
29
|
-
BASE_HEAD_PARAMS = {};
|
|
30
|
-
|
|
31
|
-
Object.assign(BASE_HEAD_PARAMS, params);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
*
|
|
36
|
-
* @param getOrDel
|
|
37
|
-
* @param url
|
|
38
|
-
* @param cb
|
|
39
|
-
* @param params
|
|
40
|
-
* @param cfg
|
|
41
|
-
*/
|
|
42
|
-
function getOrDel(getOrDel: 'get' | 'delete', url: string, cb: XhrCallback, params?: {}, cfg: XhrConfig = DEFAULT_XHR_CFG): void {
|
|
43
|
-
let xhr: XMLHttpRequest = initXhr(cfg);
|
|
44
|
-
|
|
45
|
-
if (params != null) {
|
|
46
|
-
if (url.indexOf('?') != -1)
|
|
47
|
-
url += '&' + toParams(params);
|
|
48
|
-
else
|
|
49
|
-
url += '?' + toParams(params);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
xhr.open(getOrDel.toUpperCase(), url, true);
|
|
53
|
-
xhr.onreadystatechange = function () {
|
|
54
|
-
responseHandle(this, cb, cfg);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
if (BASE_HEAD_PARAMS)// 设置自定义请求头
|
|
58
|
-
for (let key in BASE_HEAD_PARAMS)
|
|
59
|
-
xhr.setRequestHeader(key, BASE_HEAD_PARAMS[key]);
|
|
60
|
-
|
|
61
|
-
xhr.send();
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
*
|
|
66
|
-
* @param method
|
|
67
|
-
* @param url
|
|
68
|
-
* @param cb
|
|
69
|
-
* @param params
|
|
70
|
-
* @param cfg
|
|
71
|
-
*/
|
|
72
|
-
function postOrPut(method: 'post' | 'put', url: string, cb: XhrCallback, params: string | {}, cfg: XhrConfig = DEFAULT_XHR_CFG): void {
|
|
73
|
-
let xhr: XMLHttpRequest = initXhr(cfg);
|
|
74
|
-
xhr.open(method, url, true);
|
|
75
|
-
xhr.onreadystatechange = function () {
|
|
76
|
-
responseHandle(this, cb, cfg);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
if (BASE_HEAD_PARAMS) // 设置自定义请求头
|
|
80
|
-
for (let key in BASE_HEAD_PARAMS)
|
|
81
|
-
xhr.setRequestHeader(key, BASE_HEAD_PARAMS[key]);
|
|
82
|
-
|
|
83
|
-
// 此方法必须在 open() 方法和 send() 之间调用
|
|
84
|
-
|
|
85
|
-
if (!cfg.contentType) // 如未设置,默认为表单请求
|
|
86
|
-
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
87
|
-
else
|
|
88
|
-
xhr.setRequestHeader("Content-Type", cfg.contentType);
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
let _params: string = typeof params != 'string' ? toParams(params) : <string>params;
|
|
92
|
-
|
|
93
|
-
if (_params)
|
|
94
|
-
xhr.send(_params);
|
|
95
|
-
else
|
|
96
|
-
xhr.send();
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
*
|
|
101
|
-
* @param url
|
|
102
|
-
* @param cb
|
|
103
|
-
* @param params
|
|
104
|
-
* @param cfg
|
|
105
|
-
*/
|
|
106
|
-
export function xhr_post_upload(url: string, cb: XhrCallback, params: Document | XMLHttpRequestBodyInit, cfg: XhrConfig = DEFAULT_XHR_CFG): void {
|
|
107
|
-
let xhr: XMLHttpRequest = initXhr(cfg);
|
|
108
|
-
xhr.open('post', url, true);
|
|
109
|
-
xhr.onreadystatechange = function () {
|
|
110
|
-
responseHandle(this, cb, cfg);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
if (BASE_HEAD_PARAMS) // 设置自定义请求头
|
|
114
|
-
for (let key in BASE_HEAD_PARAMS)
|
|
115
|
-
xhr.setRequestHeader(key, BASE_HEAD_PARAMS[key]);
|
|
116
|
-
|
|
117
|
-
// 什么 Content-Type 都不设置
|
|
118
|
-
|
|
119
|
-
xhr.send(params);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* XHR GET 请求
|
|
124
|
-
*
|
|
125
|
-
* @param url 请求地址
|
|
126
|
-
* @param cb 回调函数 @example (json: {}, text: string) => void;
|
|
127
|
-
* @param params 参数,必填,如无填空字符串 "";参数类型是json;参数值会进行 URL 编码,最后附加到 QueryString 中
|
|
128
|
-
* @param cfg 配置,可选的
|
|
129
|
-
*/
|
|
130
|
-
export function xhr_get(url: string, cb: XhrCallback, params?: {}, cfg: XhrConfig = DEFAULT_XHR_CFG): void {
|
|
131
|
-
getOrDel('get', url, cb, params, cfg);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* XHR DELETE 请求
|
|
136
|
-
*
|
|
137
|
-
* @param url 请求地址
|
|
138
|
-
* @param cb 回调函数 @example (json: {}, text: string) => void;
|
|
139
|
-
* @param params 参数,必填,如无填空字符串 "";参数类型是json;参数值会进行 URL 编码,最后附加到 QueryString 中
|
|
140
|
-
* @param cfg 配置,可选的
|
|
141
|
-
*/
|
|
142
|
-
export function xhr_del(url: string, cb: XhrCallback, params?: {}, cfg: XhrConfig = DEFAULT_XHR_CFG): void {
|
|
143
|
-
getOrDel('delete', url, cb, params, cfg);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* XHR POST 请求
|
|
148
|
-
*
|
|
149
|
-
* @param url 请求地址
|
|
150
|
-
* @param cb 回调函数 @example (json: {}, text: string) => void;
|
|
151
|
-
* @param params 参数,必填,如无填空字符串 "";参数类型可以是字符串或 json;参数值会进行 URL 编码
|
|
152
|
-
* @param cfg 配置,可选的
|
|
153
|
-
*/
|
|
154
|
-
export function xhr_post(url: string, cb: XhrCallback, params: string | {}, cfg: XhrConfig = DEFAULT_XHR_CFG): void {
|
|
155
|
-
postOrPut('post', url, cb, params, cfg);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* XHR PUT 请求
|
|
160
|
-
*
|
|
161
|
-
* @param url 请求地址
|
|
162
|
-
* @param cb 回调函数 @example (json: {}, text: string) => void;
|
|
163
|
-
* @param params 参数,必填,如无填空字符串 "";参数类型可以是字符串或 json;参数值会进行 URL 编码
|
|
164
|
-
* @param cfg 配置,可选的
|
|
165
|
-
*/
|
|
166
|
-
export function xhr_put(url: string, cb: XhrCallback, params: string | {}, cfg: XhrConfig = DEFAULT_XHR_CFG): void {
|
|
167
|
-
postOrPut('put', url, cb, params, cfg);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* 初始化 XHR
|
|
172
|
-
*
|
|
173
|
-
* @param cfg
|
|
174
|
-
* @returns
|
|
175
|
-
*/
|
|
176
|
-
function initXhr(cfg: XhrConfig): XMLHttpRequest {
|
|
177
|
-
let xhr: XMLHttpRequest = new XMLHttpRequest();
|
|
178
|
-
|
|
179
|
-
if (cfg && cfg.timeout) {
|
|
180
|
-
xhr.timeout = cfg.timeout;
|
|
181
|
-
xhr.ontimeout = (e: ProgressEvent<EventTarget>) => console.error('系统异常,XHR 连接服务超时');
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
if (cfg && cfg.withCredentials)
|
|
185
|
-
xhr.withCredentials = true;
|
|
186
|
-
|
|
187
|
-
return xhr;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* 错误处理
|
|
192
|
-
*
|
|
193
|
-
* @param xhr
|
|
194
|
-
*/
|
|
195
|
-
function errHandle(xhr: XMLHttpRequest): void {
|
|
196
|
-
let msg: string;
|
|
197
|
-
|
|
198
|
-
if (xhr.status <= 400)
|
|
199
|
-
msg = '请求参数错误或者权限不足。';
|
|
200
|
-
else if (xhr.status <= 500)
|
|
201
|
-
msg = '服务端异常。';
|
|
202
|
-
else
|
|
203
|
-
msg = `未知异常,HTTP code:${xhr.status}。`;
|
|
204
|
-
|
|
205
|
-
if (!xhr.responseText)
|
|
206
|
-
msg += " 服务端返回空的字符串!";
|
|
207
|
-
|
|
208
|
-
console.error(msg, xhr.responseText);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* 响应处理
|
|
213
|
-
*
|
|
214
|
-
* @param xhr
|
|
215
|
-
* @param cb
|
|
216
|
-
* @param cfg
|
|
217
|
-
*/
|
|
218
|
-
function responseHandle(xhr: XMLHttpRequest, cb: XhrCallback, cfg: XhrConfig): void {
|
|
219
|
-
if (xhr.readyState == 4) {
|
|
220
|
-
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {
|
|
221
|
-
let text: string = xhr.responseText;
|
|
222
|
-
let json: any;
|
|
223
|
-
|
|
224
|
-
if (!text)
|
|
225
|
-
console.warn('服务端没有返回任何字符串');
|
|
226
|
-
|
|
227
|
-
switch (cfg.parseContentType) {
|
|
228
|
-
case 'text':
|
|
229
|
-
break;
|
|
230
|
-
case 'xml':
|
|
231
|
-
json = xhr.responseXML;
|
|
232
|
-
break;
|
|
233
|
-
case 'json':
|
|
234
|
-
default:
|
|
235
|
-
try {
|
|
236
|
-
json = JSON.parse(text);
|
|
237
|
-
} catch (e) {
|
|
238
|
-
console.error('解析 JSON 时候发生错误,非法 JSON');
|
|
239
|
-
console.warn(e);
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
cb && cb(json, text);
|
|
244
|
-
} else errHandle(xhr);
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
/**
|
|
249
|
-
* 对象转换为 URL 参数列表,用 & 分隔
|
|
250
|
-
*
|
|
251
|
-
* @param {Object} param JSON 对象
|
|
252
|
-
* @returns URL 参数列表
|
|
253
|
-
*/
|
|
254
|
-
export function toParams(param: any): string {
|
|
255
|
-
let result: string = "";
|
|
256
|
-
|
|
257
|
-
for (let name in param) {
|
|
258
|
-
if (typeof param[name] != "function")
|
|
259
|
-
result += "&" + name + "=" + encodeURIComponent(param[name]);
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
return result.substring(1);
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
/**
|
|
266
|
-
* 获取 QueryString 的某个参数
|
|
267
|
-
*
|
|
268
|
-
* @param val
|
|
269
|
-
* @returns
|
|
270
|
-
*/
|
|
271
|
-
export function getQuery(val: string): string {
|
|
272
|
-
const w: number = location.hash.indexOf('?');
|
|
273
|
-
const query: string = location.hash.substring(w + 1);
|
|
274
|
-
let vars: string[] = query.split('&');
|
|
275
|
-
|
|
276
|
-
for (let i = 0; i < vars.length; i++) {
|
|
277
|
-
const pair = vars[i].split('=');
|
|
278
|
-
|
|
279
|
-
if (pair[0] == val)
|
|
280
|
-
return pair[1];
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
return '';
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
export function getPageList(self: any, listArray: any, callback?: Function): XhrCallback {
|
|
287
|
-
return (j: any) => {
|
|
288
|
-
if (j.status) {
|
|
289
|
-
listArray.total = j.total;
|
|
290
|
-
listArray.data = j.data;
|
|
291
|
-
|
|
292
|
-
callback && callback();
|
|
293
|
-
} else
|
|
294
|
-
self.$Message.warning(j.message || '获取数据失败');
|
|
295
|
-
}
|
|
296
|
-
}
|