@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
package/README.md
CHANGED
|
@@ -1,17 +1,66 @@
|
|
|
1
1
|
[](https://www.npmjs.com/package/@ajaxjs/util)
|
|
2
|
-
[](https://vuejs.org)
|
|
3
2
|
[](https://www.typescriptlang.org/)
|
|
4
3
|
[](http://www.apache.org/licenses/LICENSE-2.0.txt)
|
|
5
4
|
[](mailto:frank@ajaxjs.com)
|
|
6
5
|
[](https://shang.qq.com/wpa/qunwpa?idkey=3877893a4ed3a5f0be01e809e7ac120e346102bd550deb6692239bb42de38e22)
|
|
7
6
|
|
|
7
|
+
# AjaxJS JS Utils
|
|
8
|
+
Some common JS functions.
|
|
8
9
|
|
|
9
|
-
|
|
10
|
+
NPM: https://www.npmjs.com/package/@ajaxjs/util
|
|
10
11
|
|
|
11
|
-
A few widgets, vue.js + typescript + less.js
|
|
12
12
|
|
|
13
|
-
NPM
|
|
13
|
+
# How to create a NPM package?
|
|
14
|
+
Use Rollup to build your package.
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
npm init
|
|
18
|
+
npm add typescript -D
|
|
19
|
+
tsc --init
|
|
20
|
+
|
|
21
|
+
npm install rollup -g # 全局安装
|
|
22
|
+
npm install rollup -D # 项目本地安装
|
|
23
|
+
npm install @rollup/plugin-typescript -D # 将Typescript转换成为 ES6+ 标准
|
|
24
|
+
npm install @rollup/plugin-commonjs -D # rollup默认不支持CommonJS,自己写的时候可以尽量避免使用CommonJS模块的语法,但有些外部库的是cjs或者umd(由webpack打包的)。如果使用这些外部库就需要支持CommonJS模块。
|
|
25
|
+
npm install @rollup/plugin-node-resolve -D
|
|
26
|
+
npm init @eslint/config
|
|
27
|
+
npm install -D eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
https://chieminchan.vercel.app/articles/Project%20Management/%E6%89%8B%E6%8A%8A%E6%89%8B%E6%95%99%E4%BD%A0%E5%8F%91%E4%B8%AAnpm%E5%8C%85.html
|
|
31
|
+
|
|
32
|
+
# Rollup Config
|
|
33
|
+
## 1
|
|
34
|
+
|
|
35
|
+
升级到最新版本 rollup: 4.14.3 后 rollup.config.js 配置文件中却不能使用 es6 import 语法了!
|
|
36
|
+
|
|
37
|
+
rollup: 4.14.3 时报错:SyntaxError: Cannot use import statement outside a module
|
|
38
|
+
|
|
39
|
+
解决方法1
|
|
40
|
+
打包命令添加: `--bundleConfigAsCjs`
|
|
41
|
+
修改打包命令: `"dev": "rollup -c rollup.config.js --bundleConfigAsCjs -w"` 重新执行则解决
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
解决方法2
|
|
45
|
+
package.json 中添加:"type": "module",
|
|
46
|
+
这样设置只是支持 es6语法,但 import 语法是不可以引入 .json文件的!
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
链接:https://juejin.cn/post/7359893210797015051
|
|
50
|
+
|
|
51
|
+
## 2
|
|
52
|
+
项目至少包含一个扩展名为 `.ts` 的文件
|
|
53
|
+
|
|
54
|
+
error TS18003: No inputs were found in config file '/Users/jiyik/workspace/ts/tsconfig.json'. Specified 'include' paths were '["src/**/*"]' and 'exclude' paths were '["node_modules"]'
|
|
14
55
|
|
|
15
|
-
|
|
56
|
+
`tsconfig.json` 文件中设置:
|
|
16
57
|
|
|
17
|
-
|
|
58
|
+
```json
|
|
59
|
+
{
|
|
60
|
+
"compilerOptions": {
|
|
61
|
+
// ... 配置项
|
|
62
|
+
},
|
|
63
|
+
"include": ["src/**/*"],
|
|
64
|
+
"exclude": ["node_modules"]
|
|
65
|
+
}
|
|
66
|
+
```
|
|
@@ -0,0 +1,525 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 获取某个 Cookie
|
|
3
|
+
*
|
|
4
|
+
* @param name 键名称
|
|
5
|
+
* @returns 值
|
|
6
|
+
*/
|
|
7
|
+
function getCookie(name) {
|
|
8
|
+
let arr, reg = 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
|
+
* 设置某个 Cookie
|
|
17
|
+
*
|
|
18
|
+
* @param name 键名称
|
|
19
|
+
* @param value 值
|
|
20
|
+
*/
|
|
21
|
+
function setCookie(name, value) {
|
|
22
|
+
let days = 2, exp = new Date();
|
|
23
|
+
exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);
|
|
24
|
+
// @ts-ignore
|
|
25
|
+
document.cookie = name + "=" + escape(value) + ";path=/;expires=" + exp.toGMTString();
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* 清空 Cookie
|
|
29
|
+
*/
|
|
30
|
+
function delCookie() {
|
|
31
|
+
let keys = document.cookie.match(/[^ =;]+(?==)/g);
|
|
32
|
+
if (keys) {
|
|
33
|
+
let date = new Date(0).toUTCString();
|
|
34
|
+
for (let i = keys.length; i--;) {
|
|
35
|
+
document.cookie = keys[i] + '=0;path=/;expires=' + date; // 清除当前域名下的,例如:m.ratingdog.cn
|
|
36
|
+
document.cookie = keys[i] + '=0;path=/;domain=' + document.domain + ';expires=' + date; // 清除当前域名下的,例如 .m.ratingdog.cn
|
|
37
|
+
document.cookie = keys[i] + '=0;path=/;domain=' + location.host + ';expires=' + date; // 清除一级域名下的或指定的,例如 .ratingdog.cn
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
var cookies = /*#__PURE__*/Object.freeze({
|
|
43
|
+
__proto__: null,
|
|
44
|
+
delCookie: delCookie,
|
|
45
|
+
getCookie: getCookie,
|
|
46
|
+
setCookie: setCookie
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 向父级元素递归搜索
|
|
51
|
+
*
|
|
52
|
+
* @param _el 当前所在元素
|
|
53
|
+
* @param tagName 目标标签名称
|
|
54
|
+
* @param className 目标元素样式类
|
|
55
|
+
* @returns 目标元素,找不到为 null
|
|
56
|
+
*/
|
|
57
|
+
function up(_el, tagName, className) {
|
|
58
|
+
if (tagName && className)
|
|
59
|
+
throw '只能任选一种参数,不能同时传';
|
|
60
|
+
let el = _el.parentNode;
|
|
61
|
+
tagName = tagName && tagName.toUpperCase();
|
|
62
|
+
while (el) {
|
|
63
|
+
if (tagName && el.tagName == tagName)
|
|
64
|
+
return el;
|
|
65
|
+
if (className && el.className && ~el.className.indexOf(className))
|
|
66
|
+
return el;
|
|
67
|
+
el = el.parentNode;
|
|
68
|
+
}
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
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
|
+
|
|
88
|
+
var dom = /*#__PURE__*/Object.freeze({
|
|
89
|
+
__proto__: null,
|
|
90
|
+
loadScript: loadScript,
|
|
91
|
+
up: up
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* 通用工具类
|
|
96
|
+
*/
|
|
97
|
+
/**
|
|
98
|
+
* 是否调试模式中
|
|
99
|
+
*
|
|
100
|
+
* 打包成组件之后不能用
|
|
101
|
+
*
|
|
102
|
+
* @returns
|
|
103
|
+
*/
|
|
104
|
+
function isDebug() {
|
|
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) {
|
|
121
|
+
let $1, o = {
|
|
122
|
+
"M+": this.getMonth() + 1, // 月份,从0开始算
|
|
123
|
+
"d+": this.getDate(), // 日期
|
|
124
|
+
"h+": this.getHours(), // 小时
|
|
125
|
+
"m+": this.getMinutes(), // 分钟
|
|
126
|
+
"s+": this.getSeconds(), // 秒钟
|
|
127
|
+
// 季度 quarter
|
|
128
|
+
"q+": Math.floor((this.getMonth() + 3) / 3),
|
|
129
|
+
"S": this.getMilliseconds() // 千秒
|
|
130
|
+
};
|
|
131
|
+
if (/(y+)/.test(format))
|
|
132
|
+
// @ts-ignore
|
|
133
|
+
$1 = RegExp.$1, format = format.replace($1, String(this.getFullYear()).substr(4 - $1));
|
|
134
|
+
let key, value;
|
|
135
|
+
for (key in o) { // 如果没有指定该参数,则子字符串将延续到 stringvar 的最后。
|
|
136
|
+
if (new RegExp("(" + key + ")").test(format)) {
|
|
137
|
+
$1 = RegExp.$1,
|
|
138
|
+
value = String(o[key]),
|
|
139
|
+
value = $1.length == 1 ? value : ("00" + value).substr(value.length),
|
|
140
|
+
format = format.replace($1, value);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return format;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* 日期格式化
|
|
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
|
+
*
|
|
175
|
+
* @author https://segmentfault.com/a/1190000013265925
|
|
176
|
+
* @param arr
|
|
177
|
+
* @param finnaly
|
|
178
|
+
*/
|
|
179
|
+
function parallel(arr, _finally) {
|
|
180
|
+
let fn, index = 0;
|
|
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
|
+
};
|
|
229
|
+
}
|
|
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
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
var utils = /*#__PURE__*/Object.freeze({
|
|
252
|
+
__proto__: null,
|
|
253
|
+
copyToClipboard: copyToClipboard,
|
|
254
|
+
dateFormat: dateFormat,
|
|
255
|
+
dateFormat2: dateFormat2,
|
|
256
|
+
isDebug: isDebug,
|
|
257
|
+
isDev: isDev,
|
|
258
|
+
parallel: parallel,
|
|
259
|
+
throttle: throttle
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* 默认的请求配置
|
|
264
|
+
*/
|
|
265
|
+
const DEFAULT_XHR_CFG = {
|
|
266
|
+
timeout: 5000,
|
|
267
|
+
withCredentials: false,
|
|
268
|
+
parseContentType: 'json'
|
|
269
|
+
};
|
|
270
|
+
/**
|
|
271
|
+
* 全局请求的 head 参数
|
|
272
|
+
*/
|
|
273
|
+
let BASE_HEAD_PARAMS = null;
|
|
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);
|
|
299
|
+
}
|
|
300
|
+
xhr.open(getOrDel.toUpperCase(), url, true);
|
|
301
|
+
xhr.onreadystatechange = function () {
|
|
302
|
+
responseHandle(this, cb, cfg);
|
|
303
|
+
};
|
|
304
|
+
if (BASE_HEAD_PARAMS) // 设置自定义请求头
|
|
305
|
+
for (let key in BASE_HEAD_PARAMS)
|
|
306
|
+
xhr.setRequestHeader(key, BASE_HEAD_PARAMS[key]);
|
|
307
|
+
xhr.send();
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
*
|
|
311
|
+
* @param method
|
|
312
|
+
* @param url
|
|
313
|
+
* @param cb
|
|
314
|
+
* @param params
|
|
315
|
+
* @param cfg
|
|
316
|
+
*/
|
|
317
|
+
function postOrPut(method, url, cb, params, cfg = DEFAULT_XHR_CFG) {
|
|
318
|
+
let xhr = initXhr(cfg);
|
|
319
|
+
xhr.open(method, url, true);
|
|
320
|
+
xhr.onreadystatechange = function () {
|
|
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);
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* 初始化 XHR
|
|
402
|
+
*
|
|
403
|
+
* @param cfg
|
|
404
|
+
* @returns
|
|
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 连接服务超时');
|
|
411
|
+
}
|
|
412
|
+
if (cfg && cfg.withCredentials)
|
|
413
|
+
xhr.withCredentials = true;
|
|
414
|
+
return xhr;
|
|
415
|
+
}
|
|
416
|
+
/**
|
|
417
|
+
* 错误处理
|
|
418
|
+
*
|
|
419
|
+
* @param xhr
|
|
420
|
+
*/
|
|
421
|
+
function errHandle(xhr) {
|
|
422
|
+
let msg;
|
|
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);
|
|
432
|
+
}
|
|
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
|
+
}
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* 对象转换为 URL 参数列表,用 & 分隔
|
|
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);
|
|
482
|
+
}
|
|
483
|
+
/**
|
|
484
|
+
* 获取 QueryString 的某个参数
|
|
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 '';
|
|
499
|
+
}
|
|
500
|
+
function getPageList(self, listArray, callback) {
|
|
501
|
+
return (j) => {
|
|
502
|
+
if (j.status) {
|
|
503
|
+
listArray.total = j.total;
|
|
504
|
+
listArray.data = j.data;
|
|
505
|
+
callback && callback();
|
|
506
|
+
}
|
|
507
|
+
else
|
|
508
|
+
self.$Message.warning(j.message || '获取数据失败');
|
|
509
|
+
};
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
var xhr = /*#__PURE__*/Object.freeze({
|
|
513
|
+
__proto__: null,
|
|
514
|
+
getPageList: getPageList,
|
|
515
|
+
getQuery: getQuery,
|
|
516
|
+
setBaseHeadParams: setBaseHeadParams,
|
|
517
|
+
toParams: toParams,
|
|
518
|
+
xhr_del: xhr_del,
|
|
519
|
+
xhr_get: xhr_get,
|
|
520
|
+
xhr_post: xhr_post,
|
|
521
|
+
xhr_post_upload: xhr_post_upload,
|
|
522
|
+
xhr_put: xhr_put
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
export { cookies as Cookies, dom as Dom, utils as Utils, xhr as Xhr };
|