Skip to content

Commit 57b1f45

Browse files
author
Shawn Wang
committed
update HttpUtils
1 parent e6375d9 commit 57b1f45

File tree

1 file changed

+52
-90
lines changed

1 file changed

+52
-90
lines changed

src/utils/http/index.ts

Lines changed: 52 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,72 @@
11
import axios from 'axios'
2-
import { AlertUtils } from '@/utils/alert'
32
import { router } from '@/routes/router'
43
import { UrlUtils } from '@/utils/url'
4+
import { AlertUtils } from '@/utils/alert'
55

66
const errorMsgMap = new Map()
77
errorMsgMap.set('Network Error', '网络或服务器错误,请稍后再试')
88

99
export class HttpUtils {
10-
static get(path: string, opts = {}) {
11-
return new Promise(function(resolve, reject) {
12-
axios
13-
.get(path, opts)
14-
.then(function(response) {
15-
resolve(response.data)
16-
})
17-
.catch(function(error) {
18-
reject(error)
19-
})
20-
})
10+
static async get(url: string, data: any = {}, opts = {}) {
11+
try {
12+
if (UrlUtils.stringify(data)) {
13+
url =
14+
url +
15+
(url.includes('?') ? '&' : '?') +
16+
UrlUtils.stringify(data)
17+
}
18+
const resp: any = await axios.get(url, opts)
19+
return resp.data
20+
} catch (e) {
21+
throw e
22+
}
2123
}
2224

23-
static post(path: string, data: any, opts = {}) {
24-
const DEFAULT_POST_OPTS = {
25+
static async post(url: string, data: any, opts = {}) {
26+
const options = {
2527
headers: {
2628
'Content-Type': 'application/x-www-form-urlencoded',
2729
},
30+
...opts,
2831
}
29-
opts = { ...DEFAULT_POST_OPTS, ...opts }
30-
return new Promise(function(resolve, reject) {
31-
axios
32-
.post(path, UrlUtils.stringify(data), opts)
33-
.catch(function(error: any) {
34-
if (typeof error.response === 'undefined') {
35-
resolve({
36-
code: -1,
37-
message: errorMsgMap.get(error.message),
38-
})
39-
} else if (error.response.status === 500) {
40-
try {
41-
resolve(JSON.parse(error.response.data.message))
42-
} catch (e) {
43-
resolve({
44-
code: error.response.status,
45-
message: error.response.data.message,
46-
})
47-
}
48-
} else {
49-
reject(error)
50-
}
32+
try {
33+
let postData = UrlUtils.stringify(data)
34+
if (options.headers['Content-Type'] === 'application/json') {
35+
postData = data
36+
}
37+
const resp: any = await axios.post(url, postData, options)
38+
if (resp && resp.data.code === 401) {
39+
AlertUtils.showSimpleAlert({
40+
title: resp.data.message,
41+
okBtnFn() {
42+
router.push('/login')
43+
},
5144
})
52-
.then(function(response: any) {
53-
if (response && response.data.code === 401) {
54-
AlertUtils.showSimpleAlert({
55-
title: response.data.message,
56-
okBtnFn() {
57-
router.push('/login')
58-
},
59-
})
60-
} else if (response) {
61-
resolve(response.data)
62-
}
63-
})
64-
})
45+
} else if (resp) {
46+
return resp.data
47+
}
48+
} catch (e) {
49+
if (typeof e.response === 'undefined') {
50+
return {
51+
code: -1,
52+
message: e.message,
53+
}
54+
} else if (e.response.status === 500) {
55+
const message = e.response.data.message
56+
try {
57+
return JSON.parse(message)
58+
} catch (e) {
59+
return { code: 500, message }
60+
}
61+
} else {
62+
throw e
63+
}
64+
}
6565
}
6666

67-
static uploadFile(url: string, data: FormData, method = 'post') {
68-
return new Promise(function(resolve, reject) {
69-
axios({
70-
url,
71-
data,
72-
method,
73-
headers: {
74-
'Content-Type': 'multipart/form-data',
75-
},
76-
})
77-
.catch(function(error: any) {
78-
if (typeof error.response === 'undefined') {
79-
resolve({
80-
code: -1,
81-
message: errorMsgMap.get(error.message),
82-
})
83-
} else if (error.response.status === 500) {
84-
try {
85-
resolve(JSON.parse(error.response.data.message))
86-
} catch (e) {
87-
resolve({
88-
code: error.response.status,
89-
message: error.response.data.message,
90-
})
91-
}
92-
} else {
93-
reject(error)
94-
}
95-
})
96-
.then(function(response: any) {
97-
if (response && response.data.code === 401) {
98-
AlertUtils.showSimpleAlert({
99-
title: response.data.message,
100-
okBtnFn() {
101-
router.push('/login')
102-
},
103-
})
104-
} else if (response) {
105-
resolve(response.data)
106-
}
107-
})
67+
static uploadFile(url: string, formData: FormData) {
68+
return HttpUtils.post(url, formData, {
69+
'Content-Type': 'multipart/form-data',
10870
})
10971
}
11072
}

0 commit comments

Comments
 (0)