tang-ui-x 1.2.1 → 1.2.3

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.
@@ -142,7 +142,7 @@ const handleClick = () => {
142
142
  <style lang="scss" scoped>
143
143
  // 覆盖 uni-button 原生组件的默认样式
144
144
  // 使用 uni-button.t-button 确保覆盖原生样式
145
- uni-button.t-button {
145
+ .t-button {
146
146
  position: relative;
147
147
  display: inline-flex;
148
148
  align-items: center;
@@ -83,7 +83,7 @@ const handleClick = (): void => {
83
83
  <style lang="scss" scoped>
84
84
  // 覆盖 uni-view 原生组件的默认样式
85
85
  // 使用 uni-view.t-card 确保覆盖原生样式
86
- uni-view.t-card {
86
+ .t-card {
87
87
  background-color: #ffffff;
88
88
  transition: all 0.3s ease;
89
89
  // 重置 uni-view 的默认样式
@@ -400,6 +400,7 @@
400
400
  .input,
401
401
  .textarea {
402
402
  width: 100%;
403
+ height: 80rpx;
403
404
  padding: 20rpx 24rpx;
404
405
  font-size: 28rpx;
405
406
  color: #333;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tang-ui-x",
3
- "version": "1.2.1",
3
+ "version": "1.2.3",
4
4
  "description": "UniApp X UI 组件库 - 基于 uni-app x 的移动端 UI 组件库",
5
5
  "main": "index.uts",
6
6
  "module": "index.uts",
@@ -1,64 +1,53 @@
1
1
  /**
2
- * 请求方法类型
2
+ * HTTP 请求封装
3
+ * @module utils/request
3
4
  */
4
- type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'OPTIONS' | 'HEAD' | 'TRACE' | 'CONNECT'
5
5
 
6
- /**
7
- * 基础配置
8
- */
9
- interface HttpInitOptions {
10
- baseURL : string
11
- header ?: UTSJSONObject
12
- showLoading ?: boolean
13
- autoAuth ?: boolean // 是否自动附加 Token
14
- timeout ?: number
15
- [key : string] : any
16
- }
17
-
18
- /**
19
- * 请求配置
20
- */
21
- interface HttpRequestOptions {
22
- url : string
23
- data ?: UTSJSONObject | null
24
- header ?: UTSJSONObject | null
25
- method ?: HttpMethod
26
- timeout ?: number
27
- showLoading ?: boolean
28
- loadingText ?: string
29
- disableAuth ?: boolean
30
- [key : string] : any
31
- }
32
-
33
- /**
34
- * 响应体结构
35
- */
36
- interface ResponseData<T = any> {
37
- code : number
38
- msg : string
39
- data : T,
40
- count : number
41
- }
42
-
43
- /**
44
- * 分页数据响应格式
45
- */
46
- interface DataResponse<T = any> {
47
- data : T | []
48
- total ?: number
49
- }
50
-
51
- /**
52
- * Http 拦截器接口
53
- */
54
- interface HttpInterceptors {
55
- request ?: (options : HttpRequestOptions) => HttpRequestOptions
56
- response ?: <T>(response : ResponseData<T>, options : HttpRequestOptions) => DataResponse<T> | T
57
- error ?: (err : any, options : HttpRequestOptions) => void
6
+ import type {
7
+ HttpMethod,
8
+ HttpInitOptions,
9
+ HttpRequestOptions,
10
+ ResponseData,
11
+ DataResponse,
12
+ HttpInterceptors
13
+ } from './type.uts'
14
+
15
+ // 导出类型供外部使用
16
+ export type {
17
+ HttpMethod,
18
+ HttpInitOptions,
19
+ HttpRequestOptions,
20
+ ResponseData,
21
+ DataResponse,
22
+ HttpInterceptors
58
23
  }
59
24
 
60
25
  /**
61
- * 优化封装 HttpRequest
26
+ * HTTP 请求类
27
+ * @description 封装 uni.request,支持拦截器、自动认证、统一错误处理
28
+ * @example
29
+ * ```ts
30
+ * const http = new HttpRequest({
31
+ * baseURL: 'https://api.example.com',
32
+ * showLoading: true,
33
+ * autoAuth: true
34
+ * })
35
+ *
36
+ * // 设置拦截器
37
+ * http.setInterceptors({
38
+ * request: (options) => {
39
+ * // 自定义请求处理
40
+ * return options
41
+ * },
42
+ * response: (res, options) => {
43
+ * // 自定义响应处理
44
+ * return res.data
45
+ * }
46
+ * })
47
+ *
48
+ * // 发起请求
49
+ * const data = await http.get<UserInfo>('/user/info')
50
+ * ```
62
51
  */
63
52
  class HttpRequest {
64
53
  private baseURL : string
@@ -83,21 +72,18 @@ class HttpRequest {
83
72
  this.timeout = options.timeout ?? 50000
84
73
  }
85
74
 
86
- /** 设置拦截器 */
87
- public setInterceptors(interceptors : HttpInterceptors) {
88
-
89
-
90
-
75
+ /**
76
+ * 设置拦截器
77
+ * @param interceptors 拦截器配置
78
+ */
79
+ public setInterceptors(interceptors : HttpInterceptors) : void {
91
80
  this.interceptors = interceptors
92
81
  }
93
82
 
94
- /** -------------------
83
+ /**
95
84
  * 核心请求逻辑
96
- * ------------------- */
85
+ */
97
86
  private coreRequest<T>(options : HttpRequestOptions) : Promise<T> {
98
-
99
-
100
-
101
87
  const config = this.beforeRequest(options)
102
88
 
103
89
  return new Promise<T>((resolve, reject) => {
@@ -118,7 +104,9 @@ class HttpRequest {
118
104
  })
119
105
  }
120
106
 
121
- /** 请求前拦截与配置处理 */
107
+ /**
108
+ * 请求前拦截与配置处理
109
+ */
122
110
  private beforeRequest(options : HttpRequestOptions) : HttpRequestOptions {
123
111
  const config : HttpRequestOptions = {
124
112
  ...options,
@@ -130,6 +118,7 @@ class HttpRequest {
130
118
  showLoading: options.showLoading ?? this.showLoading,
131
119
  disableAuth: options.disableAuth ?? this.autoAuth
132
120
  }
121
+
133
122
  // 加载提示
134
123
  if (config.showLoading ?? this.showLoading) {
135
124
  uni.showLoading({ title: config.loadingText ?? '加载中...', mask: true })
@@ -175,8 +164,10 @@ class HttpRequest {
175
164
  return config
176
165
  }
177
166
 
178
- /** 响应处理逻辑 */
179
- private handleResponse<T>(response : any, options : HttpRequestOptions) {
167
+ /**
168
+ * 响应处理逻辑
169
+ */
170
+ private handleResponse<T>(response : any, options : HttpRequestOptions) : Promise<T> {
180
171
  uni.hideLoading()
181
172
 
182
173
  const statusCode = response.statusCode
@@ -189,18 +180,14 @@ class HttpRequest {
189
180
  res = data as ResponseData<T>
190
181
  }
191
182
 
192
-
193
183
  // 自定义响应拦截器(优先)
194
184
  if (this.interceptors.response != null) {
195
- this.interceptors.response(res, options)
196
185
  const intercepted = this.interceptors.response(res, options)
197
186
  if (intercepted !== undefined) {
198
- res = intercepted as any
199
- return Promise.resolve(intercepted)
187
+ return Promise.resolve(intercepted as T)
200
188
  }
201
189
  }
202
190
 
203
-
204
191
  // HTTP 层错误
205
192
  if (statusCode < 200 || statusCode >= 300) {
206
193
  const msg = this.getHttpErrorMsg(statusCode)
@@ -218,11 +205,13 @@ class HttpRequest {
218
205
  return Promise.reject(res)
219
206
  }
220
207
 
221
- return Promise.resolve(res)
208
+ return Promise.resolve(res as T)
222
209
  }
223
210
 
224
- /** 错误统一处理 */
225
- private handleError(error : any, options : HttpRequestOptions) {
211
+ /**
212
+ * 错误统一处理
213
+ */
214
+ private handleError(error : any, options : HttpRequestOptions) : void {
226
215
  uni.hideLoading()
227
216
  if (this.interceptors.error != null) {
228
217
  this.interceptors.error(error, options)
@@ -230,24 +219,32 @@ class HttpRequest {
230
219
  uni.showToast({ title: '网络异常,请稍后重试', icon: 'none' })
231
220
  }
232
221
 
233
- /** HTTP 状态码消息 */
222
+ /**
223
+ * HTTP 状态码消息
224
+ */
234
225
  private getHttpErrorMsg(status : number) : string {
235
- if (status == 404) {
236
- return '请求的资源不存在'
237
- } else if (status == 500) {
238
- return '服务器错误'
239
- } else if (status == 503) {
240
- return '服务不可用'
241
- } else {
242
- return '请求失败'
226
+ const messages : Record<number, string> = {
227
+ 400: '请求参数错误',
228
+ 401: '未授权,请登录',
229
+ 403: '拒绝访问',
230
+ 404: '请求的资源不存在',
231
+ 405: '请求方法不允许',
232
+ 408: '请求超时',
233
+ 500: '服务器错误',
234
+ 502: '网关错误',
235
+ 503: '服务不可用',
236
+ 504: '网关超时'
243
237
  }
238
+ return messages[status] ?? '请求失败'
244
239
  }
245
240
 
246
- /** -------------------
247
- * 快捷请求方法
248
- * ------------------- */
241
+ /**
242
+ * GET 请求
243
+ * @param url 请求地址
244
+ * @param data 请求参数
245
+ * @param options 额外配置
246
+ */
249
247
  public get<T>(url : string, data ?: UTSJSONObject | null, options ?: HttpRequestOptions | null) : Promise<T> {
250
-
251
248
  const config : HttpRequestOptions = {
252
249
  url: url,
253
250
  data: data,
@@ -257,6 +254,12 @@ class HttpRequest {
257
254
  return this.coreRequest<T>(config)
258
255
  }
259
256
 
257
+ /**
258
+ * POST 请求
259
+ * @param url 请求地址
260
+ * @param data 请求数据
261
+ * @param options 额外配置
262
+ */
260
263
  public post<T>(url : string, data ?: UTSJSONObject | null, options ?: HttpRequestOptions | null) : Promise<T> {
261
264
  const config : HttpRequestOptions = {
262
265
  url: url,
@@ -267,6 +270,12 @@ class HttpRequest {
267
270
  return this.coreRequest<T>(config)
268
271
  }
269
272
 
273
+ /**
274
+ * PUT 请求
275
+ * @param url 请求地址
276
+ * @param data 请求数据
277
+ * @param options 额外配置
278
+ */
270
279
  public put<T>(url : string, data ?: UTSJSONObject | null, options ?: HttpRequestOptions | null) : Promise<T> {
271
280
  const config : HttpRequestOptions = {
272
281
  url: url,
@@ -277,6 +286,12 @@ class HttpRequest {
277
286
  return this.coreRequest<T>(config)
278
287
  }
279
288
 
289
+ /**
290
+ * DELETE 请求
291
+ * @param url 请求地址
292
+ * @param data 请求数据
293
+ * @param options 额外配置
294
+ */
280
295
  public delete<T>(url : string, data ?: UTSJSONObject | null, options ?: HttpRequestOptions | null) : Promise<T> {
281
296
  const config : HttpRequestOptions = {
282
297
  url: url,
@@ -287,14 +302,17 @@ class HttpRequest {
287
302
  return this.coreRequest<T>(config)
288
303
  }
289
304
 
290
- /** -------------------
305
+ /**
291
306
  * 文件上传
292
- * ------------------- */
307
+ * @param url 上传地址
308
+ * @param data 表单数据,需包含 File 字段(文件路径)
309
+ * @param options 额外配置
310
+ */
293
311
  public upload<T>(
294
312
  url : string,
295
313
  data ?: UTSJSONObject | null,
296
314
  options ?: HttpRequestOptions | null
297
- ) {
315
+ ) : Promise<T> {
298
316
  const config : HttpRequestOptions = {
299
317
  url: url,
300
318
  data: data,
@@ -330,9 +348,12 @@ class HttpRequest {
330
348
  })
331
349
  }
332
350
 
333
- /** -------------------
334
- * formData 提交(无文件)
335
- * ------------------- */
351
+ /**
352
+ * FormData 提交(无文件)
353
+ * @param url 请求地址
354
+ * @param data 表单数据
355
+ * @param options 额外配置
356
+ */
336
357
  public postFormData<T>(
337
358
  url : string,
338
359
  data ?: UTSJSONObject | null,
@@ -349,7 +370,6 @@ class HttpRequest {
349
370
  }
350
371
  return this.coreRequest<T>(config)
351
372
  }
352
-
353
373
  }
354
374
 
355
- export default HttpRequest
375
+ export {HttpRequest}
@@ -0,0 +1,87 @@
1
+ /**
2
+ * HTTP 请求相关类型定义
3
+ * @module utils/request/type
4
+ */
5
+
6
+ /**
7
+ * 请求方法类型
8
+ */
9
+ export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'OPTIONS' | 'HEAD' | 'TRACE' | 'CONNECT'
10
+
11
+ /**
12
+ * 基础配置
13
+ */
14
+ export interface HttpInitOptions {
15
+ /** 基础 URL */
16
+ baseURL : string
17
+ /** 请求头 */
18
+ header ?: UTSJSONObject
19
+ /** 是否显示加载提示 */
20
+ showLoading ?: boolean
21
+ /** 是否自动附加 Token */
22
+ autoAuth ?: boolean
23
+ /** 超时时间(毫秒) */
24
+ timeout ?: number
25
+ /** 其他扩展配置 */
26
+ [key : string] : any
27
+ }
28
+
29
+ /**
30
+ * 请求配置
31
+ */
32
+ export interface HttpRequestOptions {
33
+ /** 请求 URL */
34
+ url : string
35
+ /** 请求数据 */
36
+ data ?: UTSJSONObject | null
37
+ /** 请求头 */
38
+ header ?: UTSJSONObject | null
39
+ /** 请求方法 */
40
+ method ?: HttpMethod
41
+ /** 超时时间(毫秒) */
42
+ timeout ?: number
43
+ /** 是否显示加载提示 */
44
+ showLoading ?: boolean
45
+ /** 加载提示文字 */
46
+ loadingText ?: string
47
+ /** 是否禁用自动认证 */
48
+ disableAuth ?: boolean
49
+ /** 其他扩展配置 */
50
+ [key : string] : any
51
+ }
52
+
53
+ /**
54
+ * 响应体结构
55
+ */
56
+ export interface ResponseData<T = any> {
57
+ /** 业务状态码 */
58
+ code : number
59
+ /** 提示信息 */
60
+ msg : string
61
+ /** 响应数据 */
62
+ data : T
63
+ /** 数据总数(分页时使用) */
64
+ count : number
65
+ }
66
+
67
+ /**
68
+ * 分页数据响应格式
69
+ */
70
+ export interface DataResponse<T = any> {
71
+ /** 数据列表 */
72
+ data : T | []
73
+ /** 数据总数 */
74
+ total ?: number
75
+ }
76
+
77
+ /**
78
+ * HTTP 拦截器接口
79
+ */
80
+ export interface HttpInterceptors {
81
+ /** 请求拦截器 */
82
+ request ?: (options : HttpRequestOptions) => HttpRequestOptions
83
+ /** 响应拦截器 */
84
+ response ?: <T>(response : ResponseData<T>, options : HttpRequestOptions) => DataResponse<T> | T
85
+ /** 错误拦截器 */
86
+ error ?: (err : any, options : HttpRequestOptions) => void
87
+ }