fetch.js
3.2 KB
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
import router from '@/router'
// import store from '@/store'
// import { Toast } from 'vant'
import { setToken, getToken } from '@/libs/util'
const addErrorLog = errorInfo => {
const { statusText, status, url, headers, data } = errorInfo
let info = {
type: 'ajax',
code: status,
// osInfo: getOs(),
// browserInfo: getBrowser().browser + '/' + getBrowser().version,
msg: errorInfo.message ? errorInfo.message : statusText,
url: url,
dataInfo: data,
headers: headers
}
// if (!url.includes('save_error_logger')) {
// store.dispatch('addErrorLog', info)
// }
}
class HttpRequest {
path = ''
curPath = ''
constructor(baseURL) {
this.baseURL = baseURL
}
setPath(...paths) {
this.curPath = `${this.path}/${paths.join('/')}`
return this
}
replace(...params) {
let count = 0
this.curPath = this.curPath.replace(/\{.*?\}/g, _match => params[count++])
return this
}
request(parames) {
let url = parames.url || ''
let method = parames.method || 'GET'
let data = parames.data || parames.params
let requestUrl = url.indexOf('http://') === -1 && url.indexOf('https://') === -1 ? this.baseURL + url : url
if (this.curPath !== '') {
requestUrl = this.curPath
this.curPath = ''
this.path = ''
}
let options = {
method: method.toUpperCase(),
headers: {
'Content-Type': 'application/json; charset=UTF-8'
},
body: JSON.stringify(data)
}
// 设置token
if (getToken()) {
options.headers.token = getToken()
}
// get请求转换参数
if (method == 'GET') {
delete options.body
let qs = '?'
for (const key in parames.data) {
qs += key + '=' + parames.data[key] + '&'
}
qs = qs.substring(0, qs.length - 1)
qs.length > 1 ? (requestUrl += qs) : ''
}
return new Promise((resolve, reject) => {
fetch(requestUrl, options)
.then(function (response) {
if (response.ok) {
return response.json()
} else {
const errorInfo = {
statusText: response.statusText,
status: response.status,
url: response.url,
headers: JSON.stringify(response.headers),
data: response.data
}
addErrorLog(errorInfo)
if (response.status === 404) {
error('服务器开小差了,请联系客服~')
}
reject(response)
}
})
.then(res => {
successCallBack(res, resolve, reject)
})
.catch(err => {
error()
})
})
}
}
function error(msg = '请检查您的网络') {
// Toast({
// message: msg,
// icon: 'none',
// duration: 3500
// })
}
function successCallBack(res, resolve, reject) {
switch (res.code) {
case 200:
resolve(res.data)
break
case 401:
error('请重新登录!')
setToken('')
router.replace({
name: 'login'
})
break
default:
businessError(res, reject)
break
}
}
function businessError(err, reject) {
// Toast({
// message: err.message,
// duration: 2000
// })
reject(err)
}
export default HttpRequest