| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- import axios from 'axios'
- import { MessageFu } from './message'
- import { Toast } from 'antd-mobile'
- import { domShowFu } from './domShow'
- import { isMobileFu } from './history'
- const envFlag = process.env.NODE_ENV === 'development'
- const baseUrlTemp = 'https://sit-zhongliuyiyuan.4dage.com' // 测试环境
- // const baseUrlTemp = 'http://192.168.20.13:8079' // 线下环境
- const baseFlag = baseUrlTemp.includes('https://')
- // 请求基地址
- export const baseURL = envFlag ? `${baseUrlTemp}${baseFlag ? '' : '/api/'}` : ''
- // 处理 类型“AxiosResponse<any, any>”上不存在属性“code”
- declare module 'axios' {
- interface AxiosResponse {
- code: number
- // 这里追加你的参数
- }
- }
- // 创建 axios 实例
- const http = axios.create({
- baseURL: `${baseURL}${baseFlag ? '/api/' : ''}`,
- timeout: 10000
- })
- let axajInd = 0
- // 请求拦截器
- http.interceptors.request.use(
- function (config: any) {
- // 发请求前打开加载提示
- domShowFu('#AsyncSpinLoding', true)
- axajInd++
- return config
- },
- function (err) {
- return Promise.reject(err)
- }
- )
- let timeId = -1
- // 响应拦截器
- http.interceptors.response.use(
- function (response) {
- // 请求回来的关闭加载提示
- axajInd--
- if (axajInd === 0) {
- domShowFu('#AsyncSpinLoding', false)
- }
- if (response.data.code === 0) {
- // MessageFu.success(response.data.msg);
- } else {
- isMobileFu() && response.data.msg
- ? Toast.show({
- icon: 'fail',
- content: response.data.msg
- })
- : MessageFu.warning(response.data.msg)
- }
- return response.data
- },
- async function (err) {
- clearTimeout(timeId)
- timeId = window.setTimeout(() => {
- axajInd = 0
- domShowFu('#AsyncSpinLoding', false)
- // 如果因为网络原因,response没有,给提示消息
- if (!err.response) {
- isMobileFu()
- ? Toast.show({
- icon: 'fail',
- content: '网络繁忙,请稍后重试!'
- })
- : MessageFu.error('网络繁忙,请稍后重试!')
- } else {
- isMobileFu()
- ? Toast.show({
- icon: 'fail',
- content: '响应错误,请联系管理员!'
- })
- : MessageFu.error('响应错误,请联系管理员!')
- }
- }, 100)
- return Promise.reject(err)
- }
- )
- // 导出 axios 实例
- export default http
|