index.ts
6.6 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import customMessage from './message';
//将enum转换为{label:"",value:""}形式
function enumToSelect(data: any) {
const keys = Object.keys(data);
return keys.map((value) => {
return { label: data[value], value: value };
});
}
//将枚举的value值转换为label
function enumValueToLabel(value: any, enumObj: any) {
if (enumObj !== undefined) {
return enumObj[value];
}
return '';
}
//从缓存中获取用户信息
function getUserInfo() {
let userInfoString = localStorage.getItem('userInfo');
if (userInfoString === null) {
return {};
}
return JSON.parse(userInfoString);
}
//将状态枚举值转换为ProTable的enumValue格式
function enumToProTableEnumValue(enumConstants: any) {
const result = {};
for (const key in enumConstants) {
if (enumConstants.hasOwnProperty(key)) {
result[key] = {
text: enumConstants[key],
status: enumValueToLabel(key, enumConstants),
};
}
}
return result;
}
function formatDateTime(inputDateTime: string) {
const parsedDateTime = new Date(inputDateTime);
const year = parsedDateTime.getFullYear();
const month = String(parsedDateTime.getMonth() + 1).padStart(2, '0');
const day = String(parsedDateTime.getDate()).padStart(2, '0');
const hour = String(parsedDateTime.getHours()).padStart(2, '0');
const minute = String(parsedDateTime.getMinutes()).padStart(2, '0');
const second = String(parsedDateTime.getSeconds()).padStart(2, '0');
const formattedDateTime = `${year}-${month}-${day} ${hour}:${minute}:${second}`;
return formattedDateTime;
}
function formatdate(inputDateTime: string) {
const parsedDateTime = new Date(inputDateTime);
const year = parsedDateTime.getFullYear();
const month = String(parsedDateTime.getMonth() + 1).padStart(2, '0');
const day = String(parsedDateTime.getDate()).padStart(2, '0');
const formattedDate = `${year}-${month}-${day}`;
return formattedDate;
}
function formatSalesCode(salesCode: any) {
let newSalesCode = salesCode;
if (newSalesCode.indexOf('_')) {
newSalesCode = newSalesCode.split('_');
if (newSalesCode?.length === 2) {
newSalesCode = newSalesCode[1] + '(' + newSalesCode[0] + ')';
}
}
return newSalesCode;
}
// 将二进制流的字符串转换成 Blob 对象
function dataURItoBlob(dataURI: any) {
const byteString = atob(dataURI.split(',')[1]);
const mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
const ab = new ArrayBuffer(byteString.length);
const ia = new Uint8Array(ab);
for (let i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
return new Blob([ab], { type: mimeString });
}
// 将 Blob 对象转换成 File 对象
function blobToFile(blob: any, fileName: any) {
return new File([blob], fileName, { type: blob.type });
}
function appendFormData(formData: any, data: any, parentKey = null) {
for (const key in data) {
if (data.hasOwnProperty(key)) {
const value = data[key];
const formKey = parentKey ? `${parentKey}[${key}]` : key;
if (Array.isArray(value)) {
// 处理数组
value.forEach((item, index) => {
const arrayKey = `${formKey}[${index}]`;
if (typeof item === 'object' && !Array.isArray(item)) {
// 递归处理对象
appendFormData(formData, item, arrayKey);
} else {
// 直接添加到 FormData
formData.append(arrayKey, item);
}
});
} else if (typeof value === 'object' && !Array.isArray(value)) {
// 递归处理对象
appendFormData(formData, value, formKey);
} else {
// 直接添加到 FormData
formData.append(formKey, value);
}
}
}
}
function getName(str: string) {
let parts = str.split('-');
if (parts.length < 2) {
return null; // 字符串不包含'-'
}
let namePart = parts.slice(2).join('-') || parts[1];
let name = namePart.replace(/^\d+/, ''); // 移除名字前的数字
return name;
}
function getAliYunOSSFileNameFromUrl(url: string) {
try {
// 使用URL对象解析链接
let urlObject = new URL(url);
// 从路径中获取最后一个斜杠后的部分,即文件名
let pathParts = urlObject.pathname.split('/');
let fileName = pathParts[pathParts.length - 1];
// 检查文件名是否包含至少一个点(.)以确保是一个合法的文件名
if (fileName.includes('.')) {
let originName = getName(fileName);
if (
originName === '' ||
originName === undefined ||
originName === null
) {
return url;
}
return decodeURIComponent(originName);
} else {
throw new Error('Invalid file name in the URL');
}
} catch (error: any) {
// 如果解析失败或文件名不符合预期,返回原始链接
console.error('Error extracting file name:', error.message);
return url;
}
}
function transImageFile(base64Image: any) {
// 将Base64字符串解码为二进制数据
const binaryData = atob(base64Image.split(',')[1]);
// 创建一个Uint8Array来存储二进制数据
const arrayBuffer = new ArrayBuffer(binaryData.length);
const uint8Array = new Uint8Array(arrayBuffer);
for (let i = 0; i < binaryData.length; i++) {
uint8Array[i] = binaryData.charCodeAt(i);
}
// 创建Blob对象
return new Blob([uint8Array], { type: 'image/png' });
}
/**
* 复制文本到剪贴板
* @param text
*/
function copyToClipboard(text: string) {
// 创建一个临时的textarea元素
const textarea = document.createElement('textarea');
textarea.value = text;
// 将textarea元素添加到DOM中
document.body.appendChild(textarea);
// 选中textarea中的文本
textarea.select();
try {
// 尝试执行复制命令
document.execCommand('copy');
return true;
} catch (err) {
return false;
} finally {
// 移除临时的textarea元素
document.body.removeChild(textarea);
}
}
/**
* 按照前两个-,分割为数组
* @param str 字符串处理
* @returns
*/
function splitByFirstTwoDashes(str: string) {
let index1 = str.indexOf('-');
if (index1 === -1) {
return [str];
}
let index2 = str.indexOf('-', index1 + 1);
if (index2 === -1) {
return [str.slice(0, index1), str.slice(index1 + 1)];
}
return [
str.slice(0, index1),
str.slice(index1 + 1, index2),
str.slice(index2 + 1),
];
}
export {
appendFormData,
blobToFile,
copyToClipboard,
customMessage,
dataURItoBlob,
enumToProTableEnumValue,
enumToSelect,
enumValueToLabel,
formatDateTime,
formatSalesCode,
formatdate,
getAliYunOSSFileNameFromUrl,
getUserInfo,
splitByFirstTwoDashes,
transImageFile,
};