WeKnora/frontend/src/utils/index.ts

45 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import { MessagePlugin } from "tdesign-vue-next";
export function generateRandomString(length: number) {
let result = "";
const characters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
const charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
export function formatStringDate(date: any) {
let data = new Date(date);
let year = data.getFullYear();
let month = String(data.getMonth() + 1).padStart(2, '0');
let day = String(data.getDate()).padStart(2, '0');
let hour = String(data.getHours()).padStart(2, '0');
let minute = String(data.getMinutes()).padStart(2, '0');
let second = String(data.getSeconds()).padStart(2, '0');
return (
year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second
);
}
export function kbFileTypeVerification(file: any) {
let validTypes = ["pdf", "txt", "md", "docx", "doc", "jpg", "jpeg", "png"];
let type = file.name.substring(file.name.lastIndexOf(".") + 1);
if (!validTypes.includes(type)) {
MessagePlugin.error("文件类型错误!");
return true;
}
if (
(type == "pdf" || type == "docx" || type == "doc") &&
file.size > 31457280
) {
MessagePlugin.error("pdf/doc文件不能超过30M!");
return true;
}
if ((type == "txt" || type == "md") && file.size > 31457280) {
MessagePlugin.error("txt/md文件不能超过30M!");
return true;
}
return false
}