This commit is contained in:
ky_gyt
2021-04-22 01:52:03 +00:00
parent cf657fb7ed
commit 575a22954f
2 changed files with 20 additions and 10 deletions

View File

@@ -18,12 +18,13 @@ axios.defaults.baseURL = '/api'
*/
import urls from './requests'
const initInstance = () => {
const initInstance = (options) => {
const instance = axios
.create({
headers: {
Authorization: 'Bearer ' + token.value
}
},
...options
})
instance.interceptors.response.use((res) => {
@@ -73,7 +74,8 @@ for (let key in urls) {
const item = urls[key]
let url = '',
method = 'post'
method = 'post',
options = {}
if (item.constructor === String) {
url = item
} else if (item.constructor === Array) {
@@ -81,6 +83,9 @@ for (let key in urls) {
if (item[1]) {
method = item[1].toLowerCase()
}
if(item[2]) {
options = item[2]
}
} else if (item.constructor === Object) {
url = item.url
if (item.method) {
@@ -90,9 +95,9 @@ for (let key in urls) {
api[`${key}Wait`] = function (params = {}) {
if (method === 'post') {
return initInstance().post(url, params)
return initInstance(options).post(url, params)
} else {
return initInstance().get(url, {
return initInstance(options).get(url, {
params
})
}
@@ -101,14 +106,15 @@ for (let key in urls) {
api[key] = function (params = {}) {
return new Promise((reslove, reject) => {
api[`${key}Wait`](params)
.then(({ data }) => {
.then((res) => {
const { data } = res
if (errerCodes.indexOf(data.code) >= 0) {
errorNotification(data)
reject(data)
reject([ArrayBuffer, Blob].indexOf(data.constructor) > -1 ? res : data)
} else if (data.code === status.Unauthorized) {
handlerUnauthorized()
} else {
reslove(data)
reslove([ArrayBuffer, Blob].indexOf(data.constructor) > -1 ? res : data)
}
})
.catch(({ response: { data } }) => {

View File

@@ -14,12 +14,16 @@ export default {
/**
* 下载文件
*/
sysFileInfoDownload: ['/sysFileInfo/download', 'get'],
sysFileInfoDownload: ['/sysFileInfo/download', 'get', {
responseType: 'blob'
}],
/**
* 查看图片
*/
sysFileInfoPreview: ['/sysFileInfo/preview', 'get'],
sysFileInfoPreview: ['/sysFileInfo/preview', 'get', {
responseType: 'arraybuffer'
}],
/**
* 查看详情文件信息表