You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
263 lines
6.3 KiB
263 lines
6.3 KiB
/* eslint-disable */ |
|
import axios from 'axios'; |
|
import { loading, close, showMessage } from '../element'; |
|
import { i18n } from '../i18n'; |
|
const t = i18n.t; |
|
|
|
let router; |
|
|
|
const config = { |
|
baseURL: process.env.VUE_APP_BASE_URL ? '/api' : '', |
|
timeout: 60 * 1000, |
|
withCredentials: true, // Check cross-site Access-Control |
|
}; |
|
|
|
const _axios = axios.create(config); |
|
_axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8'; |
|
_axios.defaults.headers.put['Content-Type'] = 'application/json;charset=UTF-8'; |
|
_axios.defaults.headers.delete['Content-Type'] = 'application/json;charset=UTF-8'; |
|
|
|
let logout; |
|
|
|
export const registerRouter = routerP => { |
|
router = routerP; |
|
}; |
|
|
|
// Add a request interceptor |
|
_axios.interceptors.request.use( |
|
function (config) { |
|
const time = new Date().getTime().toString(); |
|
const params = config.params; |
|
if (params) { |
|
delEmpty(params); |
|
params.t = time; |
|
} |
|
const data = config.data; |
|
if (data != null && typeof data === 'object') { |
|
delEmpty(data); |
|
data.t = time; |
|
} |
|
return config; |
|
}, |
|
function (error) { |
|
return Promise.reject(error); |
|
} |
|
); |
|
|
|
function delEmpty(data) { |
|
if (data) { |
|
for (const item in data) { |
|
if (data.hasOwnProperty(item)) { |
|
const val = data[item]; |
|
if ((val == null || val == 'null' || val == '') && val !== 0) { |
|
delete data[item]; |
|
} |
|
} |
|
} |
|
} |
|
} |
|
|
|
// Add a response interceptor |
|
_axios.interceptors.response.use( |
|
function (response) { |
|
return response.data; |
|
}, |
|
function (error) { |
|
return Promise.reject(error); |
|
} |
|
); |
|
|
|
export function post(url, data?, noMsg?, noLoading?) { |
|
return new Promise((resolve, reject) => { |
|
if (!noLoading) { |
|
loading(); |
|
} |
|
_axios.post(url, data).then( |
|
(response: any) => { |
|
handResponse(response, resolve, noMsg, noLoading); |
|
}, |
|
(err) => { |
|
handError(err, reject, noMsg, noLoading); |
|
} |
|
); |
|
}); |
|
} |
|
|
|
export function get(url, data?, noMsg?, noLoading?) { |
|
return new Promise((resolve, reject) => { |
|
if (!noLoading) { |
|
loading(); |
|
} |
|
_axios.get(url, { params: data }).then( |
|
(response: any) => { |
|
handResponse(response, resolve, noMsg, noLoading); |
|
}, |
|
(err) => { |
|
handError(err, reject, noMsg, noLoading); |
|
} |
|
); |
|
}); |
|
} |
|
|
|
export function put(url, data?, noMsg?, noLoading?) { |
|
return new Promise((resolve, reject) => { |
|
if (!noLoading) { |
|
loading(); |
|
} |
|
_axios.put(url, data).then( |
|
(response: any) => { |
|
handResponse(response, resolve, noMsg, noLoading); |
|
}, |
|
(err) => { |
|
handError(err, reject, noMsg, noLoading); |
|
} |
|
); |
|
}); |
|
} |
|
|
|
export function delate(url, data?, noMsg?, noLoading?) { |
|
return new Promise((resolve, reject) => { |
|
if (!noLoading) { |
|
loading(); |
|
} |
|
// _axios.delete(url, data).then( |
|
// (response: any) => { |
|
// handResponse(response, resolve, noMsg, noLoading); |
|
// }, |
|
// (err) => { |
|
// handError(err, reject, noMsg, noLoading); |
|
// } |
|
// ); |
|
_axios({ |
|
method: 'delete', |
|
url: url, |
|
data: data, |
|
}).then( |
|
(response: any) => { |
|
handResponse(response, resolve, noMsg, noLoading); |
|
}, |
|
(err) => { |
|
handError(err, reject, noMsg, noLoading); |
|
} |
|
); |
|
}); |
|
} |
|
|
|
function handResponse(response, resolve, noMsg, noLoading) { |
|
if (!noLoading) { |
|
close(); |
|
} |
|
if (response.success) { |
|
if (response.message) { |
|
if (!noMsg) { |
|
showMessage('success', response.message); |
|
} |
|
resolve(true); |
|
} else if (response.data) { |
|
resolve(response.data); |
|
} else { |
|
resolve(true); |
|
} |
|
} else { |
|
if (response.message == 'session timeout') { |
|
router?.push('/login'); |
|
} |
|
if (response.message == 'no permission') { |
|
response.message = t('http.unPermission'); |
|
} |
|
if (response.message?.indexOf('no permission for ') == 0) { |
|
response.message = t('http.noPermission'); |
|
} |
|
if (!noMsg) { |
|
showMessage('error', response.message); |
|
} |
|
response.error && console.log(response.error); |
|
resolve(false); |
|
} |
|
} |
|
|
|
function handError(err, reject, noMsg, noLoading) { |
|
if (!noLoading) { |
|
close(); |
|
} |
|
if (!noMsg) { |
|
showMessage('error', t('http.error')); |
|
} |
|
reject(err); |
|
} |
|
|
|
export function upload(file, url, data) { |
|
return new Promise((resolve, reject) => { |
|
let param = new FormData(); // 创建form对象 |
|
param.append('file', file); // 通过append向form对象添加数据 |
|
if (data) { |
|
for (const item in data) { |
|
if (data.hasOwnProperty(item)) { |
|
param.append(item, data[item]); // 添加form表单中其他数据 |
|
} |
|
} |
|
} |
|
let config = { |
|
headers: { 'Content-Type': 'multipart/form-data' }, |
|
}; |
|
_axios.post(url, param, config).then( |
|
(response) => { |
|
handResponse(response, resolve, false, false); |
|
}, |
|
(err) => { |
|
handError(err, reject, false, false); |
|
} |
|
); |
|
}); |
|
} |
|
|
|
export function download(fileName, url, data = {}, callBack?) { |
|
loading(); |
|
_axios({ |
|
method: 'get', |
|
url: url, // 请求地址 |
|
params: data, // 参数 |
|
responseType: 'blob', // 表明返回服务器返回的数据类型 |
|
}).then( |
|
(response: any) => { |
|
close(); |
|
const reader = new FileReader() as any; |
|
reader.readAsText(response); |
|
reader.onload = function () { |
|
try { |
|
const result = JSON.parse(reader.result); |
|
if (typeof result === 'object') { |
|
showMessage('error', result.message); |
|
if (callBack != null) { |
|
callBack(false); |
|
} |
|
return; |
|
} |
|
} catch (err) {} |
|
const blob = new Blob([response], { |
|
type: 'application/vnd.ms-excel', |
|
}); |
|
const navigator = window.navigator as any; |
|
if (navigator.msSaveOrOpenBlob) { |
|
navigator.msSaveBlob(blob, fileName); |
|
} else { |
|
const link = document.createElement('a'); |
|
link.href = window.URL.createObjectURL(blob); |
|
link.download = fileName; |
|
link.click(); |
|
window.URL.revokeObjectURL(link.href); |
|
} |
|
if (callBack != null) { |
|
callBack(true); |
|
} |
|
}; |
|
}, |
|
(err) => { |
|
close(); |
|
showMessage('error', t('http.downFail')); |
|
if (callBack != null) { |
|
callBack(false); |
|
} |
|
} |
|
); |
|
}
|
|
|