|  |  |  | 
|---|
|  |  |  | import ax from "axios" | 
|---|
|  |  |  | import { IS_PRODUCTION } from "@/common/config" | 
|---|
|  |  |  | // import { wsCache } from "@/common/untils/wsCache" | 
|---|
|  |  |  | import { responseHandler, errorResponseHandler } from "./responseHandler" | 
|---|
|  |  |  | import { assign, map } from "lodash" | 
|---|
|  |  |  | import faker from "faker/locale/zh_CN" | 
|---|
|  |  |  | import { service, request } from "./service" | 
|---|
|  |  |  | import * as tools from "./tools" | 
|---|
|  |  |  |  | 
|---|
|  |  |  | const JsonText = window.getServerJson | 
|---|
|  |  |  | const axios = ax.create({ | 
|---|
|  |  |  | baseURL: IS_PRODUCTION ? JsonText.managePath : "", // url = base url + request url | 
|---|
|  |  |  | withCredentials: true, | 
|---|
|  |  |  | headers: { | 
|---|
|  |  |  | "X-Requested-With": "XMLHttpRequest", // 给后台区分异步请求 | 
|---|
|  |  |  | "Content-Type": "application/json;charset=UTF-8" | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | timeout: 300000 // request timeout | 
|---|
|  |  |  | }) | 
|---|
|  |  |  | // 添加一个请求拦截器 | 
|---|
|  |  |  | axios.interceptors.request.use( | 
|---|
|  |  |  | (request) => { | 
|---|
|  |  |  | if (request.data instanceof FormData) { | 
|---|
|  |  |  | // 不处理 | 
|---|
|  |  |  | } else if (request.data) { | 
|---|
|  |  |  | request.data["domain"] = JsonText.systemCode | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | request.data = { domain: JsonText.systemCode } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | const files = require.context("./modules", true, /\.api\.js$/) | 
|---|
|  |  |  | const generators = files.keys().map((key) => files(key).default) | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //用户登录标记 | 
|---|
|  |  |  | // const token = wsCache.get('token') | 
|---|
|  |  |  | // request.headers['token'] = token | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return request | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | (error) => { | 
|---|
|  |  |  | return Promise.reject(error) | 
|---|
|  |  |  | } | 
|---|
|  |  |  | export default assign( | 
|---|
|  |  |  | {}, | 
|---|
|  |  |  | ...map(generators, (generator) => | 
|---|
|  |  |  | generator({ | 
|---|
|  |  |  | service, | 
|---|
|  |  |  | request, | 
|---|
|  |  |  | faker, | 
|---|
|  |  |  | tools | 
|---|
|  |  |  | }) | 
|---|
|  |  |  | ) | 
|---|
|  |  |  | ) | 
|---|
|  |  |  | // 添加一个响应拦截器 | 
|---|
|  |  |  | axios.interceptors.response.use( | 
|---|
|  |  |  | (response) => { | 
|---|
|  |  |  | // 处理响应请求池 | 
|---|
|  |  |  | if (response.data.status.toString() === "200" && response.data.success === true) { | 
|---|
|  |  |  | return response | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | responseHandler(response) | 
|---|
|  |  |  | return Promise.reject(response.data) | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | (error) => { | 
|---|
|  |  |  | errorResponseHandler(error) | 
|---|
|  |  |  | return Promise.reject(error) | 
|---|
|  |  |  | } | 
|---|
|  |  |  | ) | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 通用方法 | 
|---|
|  |  |  | export const POST = (url, params, config = {}) => { | 
|---|
|  |  |  | return axios.post(`${url}`, params, config).then((res) => res.data) | 
|---|
|  |  |  | } | 
|---|
|  |  |  | export const GET = (url, params, config = {}) => { | 
|---|
|  |  |  | return axios.get(`${url}`, { params: params, ...config }).then((res) => res.data) | 
|---|
|  |  |  | } | 
|---|
|  |  |  | export const PUT = (url, params, config = {}) => { | 
|---|
|  |  |  | return axios.put(`${url}`, params, config).then((res) => res.data) | 
|---|
|  |  |  | } | 
|---|
|  |  |  | export const DELETE = (url, params, config = {}) => { | 
|---|
|  |  |  | return axios.delete(`${url}`, { params: params }, config).then((res) => res.data) | 
|---|
|  |  |  | } | 
|---|
|  |  |  | export const PATCH = (url, params, config = {}) => { | 
|---|
|  |  |  | return axios.patch(`${url}`, params, config).then((res) => res.data) | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | export const Axios = axios | 
|---|