1.request.js
import axios from 'axios'
import { message as Message } from './message.js'
import router from '../router'
import store from '@/store'
import { isMobile } from './index.js'
const INTERVAL_MAP = {}
const ERROR_MAP = {
'ECONNABORTED': '请求超时,请重新请求',
'INTERVAL_ERROR': '操作过于频繁'
}
// create an axios instance
export default function(baseURL, config = {}, { interval = 500, timeout = 5000 } = { interval: 500, timeout: 5000 }) {
const service = axios.create({
baseURL,
timeout // request timeout
})
// request interceptor
service.interceptors.request.use(
_config => {
let str = '';
if ('POST' === _config.method.toUpperCase()) {
str += ('?' + Object.entries(_config.data || {}).map(s => {
if ('object' === typeof(s[1])) {
s = [s[0], JSON.stringify(s[1])];
}
return s.join('=');
}).join('&'));
} else {
str += ('?' + Object.entries(_config.params || {}).map(s => {
if ('object' === typeof(s[1])) {
s = [s[0], JSON.stringify(s[1])];
}
return s.join('=');
}).join('&'));
}
if (INTERVAL_MAP[_config.url + str] && Date.now() - INTERVAL_MAP[_config.url + str] < interval) {
return Promise.reject(new Error(`IntervalError: ${_config.url}`))
}
const token = store.state.token
// do something before request is sent
_config = Object.assign(_config, config)
if (token) {
_config.headers['t'] = token
}
const isAdmin = store.state.isAdmin
_config.headers['device'] = isMobile() ? 'app' : (isAdmin ? 'ops' : 'otrial')
INTERVAL_MAP[_config.url + str] = Date.now();
return _config
},
error => {
// do something with request error
return Promise.reject(error)
}
)
// response interceptor
service.interceptors.response.use(
({ status, data }) => {
if (status !== 200) {
Message({
message: `Error: ${status}`,
type: 'error',
duration: 1000 * 5,
customClass: 'myMessageClass'
})
return Promise.reject(data)
} else {
if (!data.success) {
Message({
message: data.message || 'Error',
type: 'error',
duration: 1000 * 5,
customClass: 'myMessageClass'
})
return Promise.reject(data)
} else {
return data
}
}
},
({ response, message, code }) => {
if (response) {
let { data } = response;
if (data) {
let { code, message } = data;
if (message) {
Message({
message,
type: 'error',
duration: 5 * 1000,
customClass: 'myMessageClass'
});
}
if (401 === Number(code)) {
store.commit('RESET_STATE', {
token: '',
userInfo: {},
dictConstant: '',
system: ''
});
return router.push('/gcp-lm/login');
}
}
}
if (/401/.test(message)) {
store.commit('RESET_STATE', {
token: '',
userInfo: {},
dictConstant: '',
system: ''
});
return router.push('/gcp-lm/login');
}
if (/^IntervalError:/.test(message)) {
console.log(message)
Message({
message: ERROR_MAP['INTERVAL_ERROR'],
type: 'error',
duration: 5 * 1000,
customClass: 'myMessageClass'
});
return Promise.reject({
code: 10000,
message: ERROR_MAP['INTERVAL_ERROR']
});
}
Message({
message: ERROR_MAP[code] || message,
type: 'error',
duration: 5 * 1000,
customClass: 'myMessageClass'
});
return Promise.reject({
response,
message,
code
})
}
)
return service
}
2.api.js
import request from '@/utils/request';
const BASE_URL = JSON.parse(SYSTEMADMIN_BASE_URL); // 公共服务
const BASE_URL_UPLOAD = JSON.parse(UPLOAD_BASE_URL); // 文件相关服务
const service = request(BASE_URL, {});
const service_upload = request(BASE_URL_UPLOAD, {}, { timeout: 60 * 1000 });
//post方法
export function deleteFile(data) {
return service_upload.post(`/oceanus-component/client-pc/file/delete`, data);
}
// get方法
export function fileBase64(params) {
return service_upload.get(`/oceanus-component/client-pc/file/base64`, { params });
}