1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
| import { CustomRequestOptions } from '@/interceptors/request'
| import { useUserStore, useAccessStore } from '@/store'
|
| export const http = <T>(options: CustomRequestOptions) => {
| uni.showLoading({
| title: '加载中',
| })
| // 1. 返回 Promise 对象
| return new Promise<IResData<T>>((resolve, reject) => {
| uni.request({
| ...options,
| dataType: 'json',
| // #ifndef MP-WEIXIN
| responseType: 'json',
| // #endif
| // 响应成功
| success(res) {
| uni.hideLoading()
| // 状态码 2xx,参考 axios 的设计
| if (res.statusCode >= 200 && res.statusCode < 300) {
| // 2.1 提取核心数据 res.data
| if ((res.data as IResData<T>).code === 200) {
| resolve(((res.data as IResData<T>).data || res.data) as IResData<T>)
| } else if ((res.data as IResData<T>).code === 401) {
| uni.showToast({
| icon: 'none',
| title: '登录超时,请重新登录!',
| })
| // 401错误 -> 清理用户信息,跳转到登录页
| useAccessStore().clearAccessInfo()
| useUserStore().clearUserInfo()
| const loginRoute = '/pages/login/index'
| // 重新登录后返回页面
| const url = '/pages/home/index'
| const redirectRoute = `${loginRoute}?redirect=${encodeURIComponent(url)}`
| uni.navigateTo({ url: redirectRoute })
| reject(res)
| } else {
| // 其他错误 -> 根据后端错误信息轻提示
| uni.showToast({
| icon: 'none',
| title: (res.data as IResData<T>).msg || '请求错误',
| })
| reject(res)
| }
| } else if (res.statusCode === 401) {
| // 401错误 -> 清理用户信息,跳转到登录页
| useAccessStore().clearAccessInfo()
| useUserStore().clearUserInfo()
| uni.navigateTo({ url: '/pages/login/index' })
| reject(res)
| } else {
| // 其他错误 -> 根据后端错误信息轻提示
| !options.hideErrorToast &&
| uni.showToast({
| icon: 'none',
| title: (res.data as IResData<T>).msg || '请求错误',
| })
| reject(res)
| }
| },
| // 响应失败
| fail(err) {
| uni.showToast({
| icon: 'none',
| title: '网络错误,换个网络试试',
| })
| reject(err)
| uni.hideLoading()
| },
| })
| })
| }
|
| /**
| * GET 请求
| * @param url 后台地址
| * @param query 请求query参数
| * @returns
| */
| export const httpGet = <T>(url: string, query?: Record<string, any>) => {
| return http<T>({
| url,
| query,
| method: 'GET',
| })
| }
|
| /**
| * POST 请求
| * @param url 后台地址
| * @param data 请求body参数
| * @param query 请求query参数,post请求也支持query,很多微信接口都需要
| * @returns
| */
| export const httpPost = <T>(
| url: string,
| data?: Record<string, any>,
| query?: Record<string, any>,
| ) => {
| return http<T>({
| url,
| query,
| data,
| method: 'POST',
| })
| }
|
| export const httpPut = <T>(
| url: string,
| data?: Record<string, any>,
| query?: Record<string, any>,
| ) => {
| return http<T>({
| url,
| query,
| data,
| method: 'PUT',
| })
| }
|
| export const httpDel = <T>(url: string) => {
| return http<T>({
| url,
| method: 'DELETE',
| })
| }
|
| http.get = httpGet
| http.post = httpPost
| http.put = httpPut
| http.del = httpDel
|
|