zhengyiming
2025-02-10 958b79ed89b9e742540f714a80261d222c0fc09b
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
import { loadEnv } from '@build/index';
import { setOSSLink as boleSetOSSLink } from '@bole-core/core';
 
const { VITE_OSS_URL } = loadEnv();
 
export const openLink = (link: string) => {
  const $a: HTMLElement = document.createElement('a');
  $a.setAttribute('href', link);
  $a.setAttribute('target', '_blank');
  $a.setAttribute('rel', 'noreferrer noopener');
  $a.setAttribute('id', 'external');
  document.getElementById('external') &&
    document.body.removeChild(document.getElementById('external') as Node);
  document.body.appendChild($a);
  $a.click();
  $a.remove();
};
 
export const combineURLs = (baseURL: string, relativeURL: string) => {
  return relativeURL
    ? baseURL.replace(/\/+$/, '') + '/' + relativeURL.replace(/^\/+/, '')
    : baseURL;
};
 
// export const isAbsoluteURL = (url) => {
//   return /^([a-z][a-z\d+\-.]*:)?\/\//i.test(url)
// }
 
export function isAbsoluteURL(path: string): boolean {
  // eslint-disable-next-line no-useless-escape
  // const reg =
  //   /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/
  return /^([a-z][a-z\d+\-.]*:)?\/\//i.test(path);
}
 
export function isExternal(path: string) {
  return /^(https?:|mailto:|tel:)/.test(path);
}
 
export function setOSSLink(url: string) {
  return boleSetOSSLink(url, VITE_OSS_URL);
}
 
export function setOssFileName(ossUrl: string) {
  // 取出文件名 去掉上传时生成的随机数
  const fileName = ossUrl
    .replace(/(.*\/)*([^.]+).*/gi, '$2')
    .replace(/(\S+?)_[A-Za-z0-9]+?$/, '$1');
  // 匹配 . 之前除换行符以外的所有字符替换为""
  const fileType = ossUrl.replace(/.+\./, '');
 
  return `${fileName}.${fileType}`;
}