zjk
2026-09-29 54dcf0a82e7490fa58b1c056d5c0b9cb739c2931
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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));
};