UNPKG

815 BJavaScriptView Raw
1const Qs = require("querystring");
2const axios = require("axios");
3
4export const request = function request(ops) {
5 const option = Object.assign(
6 {
7 timeout: 30000,
8 withCredentials: true, //是否发送coocie
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 */
22export 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};