added: api

This commit is contained in:
none
2023-03-21 14:06:05 +08:00
parent d4e4034c70
commit 766cc25981
8 changed files with 264 additions and 0 deletions

55
src/utils/index.ts Normal file
View File

@@ -0,0 +1,55 @@
import moment from "moment";
export function getToken(): string {
return window.localStorage.getItem("playedu-backend-token") || "";
}
export function setToken(token: string) {
window.localStorage.setItem("playedu-backend-token", token);
}
export function clearToken() {
window.localStorage.removeItem("playedu-backend-token");
}
export function dateFormat(dateStr: string) {
return moment(dateStr).format("YYYY-MM-DD HH:mm");
}
export function generateUUID(): string {
let guid = "";
for (let i = 1; i <= 32; i++) {
let n = Math.floor(Math.random() * 16.0).toString(16);
guid += n;
if (i === 8 || i === 12 || i === 16 || i === 20) guid += "-";
}
return guid;
}
export function transformBase64ToBlob(
base64: string,
mime: string,
filename: string
): File {
const arr = base64.split(",");
const bstr = atob(arr[1]);
let n = bstr.length;
const u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new File([u8arr], filename, { type: mime });
}
export function getHost() {
return window.location.protocol + "//" + window.location.host + "/";
}
export function inStrArray(array: string[], value: string): boolean {
for (let i = 0; i < array.length; i++) {
if (array[i] === value) {
return true;
}
}
return false;
}