单独封装各种请求
request.js
import Vue from 'vue'
import axios from 'axios'
import store from '@/store'
import { Modal, notification } from 'ant-design-vue'
import { ACCESS_TOKEN, TENANT_ID } from '@/store/mutation-types'
const apiBaseUrl = window._CONFIG['domianURL'] || '/ccc'
const service = axios.create({
baseURL: apiBaseUrl,
timeout: 30000
})
const err = (error) => {
if (error.response) {
const that = this
const data = error.response.data
const token = Vue.ls.get(ACCESS_TOKEN)
console.log('------异常响应------', token)
console.log('------异常响应------', error.response.status)
switch (error.response.status) {
case 403:
notification.error({ message: '系统提示', description: '拒绝访问', duration: 4 })
break
case 404:
notification.error({ message: '系统提示', description: '很抱歉,资源未找到!', duration: 4 })
break
default:
notification.error({
message: '系统提示',
description: data.message,
duration: 4
})
break
}
}
return Promise.reject(error)
}
manage.js
import Vue from 'vue'
import { axios } from '@/utils/request'
export function postAction(url, parameter) {
return axios({
url: url,
method: 'post',
data: parameter
})
}
export function httpAction(url, parameter, method) {
return axios({
url: url,
method: method,
data: parameter
})
}
export function downFile(url, parameter) {
return axios({
url: url,
params: parameter,
method: 'get',
responseType: 'blob'
})
}
export function downloadFile(url, fileName, parameter) {
return downFile(url, parameter).then((data) => {
if (!data || data.size === 0) {
Vue.prototype['$message'].warning('文件下载失败')
return
}
if (typeof window.navigator.msSaveBlob !== 'undefined') {
window.navigator.msSaveBlob(new Blob([data]), fileName)
} else {
const url = window.URL.createObjectURL(new Blob([data]))
const link = document.createElement('a')
link.style.display = 'none'
link.href = url
link.setAttribute('download', fileName)
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
window.URL.revokeObjectURL(url)
}
})
}
export function uploadAction(url, parameter) {
return axios({
url: url,
data: parameter,
method: 'post',
headers: {
'Content-Type': 'multipart/form-data'
}
})
}
export function getFileAccessHttpUrl(avatar, subStr) {
if (!subStr) subStr = 'http'
if (avatar && avatar.startsWith(subStr)) {
return avatar
} else {
if (avatar && avatar.length > 0 && avatar.indexOf('[') == -1) {
return window._CONFIG['staticDomainURL'] + '/' + avatar
}
}
}