nvue3 1.1.16 → 1.1.18
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 +3 -3
- package/src/http.js +157 -32
- package/src/kernel.js +9 -5
- package/src/soft.js +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nvue3",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.18",
|
|
4
4
|
"description": "for uniapp & vue3",
|
|
5
5
|
"main": "src/kernel.js",
|
|
6
6
|
"scripts": {
|
|
@@ -9,9 +9,9 @@
|
|
|
9
9
|
"author": "",
|
|
10
10
|
"license": "ISC",
|
|
11
11
|
"dependencies": {
|
|
12
|
-
"nodejs_patch": "^1.0.
|
|
12
|
+
"nodejs_patch": "^1.0.17"
|
|
13
13
|
},
|
|
14
14
|
"devDependencies": {
|
|
15
|
-
"nodejs_patch": "^1.0.
|
|
15
|
+
"nodejs_patch": "^1.0.17"
|
|
16
16
|
}
|
|
17
17
|
}
|
package/src/http.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
const tstShow = 350;
|
|
5
5
|
const isDebug = (process.env.NODE_ENV === 'development');
|
|
6
6
|
// const isDebug = true;
|
|
7
|
+
const nextPost = [];
|
|
7
8
|
|
|
8
9
|
const baseResp = {
|
|
9
10
|
success: 1,
|
|
@@ -12,23 +13,23 @@ const baseResp = {
|
|
|
12
13
|
data: {}
|
|
13
14
|
};
|
|
14
15
|
|
|
16
|
+
const toastConf = {
|
|
17
|
+
'l': '加载中...',
|
|
18
|
+
'r': '读取中...',
|
|
19
|
+
'f': '正在刷新...',
|
|
20
|
+
's': '保存中...',
|
|
21
|
+
'd': '删除中...',
|
|
22
|
+
'm': '发送短信...',
|
|
23
|
+
'p': '请求支付...',
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const aliasConf = {};
|
|
27
|
+
|
|
15
28
|
const config = {
|
|
16
|
-
path: '',
|
|
17
|
-
|
|
18
|
-
modal: false, //出错时显示model,否则显示toast
|
|
19
|
-
timeout: 6000,
|
|
29
|
+
path: '', //API根目录
|
|
30
|
+
timeout: 6000, //请求最大耗时
|
|
20
31
|
mintime: 100, //两次modal或toast之间间隔最短时间,小于此时间的不显示
|
|
21
32
|
detection: 0, //检测相同API连续请求间隔,小于此时间的报错,0不检测
|
|
22
|
-
toast: {
|
|
23
|
-
'l': '加载中...',
|
|
24
|
-
'r': '读取中...',
|
|
25
|
-
'f': '正在刷新...',
|
|
26
|
-
's': '保存中...',
|
|
27
|
-
'd': '删除中...',
|
|
28
|
-
'm': '发送短信...',
|
|
29
|
-
'p': '请求支付...',
|
|
30
|
-
},
|
|
31
|
-
alias: {}
|
|
32
33
|
};
|
|
33
34
|
|
|
34
35
|
let processor;
|
|
@@ -50,6 +51,11 @@ const _request = class {
|
|
|
50
51
|
throw new Error('api 请求目标必须是string类型地址,且必填');
|
|
51
52
|
}
|
|
52
53
|
|
|
54
|
+
if ((nextPost.length > 0) && (this.method === 'POST')) {
|
|
55
|
+
data._append = JSON.parse(JSON.stringify(nextPost));
|
|
56
|
+
nextPost.length = 0;
|
|
57
|
+
}
|
|
58
|
+
|
|
53
59
|
let host = config.path;
|
|
54
60
|
|
|
55
61
|
if (uri[0] === '!') { //uri第1个字符为!,静默处理
|
|
@@ -69,7 +75,7 @@ const _request = class {
|
|
|
69
75
|
}
|
|
70
76
|
|
|
71
77
|
if (uri[0] !== '/') { //uri第1个字符为toast,提取出来
|
|
72
|
-
this.toast =
|
|
78
|
+
this.toast = toastConf[uri[0]] || '';
|
|
73
79
|
uri = uri.slice(1);
|
|
74
80
|
}
|
|
75
81
|
|
|
@@ -111,14 +117,14 @@ function apiAlias(api) {
|
|
|
111
117
|
let a = `/${uri[1]}`;
|
|
112
118
|
let b = `/${uri[1]}/${uri[2]}`;
|
|
113
119
|
|
|
114
|
-
if (
|
|
120
|
+
if (aliasConf[b]) {
|
|
115
121
|
let append = uri.slice(3).join('/');
|
|
116
|
-
return `${
|
|
122
|
+
return `${aliasConf[b]}/${append}`;
|
|
117
123
|
}
|
|
118
124
|
|
|
119
|
-
if (
|
|
125
|
+
if (aliasConf[a]) {
|
|
120
126
|
let append = uri.slice(2).join('/');
|
|
121
|
-
return `${
|
|
127
|
+
return `${aliasConf[a]}/${append}`;
|
|
122
128
|
}
|
|
123
129
|
|
|
124
130
|
return api;
|
|
@@ -326,11 +332,11 @@ function doComplete(request, res, resolve, reject) {
|
|
|
326
332
|
}
|
|
327
333
|
}
|
|
328
334
|
|
|
329
|
-
|
|
335
|
+
console.log(request)
|
|
330
336
|
}
|
|
331
337
|
|
|
332
338
|
|
|
333
|
-
function doRequest(request) {
|
|
339
|
+
async function doRequest(request) {
|
|
334
340
|
|
|
335
341
|
if (request.toast) {
|
|
336
342
|
request.loading = true;
|
|
@@ -340,15 +346,20 @@ function doRequest(request) {
|
|
|
340
346
|
});
|
|
341
347
|
}
|
|
342
348
|
|
|
349
|
+
|
|
343
350
|
return new Promise(async (resolve, reject) => {
|
|
344
351
|
request.timer.ready = Date.now();
|
|
352
|
+
console.log('======');
|
|
345
353
|
request.header.put = await processor.header(request.api, request.request, request.method);
|
|
354
|
+
console.log(request);
|
|
346
355
|
|
|
347
356
|
const contType = (request.method === 'UPLOAD') ? 'multipart/form-data' : 'application/json';
|
|
348
357
|
request.header.put['content-type'] = contType;
|
|
349
358
|
delete request.header.put['referer'];
|
|
350
359
|
|
|
351
360
|
request.timer.before = Date.now();
|
|
361
|
+
|
|
362
|
+
|
|
352
363
|
uni.request({
|
|
353
364
|
url: request.api,
|
|
354
365
|
method: request.method,
|
|
@@ -357,14 +368,17 @@ function doRequest(request) {
|
|
|
357
368
|
data: request.request,
|
|
358
369
|
header: request.header.put,
|
|
359
370
|
success: (res) => {
|
|
371
|
+
console.log('http success', res);
|
|
360
372
|
request.timer.after = Date.now();
|
|
361
373
|
doSuccess(request, res, resolve, reject);
|
|
362
374
|
},
|
|
363
375
|
fail: (res) => {
|
|
376
|
+
console.log('http fail', res);
|
|
364
377
|
request.timer.after = Date.now();
|
|
365
378
|
doFail(request, res, resolve, reject);
|
|
366
379
|
},
|
|
367
380
|
complete: (res) => {
|
|
381
|
+
console.log('http complete', res);
|
|
368
382
|
doComplete(request, res, resolve, reject);
|
|
369
383
|
}
|
|
370
384
|
});
|
|
@@ -389,7 +403,7 @@ function thisPost(api, data = {}) {
|
|
|
389
403
|
* save=`s/upload/save/`
|
|
390
404
|
*
|
|
391
405
|
*/
|
|
392
|
-
function doUploadAliYun(uri, option) {
|
|
406
|
+
async function doUploadAliYun(uri, option) {
|
|
393
407
|
|
|
394
408
|
return new Promise(async (resolve, reject) => {
|
|
395
409
|
let { file, mime, used, source } = option;
|
|
@@ -472,7 +486,7 @@ function doUploadAliYun(uri, option) {
|
|
|
472
486
|
* 操作频率检查
|
|
473
487
|
* @param {Object} api
|
|
474
488
|
*/
|
|
475
|
-
function FrequencyDetection(api) {
|
|
489
|
+
async function FrequencyDetection(api) {
|
|
476
490
|
|
|
477
491
|
if (api[0] === '@') return null; //不检查频率
|
|
478
492
|
|
|
@@ -494,36 +508,147 @@ function FrequencyDetection(api) {
|
|
|
494
508
|
}
|
|
495
509
|
|
|
496
510
|
|
|
511
|
+
|
|
512
|
+
async function downloadFile(url, callback) {
|
|
513
|
+
|
|
514
|
+
const key = url.md5();
|
|
515
|
+
|
|
516
|
+
async function download_file(path) {
|
|
517
|
+
return new Promise((resolve, reject) => {
|
|
518
|
+
|
|
519
|
+
const task = uni.downloadFile({
|
|
520
|
+
url: path,
|
|
521
|
+
success: (res) => {
|
|
522
|
+
if (res.statusCode === 200) {
|
|
523
|
+
console.log('下载成功', res);
|
|
524
|
+
//#ifdef H5
|
|
525
|
+
uni.setStorageSync(key, { file: res.tempFilePath });
|
|
526
|
+
resolve({ success: true, file: res.tempFilePath })
|
|
527
|
+
//#endif
|
|
528
|
+
|
|
529
|
+
//#ifndef H5
|
|
530
|
+
uni.saveFile({
|
|
531
|
+
tempFilePath: res.tempFilePath,
|
|
532
|
+
success: (file) => {
|
|
533
|
+
console.log('保存成功', file);
|
|
534
|
+
uni.setStorageSync(key, { file: file.savedFilePath });
|
|
535
|
+
resolve({ success: true, file: file.savedFilePath })
|
|
536
|
+
},
|
|
537
|
+
fail: err => {
|
|
538
|
+
resolve({ success: false, message: err.errMsg })
|
|
539
|
+
}
|
|
540
|
+
});
|
|
541
|
+
//#endif
|
|
542
|
+
}
|
|
543
|
+
else {
|
|
544
|
+
console.log('download null', { res })
|
|
545
|
+
resolve({ success: false, message: '文件不存在' })
|
|
546
|
+
}
|
|
547
|
+
},
|
|
548
|
+
fail: err => {
|
|
549
|
+
console.log('download error', { err })
|
|
550
|
+
resolve({ success: false, message: err.errMsg })
|
|
551
|
+
}
|
|
552
|
+
});
|
|
553
|
+
|
|
554
|
+
task.onProgressUpdate((res) => {
|
|
555
|
+
// console.log('下载进度', res.progress, progress);
|
|
556
|
+
// console.log('已经下载的数据长度', res.totalBytesWritten);
|
|
557
|
+
// console.log('预期需要下载的数据总长度', res.totalBytesExpectedToWrite);
|
|
558
|
+
// 满足测试条件,取消下载任务。
|
|
559
|
+
// if (res.progress > 50000) { task.abort(); }
|
|
560
|
+
|
|
561
|
+
if (typeof callback === 'function') callback(task, res);
|
|
562
|
+
|
|
563
|
+
});
|
|
564
|
+
|
|
565
|
+
|
|
566
|
+
|
|
567
|
+
});
|
|
568
|
+
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
|
|
572
|
+
return new Promise(async (resolve, reject) => {
|
|
573
|
+
|
|
574
|
+
let cache = uni.getStorageSync(key);
|
|
575
|
+
if (!cache) return resolve(await download_file(url));
|
|
576
|
+
|
|
577
|
+
uni.getSavedFileInfo({
|
|
578
|
+
filePath: cache.file,
|
|
579
|
+
success: async (res) => {
|
|
580
|
+
if (res.size > 0) {
|
|
581
|
+
console.log('从缓存读取成功', cache, res);
|
|
582
|
+
return resolve(cache)
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
console.log('从缓存读取成功,但是空文件', cache, res);
|
|
586
|
+
resolve(await download_file(url))
|
|
587
|
+
},
|
|
588
|
+
fail: async (err) => {
|
|
589
|
+
console.log('从缓存读取失败', cache, err);
|
|
590
|
+
resolve(await download_file(url))
|
|
591
|
+
}
|
|
592
|
+
});
|
|
593
|
+
|
|
594
|
+
});
|
|
595
|
+
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
|
|
599
|
+
|
|
497
600
|
export default class {
|
|
498
601
|
processor = null;
|
|
499
602
|
|
|
500
603
|
constructor(pro, conf) {
|
|
501
604
|
this.processor = processor = pro;
|
|
502
|
-
|
|
605
|
+
for (let k in config) config[k] = conf[k];
|
|
606
|
+
if (conf.toast) Object.assign(toastConf, conf.toast);
|
|
607
|
+
if (conf.alias) Object.assign(aliasConf, conf.alias);
|
|
503
608
|
if (isDebug) console.log(config);
|
|
504
609
|
}
|
|
505
610
|
|
|
506
|
-
get(api, data = {}) {
|
|
611
|
+
async get(api, data = {}) {
|
|
507
612
|
if (config.detection > 0) {
|
|
508
|
-
const fd = FrequencyDetection(api);
|
|
613
|
+
const fd = await FrequencyDetection(api);
|
|
509
614
|
if (fd) return fd;
|
|
510
615
|
}
|
|
511
616
|
|
|
512
|
-
return doRequest(new _request(api, data, 'get'));
|
|
617
|
+
return await doRequest(new _request(api, data, 'get'));
|
|
513
618
|
}
|
|
514
619
|
|
|
515
|
-
post(api, data = {}) {
|
|
620
|
+
async post(api, data = {}) {
|
|
516
621
|
if (config.detection > 0) {
|
|
517
|
-
const fd = FrequencyDetection(api);
|
|
622
|
+
const fd = await FrequencyDetection(api);
|
|
518
623
|
if (fd) return fd;
|
|
519
624
|
}
|
|
520
625
|
|
|
521
|
-
return doRequest(new _request(api, data, 'post'));
|
|
626
|
+
return await doRequest(new _request(api, data, 'post'));
|
|
522
627
|
}
|
|
523
628
|
|
|
629
|
+
next(data) {
|
|
630
|
+
/**
|
|
631
|
+
* 下一次post时请求时携带,后端用_append获取
|
|
632
|
+
* 主要用于发送不紧急的数据,没必要发起一次http请求
|
|
633
|
+
* 数据是array,后端遍历读取即可
|
|
634
|
+
*/
|
|
635
|
+
// if (data === undefined) return JSON.parse(JSON.stringify(nextPost));
|
|
636
|
+
if (data === undefined) return [...nextPost];
|
|
637
|
+
|
|
638
|
+
if (data === 'clear') {
|
|
639
|
+
nextPost.length = 0;
|
|
640
|
+
return;
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
nextPost.push(data);
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
async upload(uri, option) {
|
|
647
|
+
return await doUploadAliYun(uri, option);
|
|
648
|
+
}
|
|
524
649
|
|
|
525
|
-
|
|
526
|
-
return
|
|
650
|
+
async download(uri, callback) {
|
|
651
|
+
return await downloadFile(uri, callback);
|
|
527
652
|
}
|
|
528
653
|
|
|
529
654
|
|
package/src/kernel.js
CHANGED
|
@@ -53,12 +53,14 @@ export default {
|
|
|
53
53
|
|
|
54
54
|
if (!config.http.error) return;
|
|
55
55
|
|
|
56
|
-
app.config.errorHandler = (error, instance,
|
|
56
|
+
app.config.errorHandler = (error, instance, trace) => {
|
|
57
57
|
try {
|
|
58
58
|
if (error === lastError) return;
|
|
59
59
|
lastError = error;
|
|
60
|
-
console.log({ error, instance,
|
|
61
|
-
|
|
60
|
+
// console.log({ error, instance, trace });
|
|
61
|
+
console.trace(error)
|
|
62
|
+
console.trace(trace)
|
|
63
|
+
http.post(`!@${config.http.error}/error`, { error, instance, trace });
|
|
62
64
|
}
|
|
63
65
|
catch (err) {
|
|
64
66
|
console.log({ err })
|
|
@@ -69,8 +71,10 @@ export default {
|
|
|
69
71
|
try {
|
|
70
72
|
if (error === lastWarn) return;
|
|
71
73
|
lastWarn = error;
|
|
72
|
-
console.log({ error, instance, trace });
|
|
73
|
-
|
|
74
|
+
// console.log({ error, instance, trace });
|
|
75
|
+
console.trace(error)
|
|
76
|
+
console.trace(trace)
|
|
77
|
+
http.post(`!@${config.http.error}/warn`, { error, instance, trace });
|
|
74
78
|
}
|
|
75
79
|
catch (err) {
|
|
76
80
|
console.log({ err })
|