| | |
| | | import axios from 'axios' |
| | | import myConfig from '@/assets/js/config.js' |
| | | import myConfig from '@/assets/js/config.ts' |
| | | // 创建 axios 实例 |
| | | const service = axios.create({ |
| | | baseURL: myConfig.requestCtx, |
| | | timeout: myConfig.requestTimeOut // 请求超时时间 |
| | | baseURL: myConfig.requestCtx, |
| | | timeout: myConfig.requestTimeOut, // 请求超时时间 |
| | | }) |
| | | |
| | | // 请求拦截器 |
| | | // app-6MPX5Fr97eK6BWHpy8g7vddd |
| | | |
| | | service.interceptors.request.use( |
| | | (config) => { |
| | | const token = "app-6MPX5Fr97eK6BWHpy8g7vddd" |
| | | if (token) config.headers['Authorization'] = `Bearer ${token}` |
| | | return config |
| | | }, |
| | | (error) => { |
| | | // 发送失败 |
| | | Promise.reject(error) |
| | | } |
| | | (config) => { |
| | | const token = 'app-TmoRR8VCs9nNEb9CxVpyIDcb' |
| | | if (token) config.headers['Authorization'] = `Bearer ${token}` |
| | | config.headers['Content-Type'] = 'application/json;charset=utf-8' |
| | | return config |
| | | }, |
| | | (error) => { |
| | | // 发送失败 |
| | | Promise.reject(error) |
| | | }, |
| | | ) |
| | | |
| | | // 响应拦截器 |
| | | service.interceptors.response.use( |
| | | (response) => { |
| | | // dataAxios 是 axios 返回数据中的 data |
| | | const dataAxios = response.data |
| | | if (typeof dataAxios.data === 'boolean') { |
| | | return dataAxios.data |
| | | } |
| | | if (response.config.responseType == 'blob') { |
| | | return dataAxios |
| | | } |
| | | const { success } = dataAxios |
| | | if (dataAxios.currentDate) { |
| | | sessionStorage.currentDate = new Date(dataAxios.currentDate).getTime() |
| | | } |
| | | // 根据 code 进行判断 |
| | | if (success) { |
| | | return dataAxios.data ? dataAxios.data : dataAxios |
| | | } else { |
| | | // 提示错误 |
| | | } |
| | | }, |
| | | (error) => { |
| | | if (error.response && error.response.data && error.response.data.error) { |
| | | console.error(error.response.data.error.msg) |
| | | } else { |
| | | console.error('请求发生错误') |
| | | } |
| | | return Promise.reject(error) |
| | | } |
| | | (response) => { |
| | | if (response.status == 200 && response.statusText == 'OK') { |
| | | return response.data |
| | | } |
| | | }, |
| | | (error) => { |
| | | if (error.response && error.response.data && error.response.data.error) { |
| | | console.error(error.response.data.error.msg) |
| | | } else { |
| | | console.error('请求发生错误') |
| | | } |
| | | return Promise.reject(error) |
| | | }, |
| | | ) |
| | | |
| | | export default service |