// import { getToken } from '@/utils/authority';
|
import { parse } from 'querystring';
|
import { uploadFile } from './service'
|
/* eslint no-useless-escape:0 import/prefer-default-export:0 */
|
const reg =
|
/(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/;
|
export const isUrl = (path) => reg.test(path);
|
export const isAntDesignPro = () => {
|
if (ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site') {
|
return true;
|
}
|
|
return window.location.hostname === 'preview.pro.ant.design';
|
}; // 给官方演示站点用,用于关闭真实开发环境不需要使用的特性
|
export const customRequest = (params, type) => {
|
const { file, onSuccess, onError } = params;
|
uploadFile(file)
|
.then((ret) => {
|
if (type == 1) {
|
onSuccess({ name: file.name, url: ret.data });
|
} else {
|
onSuccess(ret.data);
|
}
|
})
|
.catch((ret) => {
|
onError();
|
});
|
};
|
export const isAntDesignProOrDev = () => {
|
const { NODE_ENV } = process.env;
|
|
if (NODE_ENV === 'development') {
|
return true;
|
}
|
|
return isAntDesignPro();
|
};
|
export const getPageQuery = () => parse(window.location.href.split('?')[1]);
|
/**
|
* props.route.routes
|
* @param router [{}]
|
* @param pathname string
|
*/
|
|
/**
|
* 导出
|
* @param {*} params
|
* @param {*} name
|
* @param {*} url
|
* @returns
|
*/
|
|
export const exportExcell = (name, params, url) => {
|
fetch(BASE_URL + url, {
|
method: 'POST',
|
body: JSON.stringify({
|
...params,
|
}),
|
headers: {
|
Authorization: localStorage.getItem('token'),
|
'ConTent-Type': 'application/json;charset=UTF-8',
|
timestamp: new Date().getTime(),
|
client: localStorage.getItem('client')
|
},
|
responseType: 'blob',
|
})
|
.then((res) => res.blob())
|
.then((res) => {
|
|
const link = document.createElement('a');
|
link.style.display = 'none';
|
link.href = URL.createObjectURL(res);
|
link.download = name;
|
document.body.appendChild(link);
|
link.click();
|
// 释放的 URL 对象以及移除 a 标签
|
URL.revokeObjectURL(link.href);
|
document.body.removeChild(link);
|
});
|
};
|
|
//下载导入末班
|
export const downLoad = (url, name) => {
|
fetch(BASE_URL + url, {
|
method: 'get',
|
responseType: 'blob',
|
headers: {
|
Authorization: localStorage.getItem('token'),
|
'ConTent-Type': 'application/json;charset=UTF-8',
|
timestamp: new Date().getTime(),
|
client: localStorage.getItem('client')
|
},
|
})
|
.then((res) => res.blob())
|
.then((res) => {
|
const link = document.createElement('a');
|
link.style.display = 'none';
|
link.href = URL.createObjectURL(res);
|
link.download = name;
|
document.body.appendChild(link);
|
link.click();
|
// 释放的 URL 对象以及移除 a 标签
|
URL.revokeObjectURL(link.href);
|
document.body.removeChild(link);
|
});
|
};
|
export const downloadFile = (url, fileName) => {
|
fetch(url)
|
.then(response => response.blob())
|
.then(blob => {
|
const url = window.URL.createObjectURL(blob);
|
const link = document.createElement('a');
|
link.href = url;
|
link.setAttribute('download', fileName);
|
document.body.appendChild(link);
|
link.click();
|
window.URL.revokeObjectURL(url);
|
document.body.removeChild(link);
|
})
|
.catch(error => console.error('Error downloading file:', error));
|
}
|