hy-app 0.3.11 → 0.3.13
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/api/http.ts
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
HttpRequestConfig,
|
|
3
|
-
HttpInterceptorManager,
|
|
4
|
-
HttpResponse,
|
|
5
|
-
} from "../typing";
|
|
1
|
+
import type { HttpRequestConfig } from "../typing";
|
|
6
2
|
import { objectToUrlParams } from "../utils";
|
|
7
3
|
|
|
8
4
|
export default class Http {
|
|
@@ -20,18 +16,27 @@ export default class Http {
|
|
|
20
16
|
timeout: 10000,
|
|
21
17
|
};
|
|
22
18
|
|
|
19
|
+
/* 私有回调槽 */
|
|
20
|
+
private _requestHook?: (
|
|
21
|
+
conf: UniNamespace.RequestOptions,
|
|
22
|
+
) => UniNamespace.RequestOptions;
|
|
23
|
+
private _responseSuccessHook?: (
|
|
24
|
+
res: UniNamespace.RequestSuccessCallbackResult,
|
|
25
|
+
) => any;
|
|
26
|
+
private _responseFailHook?: (err: UniNamespace.GeneralCallbackResult) => any;
|
|
27
|
+
|
|
23
28
|
/**
|
|
24
29
|
* @description 拦截器
|
|
25
30
|
*/
|
|
26
31
|
interceptor = {
|
|
27
32
|
/**
|
|
28
33
|
* @description 请求拦截 请求配置
|
|
29
|
-
* @param
|
|
34
|
+
* @param hook
|
|
30
35
|
*/
|
|
31
|
-
request: (
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
36
|
+
request: (
|
|
37
|
+
hook: (conf: UniNamespace.RequestOptions) => UniNamespace.RequestOptions,
|
|
38
|
+
) => {
|
|
39
|
+
this._requestHook = hook;
|
|
35
40
|
},
|
|
36
41
|
/**
|
|
37
42
|
* @description 响应拦截
|
|
@@ -39,66 +44,55 @@ export default class Http {
|
|
|
39
44
|
* @param fail 失败响应
|
|
40
45
|
*/
|
|
41
46
|
response: (
|
|
42
|
-
success
|
|
43
|
-
fail
|
|
47
|
+
success?: (response: UniNamespace.RequestSuccessCallbackResult) => any,
|
|
48
|
+
fail?: (error: UniNamespace.GeneralCallbackResult) => any,
|
|
44
49
|
) => {
|
|
45
50
|
if (success) {
|
|
46
|
-
this.
|
|
51
|
+
this._responseSuccessHook = success;
|
|
47
52
|
}
|
|
48
53
|
if (fail) {
|
|
49
|
-
this.
|
|
54
|
+
this._responseFailHook = fail;
|
|
50
55
|
}
|
|
51
56
|
},
|
|
52
57
|
};
|
|
53
58
|
|
|
54
59
|
/**
|
|
55
60
|
* @description 请求拦截
|
|
56
|
-
* @param
|
|
57
|
-
*/
|
|
58
|
-
requestBefore(config: HttpRequestConfig) {
|
|
59
|
-
return config;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* @description 成功响应
|
|
64
|
-
* @param response
|
|
65
|
-
*/
|
|
66
|
-
responseSuccess(response: UniNamespace.RequestSuccessCallbackResult) {
|
|
67
|
-
return response;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* @description 失败响应
|
|
72
|
-
* @param response
|
|
61
|
+
* @param conf 请求配置
|
|
73
62
|
*/
|
|
74
|
-
|
|
75
|
-
|
|
63
|
+
private requestBefore(
|
|
64
|
+
conf: UniNamespace.RequestOptions,
|
|
65
|
+
): UniNamespace.RequestOptions {
|
|
66
|
+
return this._requestHook ? this._requestHook(conf) : conf;
|
|
76
67
|
}
|
|
77
68
|
|
|
78
69
|
/**
|
|
79
70
|
* @description uni异步请求
|
|
80
71
|
* @param options 请求配置
|
|
81
72
|
*/
|
|
82
|
-
async request(options:
|
|
73
|
+
async request<T = any>(options: UniNamespace.RequestOptions): Promise<T> {
|
|
83
74
|
options.url = this.config.baseURL + options.url || this.config.url;
|
|
84
75
|
options.data = options.data || this.config.data;
|
|
85
76
|
options.header = options.header || this.config.header;
|
|
86
77
|
options.method = options.method || this.config.method;
|
|
87
78
|
options.responseType = options.responseType || this.config.responseType;
|
|
88
79
|
options.timeout = options.timeout || this.config.timeout;
|
|
89
|
-
|
|
90
|
-
|
|
80
|
+
/* 合并默认配置 ... */
|
|
81
|
+
return new Promise((resolve, reject) => {
|
|
82
|
+
options = this.requestBefore(options); // ⭐这里会调用钩子
|
|
91
83
|
uni.request({
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
resolve(this.responseSuccess(response));
|
|
84
|
+
...options,
|
|
85
|
+
success: (res: UniNamespace.RequestSuccessCallbackResult) => {
|
|
86
|
+
const after = this._responseSuccessHook
|
|
87
|
+
? this._responseSuccessHook(res)
|
|
88
|
+
: res;
|
|
89
|
+
resolve(after);
|
|
99
90
|
},
|
|
100
|
-
fail: (
|
|
101
|
-
|
|
91
|
+
fail: (err: UniNamespace.GeneralCallbackResult) => {
|
|
92
|
+
const after = this._responseFailHook
|
|
93
|
+
? this._responseFailHook(err)
|
|
94
|
+
: err;
|
|
95
|
+
reject(after);
|
|
102
96
|
},
|
|
103
97
|
});
|
|
104
98
|
});
|
|
@@ -110,7 +104,11 @@ export default class Http {
|
|
|
110
104
|
* @param params 请求JSON参数
|
|
111
105
|
* @param options 请求配置
|
|
112
106
|
*/
|
|
113
|
-
post
|
|
107
|
+
post<T = any>(
|
|
108
|
+
url: string,
|
|
109
|
+
params?: any,
|
|
110
|
+
options?: HttpRequestConfig,
|
|
111
|
+
): Promise<T> {
|
|
114
112
|
return this.request({
|
|
115
113
|
url: url,
|
|
116
114
|
method: "POST",
|
|
@@ -125,7 +123,11 @@ export default class Http {
|
|
|
125
123
|
* @param params URL查询参数
|
|
126
124
|
* @param options 请求配置
|
|
127
125
|
*/
|
|
128
|
-
get
|
|
126
|
+
get<T = any>(
|
|
127
|
+
url: string,
|
|
128
|
+
params?: any,
|
|
129
|
+
options?: HttpRequestConfig,
|
|
130
|
+
): Promise<T> {
|
|
129
131
|
if (params) {
|
|
130
132
|
url += "?" + objectToUrlParams(params);
|
|
131
133
|
}
|
|
@@ -73,7 +73,7 @@
|
|
|
73
73
|
v-if="showClear"
|
|
74
74
|
@click="onClear"
|
|
75
75
|
>
|
|
76
|
-
<HyIcon :name="IconConfig.CLOSE"
|
|
76
|
+
<HyIcon :name="IconConfig.CLOSE"></HyIcon>
|
|
77
77
|
</view>
|
|
78
78
|
</view>
|
|
79
79
|
<text
|
|
@@ -102,7 +102,7 @@ export default {
|
|
|
102
102
|
import { computed, nextTick, ref, watch } from "vue";
|
|
103
103
|
import type { PropType, CSSProperties } from "vue";
|
|
104
104
|
import type { ISearchEmits } from "./typing";
|
|
105
|
-
import {
|
|
105
|
+
import { addUnit } from "../../utils";
|
|
106
106
|
import { IconConfig } from "../../config";
|
|
107
107
|
import HyIcon from "../hy-icon/hy-icon.vue";
|
|
108
108
|
import type HyIconProps from "../hy-icon/typing";
|
|
@@ -13,7 +13,13 @@
|
|
|
13
13
|
¥
|
|
14
14
|
</text>
|
|
15
15
|
<view class="hy-text__prefix-icon" v-if="prefixIcon">
|
|
16
|
-
<HyIcon
|
|
16
|
+
<HyIcon
|
|
17
|
+
:name="prefixIcon"
|
|
18
|
+
:color="color"
|
|
19
|
+
:size="size"
|
|
20
|
+
space="2"
|
|
21
|
+
:customStyle="iconStyle"
|
|
22
|
+
></HyIcon>
|
|
17
23
|
</view>
|
|
18
24
|
<template v-if="openType && isMp">
|
|
19
25
|
<button
|
|
@@ -51,7 +57,12 @@
|
|
|
51
57
|
{{ value }}
|
|
52
58
|
</text>
|
|
53
59
|
<view class="hy-text__suffix-icon" v-if="suffixIcon">
|
|
54
|
-
<HyIcon
|
|
60
|
+
<HyIcon
|
|
61
|
+
:name="suffixIcon"
|
|
62
|
+
:color="color"
|
|
63
|
+
:size="size"
|
|
64
|
+
:customStyle="iconStyle"
|
|
65
|
+
></HyIcon>
|
|
55
66
|
</view>
|
|
56
67
|
</view>
|
|
57
68
|
</template>
|
package/package.json
CHANGED
package/typing/modules/http.ts
CHANGED
|
@@ -1,57 +1,6 @@
|
|
|
1
|
-
export interface HttpRequestConfig {
|
|
1
|
+
export interface HttpRequestConfig extends UniNamespace.RequestOptions {
|
|
2
2
|
/** 请求基地址 */
|
|
3
3
|
baseURL?: string;
|
|
4
|
-
/** 请求服务器接口地址 */
|
|
5
|
-
url?: string;
|
|
6
|
-
|
|
7
|
-
/** 请求查询参数,自动拼接为查询字符串 */
|
|
8
|
-
params?: AnyObject;
|
|
9
|
-
/** 请求体参数 */
|
|
10
|
-
data?: AnyObject;
|
|
11
|
-
|
|
12
|
-
/** 文件对应的 key */
|
|
13
|
-
name?: string;
|
|
14
|
-
/** HTTP 请求中其他额外的 form data */
|
|
15
|
-
formData?: AnyObject;
|
|
16
|
-
/** 要上传文件资源的路径。 */
|
|
17
|
-
filePath?: string;
|
|
18
|
-
/** 需要上传的文件列表。使用 files 时,filePath 和 name 不生效,App、H5( 2.6.15+) */
|
|
19
|
-
files?: Array<{
|
|
20
|
-
name?: string;
|
|
21
|
-
file?: File;
|
|
22
|
-
uri: string;
|
|
23
|
-
}>;
|
|
24
|
-
/** 要上传的文件对象,仅H5(2.6.15+)支持 */
|
|
25
|
-
file?: File;
|
|
26
|
-
|
|
27
|
-
/** 请求头信息 */
|
|
28
|
-
header?: AnyObject;
|
|
29
|
-
/** 请求方式 */
|
|
30
|
-
method?:
|
|
31
|
-
| "GET"
|
|
32
|
-
| "POST"
|
|
33
|
-
| "PUT"
|
|
34
|
-
| "DELETE"
|
|
35
|
-
| "CONNECT"
|
|
36
|
-
| "HEAD"
|
|
37
|
-
| "OPTIONS"
|
|
38
|
-
| "TRACE"
|
|
39
|
-
| "UPLOAD"
|
|
40
|
-
| "DOWNLOAD";
|
|
41
|
-
/** 如果设为 json,会尝试对返回的数据做一次 JSON.parse */
|
|
42
|
-
dataType?: string;
|
|
43
|
-
/** 设置响应的数据类型,支付宝小程序不支持 */
|
|
44
|
-
responseType?: "text" | "arraybuffer";
|
|
45
|
-
/** 自定义参数 */
|
|
46
|
-
custom?: AnyObject;
|
|
47
|
-
/** 超时时间,仅微信小程序(2.10.0)、支付宝小程序支持 */
|
|
48
|
-
timeout?: number;
|
|
49
|
-
/** DNS解析时优先使用ipv4,仅 App-Android 支持 (HBuilderX 2.8.0+) */
|
|
50
|
-
firstIpv4?: boolean;
|
|
51
|
-
/** 验证 ssl 证书 仅5+App安卓端支持(HBuilderX 2.3.3+) */
|
|
52
|
-
sslVerify?: boolean;
|
|
53
|
-
/** 跨域请求时是否携带凭证(cookies)仅H5支持(HBuilderX 2.6.15+) */
|
|
54
|
-
withCredentials?: boolean;
|
|
55
4
|
}
|
|
56
5
|
|
|
57
6
|
export interface HttpResponse<T = any> {
|