Taro+Vue3
# 接口封装
import Taro from '@tarojs/taro';
import {getToken} from './auth'
import {API_BASE_URL} from './config'
import {toast} from "./common";
// usePermissionStore 状态管理
import {usePermissionStore} from "../stores/permission";
interface IRequestOptions {
url: string;
method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'OPTIONS' | 'HEAD';
data?: any;
header?: any;
}
interface IResponse<T> {
code: number;
msg: string;
data?: T;
permissions?: any[];
roles?: any[];
user?: User;
}
interface User {
searchValue?: any;
createBy: string;
createTime: string;
updateBy?: any;
updateTime?: any;
remark?: any;
params: Param;
userId: string;
deptId?: any;
userName: string;
nickName: string;
email: string;
phonenumber: string;
sex: string;
avatar: string;
password: string;
status: string;
delFlag: string;
loginIp: string;
loginDate?: any;
dept?: any;
wxOpenid: string;
roles: any[];
roleIds?: any;
postIds?: any;
roleId?: any;
admin: boolean;
}
interface Param {
}
type RequestInterceptor = (options: IRequestOptions) => IRequestOptions | Promise<IRequestOptions>;
type ResponseInterceptor = <T>(response: IResponse<T>) => IResponse<T> | Promise<IResponse<T>>;
class Request {
private baseURL: string;
private requestInterceptor?: RequestInterceptor;
private responseInterceptor?: ResponseInterceptor;
constructor(baseURL: string) {
this.baseURL = baseURL;
}
private async request<T>(options: IRequestOptions): Promise<IResponse<T>> {
// 请求拦截器
if (this.requestInterceptor) {
options = await this.requestInterceptor(options);
}
const isToken = (options.header || {}).isToken === false
options.header = options.header || {}
if (getToken() && !isToken) {
options.header['Authorization'] = 'Bearer ' + getToken()
}
return Taro.request({
url: `${this.baseURL}${options.url}`,
method: options.method || 'GET',
data: options.data || {},
header: options.header
}).then(response => {
// 响应拦截器
if (this.responseInterceptor) {
response.data = this.responseInterceptor(response.data);
}
let {code} = response.data
if (code === 200) {
return response.data as IResponse<T>;
} else {
let message: string = ''
switch (code) {
case 400:
message = "请求错误(400)";
break;
case 401:
// message = "未授权,请重新登录(401)";
const permission = usePermissionStore()
permission.LogOut()
Taro.reLaunch({url: '/pages/login/index'})
break;
case 403:
message = "拒绝访问(403)";
break;
case 404:
message = "请求出错(404)";
break;
case 408:
message = "请求超时(408)";
break;
case 500:
message = "服务器错误(500)";
break;
case 501:
message = "服务未实现(501)";
break;
case 502:
message = "网络错误(502)";
break;
case 503:
message = "服务不可用(503)";
break;
case 504:
message = "网络超时(504)";
break;
case 505:
message = "HTTP版本不受支持(505)";
break;
default:
message = `连接出错(${code})!`;
}
if(code !== 401){
toast(`${message}`)
}
throw new Error(`请求失败,状态码:${code}`);
}
});
}
// 设置请求拦截器
public setRequestInterceptor(interceptor: RequestInterceptor): void {
this.requestInterceptor = interceptor;
}
// 设置响应拦截器
public setResponseInterceptor(interceptor: ResponseInterceptor): void {
this.responseInterceptor = interceptor;
}
public get<T>(url: string, data?: any, header?: any): Promise<IResponse<T>> {
return this.request<T>({url, method: 'GET', data, header});
}
public post<T>(url: string, data?: any, header?: any): Promise<IResponse<T>> {
return this.request<T>({url, method: 'POST', data, header});
}
public put<T>(url: string, data?: any, header?: any): Promise<IResponse<T>> {
return this.request<T>({url, method: 'PUT', data, header});
}
public delete<T>(url: string, data?: any, header?: any): Promise<IResponse<T>> {
return this.request<T>({url, method: 'DELETE', data, header});
}
}
const request = new Request(API_BASE_URL);
export default request;
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# 接口使用
import request from "../utils/request";
export function login(username: string, password: string, code: string, uuid: string) {
const data = {
username,
password,
code,
uuid
}
return request.post('/auth/login', data, {isToken: false});
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
上次更新: 2024/08/14, 04:14:33