修改:axios拦截器,文件下载

Signed-off-by: skyselang <215817969@qq.com>
This commit is contained in:
skyselang 2022-06-27 17:49:01 +08:00
parent 3a2686e0e8
commit 1ae5e5a26f
3 changed files with 43 additions and 44 deletions

View File

@ -24,10 +24,10 @@ service.interceptors.request.use(
config.headers[tokenName] = tokenValue
} else {
// 请求参数token
if (config.method === 'post') {
config.data = { ...config.data, [tokenName]: tokenValue }
} else if (config.method === 'get') {
if (config.method === 'get') {
config.params = { ...config.params, [tokenName]: tokenValue }
} else {
config.data = { ...config.data, [tokenName]: tokenValue }
}
}
}
@ -46,35 +46,28 @@ service.interceptors.response.use(
* 还可以通过HTTP状态代码来判断请求状态
*/
response => {
// 响应数据
const res = response.data
if (response.data && response.config.responseType === 'blob') {
// 文件下载
return response.data
if (response.data.type === 'application/json') {
const reader = new FileReader()
reader.readAsText(response.data, 'utf-8')
reader.onload = () => {
const resf = JSON.parse(reader.result)
responseHandle(resf)
return Promise.reject(new Error(resf.msg || 'Server error'))
}
return Promise.reject()
} else {
return response.data
}
} else {
// 响应数据
const res = response.data
// 返回码200成功
if (res.code === 200) {
return res
} else {
// 返回码401AdminToken 无效
if (res.code === 401) {
MessageBox.confirm(res.msg, '提示', {
confirmButtonText: '重新登录',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
store.dispatch('user/resetAdminToken').then(() => {
location.reload()
})
}).catch(() => { })
} else {
Message({
showClose: true,
message: res.msg || 'Server error',
type: 'error',
duration: 5000
})
}
responseHandle(res)
return Promise.reject(new Error(res.msg || 'Server error'))
}
}
@ -82,24 +75,7 @@ service.interceptors.response.use(
error => {
// 响应错误
const res = error.response.data
if (res.code === 401) {
MessageBox.confirm(res.message, '提示', {
confirmButtonText: '重新登录',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
store.dispatch('user/resetAdminToken').then(() => {
location.reload()
})
}).catch(() => { })
} else {
Message({
showClose: true,
message: res.message || error.message,
type: 'error',
duration: 5000
})
}
responseHandle(res)
if (process.env.NODE_ENV === 'development') {
console.log(error.response)
}
@ -107,4 +83,27 @@ service.interceptors.response.use(
}
)
// 响应处理
function responseHandle(res) {
// 返回码401Token 无效
if (res.code === 401) {
MessageBox.confirm(res.msg, '提示', {
confirmButtonText: '重新登录',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
store.dispatch('user/resetAdminToken').then(() => {
location.reload()
})
}).catch(() => { })
} else {
Message({
showClose: true,
message: res.msg || 'Server error',
type: 'error',
duration: 5000
})
}
}
export default service

View File

@ -415,7 +415,6 @@ export default {
var id = {}
id[this.idkey] = row[this.idkey]
down(id).then(res => {
console.log(res)
const blob = new Blob([res])
const url = window.URL.createObjectURL(blob)
const ele = document.createElement('a')

View File

@ -450,6 +450,7 @@ export default {
//
refresh() {
this.query = this.$options.data().query
this.query.is_extra = 0
this.list()
},
//