新增邮件发送和管理员功能
This commit is contained in:
@@ -3,6 +3,12 @@ const emailUtils = {
|
||||
if (typeof email !== 'string') return ''
|
||||
const parts = email.split('@')
|
||||
return parts.length === 2 ? parts[1] : ''
|
||||
},
|
||||
|
||||
getName(email) {
|
||||
if (typeof email !== 'string') return ''
|
||||
const parts = email.trim().split('@')
|
||||
return parts.length === 2 ? parts[0] : ''
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,8 +2,60 @@ const fileUtils = {
|
||||
getExtFileName(filename) {
|
||||
const index = filename.lastIndexOf('.');
|
||||
return index !== -1 ? filename.slice(index) : '';
|
||||
},
|
||||
|
||||
async getBuffHash(buff) {
|
||||
const hashBuffer = await crypto.subtle.digest('SHA-256', buff);
|
||||
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
||||
return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
|
||||
},
|
||||
|
||||
base64ToUint8Array(base64) {
|
||||
const binaryStr = atob(base64);
|
||||
const len = binaryStr.length;
|
||||
const bytes = new Uint8Array(len);
|
||||
for (let i = 0; i < len; i++) {
|
||||
bytes[i] = binaryStr.charCodeAt(i);
|
||||
}
|
||||
return bytes;
|
||||
},
|
||||
|
||||
/**
|
||||
* 将 Base64 数据转换为 File 对象(自动识别 MIME 类型和文件扩展名)
|
||||
* @param {string} base64Data 带有 data: 前缀的 base64 数据
|
||||
* @param {string} [customFilename] 可选,传入自定义文件名(不含扩展名)
|
||||
* @returns {File} File 对象
|
||||
*/
|
||||
base64ToFile(base64Data, customFilename) {
|
||||
const match = base64Data.match(/^data:(image|video)\/([a-zA-Z0-9.+-]+);base64,/);
|
||||
if (!match) {
|
||||
throw new Error('Invalid base64 data format');
|
||||
}
|
||||
|
||||
const type = match[1]; // image 或 video
|
||||
const ext = match[2]; // jpg, png, mp4 等
|
||||
const mimeType = `${type}/${ext}`;
|
||||
const cleanBase64 = base64Data.replace(/^data:(image|video)\/[a-zA-Z0-9.+-]+;base64,/, '');
|
||||
|
||||
const byteCharacters = atob(cleanBase64);
|
||||
const byteArrays = [];
|
||||
|
||||
for (let offset = 0; offset < byteCharacters.length; offset += 1024) {
|
||||
const slice = byteCharacters.slice(offset, offset + 1024);
|
||||
const byteNumbers = new Array(slice.length);
|
||||
for (let i = 0; i < slice.length; i++) {
|
||||
byteNumbers[i] = slice.charCodeAt(i);
|
||||
}
|
||||
byteArrays.push(new Uint8Array(byteNumbers));
|
||||
}
|
||||
|
||||
const blob = new Blob(byteArrays, { type: mimeType });
|
||||
|
||||
const filename = `${customFilename || `${type}_${Date.now()}`}.${ext}`;
|
||||
return new File([blob], filename, { type: mimeType });
|
||||
}
|
||||
};
|
||||
|
||||
export default fileUtils
|
||||
|
||||
export default fileUtils;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user