import * as R from 'ramda'; const addQueries = (params: { [x: string]: any; token?: string | null; }): string => { const arr: string[] = []; Object.keys(params).forEach((key) => { if (!(R.isEmpty(params[key]) || R.isNil(params[key]))) { arr.push(`${key}=${params[key]}`); } }); return arr.join('&'); }; /** * * @param url 下载地址 * @param params 参数集合 */ export const exportExcel = (url: string, params = {}): void => { let token: string | null; if (typeof window !== 'undefined') { token = window.localStorage.getItem('token'); const data = { ...params, token, }; if (!/^https.*/.test(url)) { // eslint-disable-next-line no-param-reassign url = url.replace('http', 'https'); } const downloadUrl = `${url}?${addQueries(data)}`; console.log('YWFE下载地址=>', downloadUrl); const id = `alink_${new Date().getTime()}`; const aDom = document.createElement('a'); aDom.setAttribute('href', downloadUrl); aDom.setAttribute('target', '_blank'); aDom.setAttribute('id', id); if (!document.getElementById(id)) { document.body.appendChild(aDom); } aDom.click(); setTimeout(() => { document.body.removeChild(aDom); }, 0); } };