提交 074386c8 编写于 作者: Mr.奇淼('s avatar Mr.奇淼(

超时遮罩封装

上级 8fac89ad
import axios from 'axios'; // 引入axios import axios from 'axios'; // 引入axios
import { Message } from 'element-ui'; import { Message, Loading } from 'element-ui';
import { store } from '@/store/index' import { store } from '@/store/index'
const service = axios.create({ const service = axios.create({
baseURL: process.env.VUE_APP_BASE_API, baseURL: process.env.VUE_APP_BASE_API,
timeout: 99999 timeout: 99999
}) })
let acitveAxios = 0
let loadingInstance
let timer
const showLoading = () => {
acitveAxios++
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(() => {
if (acitveAxios > 0) {
loadingInstance = Loading.service({ fullscreen: true })
}
}, 400);
}
//http request 拦截器 const closeLoading = () => {
acitveAxios--
if (acitveAxios <= 0) {
clearTimeout(timer)
loadingInstance && loadingInstance.close()
}
}
//http request 拦截器
service.interceptors.request.use( service.interceptors.request.use(
config => { config => {
showLoading()
const token = store.getters['user/token'] const token = store.getters['user/token']
config.data = JSON.stringify(config.data); config.data = JSON.stringify(config.data);
config.headers = { config.headers = {
...@@ -19,6 +40,7 @@ service.interceptors.request.use( ...@@ -19,6 +40,7 @@ service.interceptors.request.use(
return config; return config;
}, },
error => { error => {
closeLoading()
Message({ Message({
showClose: true, showClose: true,
message: error, message: error,
...@@ -32,6 +54,7 @@ service.interceptors.request.use( ...@@ -32,6 +54,7 @@ service.interceptors.request.use(
//http response 拦截器 //http response 拦截器
service.interceptors.response.use( service.interceptors.response.use(
response => { response => {
closeLoading()
if (response.data.success) { if (response.data.success) {
return response.data return response.data
} else { } else {
...@@ -47,6 +70,7 @@ service.interceptors.response.use( ...@@ -47,6 +70,7 @@ service.interceptors.response.use(
} }
}, },
error => { error => {
closeLoading()
Message({ Message({
showClose: true, showClose: true,
message: error, message: error,
......
<template> <template>
<div> <div v-loading.fullscreen.lock="fullscreenLoading">
<el-upload <el-upload
:action="`${path}/fileUploadAndDownload/upload`" :action="`${path}/fileUploadAndDownload/upload`"
:before-upload="checkFile" :before-upload="checkFile"
...@@ -64,6 +64,7 @@ export default { ...@@ -64,6 +64,7 @@ export default {
mixins: [infoList], mixins: [infoList],
data() { data() {
return { return {
fullscreenLoading:false,
listApi: getFileList, listApi: getFileList,
listKey: 'list', listKey: 'list',
path: path, path: path,
...@@ -121,14 +122,17 @@ export default { ...@@ -121,14 +122,17 @@ export default {
}) })
}, },
checkFile(file) { checkFile(file) {
this.fullscreenLoading = true
const isJPG = file.type === 'image/jpeg' const isJPG = file.type === 'image/jpeg'
const isPng = file.type === 'image/png' const isPng = file.type === 'image/png'
const isLt2M = file.size / 1024 / 1024 < 2 const isLt2M = file.size / 1024 / 1024 < 2
if (!isJPG && !isPng) { if (!isJPG && !isPng) {
this.$message.error('上传头像图片只能是 JPG或png 格式!') this.$message.error('上传头像图片只能是 JPG或png 格式!')
this.fullscreenLoading = false
} }
if (!isLt2M) { if (!isLt2M) {
this.$message.error('上传头像图片大小不能超过 2MB!') this.$message.error('上传头像图片大小不能超过 2MB!')
this.fullscreenLoading = false
} }
return (isPng || isJPG) && isLt2M return (isPng || isJPG) && isLt2M
}, },
...@@ -140,12 +144,15 @@ export default { ...@@ -140,12 +144,15 @@ export default {
if (res.success) { if (res.success) {
this.getTableData() this.getTableData()
} }
this.fullscreenLoading = false
}, },
uploadError() { uploadError() {
this.$message({ this.$message({
type: 'error', type: 'error',
message: '上传失败' message: '上传失败'
}) })
this.fullscreenLoading = false
}, },
downloadFile(row) { downloadFile(row) {
downloadImage(row.url, row.name) downloadImage(row.url, row.name)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册