From ba7a648cbee8f77603568d2029abe65c10484750 Mon Sep 17 00:00:00 2001 From: keven1024 Date: Sun, 8 Jun 2025 16:23:14 +0800 Subject: [PATCH] refactor: enhance file sharing functionality by implementing token caching and improving error handling in download processes --- front/components/Share/FileShareView.vue | 108 ++++++++-------- front/components/Share/TextShareView.vue | 152 ++++++++++++----------- front/composables/useMyAppShare.ts | 58 ++++++--- 3 files changed, 173 insertions(+), 145 deletions(-) diff --git a/front/components/Share/FileShareView.vue b/front/components/Share/FileShareView.vue index 01c1a2f..853d493 100644 --- a/front/components/Share/FileShareView.vue +++ b/front/components/Share/FileShareView.vue @@ -1,73 +1,69 @@ \ No newline at end of file +
+ 下载 +
+ + diff --git a/front/components/Share/TextShareView.vue b/front/components/Share/TextShareView.vue index e04124d..d7522ee 100644 --- a/front/components/Share/TextShareView.vue +++ b/front/components/Share/TextShareView.vue @@ -1,87 +1,93 @@ \ No newline at end of file +
+

查看文本

+ + +
+ diff --git a/front/composables/useMyAppShare.ts b/front/composables/useMyAppShare.ts index b4831f8..92da4f9 100644 --- a/front/composables/useMyAppShare.ts +++ b/front/composables/useMyAppShare.ts @@ -1,20 +1,45 @@ -const downloadFile = async (share_id: string) => { - const data = await $fetch<{ - code: number; - data: { - token?: string; - }; - }>(`/api/download`, { - method: "POST", - body: { - share_id, - }, - }); - const { token } = data?.data || {}; - if (!token) { - return; +import { toast } from "vue-sonner"; + +let shareIdTokenMap: WeakMap<{ share_id: string }, string>; + +const getShareToken = async (share_id: string): Promise => { + if (!shareIdTokenMap) { + shareIdTokenMap = new WeakMap(); + } + let token = shareIdTokenMap.get({ share_id }); + if (!token) { + const data = await $fetch<{ + code: number; + message: string; + data: { + token?: string; + }; + }>(`/api/download`, { + method: "POST", + body: { + share_id, + }, + }); + if (!data?.data?.token) { + throw new Error(data?.message || "获取token失败"); + } + token = data.data.token; + shareIdTokenMap.set({ share_id }, token); + } + return token; +}; + +const downloadFile = async (share_id: string) => { + try { + const token = await getShareToken(share_id); + if (!token) { + throw new Error("获取token失败"); + return; + } + (window as any)?.open(`/api/download?token=${token}`); + } catch (e) { + toast.error((e as any)?.data?.message || e); } - (window as any)?.open(`/api/download?token=${token}`); }; const createShare = async (data: any) => { @@ -66,6 +91,7 @@ const useMyAppShare = () => { createShare, createFileShare, createTextShare, + getShareToken, }; };