落日与鲸
2025-02-22 8646f0866d09df5e8b5518e8094e451c29b87c48
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
149
150
151
// import { getToken } from '@/utils/authority';
import pathRegexp from 'path-to-regexp';
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));
}
export const getAuthorityFromRouter = (router = [], pathname) => {
  const authority = router.find(
    ({ routes, path = '/' }) =>
      (path && pathRegexp(path).exec(pathname)) ||
      (routes && getAuthorityFromRouter(routes, pathname)),
  );
  if (authority) return authority;
  return undefined;
};
export const getRouteAuthority = (path, routeData) => {
  let authorities;
  routeData.forEach((route) => {
    // match prefix
    if (pathRegexp(`${route.path}/(.*)`).test(`${path}/`)) {
      if (route.authority) {
        authorities = route.authority;
      } // exact match
 
      if (route.path === path) {
        authorities = route.authority || authorities;
      } // get children authority recursively
 
      if (route.routes) {
        authorities = getRouteAuthority(path, route.routes) || authorities;
      }
    }
  });
  return authorities;
};