| | |
| | | import axios from 'axios'; |
| | | import { ElMessage } from 'element-plus'; |
| | | |
| | | const openLog = false; |
| | | const debug = true; |
| | | |
| | | // let ip1 = 'http://114.215.109.124:8805/'; |
| | | let ip1 = 'http://47.100.191.150:9029/'; |
| | | let ws = `47.100.191.150:9030`; |
| | | // console.log(import.meta.env); |
| | | if (import.meta.env.VITE_DATA_MODE == 'jingan') { |
| | | ip1 = 'http://114.215.109.124:8805/'; |
| | | ws = `114.215.109.124:9030`; |
| | | } |
| | | |
| | | if (debug) { |
| | | ip1 = 'http://192.168.0.138:8084/'; |
| | | ip1 = 'http://192.168.0.110:8084/'; |
| | | // ip1 = 'http://localhost:8084/'; |
| | | ws = `192.168.0.110:9031`; |
| | | // ws = `localhost:9031`; |
| | | } |
| | | |
| | | const $http = axios.create({ |
| | | baseURL: ip1, |
| | | timeout: 20000 |
| | | timeout: 30000 |
| | | }); |
| | | |
| | | //添加拦截器 |
| | |
| | | i.interceptors.request.use( |
| | | function (config) { |
| | | // 在发送请求之前做些什么 |
| | | console.log('==>请求开始'); |
| | | console.log(`${config.baseURL}${config.url}`); |
| | | if (config.data) { |
| | | console.log('==>请求数据', config.data); |
| | | if (import.meta.env.DEV && openLog) { |
| | | console.log('==>请求开始'); |
| | | console.log(`${config.baseURL}${config.url}`); |
| | | if (config.data) { |
| | | console.log('==>请求数据', config.data); |
| | | } |
| | | } |
| | | return config; |
| | | }, |
| | | function (error) { |
| | | // 对请求错误做些什么 |
| | | console.log('==>请求开始'); |
| | | console.log(error); |
| | | if (import.meta.env.DEV && openLog) { |
| | | console.log('==>请求开始'); |
| | | console.log(error); |
| | | } |
| | | ElMessage({ |
| | | message: error, |
| | | type: 'error' |
| | |
| | | function (response) { |
| | | // 2xx 范围内的状态码都会触发该函数。 |
| | | // 对响应数据做点什么 |
| | | console.log(response); |
| | | console.log('==>请求结束'); |
| | | if (import.meta.env.DEV && openLog) { |
| | | console.log(response); |
| | | console.log('==>请求结束'); |
| | | } |
| | | if (response.status == 200) { |
| | | if ( |
| | | response.data.success != undefined && |
| | |
| | | function (error) { |
| | | // 超出 2xx 范围的状态码都会触发该函数。 |
| | | // 对响应错误做点什么 |
| | | console.log(error); |
| | | console.log('==>请求结束'); |
| | | if (import.meta.env.DEV && openLog) { |
| | | console.log(error); |
| | | console.log('==>请求结束'); |
| | | } |
| | | ElMessage({ |
| | | message: error, |
| | | type: 'error' |
| | |
| | | ); |
| | | }); |
| | | |
| | | export { $http }; |
| | | function resToData(res) { |
| | | res.resp = res.resp.then((res) => res.data); |
| | | return res; |
| | | } |
| | | |
| | | export { $http, resToData, ws }; |