🚸 optimize

This commit is contained in:
2024-12-20 01:23:51 +08:00
parent 8ab873f5ce
commit 76336d1553
19 changed files with 276 additions and 161 deletions

View File

@@ -41,7 +41,7 @@ export const service = createAlova({
requestAdapter: axiosRequestAdapter(),
l2Cache: localforageStorageAdapter,
cacheLogger: import.meta.env.VITE_NODE_ENV === 'development',
cacheFor: {},
cacheFor: null,
// 设置全局的请求拦截器
beforeRequest: onAuthRequired(async (method: any) => {
if (!method.meta?.ignoreToken) {

View File

@@ -6,7 +6,7 @@ export function handleErrorCode(code: number): void {
message
.open({
content: i18n.global.t("error.400"),
duration: 5,
duration: 3,
type: "error",
})
.then();
@@ -15,7 +15,7 @@ export function handleErrorCode(code: number): void {
message
.open({
content: i18n.global.t("error.401"),
duration: 5,
duration: 3,
type: "error",
})
.then();
@@ -24,7 +24,7 @@ export function handleErrorCode(code: number): void {
message
.open({
content: i18n.global.t("error.403"),
duration: 5,
duration: 3,
type: "error",
})
.then();
@@ -33,7 +33,7 @@ export function handleErrorCode(code: number): void {
message
.open({
content: i18n.global.t("error.404"),
duration: 5,
duration: 3,
type: "error",
})
.then();
@@ -42,7 +42,7 @@ export function handleErrorCode(code: number): void {
message
.open({
content: i18n.global.t("error.408"),
duration: 5,
duration: 3,
type: "error",
})
.then();
@@ -51,7 +51,7 @@ export function handleErrorCode(code: number): void {
message
.open({
content: i18n.global.t("error.500"),
duration: 5,
duration: 3,
type: "error",
})
.then();
@@ -60,7 +60,7 @@ export function handleErrorCode(code: number): void {
message
.open({
content: i18n.global.t("error.501"),
duration: 5,
duration: 3,
type: "error",
})
.then();
@@ -69,7 +69,7 @@ export function handleErrorCode(code: number): void {
message
.open({
content: i18n.global.t("error.502"),
duration: 5,
duration: 3,
type: "error",
})
.then();
@@ -78,7 +78,7 @@ export function handleErrorCode(code: number): void {
message
.open({
content: i18n.global.t("error.503"),
duration: 5,
duration: 3,
type: "error",
})
.then();
@@ -87,7 +87,7 @@ export function handleErrorCode(code: number): void {
message
.open({
content: i18n.global.t("error.504"),
duration: 5,
duration: 3,
type: "error",
})
.then();
@@ -96,7 +96,7 @@ export function handleErrorCode(code: number): void {
message
.open({
content: i18n.global.t("error.505"),
duration: 5,
duration: 3,
type: "error",
})
.then();
@@ -105,7 +105,7 @@ export function handleErrorCode(code: number): void {
message
.open({
content: i18n.global.t("error.other") + `(${code})`,
duration: 5,
duration: 3,
type: "error",
})
.then();

View File

@@ -0,0 +1,29 @@
async function getImageSizeWithUnit(url: string): Promise<string> {
let sizeInBytes: number;
// 处理 base64 格式的图片 URL
if (url.startsWith('data:')) {
sizeInBytes = atob(url.split(',')[1]).length * 0.75;
}
// 处理 blob 格式的图片 URL
else if (url.startsWith('blob:')) {
const response = await fetch(url);
const blob = await response.blob();
sizeInBytes = blob.size;
} else {
throw new Error('Unsupported URL format');
}
let unit = 'Bytes';
if (sizeInBytes >= 1024 * 1024) {
sizeInBytes /= (1024 * 1024);
unit = 'MB';
} else if (sizeInBytes >= 1024) {
sizeInBytes /= 1024;
unit = 'KB';
}
return `${parseFloat(sizeInBytes.toFixed(2))} ${unit}`;
}
export default getImageSizeWithUnit;

View File

@@ -53,7 +53,7 @@ export class WebSocketService {
};
private handleMessage = (event: MessageEvent): void => {
const data = JSON.parse(event.data);
const {data} = event;
if (this.callbacks.message) {
this.callbacks.message.forEach((cb) => (cb as MessageCallback)(data));
}
@@ -71,9 +71,6 @@ export class WebSocketService {
console.log('WebSocket连接已关闭');
if (this.callbacks.close) {
this.callbacks.close.forEach((cb) => (cb as EventCallback)());
if (!this.options.reconnectTimeout) {
this.reconnect();
}
}
};
@@ -84,8 +81,4 @@ export class WebSocketService {
console.warn('尝试发送消息时WebSocket未连接');
}
}
public getReadyState(): number {
return this.ws ? this.ws.readyState : WebSocket.CLOSED;
}
}