Uniapp 网络请求封装

Uniapp 网络请求封装

request.js

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

export default {
config: {
baseUrl: '',
header: {
"Content-Type": "application/json"
},
// h5 跨域请求时是否携带凭证(cookies)
withCredentials: true,
// 可在headers中编辑cookie
enableCookie: true,
data: {},
method: "GET",
dataType: "json",
/* 如设为json,会对返回的数据做一次 JSON.parse */
responseType: "text",
success() {},
fail() {},
complete() {}
},
// 请求拦截器
interceptor: {
request: null,
response: null
},
request(options = {}) {
// 获取登录后存储在本地的token信息
const token = uni.getStorageSync('token')

// const requestType = options.url.split('/');
// let typeVal = requestType[requestType.length - 1]

if(token) {
this.config.header.Authorization = token
}

options.baseUrl = options.baseUrl || this.config.baseUrl
options.dataType = options.dataType || this.config.dataType
options.url = options.baseUrl + options.url
options.data = options.data || {}
options.method = options.method || this.config.method


// 基于 Promise 的网络请求
return new Promise((resolve, reject) => {
uni.showLoading()
let _config = null
options.complete = (response) => {
uni.hideLoading()
let statusCode = response.statusCode
response.config = _config
// if (process.env.NODE_ENV === 'development') {
// if (statusCode === 200) {
// // console.log("【" + _config.requestId + "】 结果:" + JSON.stringify(response.data))
// }
// }
if (this.interceptor.response) {
let newResponse = this.interceptor.response(response)
if (newResponse) {
response = newResponse
}
}


// 路由拦截
if(response.errMsg && response.errMsg.split(' ')[0] === "request:fail"){
tool.msg('网络连接失败');
return
}


// if(response.errMsg === "request:fail timeout"){
// tool.msg('网络连接超时');
// return
// }

if(response.statusCode === 401){
if(typeVal === "login"){
tool.msg('账号或密码错误');
return
}else {
tool.msg('登录已过期,请重新登录');
setTimeout( () => {
uni.reLaunch({
url:'/pages/login/login'
})
},1e3)
}
}

// let {
// code,
// msg
// } = response.data
// 请求返回400请求报文存在语法错误时reLaunch到登录页
// if (response.data.code>= 400||response.statusCode>=400||response.status>=400) {
// reject(response)
// }

if(response.statusCode>=400){
reject(response)
}else{
resolve(response)
}

}
_config = Object.assign({}, this.config, options)
_config.requestId = new Date().getTime()
if (this.interceptor.request) {
this.interceptor.request(_config)
}
uni.request(_config);
});
},
// get请求
get(url, data, options = {}) {
options.url = url
options.data = data
options.method = 'GET'
return this.request(options)
},
// post请求
post(url, data, options = {}) {
options.url = url
options.data = data
options.method = 'POST'
return this.request(options)
},
// put请求
put(url, data, options = {}) {
options.url = url
options.data = data
options.method = 'PUT'
return this.request(options)
},
// delete请求
delete(url, data, options = {}) {
options.url = url
options.data = data
options.method = 'DELETE'
return this.request(options)
}
}

使用request.js

login.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import api from '../request.js'

const login = {
loginUser: (data) => {
return api.request({
url: '网络请求地址',
method: 'POST',
data
})
},
account: () => {
return api.request({
url: "请求地址",
method: 'GET'
})
}
}

使用login.js

1
2
3
4
5
6
import login from '@/api/service/login.js'

login.loginUser({username: ''}).then(res => {
// 处理服务器返回的数据
console.error(res)
})

本文作者: 孟 虎
本文链接: https://menghu1994.github.io/blog/2023/10/uni-app/request/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!