sard-uniapp 1.19.4 → 1.20.0
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/CHANGELOG.md +31 -0
- package/README.md +1 -1
- package/components/calendar/README.md +20 -20
- package/components/calendar/calendar.vue +1 -1
- package/components/calendar-input/README.md +10 -10
- package/components/calendar-input/calendar-input.vue +2 -2
- package/components/calendar-month/calendar-month.vue +17 -21
- package/components/calendar-popout/calendar-popout.vue +1 -1
- package/components/card/README.md +16 -8
- package/components/card/card.vue +7 -1
- package/components/card/common.d.ts +2 -0
- package/components/card/index.scss +30 -2
- package/components/card/variables.scss +16 -1
- package/components/checkbox/common.d.ts +1 -0
- package/components/checkbox/common.js +1 -0
- package/components/checkbox-input/checkbox-input.vue +9 -0
- package/components/checkbox-popout/README.md +27 -6
- package/components/checkbox-popout/checkbox-popout.vue +113 -47
- package/components/checkbox-popout/common.d.ts +3 -0
- package/components/checkbox-popout/index.scss +16 -0
- package/components/config/index.d.ts +6 -0
- package/components/config/index.js +6 -0
- package/components/datetime-picker/README.md +1 -71
- package/components/datetime-picker/common.d.ts +1 -0
- package/components/datetime-picker/common.js +21 -8
- package/components/datetime-picker/datetime-picker.vue +3 -3
- package/components/datetime-picker-input/README.md +10 -10
- package/components/datetime-picker-input/datetime-picker-input.vue +2 -2
- package/components/datetime-picker-popout/datetime-picker-popout.vue +1 -1
- package/components/datetime-range-picker/datetime-range-picker.vue +12 -10
- package/components/datetime-range-picker-input/README.md +10 -10
- package/components/datetime-range-picker-input/datetime-range-picker-input.vue +2 -2
- package/components/datetime-range-picker-popout/README.md +0 -6
- package/components/datetime-range-picker-popout/datetime-range-picker-popout.vue +11 -8
- package/components/floating-bubble/floating-bubble.vue +3 -3
- package/components/form/README.md +19 -19
- package/components/form/form.vue +2 -4
- package/components/form-item/form-item.vue +12 -0
- package/components/icon/icon.d.ts +1 -1
- package/components/indexes/README.md +4 -0
- package/components/indexes-nav/indexes-nav.vue +2 -2
- package/components/keyboard/common.d.ts +1 -2
- package/components/keyboard/common.js +2 -11
- package/components/keyboard/keyboard.d.ts +2 -2
- package/components/list-item/index.scss +4 -0
- package/components/locale/index.d.ts +4 -0
- package/components/locale/index.js +14 -1
- package/components/locale/lang/en-US.d.ts +4 -0
- package/components/locale/lang/en-US.js +4 -0
- package/components/locale/lang/zh-CN.d.ts +4 -0
- package/components/locale/lang/zh-CN.js +4 -0
- package/components/marquee/README.md +8 -2
- package/components/marquee/marquee.vue +9 -1
- package/components/notice-bar/README.md +8 -2
- package/components/notice-bar/notice-bar.vue +14 -8
- package/components/notify/notify.d.ts +1 -1
- package/components/notify-agent/notify-agent.d.ts +1 -1
- package/components/pagination/pagination.vue +2 -2
- package/components/popover/utils.js +4 -4
- package/components/pull-down-refresh/common.d.ts +1 -1
- package/components/radio/common.d.ts +1 -0
- package/components/radio/common.js +1 -0
- package/components/radio-input/radio-input.vue +6 -0
- package/components/radio-popout/README.md +20 -6
- package/components/radio-popout/common.d.ts +2 -0
- package/components/radio-popout/index.scss +4 -0
- package/components/radio-popout/radio-popout.vue +91 -45
- package/components/read-more/README.md +82 -0
- package/components/read-more/common.d.ts +24 -0
- package/components/read-more/common.js +2 -0
- package/components/read-more/index.d.ts +1 -0
- package/components/read-more/index.js +1 -0
- package/components/read-more/index.scss +41 -0
- package/components/read-more/read-more.d.ts +20 -0
- package/components/read-more/read-more.vue +168 -0
- package/components/read-more/variables.scss +12 -0
- package/components/resize-sensor/README.md +55 -0
- package/components/resize-sensor/common.d.ts +19 -0
- package/components/resize-sensor/common.js +2 -0
- package/components/resize-sensor/index.d.ts +1 -0
- package/components/resize-sensor/index.js +1 -0
- package/components/resize-sensor/index.scss +33 -0
- package/components/resize-sensor/resize-sensor.d.ts +16 -0
- package/components/resize-sensor/resize-sensor.vue +152 -0
- package/components/scroll-list/README.md +79 -0
- package/components/scroll-list/common.d.ts +23 -0
- package/components/scroll-list/common.js +1 -0
- package/components/scroll-list/index.d.ts +1 -0
- package/components/scroll-list/index.js +1 -0
- package/components/scroll-list/index.scss +35 -0
- package/components/scroll-list/scroll-list.d.ts +20 -0
- package/components/scroll-list/scroll-list.vue +158 -0
- package/components/scroll-list/variables.scss +11 -0
- package/components/sidebar/README.md +8 -8
- package/components/sidebar/sidebar.vue +1 -1
- package/components/slider/slider.vue +4 -4
- package/components/stepper/stepper.vue +2 -2
- package/components/sticky/README.md +92 -0
- package/components/sticky/common.d.ts +15 -0
- package/components/sticky/common.js +1 -0
- package/components/sticky/index.d.ts +1 -0
- package/components/sticky/index.js +1 -0
- package/components/sticky/index.scss +18 -0
- package/components/sticky/sticky.d.ts +10 -0
- package/components/sticky/sticky.vue +190 -0
- package/components/sticky-box/common.d.ts +19 -0
- package/components/sticky-box/common.js +1 -0
- package/components/sticky-box/index.d.ts +1 -0
- package/components/sticky-box/index.js +1 -0
- package/components/sticky-box/index.scss +19 -0
- package/components/sticky-box/sticky-box.d.ts +10 -0
- package/components/sticky-box/sticky-box.vue +82 -0
- package/components/style/mixins/border.scss +101 -38
- package/components/style/variables-dark.scss +4 -0
- package/components/style/variables.scss +8 -0
- package/components/table/table.vue +2 -1
- package/components/table-fixation/table-fixation.vue +2 -1
- package/components/toast/toast.d.ts +1 -1
- package/components/toast-agent/toast-agent.d.ts +1 -1
- package/components/tree/tree.d.ts +1 -1
- package/components/tree-node/tree-node.vue +2 -2
- package/components/upload/README.md +23 -15
- package/components/upload/common.d.ts +6 -4
- package/components/upload/upload.d.ts +2 -0
- package/components/upload/upload.vue +51 -13
- package/components/upload/utils.d.ts +1 -0
- package/components/upload/utils.js +2 -0
- package/components/upload-preview/index.scss +1 -0
- package/components/upload-preview/upload-preview.d.ts +6 -4
- package/components/upload-preview/upload-preview.vue +17 -24
- package/global.d.ts +5 -0
- package/index.d.ts +5 -0
- package/index.js +5 -0
- package/index.scss +2 -0
- package/package.json +4 -2
- package/use/index.d.ts +4 -0
- package/use/index.js +4 -0
- package/use/useIndeterminate.d.ts +10 -0
- package/use/useIndeterminate.js +27 -0
- package/use/useIntersectionObserver.d.ts +16 -0
- package/use/useIntersectionObserver.js +52 -0
- package/use/useSimulatedClick.js +2 -2
- package/use/useSimulatedPress.js +2 -2
- package/use/useSingleTask.d.ts +6 -0
- package/use/useSingleTask.js +40 -0
- package/use/useWindowResize.d.ts +4 -0
- package/use/useWindowResize.js +15 -0
- package/utils/array.d.ts +28 -0
- package/utils/array.js +90 -0
- package/utils/common.d.ts +3 -0
- package/utils/common.js +1 -0
- package/utils/date.d.ts +84 -11
- package/utils/date.js +120 -63
- package/utils/dom.js +2 -0
- package/utils/geometry.d.ts +56 -38
- package/utils/geometry.js +56 -30
- package/utils/index.d.ts +8 -1
- package/utils/index.js +8 -1
- package/utils/is.d.ts +12 -0
- package/utils/is.js +16 -0
- package/utils/number.d.ts +24 -0
- package/utils/number.js +37 -0
- package/utils/object.d.ts +29 -0
- package/utils/object.js +139 -0
- package/utils/persist.d.ts +15 -0
- package/utils/persist.js +89 -0
- package/utils/physics.d.ts +34 -0
- package/utils/physics.js +66 -0
- package/utils/request.d.ts +227 -0
- package/utils/request.js +249 -0
- package/utils/router.d.ts +47 -0
- package/utils/router.js +115 -0
- package/utils/string.d.ts +46 -0
- package/utils/string.js +134 -0
- package/utils/url-query.d.ts +19 -0
- package/utils/url-query.js +95 -0
- package/utils/utils.d.ts +0 -166
- package/utils/utils.js +0 -441
- package/utils/case.d.ts +0 -4
- package/utils/case.js +0 -26
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
interface UploadFileOptionFiles {
|
|
2
|
+
/**
|
|
3
|
+
* multipart 提交时,表单的项目名,默认为 file,如果 name 不填或填的值相同,可能导致服务端读取文件时只能读取到一个文件。
|
|
4
|
+
*/
|
|
5
|
+
name?: string;
|
|
6
|
+
/**
|
|
7
|
+
* 要上传的文件对象
|
|
8
|
+
*/
|
|
9
|
+
file?: File;
|
|
10
|
+
/**
|
|
11
|
+
* 要上传文件资源的路径
|
|
12
|
+
*/
|
|
13
|
+
uri?: string;
|
|
14
|
+
}
|
|
15
|
+
export interface RequestConfig<D extends string | AnyObject | ArrayBuffer = any, E extends Record<string, any> = any, Task extends UniApp.RequestTask | UniApp.UploadTask | UniApp.DownloadTask = UniApp.RequestTask> {
|
|
16
|
+
/**
|
|
17
|
+
* 自动加在 `url` 前面,除非 `url` 是一个绝对 URL
|
|
18
|
+
*/
|
|
19
|
+
baseURL?: string;
|
|
20
|
+
/**
|
|
21
|
+
* 与请求一起发送的 URL 参数
|
|
22
|
+
*/
|
|
23
|
+
params?: AnyObject;
|
|
24
|
+
/**
|
|
25
|
+
* 获取 uni.request 返回值,可用于中断请求
|
|
26
|
+
*/
|
|
27
|
+
getTask?: (task: Task, config: RequestConfig<D, E>) => void;
|
|
28
|
+
/**
|
|
29
|
+
* 请求的参数
|
|
30
|
+
*/
|
|
31
|
+
data?: D;
|
|
32
|
+
/**
|
|
33
|
+
* 额外的请求参数,用于实现自定义的行为
|
|
34
|
+
*/
|
|
35
|
+
extra?: E;
|
|
36
|
+
/**
|
|
37
|
+
* 资源url
|
|
38
|
+
*/
|
|
39
|
+
url?: string;
|
|
40
|
+
/**
|
|
41
|
+
* 设置请求的 header,header 中不能设置 Referer。
|
|
42
|
+
*/
|
|
43
|
+
header?: any;
|
|
44
|
+
/**
|
|
45
|
+
* 默认为 GET
|
|
46
|
+
* 可以是:OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE,CONNECT
|
|
47
|
+
*/
|
|
48
|
+
method?: 'OPTIONS' | 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'TRACE' | 'CONNECT' | 'DOWNLOAD' | 'UPLOAD';
|
|
49
|
+
/**
|
|
50
|
+
* 超时时间
|
|
51
|
+
*/
|
|
52
|
+
timeout?: number;
|
|
53
|
+
/**
|
|
54
|
+
* 如果设为json,会尝试对返回的数据做一次 JSON.parse
|
|
55
|
+
*/
|
|
56
|
+
dataType?: string;
|
|
57
|
+
/**
|
|
58
|
+
* 设置响应的数据类型。合法值:text、arraybuffer
|
|
59
|
+
*/
|
|
60
|
+
responseType?: string;
|
|
61
|
+
/**
|
|
62
|
+
* 验证 ssl 证书
|
|
63
|
+
*/
|
|
64
|
+
sslVerify?: boolean;
|
|
65
|
+
/**
|
|
66
|
+
* 跨域请求时是否携带凭证
|
|
67
|
+
*/
|
|
68
|
+
withCredentials?: boolean;
|
|
69
|
+
/**
|
|
70
|
+
* DNS解析时优先使用 ipv4
|
|
71
|
+
*/
|
|
72
|
+
firstIpv4?: boolean;
|
|
73
|
+
/**
|
|
74
|
+
* 开启 http2
|
|
75
|
+
*/
|
|
76
|
+
enableHttp2?: boolean;
|
|
77
|
+
/**
|
|
78
|
+
* 开启 quic
|
|
79
|
+
*/
|
|
80
|
+
enableQuic?: boolean;
|
|
81
|
+
/**
|
|
82
|
+
* 开启 cache
|
|
83
|
+
*/
|
|
84
|
+
enableCache?: boolean;
|
|
85
|
+
/**
|
|
86
|
+
* 是否开启 HttpDNS 服务。如开启,需要同时填入 httpDNSServiceId 。 HttpDNS 用法详见 [移动解析HttpDNS](https://developers.weixin.qq.com/miniprogram/dev/framework/ability/HTTPDNS.html)
|
|
87
|
+
*/
|
|
88
|
+
enableHttpDNS?: boolean;
|
|
89
|
+
/**
|
|
90
|
+
* HttpDNS 服务商 Id。 HttpDNS 用法详见 [移动解析HttpDNS](https://developers.weixin.qq.com/miniprogram/dev/framework/ability/HTTPDNS.html)
|
|
91
|
+
*/
|
|
92
|
+
httpDNSServiceId?: boolean;
|
|
93
|
+
/**
|
|
94
|
+
* 开启 transfer-encoding chunked
|
|
95
|
+
*/
|
|
96
|
+
enableChunked?: boolean;
|
|
97
|
+
/**
|
|
98
|
+
* wifi下使用移动网络发送请求
|
|
99
|
+
*/
|
|
100
|
+
forceCellularNetwork?: boolean;
|
|
101
|
+
/**
|
|
102
|
+
* 默认 false,开启后可在headers中编辑cookie(支付宝小程序10.2.33版本开始支持)
|
|
103
|
+
*/
|
|
104
|
+
enableCookie?: boolean;
|
|
105
|
+
/**
|
|
106
|
+
* 是否开启云加速(详见[云加速服务](https://smartprogram.baidu.com/docs/develop/extended/component-codeless/cloud-speed/introduction/))
|
|
107
|
+
*/
|
|
108
|
+
cloudCache?: object | boolean;
|
|
109
|
+
/**
|
|
110
|
+
* 控制当前请求是否延时至首屏内容渲染后发送
|
|
111
|
+
*/
|
|
112
|
+
defer?: boolean;
|
|
113
|
+
/**
|
|
114
|
+
* 需要上传的文件列表。App、H5( 2.6.15+)
|
|
115
|
+
*/
|
|
116
|
+
files?: UploadFileOptionFiles[];
|
|
117
|
+
/**
|
|
118
|
+
* 文件类型,image/video/audio,仅支付宝小程序,且必填。
|
|
119
|
+
* - image: 图像
|
|
120
|
+
* - video: 视频
|
|
121
|
+
* - audio: 音频
|
|
122
|
+
*/
|
|
123
|
+
fileType?: 'image' | 'video' | 'audio';
|
|
124
|
+
/**
|
|
125
|
+
* 要上传的文件对象。 仅H5(2.6.15+)支持
|
|
126
|
+
*/
|
|
127
|
+
file?: File;
|
|
128
|
+
/**
|
|
129
|
+
* 要上传/下载文件资源的路径
|
|
130
|
+
*/
|
|
131
|
+
filePath?: string;
|
|
132
|
+
/**
|
|
133
|
+
* 文件对应的 key , 开发者在服务器端通过这个 key 可以获取到文件二进制内容
|
|
134
|
+
*/
|
|
135
|
+
name?: string;
|
|
136
|
+
/**
|
|
137
|
+
* HTTP 请求中其他额外的 form data
|
|
138
|
+
*/
|
|
139
|
+
formData?: AnyObject;
|
|
140
|
+
}
|
|
141
|
+
export interface Response<T = any, D extends string | AnyObject | ArrayBuffer = any> {
|
|
142
|
+
/**
|
|
143
|
+
* 开发者服务器返回的数据
|
|
144
|
+
*/
|
|
145
|
+
data: T;
|
|
146
|
+
/**
|
|
147
|
+
* 开发者服务器返回的 HTTP 状态码
|
|
148
|
+
*/
|
|
149
|
+
statusCode: number;
|
|
150
|
+
/**
|
|
151
|
+
* 开发者服务器返回的 HTTP Response Header
|
|
152
|
+
*/
|
|
153
|
+
header: any;
|
|
154
|
+
/**
|
|
155
|
+
* 开发者服务器返回的 cookies,格式为字符串数组
|
|
156
|
+
*/
|
|
157
|
+
cookies: string[];
|
|
158
|
+
/**
|
|
159
|
+
* 请求的配置信息
|
|
160
|
+
*/
|
|
161
|
+
config: RequestConfig<D>;
|
|
162
|
+
/**
|
|
163
|
+
* 错误信息
|
|
164
|
+
*/
|
|
165
|
+
errMsg: string;
|
|
166
|
+
/**
|
|
167
|
+
* 临时文件路径,下载后的文件会存储到一个临时文件。微信小程序、支付宝小程序、百度小程序、抖音小程序、飞书小程序
|
|
168
|
+
*/
|
|
169
|
+
tempFilePath: string;
|
|
170
|
+
/**
|
|
171
|
+
* 下载文件保存的路径(本地临时文件)。入参未指定 filePath 的情况下可用。支付宝小程序
|
|
172
|
+
*/
|
|
173
|
+
apFilePath: string;
|
|
174
|
+
/**
|
|
175
|
+
* 用户文件路径 (本地路径)。传入 filePath 时会返回,跟传入的 filePath 一致。微信小程序、支付宝小程序、抖音小程序、飞书小程序
|
|
176
|
+
*/
|
|
177
|
+
filePath: string;
|
|
178
|
+
/**
|
|
179
|
+
* 文件内容。QQ小程序
|
|
180
|
+
*/
|
|
181
|
+
fileContent?: any;
|
|
182
|
+
/**
|
|
183
|
+
* 需要基础库: `2.10.4`
|
|
184
|
+
*
|
|
185
|
+
* 网络请求过程中一些调试信息,[查看详细说明](https://developers.weixin.qq.com/miniprogram/dev/framework/performance/network.html)
|
|
186
|
+
*/
|
|
187
|
+
profile?: UniApp.RequestProfile;
|
|
188
|
+
}
|
|
189
|
+
export interface ErrorResponse<D extends string | AnyObject | ArrayBuffer = any> {
|
|
190
|
+
/**
|
|
191
|
+
* 请求的配置信息
|
|
192
|
+
*/
|
|
193
|
+
config: RequestConfig<D>;
|
|
194
|
+
/**
|
|
195
|
+
* 错误信息
|
|
196
|
+
*/
|
|
197
|
+
errMsg: string;
|
|
198
|
+
}
|
|
199
|
+
declare function createInterceptors<T, U, E>(): {
|
|
200
|
+
request: {
|
|
201
|
+
use: (onFulfilled: (value: T) => T | Promise<T>, onRejected?: ((error: E) => any) | null | undefined) => void;
|
|
202
|
+
resolve: (data: T) => Promise<T>;
|
|
203
|
+
reject: (error: any) => Promise<any>;
|
|
204
|
+
};
|
|
205
|
+
response: {
|
|
206
|
+
use: (onFulfilled: (value: U) => U | Promise<U>, onRejected?: ((error: E) => any) | null | undefined) => void;
|
|
207
|
+
resolve: (data: U) => Promise<U>;
|
|
208
|
+
reject: (error: any) => Promise<any>;
|
|
209
|
+
};
|
|
210
|
+
};
|
|
211
|
+
export declare class Request<E extends Record<string, any> = any> {
|
|
212
|
+
readonly interceptors: ReturnType<typeof createInterceptors<RequestConfig<any, E, any>, Response, ErrorResponse>>;
|
|
213
|
+
protected defaultConfig: RequestConfig;
|
|
214
|
+
constructor(defaultConfig?: RequestConfig);
|
|
215
|
+
request<T = any, R = Response<T>, D extends string | AnyObject | ArrayBuffer = any, Task extends UniApp.RequestTask | UniApp.UploadTask | UniApp.DownloadTask = UniApp.RequestTask>(config: RequestConfig<D, E, Task>): Promise<R>;
|
|
216
|
+
get<T = any, R = Response<T>, D extends string | AnyObject | ArrayBuffer = any>(url: string, config?: RequestConfig<D, E>): Promise<R>;
|
|
217
|
+
head<T = any, R = Response<T>, D extends string | AnyObject | ArrayBuffer = any>(url: string, config?: RequestConfig<D, E>): Promise<R>;
|
|
218
|
+
connect<T = any, R = Response<T>, D extends string | AnyObject | ArrayBuffer = any>(url: string, config?: RequestConfig<D, E>): Promise<R>;
|
|
219
|
+
trace<T = any, R = Response<T>, D extends string | AnyObject | ArrayBuffer = any>(url: string, config?: RequestConfig<D, E>): Promise<R>;
|
|
220
|
+
post<T = any, R = Response<T>, D extends string | AnyObject | ArrayBuffer = any>(url: string, data?: D, config?: RequestConfig<D, E>): Promise<R>;
|
|
221
|
+
put<T = any, R = Response<T>, D extends string | AnyObject | ArrayBuffer = any>(url: string, data?: D, config?: RequestConfig<D, E>): Promise<R>;
|
|
222
|
+
delete<T = any, R = Response<T>, D extends string | AnyObject | ArrayBuffer = any>(url: string, data?: D, config?: RequestConfig<D, E>): Promise<R>;
|
|
223
|
+
options<T = any, R = Response<T>, D extends string | AnyObject | ArrayBuffer = any>(url: string, data?: D, config?: RequestConfig<D, E>): Promise<R>;
|
|
224
|
+
upload<T = any, R = Response<T>, D extends string | AnyObject | ArrayBuffer = any>(url: string, config?: RequestConfig<D, E, UniApp.UploadTask>): Promise<R>;
|
|
225
|
+
download<T = any, R = Response<T>, D extends string | AnyObject | ArrayBuffer = any>(url: string, config?: RequestConfig<D, E, UniApp.DownloadTask>): Promise<R>;
|
|
226
|
+
}
|
|
227
|
+
export {};
|
package/utils/request.js
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import { URLQuery } from './url-query';
|
|
2
|
+
function reducerFactory() {
|
|
3
|
+
const handlers = [];
|
|
4
|
+
function use(onFulfilled, onRejected) {
|
|
5
|
+
handlers.push({
|
|
6
|
+
fulfilled: onFulfilled,
|
|
7
|
+
rejected: onRejected,
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
async function resolve(data) {
|
|
11
|
+
for (const { fulfilled } of handlers) {
|
|
12
|
+
try {
|
|
13
|
+
data = await fulfilled(data);
|
|
14
|
+
}
|
|
15
|
+
catch (err) {
|
|
16
|
+
return Promise.reject(err);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return Promise.resolve(data);
|
|
20
|
+
}
|
|
21
|
+
async function reject(error) {
|
|
22
|
+
for (const { rejected } of handlers) {
|
|
23
|
+
try {
|
|
24
|
+
if (rejected) {
|
|
25
|
+
const data = await rejected(error);
|
|
26
|
+
return Promise.resolve(data);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
catch (newError) {
|
|
30
|
+
error = newError;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return Promise.reject(error);
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
use,
|
|
37
|
+
resolve,
|
|
38
|
+
reject,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function createInterceptors() {
|
|
42
|
+
return {
|
|
43
|
+
request: reducerFactory(),
|
|
44
|
+
response: reducerFactory(),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
const defaultRequestConfig = {};
|
|
48
|
+
function mergeConfig(...configs) {
|
|
49
|
+
return configs.reduce((result, config) => {
|
|
50
|
+
return {
|
|
51
|
+
...result,
|
|
52
|
+
...config,
|
|
53
|
+
header: {
|
|
54
|
+
...result.header,
|
|
55
|
+
...config.header,
|
|
56
|
+
},
|
|
57
|
+
params: {
|
|
58
|
+
...result.params,
|
|
59
|
+
...config.params,
|
|
60
|
+
},
|
|
61
|
+
extra: {
|
|
62
|
+
...result.extra,
|
|
63
|
+
...config.extra,
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
}, {});
|
|
67
|
+
}
|
|
68
|
+
function mergeUrl(options) {
|
|
69
|
+
const { baseURL = '', url = '', params } = options;
|
|
70
|
+
let mergedUrl = '';
|
|
71
|
+
if (/^(https?:)?\/\//.test(url)) {
|
|
72
|
+
mergedUrl = url;
|
|
73
|
+
}
|
|
74
|
+
else if (baseURL && url) {
|
|
75
|
+
mergedUrl = baseURL.replace(/\/*$/, '') + '/' + url.replace(/^\/*/, '');
|
|
76
|
+
}
|
|
77
|
+
else if (baseURL) {
|
|
78
|
+
mergedUrl = baseURL;
|
|
79
|
+
}
|
|
80
|
+
else if (url) {
|
|
81
|
+
mergedUrl = url;
|
|
82
|
+
}
|
|
83
|
+
if (params) {
|
|
84
|
+
const query = new URLQuery(params);
|
|
85
|
+
const index = mergedUrl.indexOf('?');
|
|
86
|
+
if (index !== -1) {
|
|
87
|
+
const originPath = mergedUrl.slice(index);
|
|
88
|
+
const search = mergedUrl.slice(index);
|
|
89
|
+
mergedUrl +=
|
|
90
|
+
originPath +
|
|
91
|
+
'?' +
|
|
92
|
+
new URLQuery([...query, ...new URLQuery(search)]).toString();
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
mergedUrl += '?' + query.toString();
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return mergedUrl;
|
|
99
|
+
}
|
|
100
|
+
export class Request {
|
|
101
|
+
interceptors;
|
|
102
|
+
defaultConfig;
|
|
103
|
+
constructor(defaultConfig = {}) {
|
|
104
|
+
this.defaultConfig = mergeConfig(defaultRequestConfig, defaultConfig);
|
|
105
|
+
this.interceptors = createInterceptors();
|
|
106
|
+
}
|
|
107
|
+
request(config) {
|
|
108
|
+
return new Promise((resolve, reject) => {
|
|
109
|
+
const interceptors = this.interceptors;
|
|
110
|
+
config = mergeConfig(this.defaultConfig, config);
|
|
111
|
+
interceptors.request
|
|
112
|
+
.resolve(config)
|
|
113
|
+
.then((config) => {
|
|
114
|
+
const { baseURL, params, getTask, url, extra, ...rest } = config;
|
|
115
|
+
void extra;
|
|
116
|
+
const mergedUrl = mergeUrl({ baseURL, url, params });
|
|
117
|
+
const mergedConfig = {
|
|
118
|
+
...rest,
|
|
119
|
+
url: mergedUrl,
|
|
120
|
+
success(result) {
|
|
121
|
+
const { data, statusCode, header = {}, cookies = [], errMsg = '', tempFilePath = '', apFilePath = '', filePath = '', fileContent, profile, } = result;
|
|
122
|
+
let parsedData = data;
|
|
123
|
+
if (typeof data === 'string') {
|
|
124
|
+
try {
|
|
125
|
+
parsedData = JSON.parse(data);
|
|
126
|
+
}
|
|
127
|
+
catch {
|
|
128
|
+
void 0;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
interceptors.response
|
|
132
|
+
.resolve({
|
|
133
|
+
data: parsedData,
|
|
134
|
+
statusCode,
|
|
135
|
+
header,
|
|
136
|
+
cookies,
|
|
137
|
+
errMsg,
|
|
138
|
+
tempFilePath,
|
|
139
|
+
apFilePath,
|
|
140
|
+
filePath,
|
|
141
|
+
fileContent,
|
|
142
|
+
profile,
|
|
143
|
+
config,
|
|
144
|
+
})
|
|
145
|
+
.then(resolve, reject);
|
|
146
|
+
},
|
|
147
|
+
fail(result) {
|
|
148
|
+
interceptors.response
|
|
149
|
+
.reject({
|
|
150
|
+
...result,
|
|
151
|
+
config,
|
|
152
|
+
})
|
|
153
|
+
.then(resolve, reject);
|
|
154
|
+
},
|
|
155
|
+
};
|
|
156
|
+
let task;
|
|
157
|
+
switch (config.method) {
|
|
158
|
+
case 'DOWNLOAD':
|
|
159
|
+
task = uni.downloadFile(mergedConfig);
|
|
160
|
+
break;
|
|
161
|
+
case 'UPLOAD':
|
|
162
|
+
task = uni.uploadFile(mergedConfig);
|
|
163
|
+
break;
|
|
164
|
+
default:
|
|
165
|
+
task = uni.request(mergedConfig);
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
if (getTask) {
|
|
169
|
+
getTask(task, config);
|
|
170
|
+
}
|
|
171
|
+
})
|
|
172
|
+
.catch(reject);
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
get(url, config) {
|
|
176
|
+
return this.request({
|
|
177
|
+
...config,
|
|
178
|
+
url,
|
|
179
|
+
method: 'GET',
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
head(url, config) {
|
|
183
|
+
return this.request({
|
|
184
|
+
...config,
|
|
185
|
+
url,
|
|
186
|
+
method: 'HEAD',
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
connect(url, config) {
|
|
190
|
+
return this.request({
|
|
191
|
+
...config,
|
|
192
|
+
url,
|
|
193
|
+
method: 'CONNECT',
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
trace(url, config) {
|
|
197
|
+
return this.request({
|
|
198
|
+
...config,
|
|
199
|
+
url,
|
|
200
|
+
method: 'TRACE',
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
post(url, data, config) {
|
|
204
|
+
return this.request({
|
|
205
|
+
...config,
|
|
206
|
+
url,
|
|
207
|
+
data,
|
|
208
|
+
method: 'POST',
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
put(url, data, config) {
|
|
212
|
+
return this.request({
|
|
213
|
+
...config,
|
|
214
|
+
url,
|
|
215
|
+
data,
|
|
216
|
+
method: 'PUT',
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
delete(url, data, config) {
|
|
220
|
+
return this.request({
|
|
221
|
+
...config,
|
|
222
|
+
url,
|
|
223
|
+
data,
|
|
224
|
+
method: 'DELETE',
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
options(url, data, config) {
|
|
228
|
+
return this.request({
|
|
229
|
+
...config,
|
|
230
|
+
url,
|
|
231
|
+
data,
|
|
232
|
+
method: 'OPTIONS',
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
upload(url, config) {
|
|
236
|
+
return this.request({
|
|
237
|
+
...config,
|
|
238
|
+
url,
|
|
239
|
+
method: 'UPLOAD',
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
download(url, config) {
|
|
243
|
+
return this.request({
|
|
244
|
+
...config,
|
|
245
|
+
url,
|
|
246
|
+
method: 'DOWNLOAD',
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
interface RouterExtraOptions {
|
|
2
|
+
query?: Record<string, any>;
|
|
3
|
+
}
|
|
4
|
+
interface Route {
|
|
5
|
+
url: string;
|
|
6
|
+
query?: Record<string, any>;
|
|
7
|
+
}
|
|
8
|
+
export interface PagesPath {
|
|
9
|
+
}
|
|
10
|
+
type ChangeOptionsUrl<T extends {
|
|
11
|
+
url: string.PageURIString;
|
|
12
|
+
}> = Omit<T, 'url'> & {
|
|
13
|
+
url: (string & {}) | PagesPath['path' extends keyof PagesPath ? 'path' : never];
|
|
14
|
+
};
|
|
15
|
+
type RouterGuard = (to: Route, from: Route) => boolean | undefined | null | void | string | Route | Promise<boolean | undefined | null | void | string | Route>;
|
|
16
|
+
export declare class Router {
|
|
17
|
+
status: 'idle' | 'busy';
|
|
18
|
+
guards: RouterGuard[];
|
|
19
|
+
protected resolvePath(currentPath: string, path: string): string;
|
|
20
|
+
protected parseQuery(url: string): {
|
|
21
|
+
url: string;
|
|
22
|
+
query?: Record<string, string>;
|
|
23
|
+
};
|
|
24
|
+
protected getPathQuery(currentPath: string, route: string | Route): {
|
|
25
|
+
url: string;
|
|
26
|
+
query: {
|
|
27
|
+
[x: string]: any;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
protected makeUniRouteOptions(to: {
|
|
31
|
+
url: string;
|
|
32
|
+
query: Record<string, any>;
|
|
33
|
+
}, options: any): any;
|
|
34
|
+
protected intercept(options: {
|
|
35
|
+
url: string;
|
|
36
|
+
success?: (result: any) => void;
|
|
37
|
+
fail?: (result: any) => void;
|
|
38
|
+
complete?: (result: any) => void;
|
|
39
|
+
} & RouterExtraOptions, action: (...args: any[]) => any): Promise<any>;
|
|
40
|
+
navigateTo(options: ChangeOptionsUrl<UniApp.NavigateToOptions> & RouterExtraOptions): Promise<any>;
|
|
41
|
+
redirectTo(options: ChangeOptionsUrl<UniApp.RedirectToOptions> & RouterExtraOptions): Promise<any>;
|
|
42
|
+
reLaunch(options: ChangeOptionsUrl<UniApp.ReLaunchOptions> & RouterExtraOptions): Promise<any>;
|
|
43
|
+
switchTab(options: ChangeOptionsUrl<UniApp.SwitchTabOptions>): Promise<any>;
|
|
44
|
+
navigateBack(options: UniApp.NavigateBackOptions): Promise<any>;
|
|
45
|
+
beforeEach(guard: RouterGuard): void;
|
|
46
|
+
}
|
|
47
|
+
export {};
|
package/utils/router.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { URLQuery } from './url-query';
|
|
2
|
+
export class Router {
|
|
3
|
+
status = 'idle';
|
|
4
|
+
guards = [];
|
|
5
|
+
resolvePath(currentPath, path) {
|
|
6
|
+
if (/^\//.test(path)) {
|
|
7
|
+
return path;
|
|
8
|
+
}
|
|
9
|
+
const fragments = currentPath.replace(/\/[^/]+$/, '').split('/');
|
|
10
|
+
if (path.startsWith('./')) {
|
|
11
|
+
path = path.slice(2);
|
|
12
|
+
}
|
|
13
|
+
fragments.push(...path.split('/'));
|
|
14
|
+
const count = fragments.filter((frag) => frag === '..').length;
|
|
15
|
+
for (let i = 0; i < count; i++) {
|
|
16
|
+
const index = fragments.indexOf('..');
|
|
17
|
+
fragments.splice(index - 1, 2);
|
|
18
|
+
}
|
|
19
|
+
return '/' + fragments.join('/');
|
|
20
|
+
}
|
|
21
|
+
parseQuery(url) {
|
|
22
|
+
const [pathPart, queryPart] = url.split('?');
|
|
23
|
+
const pathQuery = {
|
|
24
|
+
url: pathPart,
|
|
25
|
+
};
|
|
26
|
+
if (queryPart) {
|
|
27
|
+
pathQuery.query = Object.fromEntries(new URLQuery(queryPart));
|
|
28
|
+
}
|
|
29
|
+
return pathQuery;
|
|
30
|
+
}
|
|
31
|
+
getPathQuery(currentPath, route) {
|
|
32
|
+
const toRoute = typeof route === 'string'
|
|
33
|
+
? this.parseQuery(route)
|
|
34
|
+
: this.parseQuery(route.url);
|
|
35
|
+
return {
|
|
36
|
+
url: this.resolvePath(currentPath, toRoute.url),
|
|
37
|
+
query: {
|
|
38
|
+
...toRoute.query,
|
|
39
|
+
...(typeof route !== 'string' ? route.query : null),
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
makeUniRouteOptions(to, options) {
|
|
44
|
+
const url = to.url +
|
|
45
|
+
(Object.keys(to.query).length > 0
|
|
46
|
+
? '?' + new URLQuery(Object.entries(to.query)).toString()
|
|
47
|
+
: '');
|
|
48
|
+
return {
|
|
49
|
+
...options,
|
|
50
|
+
url,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
async intercept(options, action) {
|
|
54
|
+
if (this.status === 'busy') {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
this.status = 'busy';
|
|
58
|
+
const complete = options.complete;
|
|
59
|
+
options.complete = (result) => {
|
|
60
|
+
this.status = 'idle';
|
|
61
|
+
complete?.(result);
|
|
62
|
+
};
|
|
63
|
+
const pages = getCurrentPages();
|
|
64
|
+
const currentPath = pages[pages.length - 1].route;
|
|
65
|
+
const toPathQuery = this.getPathQuery(currentPath, {
|
|
66
|
+
url: options.url,
|
|
67
|
+
query: options.query,
|
|
68
|
+
});
|
|
69
|
+
const fromPathQuery = {
|
|
70
|
+
url: '/' + currentPath,
|
|
71
|
+
query: {},
|
|
72
|
+
};
|
|
73
|
+
try {
|
|
74
|
+
for (const guard of this.guards) {
|
|
75
|
+
const result = await guard(toPathQuery, fromPathQuery);
|
|
76
|
+
if (result === false) {
|
|
77
|
+
this.status = 'idle';
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
if (typeof result === 'string') {
|
|
81
|
+
return action(this.makeUniRouteOptions(this.getPathQuery(currentPath, result), options));
|
|
82
|
+
}
|
|
83
|
+
if (result !== null && typeof result === 'object') {
|
|
84
|
+
return action(this.makeUniRouteOptions(this.getPathQuery(currentPath, result), options));
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
catch (err) {
|
|
89
|
+
this.status = 'idle';
|
|
90
|
+
console.error(err);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
return action(this.makeUniRouteOptions(toPathQuery, options));
|
|
94
|
+
}
|
|
95
|
+
navigateTo(options) {
|
|
96
|
+
return this.intercept(options, uni.navigateTo);
|
|
97
|
+
}
|
|
98
|
+
redirectTo(options) {
|
|
99
|
+
return this.intercept(options, uni.redirectTo);
|
|
100
|
+
}
|
|
101
|
+
reLaunch(options) {
|
|
102
|
+
return this.intercept(options, uni.reLaunch);
|
|
103
|
+
}
|
|
104
|
+
switchTab(options) {
|
|
105
|
+
return this.intercept(options, uni.switchTab);
|
|
106
|
+
}
|
|
107
|
+
navigateBack(options) {
|
|
108
|
+
return uni.navigateBack(options);
|
|
109
|
+
}
|
|
110
|
+
beforeEach(guard) {
|
|
111
|
+
if (!this.guards.includes(guard)) {
|
|
112
|
+
this.guards.push(guard);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 将字符串首字母转换为小写格式。
|
|
3
|
+
*/
|
|
4
|
+
export declare function lowerFirst(string: string): string;
|
|
5
|
+
/**
|
|
6
|
+
* 将字符串首字母转换为大写格式。
|
|
7
|
+
*/
|
|
8
|
+
export declare function upperFirst(string: string): string;
|
|
9
|
+
/**
|
|
10
|
+
* 将字符串转换为 PascalCase 格式(大驼峰)。
|
|
11
|
+
*/
|
|
12
|
+
export declare function pascalCase(string: string): string;
|
|
13
|
+
/**
|
|
14
|
+
* 将字符串转换为 camelCase 格式(小驼峰)。
|
|
15
|
+
*/
|
|
16
|
+
export declare function camelCase(string: string): string;
|
|
17
|
+
/**
|
|
18
|
+
* 将字符串转换为 Capitalize 格式(首字母大写,其余小写)。
|
|
19
|
+
*/
|
|
20
|
+
export declare function capitalize(string: string): string;
|
|
21
|
+
/**
|
|
22
|
+
* 将字符串转换为 kebab-case 格式(使用连字符拼接单词)。
|
|
23
|
+
*/
|
|
24
|
+
export declare function kebabCase(string: string): string;
|
|
25
|
+
/**
|
|
26
|
+
* 将字符串转换为 snake_case 格式(使用下划线拼接单词)。
|
|
27
|
+
*/
|
|
28
|
+
export declare function snakeCase(string: string): string;
|
|
29
|
+
/**
|
|
30
|
+
* 每n位数字添加一个分隔符
|
|
31
|
+
*/
|
|
32
|
+
export declare function addSeparator(num: number | string, separator?: string, digit?: number): string;
|
|
33
|
+
/**
|
|
34
|
+
* 生成唯一ID,用于设置元素的ID,以便获取
|
|
35
|
+
*/
|
|
36
|
+
export declare function uniqid(prefix?: string): string;
|
|
37
|
+
export type ClassProp = string | number | null | undefined | Record<string, any> | ClassProp[];
|
|
38
|
+
/**
|
|
39
|
+
* 把各种类型的参数拼接成字符串类名,以便解决小程序不支持 classObject 的问题
|
|
40
|
+
*/
|
|
41
|
+
export declare function classNames(...args: ClassProp[]): string;
|
|
42
|
+
export type StyleProp = string | Record<any, any> | null | undefined | false | StyleProp[];
|
|
43
|
+
/**
|
|
44
|
+
* 把样式对象拼接成字符串,解决小程序不支持 styleObject 的问题。
|
|
45
|
+
*/
|
|
46
|
+
export declare function stringifyStyle(...args: StyleProp[]): string;
|