From 420b5d4389ec901170d3f228387cb865022cfc2e Mon Sep 17 00:00:00 2001 From: Forte Scarlet <1149159218@qq.com> Date: Thu, 4 Dec 2025 15:04:39 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=E4=B8=BA=E5=88=87=E6=8D=A2?= =?UTF-8?q?=E5=BC=B9=E5=87=BA=E6=8F=90=E7=A4=BA=E6=A1=86=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=8F=AF=E5=85=B3=E9=97=AD=E6=8C=89=E9=92=AE=20(#350)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/query/mutations.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/lib/query/mutations.ts b/src/lib/query/mutations.ts index b78340d31..24a49becd 100644 --- a/src/lib/query/mutations.ts +++ b/src/lib/query/mutations.ts @@ -35,7 +35,9 @@ export const useAddProviderMutation = (appId: AppId) => { toast.success( t("notifications.providerAdded", { defaultValue: "供应商已添加", - }), + }), { + closeButton: true + } ); }, onError: (error: Error) => { @@ -63,7 +65,9 @@ export const useUpdateProviderMutation = (appId: AppId) => { toast.success( t("notifications.updateSuccess", { defaultValue: "供应商更新成功", - }), + }), { + closeButton: true + } ); }, onError: (error: Error) => { @@ -101,7 +105,9 @@ export const useDeleteProviderMutation = (appId: AppId) => { toast.success( t("notifications.deleteSuccess", { defaultValue: "供应商已删除", - }), + }), { + closeButton: true + } ); }, onError: (error: Error) => { @@ -140,7 +146,9 @@ export const useSwitchProviderMutation = (appId: AppId) => { t("notifications.switchSuccess", { defaultValue: "切换供应商成功", appName: t(`apps.${appId}`, { defaultValue: appId }), - }), + }), { + closeButton: true + } ); }, onError: (error: Error) => { From bf9228093b11b0b40c1c1a1a8ead8cf099f8157a Mon Sep 17 00:00:00 2001 From: Jason Date: Thu, 4 Dec 2025 17:01:17 +0800 Subject: [PATCH 2/2] fix: add fallback for crypto.randomUUID() on older WebViews crypto.randomUUID() is not available on older systems (macOS < 12.3, Safari < 15.4). This caused "crypto.randomUUID is not a function" error when adding providers. - Add generateUUID() utility with getRandomValues() fallback - Provide user-friendly error message if crypto API unavailable --- src/lib/query/mutations.ts | 3 ++- src/utils/uuid.ts | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 src/utils/uuid.ts diff --git a/src/lib/query/mutations.ts b/src/lib/query/mutations.ts index 24a49becd..061bdb098 100644 --- a/src/lib/query/mutations.ts +++ b/src/lib/query/mutations.ts @@ -4,6 +4,7 @@ import { toast } from "sonner"; import { providersApi, settingsApi, type AppId } from "@/lib/api"; import type { Provider, Settings } from "@/types"; import { extractErrorMessage } from "@/utils/errorUtils"; +import { generateUUID } from "@/utils/uuid"; export const useAddProviderMutation = (appId: AppId) => { const queryClient = useQueryClient(); @@ -13,7 +14,7 @@ export const useAddProviderMutation = (appId: AppId) => { mutationFn: async (providerInput: Omit) => { const newProvider: Provider = { ...providerInput, - id: crypto.randomUUID(), + id: generateUUID(), createdAt: Date.now(), }; await providersApi.add(newProvider, appId); diff --git a/src/utils/uuid.ts b/src/utils/uuid.ts new file mode 100644 index 000000000..8c3822c4f --- /dev/null +++ b/src/utils/uuid.ts @@ -0,0 +1,37 @@ +/** + * 生成 UUID v4 + * + * 优先使用 crypto.randomUUID(),不可用时使用 crypto.getRandomValues() 实现 + * + * 兼容性: + * - crypto.randomUUID(): Chrome 92+, Safari 15.4+, Firefox 95+ + * - crypto.getRandomValues(): Chrome 11+, Safari 5+, Firefox 21+ + */ +export function generateUUID(): string { + const cryptoApi = globalThis.crypto; + + // 优先使用原生 API + if (typeof cryptoApi?.randomUUID === "function") { + return cryptoApi.randomUUID(); + } + + // Fallback: 使用 crypto.getRandomValues 实现 UUID v4 + if (!cryptoApi?.getRandomValues) { + throw new Error( + "crypto API not available - please update your operating system", + ); + } + + const bytes = new Uint8Array(16); + cryptoApi.getRandomValues(bytes); + + // 设置版本 (4) 和变体 (RFC 4122) + bytes[6] = (bytes[6] & 0x0f) | 0x40; + bytes[8] = (bytes[8] & 0x3f) | 0x80; + + const hex = Array.from(bytes) + .map((b) => b.toString(16).padStart(2, "0")) + .join(""); + + return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`; +}