import { uploadFile } from './service'
|
import CryptoJS from 'crypto-js';
|
|
const normalizePath = (path) => {
|
if (typeof path !== 'string') {
|
return '';
|
}
|
if (path.length > 1 && path.endsWith('/')) {
|
return path.replace(/\/+$/, '');
|
}
|
return path;
|
};
|
|
const isRouteMatch = (targetPath, routePath) => {
|
const normalizedTargetPath = normalizePath(targetPath);
|
const normalizedRoutePath = normalizePath(routePath);
|
return (
|
normalizedTargetPath === normalizedRoutePath ||
|
normalizedTargetPath.startsWith(`${normalizedRoutePath}/`)
|
);
|
};
|
|
const flattenRoutes = (routes = [], parentAuthority) => {
|
return routes.reduce((accumulator, route) => {
|
const routeAuthority = route.authority || parentAuthority;
|
accumulator.push({
|
path: normalizePath(route.path),
|
authority: routeAuthority,
|
});
|
if (Array.isArray(route.routes) && route.routes.length > 0) {
|
accumulator.push(...flattenRoutes(route.routes, routeAuthority));
|
}
|
return accumulator;
|
}, []);
|
};
|
|
export const isUrl = (path) => {
|
if (typeof path !== 'string' || !path) {
|
return false;
|
}
|
return /^(https?:\/\/)((([a-z\d]([a-z\d-]*[a-z\d])*)\.)+[a-z]{2,}|((\d{1,3}\.){3}\d{1,3}))(:\d+)?(\/[^\s]*)?$/i.test(
|
path,
|
);
|
};
|
|
export const getRouteAuthority = (path, routeData = []) => {
|
const matchedRoute = flattenRoutes(routeData)
|
.filter((route) => route.path && isRouteMatch(path, route.path))
|
.sort((prev, next) => next.path.length - prev.path.length)
|
.find((route) => route.authority !== undefined);
|
|
return matchedRoute?.authority;
|
};
|
|
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 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));
|
}
|
|
export const encryptData = (data) => {
|
return CryptoJS.AES.encrypt(JSON.stringify(data), 'your-secret-key-123').toString();
|
};
|
|
export const decryptData = (cipherText) => {
|
if (!cipherText) return null;
|
const bytes = CryptoJS.AES.decrypt(cipherText, 'your-secret-key-123');
|
return JSON.parse(bytes.toString(CryptoJS.enc.Utf8));
|
};
|