zhengyiming
2025-06-11 91f00f1df35a964d69f48b9f71b484e2d4ef357e
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
import _ from 'lodash';
import { setOSSLink } from '@life-payment/utils';
import Taro from '@tarojs/taro';
import { FileItem } from '@nutui/nutui-taro/dist/types/__VUE/uploader/type';
 
/**
 * 获取文件扩展名
 */
export function getFileExtension(url: string): string {
  return _.last(url.split('.'));
}
 
export function getFileNameInfo(fileUrl: string) {
  const fileNameList = fileUrl.split('/').pop().split('.');
  const extension = fileNameList.pop();
  const newFileNameList = fileNameList.join('.').split('_');
  if (newFileNameList.length > 1) {
    newFileNameList.pop();
  }
  return {
    fileName: decodeURI(newFileNameList.join('_')),
    extension,
  };
}
 
export function extractFileNameFromUrl(fileUrl: string) {
  const { fileName, extension } = getFileNameInfo(fileUrl);
  return `${fileName}.${extension}`;
}
 
export function urlOmitDomain(url: string) {
  return url.replace(/^https?:\/\/[^/]+/, '');
}
 
const imgReg = /\.(png|jpeg|jpg|webp|gif)$/i;
 
/**
 * 把api返回的路径转换为upload的路径
 */
export function convertApiPath2Url(path: string) {
  return {
    name: extractFileNameFromUrl(path),
    path: urlOmitDomain(path),
    url: setOSSLink(path),
    status: 'success',
    type: imgReg.test(path) ? 'image' : '',
    uid: _.uniqueId(),
  } as FileItem;
}
 
export function convertApi2FormUrlOnlyOne(path: string) {
  return path ? [convertApiPath2Url(path)] : [];
}
 
/**
 * 把upload的路径转换为上传到api的路径
 */
export function convertFormUrl2Api<
  T extends { path?: string; url?: string; status?: FileItem['status'] }
>(urls: T[]) {
  return urls.filter((x) => x.path && x.status === 'success').map((x) => urlOmitDomain(x.path));
}
 
export function convertFilterFormUrl2Api<T extends { path?: string }>(urls: T[]) {
  return urls.filter((x) => x.path);
}
 
export function isFileAllUploaded(fileList: FileItem[]) {
  return fileList.every((x) => !!x.path && x.status === 'success');
}
 
type Base64ListItem = {
  base64: string;
  fileName: string;
};
 
export function base64SaveTolocal(base64List: Base64ListItem[]) {
  const fsm = Taro.getFileSystemManager();
 
  return base64List.map((base64ListItem) => {
    const arrayBuffer = Taro.base64ToArrayBuffer(
      base64ListItem.base64.replace('data:image/png;base64,', '')
    );
    const fileName = `${Taro.env.USER_DATA_PATH}/${base64ListItem.fileName}.png`;
    fsm.writeFileSync(fileName, arrayBuffer, 'binary');
    return fileName;
  });
}
 
export function downloadBase64File(base64Data: string, filename: string) {
  // 将base64数据分割,获取mime-type
  const [, mime, b64data] = base64Data.match(/^data:([^;]+);base64,(.+)$/);
 
  // 解码base64数据
  const byteString = atob(b64data);
  const arrayBuffer = new ArrayBuffer(byteString.length);
  const intArray = new Uint8Array(arrayBuffer);
 
  for (let i = 0; i < byteString.length; i++) {
    intArray[i] = byteString.charCodeAt(i);
  }
 
  // 创建Blob对象
  const blob = new Blob([intArray], { type: mime });
 
  //@ts-ignore
  if (typeof navigator !== 'undefined' && navigator.msSaveOrOpenBlob) {
    //@ts-ignore 兼容IE
    navigator.msSaveOrOpenBlob(blob, filename);
  } else {
    // 创建一个临时的a标签用于触发下载
    const a = document.createElement('a');
    const url = window.URL.createObjectURL(blob);
    a.href = url;
    a.download = filename; // 设置下载后的文件名
    document.body.appendChild(a);
    a.click(); // 触发下载
    document.body.removeChild(a);
    setTimeout(() => {
      window.URL.revokeObjectURL(url); // 清除创建的对象URL
    }, 0);
  }
}