1 | const Qs = require("querystring");
|
2 | const axios = require("axios");
|
3 |
|
4 | const request = function request(ops) {
|
5 | const option = Object.assign(
|
6 | {
|
7 | timeout: 30000,
|
8 | withCredentials: true,
|
9 | headers: {
|
10 | "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
|
11 | },
|
12 | transformRequest: params => Qs.stringify(params)
|
13 | },
|
14 | ops
|
15 | );
|
16 | return axios.create(option);
|
17 | };
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | const attach = function attach(promise) {
|
23 | return promise.catch(function(err) {
|
24 | if (err.code === "ECONNABORTED") {
|
25 | console.log("超时");
|
26 | }
|
27 | const response = err.response;
|
28 |
|
29 | if (response) {
|
30 | console.log("服务器错误", response.status);
|
31 | return { data: { status: "error", data: response.status } };
|
32 | }
|
33 | });
|
34 | };
|
35 |
|
36 | module.exports = { attach, request };
|