首次提交
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
import axios from 'axios'
|
||||
import { ElMessage, ElLoading } from 'element-plus'
|
||||
import { getToken, removeToken } from './auth'
|
||||
|
||||
// 创建axios实例
|
||||
const service = axios.create({
|
||||
baseURL: process.env.VUE_APP_BASE_API || '/',
|
||||
timeout: 30000 // 30秒超时
|
||||
})
|
||||
|
||||
// 请求拦截器
|
||||
service.interceptors.request.use(
|
||||
config => {
|
||||
// 添加token
|
||||
const token = getToken()
|
||||
if (token) {
|
||||
config.headers['Authorization'] = `Bearer ${token}`
|
||||
}
|
||||
|
||||
// 添加请求头
|
||||
config.headers['Content-Type'] = 'application/json;charset=UTF-8'
|
||||
|
||||
return config
|
||||
},
|
||||
error => {
|
||||
console.error('请求错误:', error)
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
// 响应拦截器
|
||||
service.interceptors.response.use(
|
||||
response => {
|
||||
const { data, config } = response
|
||||
|
||||
// 处理文件下载
|
||||
if (config.responseType === 'blob') {
|
||||
return response
|
||||
}
|
||||
|
||||
// 处理正常响应
|
||||
if (data.code === 200 || data.success) {
|
||||
return data
|
||||
} else {
|
||||
ElMessage.error(data.message || '请求失败')
|
||||
return Promise.reject(new Error(data.message || 'Error'))
|
||||
}
|
||||
},
|
||||
error => {
|
||||
console.error('响应错误:', error)
|
||||
|
||||
const { response } = error
|
||||
|
||||
if (response) {
|
||||
const { status, data } = response
|
||||
|
||||
switch (status) {
|
||||
case 400:
|
||||
ElMessage.error(data.message || '请求参数错误')
|
||||
break
|
||||
case 401:
|
||||
ElMessage.error('登录已过期,请重新登录')
|
||||
removeToken()
|
||||
window.location.href = '/login'
|
||||
break
|
||||
case 403:
|
||||
ElMessage.error('没有权限访问')
|
||||
break
|
||||
case 404:
|
||||
ElMessage.error('请求的资源不存在')
|
||||
break
|
||||
case 500:
|
||||
ElMessage.error('服务器内部错误')
|
||||
break
|
||||
default:
|
||||
ElMessage.error(data.message || '请求失败')
|
||||
}
|
||||
} else {
|
||||
ElMessage.error('网络连接错误')
|
||||
}
|
||||
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
// 封装请求方法
|
||||
export default service
|
||||
|
||||
// 导出常用方法
|
||||
export function get(url, params = {}, config = {}) {
|
||||
return service.get(url, { params, ...config })
|
||||
}
|
||||
|
||||
export function post(url, data = {}, config = {}) {
|
||||
return service.post(url, data, config)
|
||||
}
|
||||
|
||||
export function put(url, data = {}, config = {}) {
|
||||
return service.put(url, data, config)
|
||||
}
|
||||
|
||||
export function del(url, config = {}) {
|
||||
return service.delete(url, config)
|
||||
}
|
||||
|
||||
// 文件上传
|
||||
export function upload(url, file, data = {}) {
|
||||
const formData = new FormData()
|
||||
formData.append('file', file)
|
||||
|
||||
Object.keys(data).forEach(key => {
|
||||
formData.append(key, data[key])
|
||||
})
|
||||
|
||||
return service.post(url, formData, {
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data'
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 文件下载
|
||||
export function download(url, params = {}, filename = '') {
|
||||
return service.get(url, {
|
||||
params,
|
||||
responseType: 'blob'
|
||||
}).then(response => {
|
||||
const blob = new Blob([response.data])
|
||||
const downloadUrl = window.URL.createObjectURL(blob)
|
||||
const link = document.createElement('a')
|
||||
link.href = downloadUrl
|
||||
link.download = filename || 'download'
|
||||
document.body.appendChild(link)
|
||||
link.click()
|
||||
document.body.removeChild(link)
|
||||
window.URL.revokeObjectURL(downloadUrl)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user