wui-components-v2 1.1.94 → 1.1.95

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.
Files changed (2) hide show
  1. package/api/core/index.ts +51 -13
  2. package/package.json +1 -1
package/api/core/index.ts CHANGED
@@ -12,7 +12,7 @@ interface RequestOptions {
12
12
  url: string
13
13
  method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD' | 'OPTIONS' | 'TRACE' | 'CONNECT'
14
14
  data?: any
15
- header?: any
15
+ header?: Record<string, string>
16
16
  timeout?: number
17
17
  }
18
18
 
@@ -22,6 +22,7 @@ function request(options: RequestOptions): Promise<any> {
22
22
  const token = uni.getStorageSync('TOKEN') || uni.getStorageSync('token') || ''
23
23
  const hydrocarbonClient = uni.getStorageSync('HYDROCARBON_CLIENT') || ''
24
24
  const applicationToken = uni.getStorageSync('HYDROCARBON_APPLICATION_TOKEN') || ''
25
+
25
26
  return new Promise((resolve, reject) => {
26
27
  // 设置默认值
27
28
  const defaultOptions = {
@@ -50,23 +51,60 @@ function request(options: RequestOptions): Promise<any> {
50
51
 
51
52
  uni.request({
52
53
  ...requestOptions,
53
- success: (res: any) => {
54
- // 可以根据实际业务需求调整这里的判断条件
55
- if (res.statusCode >= 200 && res.statusCode < 300) {
56
- if (res.data.status === 'error') {
57
- uni.showToast({ title: res.data.message, icon: 'none' })
58
- uni.removeStorageSync('token')
59
- reject(res)
60
- return
61
- }
62
- resolve(res.data)
63
- } else {
54
+ success: (res: UniApp.RequestSuccessCallbackResult) => {
55
+ console.log('res111111', res)
56
+ const { statusCode, data } = res
57
+
58
+ // 细分HTTP状态码错误处理,移除200-300区间整体判断
59
+ if (statusCode === 401) {
60
+ uni.showToast({ title: '登录身份已失效,请重新登录', icon: 'none' })
61
+ uni.removeStorageSync('token')
62
+ uni.removeStorageSync('TOKEN')
63
+ reject(res)
64
+ uni.reLaunch({ url: '/pages/login/index' })
65
+ return
66
+ }
67
+ if (statusCode === 403) {
68
+ uni.showToast({ title: '当前账号无访问权限', icon: 'none' })
69
+ reject(res)
70
+ return
71
+ }
72
+ if (statusCode === 404) {
73
+ uni.showToast({ title: '请求接口不存在', icon: 'none' })
74
+ reject(res)
75
+ return
76
+ }
77
+ if (statusCode >= 500) {
78
+ uni.showToast({ title: '服务器异常,请稍后重试', icon: 'none' })
64
79
  reject(res)
80
+ return
65
81
  }
82
+ if (statusCode < 200 || statusCode >= 300) {
83
+ uni.showToast({ title: `请求异常 ${statusCode}`, icon: 'none' })
84
+ reject(res)
85
+ return
86
+ }
87
+
88
+ // 业务状态错误判断
89
+ if (data.status === 'error') {
90
+ uni.showToast({ title: data.message || '操作失败', icon: 'none' })
91
+ uni.removeStorageSync('token')
92
+ uni.removeStorageSync('TOKEN')
93
+ reject(res)
94
+ return
95
+ }
96
+
97
+ // 全部校验通过,返回原始业务data
98
+ resolve(data)
66
99
  },
67
- fail: (err: any) => {
100
+ fail: (err: UniApp.RequestFailCallbackResult) => {
101
+ uni.showToast({ title: '网络请求失败,请检查网络或接口地址', icon: 'none' })
68
102
  reject(err)
69
103
  },
104
+ // 增加complete统一处理(可选,如需loading可在这里隐藏)
105
+ complete: () => {
106
+ // uni.hideLoading()
107
+ },
70
108
  })
71
109
  })
72
110
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wui-components-v2",
3
- "version": "1.1.94",
3
+ "version": "1.1.95",
4
4
  "description": "wui 组件库",
5
5
  "author": "wgxshh",
6
6
  "license": "MIT",