|
|
@@ -2,7 +2,7 @@ import axios from 'axios'
|
|
|
import { Notification, MessageBox, Message } from 'element-ui'
|
|
|
import store from '@/store'
|
|
|
// import { getToken } from '@/utils/auth'
|
|
|
-// import errorCode from '@/utils/errorCode'
|
|
|
+import errorCode from '@/utils/errorCode'
|
|
|
|
|
|
axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
|
|
|
|
|
|
@@ -29,7 +29,6 @@ const service = axios.create({
|
|
|
// Promise.reject(error)
|
|
|
// })
|
|
|
|
|
|
-
|
|
|
// 响应拦截器
|
|
|
// service.interceptors.response.use(res => {
|
|
|
// // 未设置状态码则默认成功状态
|
|
|
@@ -77,6 +76,36 @@ const service = axios.create({
|
|
|
// return Promise.reject(error)
|
|
|
// })
|
|
|
|
|
|
+service.interceptors.response.use(res => {
|
|
|
+ console.info(res)
|
|
|
+ if (res.request.responseType == 'blob'){ // 判断是否是文件流
|
|
|
+ let fileReader = new FileReader()
|
|
|
+ fileReader.readAsText(res.data)
|
|
|
+ fileReader.onload = function () {
|
|
|
+ try {
|
|
|
+ let jsonData = JSON.parse(this.result) // 解析成功说明是普通对象数据,后端返回的是文件上传的错误信息
|
|
|
+ res.data = jsonData
|
|
|
+ processResponse(res)
|
|
|
+ return
|
|
|
+ } catch (err) { // 解析成对象失败,说明是文件流
|
|
|
+ downLoadBlobFile(res)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+ // 常规响应处理
|
|
|
+ return processResponse(res)
|
|
|
+},
|
|
|
+ error => {
|
|
|
+ console.log('err' + error)
|
|
|
+ Message({
|
|
|
+ message: error.message,
|
|
|
+ type: 'error',
|
|
|
+ duration: 5 * 1000
|
|
|
+ })
|
|
|
+ return Promise.reject(error)
|
|
|
+})
|
|
|
+
|
|
|
service.postRequest = function postRequest(basePath, srvName, funcName, data) {
|
|
|
if (data == undefined){
|
|
|
let nullParam = {"nullparam": 0}
|
|
|
@@ -93,7 +122,7 @@ service.postRequest = function postRequest(basePath, srvName, funcName, data) {
|
|
|
},
|
|
|
data: data
|
|
|
})
|
|
|
-};
|
|
|
+}
|
|
|
|
|
|
// Excel文件下载(服务端生成文件流)
|
|
|
service.downloadExcel = function downloadExcel(basePath, srvName, funcName, fileName, data) {
|
|
|
@@ -115,15 +144,81 @@ service.downloadExcel = function downloadExcel(basePath, srvName, funcName, file
|
|
|
'X-RPCX-ServiceMethod': funcName
|
|
|
},
|
|
|
data: data
|
|
|
- }).then(res => {
|
|
|
- const aLink = document.createElement('a')
|
|
|
- var blob = new Blob([res.data], { type: 'application/vnd.ms-excel' })
|
|
|
- aLink.href = URL.createObjectURL(blob)
|
|
|
- aLink.setAttribute('download', fileName) // 设置下载文件名称
|
|
|
- document.body.appendChild(aLink)
|
|
|
- aLink.click()
|
|
|
- document.body.appendChild(aLink)
|
|
|
- })
|
|
|
+ })
|
|
|
};
|
|
|
|
|
|
+// .then(res => {
|
|
|
+// const aLink = document.createElement('a')
|
|
|
+// var blob = new Blob([res.data], { type: 'application/vnd.ms-excel' })
|
|
|
+// aLink.href = URL.createObjectURL(blob)
|
|
|
+// aLink.setAttribute('download', fileName) // 设置下载文件名称
|
|
|
+// document.body.appendChild(aLink)
|
|
|
+// aLink.click()
|
|
|
+// document.body.appendChild(aLink)
|
|
|
+// })
|
|
|
+
|
|
|
+function processResponse(res){
|
|
|
+ // 未设置状态码则默认成功状态
|
|
|
+ const code = res.data.code || 200;
|
|
|
+ // 获取错误信息
|
|
|
+ const message = errorCode[code] || res.data.msg || errorCode['default']
|
|
|
+ if (code === 401) {
|
|
|
+ MessageBox.confirm(
|
|
|
+ '登录状态已过期,您可以继续留在该页面,或者重新登录',
|
|
|
+ '系统提示',
|
|
|
+ {
|
|
|
+ confirmButtonText: '重新登录',
|
|
|
+ cancelButtonText: '取消',
|
|
|
+ type: 'warning'
|
|
|
+ }
|
|
|
+ ).then(() => {
|
|
|
+ store.dispatch('LogOut').then(() => {
|
|
|
+ location.reload() // 为了重新实例化vue-router对象 避免bug
|
|
|
+ })
|
|
|
+ })
|
|
|
+ }
|
|
|
+ else if (code === 500) {
|
|
|
+ Message({
|
|
|
+ message: message,
|
|
|
+ type: 'error'
|
|
|
+ })
|
|
|
+ return Promise.reject(new Error(message))
|
|
|
+ } else if (code !== 200) {
|
|
|
+ Notification.error({
|
|
|
+ title: message
|
|
|
+ })
|
|
|
+ return Promise.reject('error')
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ return res.data
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function downLoadBlobFile(res, mimeType){
|
|
|
+ if (mimeType == undefined) {
|
|
|
+ mimeType = 'application/vnd.ms-excel'
|
|
|
+ }
|
|
|
+ const aLink = document.createElement('a')
|
|
|
+ var blob = new Blob([res.data], {
|
|
|
+ type: mimeType
|
|
|
+ })
|
|
|
+ // //从response的headers中获取filename, 后端response.setHeader("Content-disposition", "attachment; filename=xxxx.docx") 设置的文件名;
|
|
|
+ var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
|
|
|
+ var contentDisposition = decodeURI(res.headers['content-disposition'] || res.headers['Content-Disposition'])
|
|
|
+ console.info(res)
|
|
|
+ console.info(contentDisposition)
|
|
|
+ var result = patt.exec(contentDisposition)
|
|
|
+ var fileName = 'data.xlsx'
|
|
|
+ if (result != undefined) {
|
|
|
+ fileName = result[1]
|
|
|
+ fileName = decodeURI(escape(fileName))
|
|
|
+ fileName = fileName.replace(/\"/g, '')
|
|
|
+ }
|
|
|
+ aLink.href = URL.createObjectURL(blob)
|
|
|
+ aLink.setAttribute('download', fileName) // 设置下载文件名称
|
|
|
+ document.body.appendChild(aLink)
|
|
|
+ aLink.click()
|
|
|
+ document.body.appendChild(aLink)
|
|
|
+}
|
|
|
+
|
|
|
export default service
|