axios请求:
调用get、post
let name=`${time}报表`,data={};
await getExport(exportUrl.elevator.alarm,{data, responseType: 'blob'},name)
await postExport(exportUrl.project.project,data,'项目列表导出')
公共方法
import axios from "@/api";
import {elMessagefun} from "@/assets/common/utils";
import {requestCode} from "@/assets/common/common";
function commonExport(res,name){
const url = window.URL.createObjectURL(new Blob([res], {type: "application/vnd.ms-excel"}))
const a = document.createElement('a');
a.href = url;
a.setAttribute('download', name);
a.click();
a.remove();
}
export const postExport = async function (url, data, name){
let res = await axios.post(url, data, {responseType: 'blob'})
if (res.code&&(res.code !== requestCode)) {
elMessagefun(res.msg);//提示的方法
return
};
commonExport(res,name)
}
export const getExport = async function (url, config,name){
let res = await axios.get(url, config)
commonExport(res,name)
}
ajax请求:
downLoad: function(data) {
formData.token=window.sessionStorage.token;
$.ajax({
url: G_IP + 'xxx',
method: "GET",
xhrFields: {
responseType: "blob"
},
data,
success: function (res) {
const url = window.URL.createObjectURL(new Blob([res], {type: "application/vnd.ms-excel"}))
const a = document.createElement('a');
a.href = url;
a.setAttribute('download', vm.$data.searchTime);
a.click();
a.remove();
}
})
},