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