//接口
//群成员数据导出
async groupMemberDataExportApi(data: any) {
// return http("/gw-console/v1/data-platform/group-members-data-export", {
// method: "GET",
// params: data,
// responseType: "blob"
// });
let url = Serve + "接口地址?";
for (const k in data) {
url += `${k}=${data[k]}&`;
}
const name = "导出文件名.xlsx";
const code: number = await downLoadPromise(
url,
name,
LoginManager.getToken() || ""
);
return code;
},
// ---------
//逻辑
private async groupMemberDataExport() {
try {
//此路成功
await API.groupMemberDataExportApi({
此处传参数
});
} catch (error) {
//此路error
console.log(error, "on error");
this.$message.warning(error.message || "下载失败");
}
}
//----------------------------------------
//在接口中:引入两个外部的方法
//1.
import { Serve } from "@/utils/serve";
export const Serve = process.env.VUE_APP_API_ROOT;
//2.
import { downLoadPromise } from "@/utils/down-load";
// 下载文件到本地的方法
export function downLoadPromise(url: string, name: string, token?: string) {
const promise = new Promise<number>(function(resolve, reject) {
const x = new XMLHttpRequest();
x.open("GET", url, true);
if (token) {
x.setRequestHeader("Auth-Token", token);
}
x.responseType = "blob";
x.send();
x.onload = function() {
if (x.status === 200) {
const urlResponse = window.URL.createObjectURL(x.response);
const a = document.createElement("a");
a.href = urlResponse;
a.download = name;
a.click();
resolve(x.status);
} else {
reject(x.status);
}
};
});
return promise;
}
//3,在接口的最后传token
/**登陆流程相关控制 */
class LoginManager {
static MOCK_ACCOUNT = "yuruizhi@vchangyi.com"; //本地静默登录账号
static PASS_WORD = "hOJYZv"; //本地静默登录密码
// 获取登陆数据
static getLoginData: () => IUserInfo = function() {
try {
return JSON.parse(localStorage.getItem("USERINFO") || "");
} catch (error) {
return null;
}
};
// 获取token
static getToken: () => string | null = function() {
try {
return JSON.parse(localStorage.getItem("TOKEN") || "");
} catch (error) {
return null;
}
};
下载文件excel
最新推荐文章于 2023-11-28 11:27:59 发布