utils.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * @Author: your name
  3. * @Date: 2022-01-13 17:09:05
  4. * @LastEditTime: 2023-03-24 09:23:30
  5. * @LastEditors: code4eat awesomedema@gmail.com
  6. * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  7. * @FilePath: /KC-MiddlePlatform/src/utils.js
  8. */
  9. import axios from 'axios';
  10. export const randomString = (length: number) => {
  11. const chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_=-';
  12. let result = '';
  13. for (let i = length; i > 0; --i) {
  14. result += chars[Math.floor(Math.random() * chars.length)];
  15. }
  16. return result;
  17. };
  18. export interface TreeItemType {
  19. children?: TreeItemType[];
  20. [key: string]: any;
  21. }
  22. //获取树结构指定的值集合
  23. export const getValsFromTree = (data: TreeItemType[], key: string) => {
  24. let result: any[] = [];
  25. function looper(data: TreeItemType[], key: string) {
  26. data.forEach((t) => {
  27. if (t[key]) {
  28. result.push(t[key]);
  29. }
  30. if (t.children && t.children.length > 0) {
  31. looper(t.children, key);
  32. }
  33. });
  34. }
  35. looper(data, key);
  36. return result;
  37. };
  38. export const searchTree = (treeData: TreeItemType[]) => {};
  39. export const downloadTemplateReq = (path:string) => {
  40. const userData = localStorage.getItem('userData');
  41. const { token = '' } = JSON.parse(userData as any);
  42. axios({
  43. method: 'get',
  44. url: path,
  45. responseType: 'blob',
  46. headers: { token },
  47. })
  48. .then(function (response:any) {
  49. //console.log({ 'chunk': response });
  50. const filename = decodeURI(response.headers["content-disposition"]);
  51. const objectUrl = URL.createObjectURL(
  52. new Blob([response.data], {
  53. type: 'application/vnd.ms-excel',
  54. })
  55. )
  56. const link = document.createElement('a')
  57. // 设置导出的文件名称
  58. link.download = `${filename}` + '.xls'
  59. link.style.display = 'none'
  60. link.href = objectUrl
  61. link.click()
  62. document.body.appendChild(link)
  63. });
  64. }
  65. let parent:any = undefined;
  66. export const getDeepestTreeData:any= (tree:any,childrenName:string)=>{
  67. if(tree[`${childrenName}`]&&tree[`${childrenName}`].length>0){
  68. parent = tree;
  69. return getDeepestTreeData(tree[`${childrenName}`][0],childrenName)
  70. }
  71. return [tree,parent]
  72. }
  73. export const uniqueFunc = (arr:any[], uniId:string) => {
  74. const res = new Map();
  75. return arr.filter((item) => !res.has(item[uniId]) && res.set(item[uniId], 1));
  76. }