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