hy-app 0.3.11 → 0.3.12

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 config
34
+ * @param hook
30
35
  */
31
- request: (config: HttpRequestConfig) => {
32
- if (config) {
33
- this.requestBefore(config);
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: (response: HttpResponse) => any,
43
- fail: (error: HttpResponse) => any,
47
+ success?: (response: UniNamespace.RequestSuccessCallbackResult) => any,
48
+ fail?: (error: UniNamespace.GeneralCallbackResult) => any,
44
49
  ) => {
45
50
  if (success) {
46
- this.responseSuccess = success;
51
+ this._responseSuccessHook = success;
47
52
  }
48
53
  if (fail) {
49
- this.responseFail = fail;
54
+ this._responseFailHook = fail;
50
55
  }
51
56
  },
52
57
  };
53
58
 
54
59
  /**
55
60
  * @description 请求拦截
56
- * @param config 请求配置
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
- responseFail(response: UniNamespace.GeneralCallbackResult) {
75
- return response;
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: Record<string, any>) {
73
+ async request(options: UniNamespace.RequestOptions) {
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
- return new Promise<any>((resolve, reject) => {
90
- options = this.requestBefore(options);
80
+ /* 合并默认配置 ... */
81
+ return new Promise((resolve, reject) => {
82
+ options = this.requestBefore(options); // ⭐这里会调用钩子
91
83
  uni.request({
92
- url: options.url,
93
- data: options.data,
94
- header: options.header,
95
- method: options.method,
96
- timeout: options.timeout,
97
- success: (response: UniNamespace.RequestSuccessCallbackResult) => {
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: (error: UniNamespace.GeneralCallbackResult) => {
101
- reject(this.responseFail(error));
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,7 @@ export default class Http {
110
104
  * @param params 请求JSON参数
111
105
  * @param options 请求配置
112
106
  */
113
- post(url: string, params?: any, options?: Record<string, any>) {
107
+ post(url: string, params?: any, options?: HttpRequestConfig) {
114
108
  return this.request({
115
109
  url: url,
116
110
  method: "POST",
@@ -125,7 +119,7 @@ export default class Http {
125
119
  * @param params URL查询参数
126
120
  * @param options 请求配置
127
121
  */
128
- get(url: string, params?: any, options?: Record<string, any>) {
122
+ get(url: string, params?: any, options?: HttpRequestConfig) {
129
123
  if (params) {
130
124
  url += "?" + objectToUrlParams(params);
131
125
  }
@@ -134,7 +134,7 @@ const props = defineProps({
134
134
  /** 卡片与屏幕两边和上下元素的间距,需带单位,如"30px 20px" */
135
135
  margin: {
136
136
  type: String,
137
- default: "10px",
137
+ default: "0 0 20rpx",
138
138
  },
139
139
  /** 卡片整体的圆角值,单位px */
140
140
  borderRadius: {
@@ -73,7 +73,7 @@
73
73
  v-if="showClear"
74
74
  @click="onClear"
75
75
  >
76
- <HyIcon :name="IconConfig.CLOSE" color="#ffffff"></HyIcon>
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 { sleep, addUnit } from "../../utils";
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 :name="prefixIcon" :customStyle="iconStyle"></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 :name="suffixIcon" :customStyle="iconStyle"></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>
@@ -111,3 +111,9 @@ $u-zoom-scale: scale(0.95);
111
111
  .u-zoom-leave-to {
112
112
  transform: $u-zoom-scale
113
113
  }
114
+
115
+ @include b(transition) {
116
+ display: flex;
117
+ justify-content: center;
118
+ align-items: center;
119
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "hy-app",
3
- "version": "0.3.11",
4
- "description": "修复搜索输入框,有值时候获取焦点会立马失去焦点问题",
3
+ "version": "0.3.12",
4
+ "description": "http网络请求修复",
5
5
  "main": "./index.ts",
6
6
  "private": false,
7
7
  "scripts": {},
@@ -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> {