2 changed files with 344 additions and 0 deletions
@ -0,0 +1,258 @@
@@ -0,0 +1,258 @@
|
||||
/* eslint-disable */ |
||||
import axios from 'axios'; |
||||
import { useStore } from 'vuex'; |
||||
import { loading, close, showMessage } from './element'; |
||||
const store = useStore(); |
||||
|
||||
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 curMsg: any; |
||||
|
||||
// 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, false); |
||||
} |
||||
resolve(true); |
||||
} else if (response.data) { |
||||
resolve(response.data); |
||||
} else { |
||||
resolve(true); |
||||
} |
||||
} else { |
||||
if (response.message == 'session timeout') { |
||||
store.dispatch('logout'); |
||||
} |
||||
if (response.message == 'no permission') { |
||||
response.message = '您暂无权访问,请联系管理员添加'; |
||||
} |
||||
if (response.message?.indexOf('no permission for ') == 0) { |
||||
response.message = '此权限尚未开放,请勿越权访问'; |
||||
} |
||||
if (!noMsg) { |
||||
showMessage('error', response.message, false); |
||||
} |
||||
response.error && console.log(response.error); |
||||
resolve(false); |
||||
} |
||||
} |
||||
|
||||
function handError(err, reject, noMsg, noLoading) { |
||||
if (!noLoading) { |
||||
close(); |
||||
} |
||||
if (!noMsg) { |
||||
showMessage('error', '网络错误', false); |
||||
} |
||||
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, false); |
||||
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', '下载失败,网络异常!', false); |
||||
if (callBack != null) { |
||||
callBack(false); |
||||
} |
||||
} |
||||
); |
||||
} |
@ -0,0 +1,86 @@
@@ -0,0 +1,86 @@
|
||||
import { |
||||
ElLoading, |
||||
ElMessage, |
||||
ElNotification, |
||||
ElMessageBox, |
||||
Action, |
||||
} from 'element-plus'; |
||||
let curMsg; |
||||
let count = 0; |
||||
let instance; |
||||
|
||||
export function loading() { |
||||
if (count === 0) { |
||||
instance = ElLoading.service({ |
||||
lock: true, |
||||
text: '加载中...', |
||||
spinner: 'el-icon-loading', |
||||
background: 'rgba(0, 0, 0, 0.3)', |
||||
}); |
||||
} |
||||
count++; |
||||
} |
||||
|
||||
export function close() { |
||||
if (count <= 0) { |
||||
return; |
||||
} |
||||
if (--count === 0) { |
||||
instance.close(); |
||||
} |
||||
} |
||||
|
||||
export function showMessage(type, info, unClose) { |
||||
if (curMsg != null) { |
||||
curMsg.close(); |
||||
} |
||||
const tmp = ElMessage({ |
||||
type: type, |
||||
showClose: true, |
||||
message: info, |
||||
duration: 3000, |
||||
offset: 50, |
||||
}); |
||||
if (!unClose) { |
||||
curMsg = tmp; |
||||
} |
||||
} |
||||
|
||||
export function showNotify(type, title, message, position) { |
||||
if (position == null) { |
||||
position = 'top-right'; |
||||
} |
||||
ElNotification({ |
||||
title: title, |
||||
type: type, |
||||
message: message, |
||||
position: position, |
||||
duration: 0, |
||||
}); |
||||
} |
||||
|
||||
export function warning(msg) { |
||||
showMessage('warning', msg, false); |
||||
} |
||||
|
||||
export function error(msg) { |
||||
showMessage('error', msg, false); |
||||
} |
||||
|
||||
export function success(msg) { |
||||
showMessage('success', msg, false); |
||||
} |
||||
|
||||
export const confirm = (msg: string, title: string) => { |
||||
return new Promise((resolve, reject) => { |
||||
ElMessageBox.confirm(msg, title, { |
||||
confirmButtonText: '确定', |
||||
cancelButtonText: '取消', |
||||
buttonSize: 'default', |
||||
confirmButtonClass: 'el-button--info', |
||||
type: 'warning', |
||||
}) |
||||
.then(resolve) |
||||
.catch(reject); |
||||
}); |
||||
}; |
Loading…
Reference in new issue