|
1 |
| -import axios from 'axios' |
2 |
| -import {server} from "./env"; |
3 |
| -import {message} from 'antd'; |
4 |
| -import {getHeaders} from "../utils/utils"; |
| 1 | +import axios from 'axios'; |
| 2 | +import { server } from './env'; |
| 3 | +import { message } from 'antd'; |
| 4 | +import { getHeaders } from '../utils/utils'; |
5 | 5 |
|
6 | 6 | // 测试地址
|
7 | 7 | // axios.defaults.baseURL = server;
|
8 | 8 | // 线上地址
|
9 | 9 | axios.defaults.baseURL = server;
|
10 | 10 |
|
11 | 11 | const handleError = (error) => {
|
12 |
| - if ("Network Error" === error.toString()) { |
13 |
| - message.error('网络异常'); |
14 |
| - return false; |
15 |
| - } |
16 |
| - if (error.response !== undefined && error.response.status === 401) { |
17 |
| - window.location.href = '#/login'; |
18 |
| - return false; |
19 |
| - } |
20 |
| - if (error.response !== undefined) { |
21 |
| - message.error(error.response.data.message); |
22 |
| - return false; |
23 |
| - } |
24 |
| - return true; |
| 12 | + if ('Network Error' === error.toString()) { |
| 13 | + message.error('网络异常'); |
| 14 | + return false; |
| 15 | + } |
| 16 | + if (error.response !== undefined && error.response.status === 401) { |
| 17 | + window.location.href = '#/login'; |
| 18 | + return false; |
| 19 | + } |
| 20 | + if (error.response !== undefined) { |
| 21 | + message.error(error.response.data.message); |
| 22 | + return false; |
| 23 | + } |
| 24 | + return true; |
25 | 25 | };
|
26 | 26 |
|
27 | 27 | const handleResult = (result) => {
|
28 |
| - if (result['code'] === 401) { |
29 |
| - window.location.href = '#/login'; |
30 |
| - return false; |
31 |
| - }if (result['code'] === 403) { |
32 |
| - window.location.href = '#/permission-denied'; |
33 |
| - return false; |
34 |
| - } else if (result['code'] === 100) { |
35 |
| - return true; |
36 |
| - } else if (result['code'] !== 1) { |
37 |
| - message.error(result['message']); |
38 |
| - return false; |
39 |
| - } |
| 28 | + if (result['code'] === 401) { |
| 29 | + window.location.href = '#/login'; |
| 30 | + return false; |
| 31 | + } |
| 32 | + if (result['code'] === 403) { |
| 33 | + window.location.href = '#/permission-denied'; |
| 34 | + return false; |
| 35 | + } else if (result['code'] === 100) { |
40 | 36 | return true;
|
41 |
| -} |
| 37 | + } else if (result['code'] !== 1) { |
| 38 | + message.error(result['message']); |
| 39 | + return false; |
| 40 | + } |
| 41 | + return true; |
| 42 | +}; |
42 | 43 |
|
43 | 44 | const request = {
|
44 |
| - |
45 |
| - get: function (url) { |
46 |
| - const headers = getHeaders(); |
47 |
| - |
48 |
| - return new Promise((resolve, reject) => { |
49 |
| - axios.get(url, {headers: headers}) |
50 |
| - .then((response) => { |
51 |
| - let contentType = response.headers['content-type']; |
52 |
| - if (contentType !== '' && contentType.includes('application/json')) { |
53 |
| - handleResult(response.data); |
54 |
| - } |
55 |
| - resolve(response.data); |
56 |
| - }) |
57 |
| - .catch((error) => { |
58 |
| - if (!handleError(error)) { |
59 |
| - return; |
60 |
| - } |
61 |
| - reject(error); |
62 |
| - }); |
| 45 | + get: function (url) { |
| 46 | + const headers = getHeaders(); |
| 47 | + |
| 48 | + return new Promise((resolve, reject) => { |
| 49 | + fetch(url, { headers }) |
| 50 | + .then((response) => { |
| 51 | + let contentType = response.headers.get('content-type'); |
| 52 | + if (contentType !== '' && contentType.includes('application/json')) { |
| 53 | + return response.json(); |
| 54 | + } |
| 55 | + return response.text(); |
63 | 56 | })
|
64 |
| - }, |
65 |
| - |
66 |
| - post: function (url, params, header) { |
67 |
| - |
68 |
| - const headers = getHeaders(); |
69 |
| - if (header) { |
70 |
| - for (const k in header) { |
71 |
| - headers[k] = header[k]; |
72 |
| - } |
73 |
| - } |
74 |
| - |
75 |
| - |
76 |
| - return new Promise((resolve, reject) => { |
77 |
| - axios.post(url, params, {headers: headers}) |
78 |
| - .then((response) => { |
79 |
| - handleResult(response.data); |
80 |
| - resolve(response.data); |
81 |
| - }) |
82 |
| - .catch((error) => { |
83 |
| - if (!handleError(error)) { |
84 |
| - return; |
85 |
| - } |
86 |
| - reject(error); |
87 |
| - }); |
| 57 | + .then((result) => { |
| 58 | + if (typeof result === 'object') { |
| 59 | + handleResult(result); |
| 60 | + } |
| 61 | + resolve(result); |
88 | 62 | })
|
89 |
| - }, |
90 |
| - |
91 |
| - put: function (url, params) { |
92 |
| - |
93 |
| - const headers = getHeaders(); |
| 63 | + .catch((error) => { |
| 64 | + if (!handleError(error)) { |
| 65 | + return; |
| 66 | + } |
| 67 | + reject(error); |
| 68 | + }); |
| 69 | + }); |
| 70 | + }, |
| 71 | + |
| 72 | + post: function (url, params, header) { |
| 73 | + const headers = getHeaders(); |
| 74 | + if (header) { |
| 75 | + for (const k in header) { |
| 76 | + headers[k] = header[k]; |
| 77 | + } |
| 78 | + } |
94 | 79 |
|
95 |
| - return new Promise((resolve, reject) => { |
96 |
| - axios.put(url, params, {headers: headers}) |
97 |
| - .then((response) => { |
98 |
| - handleResult(response.data); |
99 |
| - resolve(response.data); |
100 |
| - }) |
101 |
| - .catch((error) => { |
102 |
| - if (!handleError(error)) { |
103 |
| - return; |
104 |
| - } |
105 |
| - reject(error); |
106 |
| - }); |
| 80 | + return new Promise((resolve, reject) => { |
| 81 | + axios |
| 82 | + .post(url, params, { headers: headers }) |
| 83 | + .then((response) => { |
| 84 | + handleResult(response.data); |
| 85 | + resolve(response.data); |
107 | 86 | })
|
108 |
| - }, |
109 |
| - |
110 |
| - delete: function (url) { |
111 |
| - const headers = getHeaders(); |
112 |
| - |
113 |
| - return new Promise((resolve, reject) => { |
114 |
| - axios.delete(url, {headers: headers}) |
115 |
| - .then((response) => { |
116 |
| - handleResult(response.data); |
117 |
| - resolve(response.data); |
118 |
| - }) |
119 |
| - .catch((error) => { |
120 |
| - if (!handleError(error)) { |
121 |
| - return; |
122 |
| - } |
123 |
| - reject(error); |
124 |
| - }); |
| 87 | + .catch((error) => { |
| 88 | + if (!handleError(error)) { |
| 89 | + return; |
| 90 | + } |
| 91 | + reject(error); |
| 92 | + }); |
| 93 | + }); |
| 94 | + }, |
| 95 | + |
| 96 | + put: function (url, params) { |
| 97 | + const headers = getHeaders(); |
| 98 | + |
| 99 | + return new Promise((resolve, reject) => { |
| 100 | + axios |
| 101 | + .put(url, params, { headers: headers }) |
| 102 | + .then((response) => { |
| 103 | + handleResult(response.data); |
| 104 | + resolve(response.data); |
125 | 105 | })
|
126 |
| - }, |
127 |
| - |
128 |
| - patch: function (url, params) { |
129 |
| - const headers = getHeaders(); |
130 |
| - |
131 |
| - return new Promise((resolve, reject) => { |
132 |
| - axios.patch(url, params, {headers: headers}) |
133 |
| - .then((response) => { |
134 |
| - handleResult(response.data); |
135 |
| - resolve(response.data); |
136 |
| - }) |
137 |
| - .catch((error) => { |
138 |
| - if (!handleError(error)) { |
139 |
| - return; |
140 |
| - } |
141 |
| - reject(error); |
142 |
| - }); |
| 106 | + .catch((error) => { |
| 107 | + if (!handleError(error)) { |
| 108 | + return; |
| 109 | + } |
| 110 | + reject(error); |
| 111 | + }); |
| 112 | + }); |
| 113 | + }, |
| 114 | + |
| 115 | + delete: function (url) { |
| 116 | + const headers = getHeaders(); |
| 117 | + |
| 118 | + return new Promise((resolve, reject) => { |
| 119 | + axios |
| 120 | + .delete(url, { headers: headers }) |
| 121 | + .then((response) => { |
| 122 | + handleResult(response.data); |
| 123 | + resolve(response.data); |
| 124 | + }) |
| 125 | + .catch((error) => { |
| 126 | + if (!handleError(error)) { |
| 127 | + return; |
| 128 | + } |
| 129 | + reject(error); |
| 130 | + }); |
| 131 | + }); |
| 132 | + }, |
| 133 | + |
| 134 | + patch: function (url, params) { |
| 135 | + const headers = getHeaders(); |
| 136 | + |
| 137 | + return new Promise((resolve, reject) => { |
| 138 | + axios |
| 139 | + .patch(url, params, { headers: headers }) |
| 140 | + .then((response) => { |
| 141 | + handleResult(response.data); |
| 142 | + resolve(response.data); |
143 | 143 | })
|
144 |
| - }, |
| 144 | + .catch((error) => { |
| 145 | + if (!handleError(error)) { |
| 146 | + return; |
| 147 | + } |
| 148 | + reject(error); |
| 149 | + }); |
| 150 | + }); |
| 151 | + }, |
145 | 152 | };
|
146 |
| -export default request |
| 153 | +export default request; |
0 commit comments